os/textandloc/fontservices/textshaperplugin/IcuSource/common/unicode/usetiter.h
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2 **********************************************************************
     3 * Copyright (c) 2002-2005, International Business Machines
     4 * Corporation and others.  All Rights Reserved.
     5 **********************************************************************
     6 */
     7 #ifndef USETITER_H
     8 #define USETITER_H
     9 
    10 #include "unicode/utypes.h"
    11 #include "unicode/uobject.h"
    12 #include "unicode/unistr.h"
    13 
    14 /**
    15  * \file 
    16  * \brief C++ API: UnicodeSetIterator iterates over the contents of a UnicodeSet.
    17  */
    18 
    19 U_NAMESPACE_BEGIN
    20 
    21 class UnicodeSet;
    22 class UnicodeString;
    23 
    24 /**
    25  *
    26  * UnicodeSetIterator iterates over the contents of a UnicodeSet.  It
    27  * iterates over either code points or code point ranges.  After all
    28  * code points or ranges have been returned, it returns the
    29  * multicharacter strings of the UnicodSet, if any.
    30  *
    31  * <p>To iterate over code points, use a loop like this:
    32  * <pre>
    33  * UnicodeSetIterator it(set);
    34  * while (set.next()) {
    35  *   if (set.isString()) {
    36  *     processString(set.getString());
    37  *   } else {
    38  *     processCodepoint(set.getCodepoint());
    39  *   }
    40  * }
    41  * </pre>
    42  *
    43  * <p>To iterate over code point ranges, use a loop like this:
    44  * <pre>
    45  * UnicodeSetIterator it(set);
    46  * while (it.nextRange()) {
    47  *   if (it.isString()) {
    48  *     processString(it.getString());
    49  *   } else {
    50  *     processCodepointRange(it.getCodepoint(), it.getCodepointEnd());
    51  *   }
    52  * }
    53  * </pre>
    54  * @author M. Davis
    55  * @stable ICU 2.4
    56  */
    57 class U_COMMON_API UnicodeSetIterator : public UObject {
    58 
    59  protected:
    60 
    61     /**
    62      * Value of <tt>codepoint</tt> if the iterator points to a string.
    63      * If <tt>codepoint == IS_STRING</tt>, then examine
    64      * <tt>string</tt> for the current iteration result.
    65      * @stable ICU 2.4
    66      */
    67     enum { IS_STRING = -1 };
    68 
    69     /**
    70      * Current code point, or the special value <tt>IS_STRING</tt>, if
    71      * the iterator points to a string.
    72      * @stable ICU 2.4
    73      */
    74     UChar32 codepoint;
    75 
    76     /**
    77      * When iterating over ranges using <tt>nextRange()</tt>,
    78      * <tt>codepointEnd</tt> contains the inclusive end of the
    79      * iteration range, if <tt>codepoint != IS_STRING</tt>.  If
    80      * iterating over code points using <tt>next()</tt>, or if
    81      * <tt>codepoint == IS_STRING</tt>, then the value of
    82      * <tt>codepointEnd</tt> is undefined.
    83      * @stable ICU 2.4
    84      */
    85     UChar32 codepointEnd;
    86 
    87     /**
    88      * If <tt>codepoint == IS_STRING</tt>, then <tt>string</tt> points
    89      * to the current string.  If <tt>codepoint != IS_STRING</tt>, the
    90      * value of <tt>string</tt> is undefined.
    91      * @stable ICU 2.4
    92      */
    93     const UnicodeString* string;
    94 
    95  public:
    96 
    97     /**
    98      * Create an iterator over the given set.  The iterator is valid
    99      * only so long as <tt>set</tt> is valid.
   100      * @param set set to iterate over
   101      * @stable ICU 2.4
   102      */
   103     UnicodeSetIterator(const UnicodeSet& set);
   104 
   105     /**
   106      * Create an iterator over nothing.  <tt>next()</tt> and
   107      * <tt>nextRange()</tt> return false. This is a convenience
   108      * constructor allowing the target to be set later.
   109      * @stable ICU 2.4
   110      */
   111     UnicodeSetIterator();
   112 
   113     /**
   114      * Destructor.
   115      * @stable ICU 2.4
   116      */
   117     virtual ~UnicodeSetIterator();
   118 
   119     /**
   120      * Returns true if the current element is a string.  If so, the
   121      * caller can retrieve it with <tt>getString()</tt>.  If this
   122      * method returns false, the current element is a code point or
   123      * code point range, depending on whether <tt>next()</tt> or
   124      * <tt>nextRange()</tt> was called, and the caller can retrieve it
   125      * with <tt>getCodepoint()</tt> and, for a range,
   126      * <tt>getCodepointEnd()</tt>.
   127      * @stable ICU 2.4
   128      */
   129     inline UBool isString() const;
   130 
   131     /**
   132      * Returns the current code point, if <tt>isString()</tt> returned
   133      * false.  Otherwise returns an undefined result.
   134      * @stable ICU 2.4
   135      */
   136     inline UChar32 getCodepoint() const;
   137 
   138     /**
   139      * Returns the end of the current code point range, if
   140      * <tt>isString()</tt> returned false and <tt>nextRange()</tt> was
   141      * called.  Otherwise returns an undefined result.
   142      * @stable ICU 2.4
   143      */
   144     inline UChar32 getCodepointEnd() const;
   145 
   146     /**
   147      * Returns the current string, if <tt>isString()</tt> returned
   148      * true.  Otherwise returns an undefined result.
   149      * @stable ICU 2.4
   150      */
   151     inline const UnicodeString& getString() const;
   152 
   153     /**
   154      * Returns the next element in the set, either a single code point
   155      * or a string.  If there are no more elements in the set, return
   156      * false.  If <tt>codepoint == IS_STRING</tt>, the value is a
   157      * string in the <tt>string</tt> field.  Otherwise the value is a
   158      * single code point in the <tt>codepoint</tt> field.
   159      *
   160      * <p>The order of iteration is all code points in sorted order,
   161      * followed by all strings sorted order.  <tt>codepointEnd</tt> is
   162      * undefined after calling this method.  <tt>string</tt> is
   163      * undefined unless <tt>codepoint == IS_STRING</tt>.  Do not mix
   164      * calls to <tt>next()</tt> and <tt>nextRange()</tt> without
   165      * calling <tt>reset()</tt> between them.  The results of doing so
   166      * are undefined.
   167      *
   168      * @return true if there was another element in the set and this
   169      * object contains the element.
   170      * @stable ICU 2.4
   171      */
   172     UBool next();
   173 
   174     /**
   175      * Returns the next element in the set, either a code point range
   176      * or a string.  If there are no more elements in the set, return
   177      * false.  If <tt>codepoint == IS_STRING</tt>, the value is a
   178      * string in the <tt>string</tt> field.  Otherwise the value is a
   179      * range of one or more code points from <tt>codepoint</tt> to
   180      * <tt>codepointeEnd</tt> inclusive.
   181      *
   182      * <p>The order of iteration is all code points ranges in sorted
   183      * order, followed by all strings sorted order.  Ranges are
   184      * disjoint and non-contiguous.  <tt>string</tt> is undefined
   185      * unless <tt>codepoint == IS_STRING</tt>.  Do not mix calls to
   186      * <tt>next()</tt> and <tt>nextRange()</tt> without calling
   187      * <tt>reset()</tt> between them.  The results of doing so are
   188      * undefined.
   189      *
   190      * @return true if there was another element in the set and this
   191      * object contains the element.
   192      * @stable ICU 2.4
   193      */
   194     UBool nextRange();
   195 
   196     /**
   197      * Sets this iterator to visit the elements of the given set and
   198      * resets it to the start of that set.  The iterator is valid only
   199      * so long as <tt>set</tt> is valid.
   200      * @param set the set to iterate over.
   201      * @stable ICU 2.4
   202      */
   203     void reset(const UnicodeSet& set);
   204 
   205     /**
   206      * Resets this iterator to the start of the set.
   207      * @stable ICU 2.4
   208      */
   209     void reset();
   210 
   211     /**
   212      * ICU "poor man's RTTI", returns a UClassID for this class.
   213      *
   214      * @stable ICU 2.4
   215      */
   216     static UClassID U_EXPORT2 getStaticClassID();
   217 
   218     /**
   219      * ICU "poor man's RTTI", returns a UClassID for the actual class.
   220      *
   221      * @stable ICU 2.4
   222      */
   223     virtual UClassID getDynamicClassID() const;
   224 
   225     // ======================= PRIVATES ===========================
   226 
   227  protected:
   228 
   229     // endElement and nextElements are really UChar32's, but we keep
   230     // them as signed int32_t's so we can do comparisons with
   231     // endElement set to -1.  Leave them as int32_t's.
   232     /** The set
   233      * @stable ICU 2.4
   234      */
   235     const UnicodeSet* set;
   236     /** End range
   237      * @stable ICU 2.4
   238      */
   239     int32_t endRange;
   240     /** Range
   241      * @stable ICU 2.4
   242      */
   243     int32_t range;
   244     /** End element
   245      * @stable ICU 2.4
   246      */
   247     int32_t endElement;
   248     /** Next element
   249      * @stable ICU 2.4
   250      */
   251     int32_t nextElement;
   252     //UBool abbreviated;
   253     /** Next string
   254      * @stable ICU 2.4
   255      */
   256     int32_t nextString;
   257     /** String count
   258      * @stable ICU 2.4
   259      */
   260     int32_t stringCount;
   261 
   262     /** Copy constructor. Disallowed.
   263      * @stable ICU 2.4
   264      */
   265     UnicodeSetIterator(const UnicodeSetIterator&); // disallow
   266 
   267     /** Assignment operator. Disallowed.
   268      * @stable ICU 2.4
   269      */
   270     UnicodeSetIterator& operator=(const UnicodeSetIterator&); // disallow
   271 
   272     /** Load range
   273      * @stable ICU 2.4
   274      */
   275     virtual void loadRange(int32_t range);
   276 
   277 };
   278 
   279 inline UBool UnicodeSetIterator::isString() const {
   280     return codepoint == (UChar32)IS_STRING;
   281 }
   282 
   283 inline UChar32 UnicodeSetIterator::getCodepoint() const {
   284     return codepoint;
   285 }
   286 
   287 inline UChar32 UnicodeSetIterator::getCodepointEnd() const {
   288     return codepointEnd;
   289 }
   290 
   291 inline const UnicodeString& UnicodeSetIterator::getString() const {
   292     return *string;
   293 }
   294 
   295 U_NAMESPACE_END
   296 
   297 #endif