os/textandloc/fontservices/textshaperplugin/IcuSource/common/unicode/strenum.h
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2 *******************************************************************************
     3 *
     4 *   Copyright (C) 2002-2005, International Business Machines
     5 *   Corporation and others.  All Rights Reserved.
     6 *
     7 *******************************************************************************
     8 */
     9 
    10 #ifndef STRENUM_H
    11 #define STRENUM_H
    12 
    13 #include "unicode/uobject.h"
    14 #include "unicode/unistr.h"
    15 
    16 /**
    17  * \file 
    18  * \brief C++ API: String Enumeration
    19  */
    20  
    21 U_NAMESPACE_BEGIN
    22 
    23 /**
    24  * Base class for 'pure' C++ implementations of uenum api.  Adds a
    25  * method that returns the next UnicodeString since in C++ this can
    26  * be a common storage format for strings.
    27  *
    28  * <p>The model is that the enumeration is over strings maintained by
    29  * a 'service.'  At any point, the service might change, invalidating
    30  * the enumerator (though this is expected to be rare).  The iterator
    31  * returns an error if this has occurred.  Lack of the error is no
    32  * guarantee that the service didn't change immediately after the
    33  * call, so the returned string still might not be 'valid' on
    34  * subsequent use.</p>
    35  *
    36  * <p>Strings may take the form of const char*, const UChar*, or const
    37  * UnicodeString*.  The type you get is determine by the variant of
    38  * 'next' that you call.  In general the StringEnumeration is
    39  * optimized for one of these types, but all StringEnumerations can
    40  * return all types.  Returned strings are each terminated with a NUL.
    41  * Depending on the service data, they might also include embedded NUL
    42  * characters, so API is provided to optionally return the true
    43  * length, counting the embedded NULs but not counting the terminating
    44  * NUL.</p>
    45  *
    46  * <p>The pointers returned by next, unext, and snext become invalid
    47  * upon any subsequent call to the enumeration's destructor, next,
    48  * unext, snext, or reset.</p>
    49  *
    50  * ICU 2.8 adds some default implementations and helper functions
    51  * for subclasses.
    52  *
    53  * @stable ICU 2.4 
    54  */
    55 class U_COMMON_API StringEnumeration : public UObject { 
    56 public:
    57     /**
    58      * Destructor.
    59      * @stable ICU 2.4
    60      */
    61     virtual ~StringEnumeration();
    62 
    63     /**
    64      * Clone this object, an instance of a subclass of StringEnumeration.
    65      * Clones can be used concurrently in multiple threads.
    66      * If a subclass does not implement clone(), or if an error occurs,
    67      * then NULL is returned.
    68      * The clone functions in all subclasses return a base class pointer
    69      * because some compilers do not support covariant (same-as-this)
    70      * return types; cast to the appropriate subclass if necessary.
    71      * The caller must delete the clone.
    72      *
    73      * @return a clone of this object
    74      *
    75      * @see getDynamicClassID
    76      * @stable ICU 2.8
    77      */
    78     virtual StringEnumeration *clone() const;
    79 
    80     /**
    81      * <p>Return the number of elements that the iterator traverses.  If
    82      * the iterator is out of sync with its service, status is set to
    83      * U_ENUM_OUT_OF_SYNC_ERROR, and the return value is zero.</p>
    84      *
    85      * <p>The return value will not change except possibly as a result of
    86      * a subsequent call to reset, or if the iterator becomes out of sync.</p>
    87      *
    88      * <p>This is a convenience function. It can end up being very
    89      * expensive as all the items might have to be pre-fetched
    90      * (depending on the storage format of the data being
    91      * traversed).</p>
    92      *
    93      * @param status the error code.
    94      * @return number of elements in the iterator.
    95      *
    96      * @stable ICU 2.4 */
    97     virtual int32_t count(UErrorCode& status) const = 0;
    98 
    99     /**
   100      * <p>Returns the next element as a NUL-terminated char*.  If there
   101      * are no more elements, returns NULL.  If the resultLength pointer
   102      * is not NULL, the length of the string (not counting the
   103      * terminating NUL) is returned at that address.  If an error
   104      * status is returned, the value at resultLength is undefined.</p>
   105      *
   106      * <p>The returned pointer is owned by this iterator and must not be
   107      * deleted by the caller.  The pointer is valid until the next call
   108      * to next, unext, snext, reset, or the enumerator's destructor.</p>
   109      *
   110      * <p>If the iterator is out of sync with its service, status is set
   111      * to U_ENUM_OUT_OF_SYNC_ERROR and NULL is returned.</p>
   112      *
   113      * <p>If the native service string is a UChar* string, it is
   114      * converted to char* with the invariant converter.  If the
   115      * conversion fails (because a character cannot be converted) then
   116      * status is set to U_INVARIANT_CONVERSION_ERROR and the return
   117      * value is undefined (though not NULL).</p>
   118      *
   119      * Starting with ICU 2.8, the default implementation calls snext()
   120      * and handles the conversion.
   121      *
   122      * @param status the error code.
   123      * @param resultLength a pointer to receive the length, can be NULL.
   124      * @return a pointer to the string, or NULL.
   125      *
   126      * @stable ICU 2.4 
   127      */
   128     virtual const char* next(int32_t *resultLength, UErrorCode& status);
   129 
   130     /**
   131      * <p>Returns the next element as a NUL-terminated UChar*.  If there
   132      * are no more elements, returns NULL.  If the resultLength pointer
   133      * is not NULL, the length of the string (not counting the
   134      * terminating NUL) is returned at that address.  If an error
   135      * status is returned, the value at resultLength is undefined.</p>
   136      *
   137      * <p>The returned pointer is owned by this iterator and must not be
   138      * deleted by the caller.  The pointer is valid until the next call
   139      * to next, unext, snext, reset, or the enumerator's destructor.</p>
   140      *
   141      * <p>If the iterator is out of sync with its service, status is set
   142      * to U_ENUM_OUT_OF_SYNC_ERROR and NULL is returned.</p>
   143      *
   144      * Starting with ICU 2.8, the default implementation calls snext()
   145      * and handles the conversion.
   146      *
   147      * @param status the error code.
   148      * @param resultLength a ponter to receive the length, can be NULL.
   149      * @return a pointer to the string, or NULL.
   150      *
   151      * @stable ICU 2.4 
   152      */
   153     virtual const UChar* unext(int32_t *resultLength, UErrorCode& status);
   154 
   155     /**
   156      * <p>Returns the next element a UnicodeString*.  If there are no
   157      * more elements, returns NULL.</p>
   158      *
   159      * <p>The returned pointer is owned by this iterator and must not be
   160      * deleted by the caller.  The pointer is valid until the next call
   161      * to next, unext, snext, reset, or the enumerator's destructor.</p>
   162      *
   163      * <p>If the iterator is out of sync with its service, status is set
   164      * to U_ENUM_OUT_OF_SYNC_ERROR and NULL is returned.</p>
   165      *
   166      * @param status the error code.
   167      * @return a pointer to the string, or NULL.
   168      *
   169      * @stable ICU 2.4 
   170      */
   171     virtual const UnicodeString* snext(UErrorCode& status) = 0;
   172 
   173     /**
   174      * <p>Resets the iterator.  This re-establishes sync with the
   175      * service and rewinds the iterator to start at the first
   176      * element.</p>
   177      *
   178      * <p>Previous pointers returned by next, unext, or snext become
   179      * invalid, and the value returned by count might change.</p>
   180      *
   181      * @param status the error code.
   182      *
   183      * @stable ICU 2.4 
   184      */
   185     virtual void reset(UErrorCode& status) = 0;
   186 
   187 protected:
   188     /**
   189      * UnicodeString field for use with default implementations and subclasses.
   190      * @stable ICU 2.8
   191      */
   192     UnicodeString unistr;
   193     /**
   194      * char * default buffer for use with default implementations and subclasses.
   195      * @stable ICU 2.8
   196      */
   197     char charsBuffer[32];
   198     /**
   199      * char * buffer for use with default implementations and subclasses.
   200      * Allocated in constructor and in ensureCharsCapacity().
   201      * @stable ICU 2.8
   202      */
   203     char *chars;
   204     /**
   205      * Capacity of chars, for use with default implementations and subclasses.
   206      * @stable ICU 2.8
   207      */
   208     int32_t charsCapacity;
   209 
   210     /**
   211      * Default constructor for use with default implementations and subclasses.
   212      * @stable ICU 2.8
   213      */
   214     StringEnumeration();
   215 
   216     /**
   217      * Ensures that chars is at least as large as the requested capacity.
   218      * For use with default implementations and subclasses.
   219      *
   220      * @param capacity Requested capacity.
   221      * @param status ICU in/out error code.
   222      * @stable ICU 2.8
   223      */
   224     void ensureCharsCapacity(int32_t capacity, UErrorCode &status);
   225 
   226     /**
   227      * Converts s to Unicode and sets unistr to the result.
   228      * For use with default implementations and subclasses,
   229      * especially for implementations of snext() in terms of next().
   230      * This is provided with a helper function instead of a default implementation
   231      * of snext() to avoid potential infinite loops between next() and snext().
   232      *
   233      * For example:
   234      * \code
   235      * const UnicodeString* snext(UErrorCode& status) {
   236      *   int32_t resultLength=0;
   237      *   const char *s=next(&resultLength, status);
   238      *   return setChars(s, resultLength, status);
   239      * }
   240      * \endcode
   241      *
   242      * @param s String to be converted to Unicode.
   243      * @param length Length of the string.
   244      * @param status ICU in/out error code.
   245      * @return A pointer to unistr.
   246      * @stable ICU 2.8
   247      */
   248     UnicodeString *setChars(const char *s, int32_t length, UErrorCode &status);
   249 };
   250 
   251 U_NAMESPACE_END
   252 
   253 /* STRENUM_H */
   254 #endif