Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/unicode/strenum.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // © 2016 and later: Unicode, Inc. and others.
0002 // License & terms of use: http://www.unicode.org/copyright.html
0003 /*
0004 *******************************************************************************
0005 *
0006 *   Copyright (C) 2002-2012, International Business Machines
0007 *   Corporation and others.  All Rights Reserved.
0008 *
0009 *******************************************************************************
0010 */
0011 
0012 #ifndef STRENUM_H
0013 #define STRENUM_H
0014 
0015 #include "unicode/utypes.h"
0016 
0017 #if U_SHOW_CPLUSPLUS_API
0018 
0019 #include "unicode/uobject.h"
0020 #include "unicode/unistr.h"
0021 
0022 /**
0023  * \file 
0024  * \brief C++ API: String Enumeration
0025  */
0026  
0027 U_NAMESPACE_BEGIN
0028 
0029 /**
0030  * Base class for 'pure' C++ implementations of uenum api.  Adds a
0031  * method that returns the next UnicodeString since in C++ this can
0032  * be a common storage format for strings.
0033  *
0034  * <p>The model is that the enumeration is over strings maintained by
0035  * a 'service.'  At any point, the service might change, invalidating
0036  * the enumerator (though this is expected to be rare).  The iterator
0037  * returns an error if this has occurred.  Lack of the error is no
0038  * guarantee that the service didn't change immediately after the
0039  * call, so the returned string still might not be 'valid' on
0040  * subsequent use.</p>
0041  *
0042  * <p>Strings may take the form of const char*, const char16_t*, or const
0043  * UnicodeString*.  The type you get is determine by the variant of
0044  * 'next' that you call.  In general the StringEnumeration is
0045  * optimized for one of these types, but all StringEnumerations can
0046  * return all types.  Returned strings are each terminated with a NUL.
0047  * Depending on the service data, they might also include embedded NUL
0048  * characters, so API is provided to optionally return the true
0049  * length, counting the embedded NULs but not counting the terminating
0050  * NUL.</p>
0051  *
0052  * <p>The pointers returned by next, unext, and snext become invalid
0053  * upon any subsequent call to the enumeration's destructor, next,
0054  * unext, snext, or reset.</p>
0055  *
0056  * ICU 2.8 adds some default implementations and helper functions
0057  * for subclasses.
0058  *
0059  * @stable ICU 2.4 
0060  */
0061 class U_COMMON_API StringEnumeration : public UObject { 
0062 public:
0063     /**
0064      * Destructor.
0065      * @stable ICU 2.4
0066      */
0067     virtual ~StringEnumeration();
0068 
0069     /**
0070      * Clone this object, an instance of a subclass of StringEnumeration.
0071      * Clones can be used concurrently in multiple threads.
0072      * If a subclass does not implement clone(), or if an error occurs,
0073      * then nullptr is returned.
0074      * The caller must delete the clone.
0075      *
0076      * @return a clone of this object
0077      *
0078      * @see getDynamicClassID
0079      * @stable ICU 2.8
0080      */
0081     virtual StringEnumeration *clone() const;
0082 
0083     /**
0084      * <p>Return the number of elements that the iterator traverses.  If
0085      * the iterator is out of sync with its service, status is set to
0086      * U_ENUM_OUT_OF_SYNC_ERROR, and the return value is zero.</p>
0087      *
0088      * <p>The return value will not change except possibly as a result of
0089      * a subsequent call to reset, or if the iterator becomes out of sync.</p>
0090      *
0091      * <p>This is a convenience function. It can end up being very
0092      * expensive as all the items might have to be pre-fetched
0093      * (depending on the storage format of the data being
0094      * traversed).</p>
0095      *
0096      * @param status the error code.
0097      * @return number of elements in the iterator.
0098      *
0099      * @stable ICU 2.4 */
0100     virtual int32_t count(UErrorCode& status) const = 0;
0101 
0102     /**
0103      * <p>Returns the next element as a NUL-terminated char*.  If there
0104      * are no more elements, returns nullptr.  If the resultLength pointer
0105      * is not nullptr, the length of the string (not counting the
0106      * terminating NUL) is returned at that address.  If an error
0107      * status is returned, the value at resultLength is undefined.</p>
0108      *
0109      * <p>The returned pointer is owned by this iterator and must not be
0110      * deleted by the caller.  The pointer is valid until the next call
0111      * to next, unext, snext, reset, or the enumerator's destructor.</p>
0112      *
0113      * <p>If the iterator is out of sync with its service, status is set
0114      * to U_ENUM_OUT_OF_SYNC_ERROR and nullptr is returned.</p>
0115      *
0116      * <p>If the native service string is a char16_t* string, it is
0117      * converted to char* with the invariant converter.  If the
0118      * conversion fails (because a character cannot be converted) then
0119      * status is set to U_INVARIANT_CONVERSION_ERROR and the return
0120      * value is undefined (though not nullptr).</p>
0121      *
0122      * Starting with ICU 2.8, the default implementation calls snext()
0123      * and handles the conversion.
0124      * Either next() or snext() must be implemented differently by a subclass.
0125      *
0126      * @param status the error code.
0127      * @param resultLength a pointer to receive the length, can be nullptr.
0128      * @return a pointer to the string, or nullptr.
0129      *
0130      * @stable ICU 2.4 
0131      */
0132     virtual const char* next(int32_t *resultLength, UErrorCode& status);
0133 
0134     /**
0135      * <p>Returns the next element as a NUL-terminated char16_t*.  If there
0136      * are no more elements, returns nullptr.  If the resultLength pointer
0137      * is not nullptr, the length of the string (not counting the
0138      * terminating NUL) is returned at that address.  If an error
0139      * status is returned, the value at resultLength is undefined.</p>
0140      *
0141      * <p>The returned pointer is owned by this iterator and must not be
0142      * deleted by the caller.  The pointer is valid until the next call
0143      * to next, unext, snext, reset, or the enumerator's destructor.</p>
0144      *
0145      * <p>If the iterator is out of sync with its service, status is set
0146      * to U_ENUM_OUT_OF_SYNC_ERROR and nullptr is returned.</p>
0147      *
0148      * Starting with ICU 2.8, the default implementation calls snext()
0149      * and handles the conversion.
0150      *
0151      * @param status the error code.
0152      * @param resultLength a pointer to receive the length, can be nullptr.
0153      * @return a pointer to the string, or nullptr.
0154      *
0155      * @stable ICU 2.4 
0156      */
0157     virtual const char16_t* unext(int32_t *resultLength, UErrorCode& status);
0158 
0159     /**
0160      * <p>Returns the next element a UnicodeString*.  If there are no
0161      * more elements, returns nullptr.</p>
0162      *
0163      * <p>The returned pointer is owned by this iterator and must not be
0164      * deleted by the caller.  The pointer is valid until the next call
0165      * to next, unext, snext, reset, or the enumerator's destructor.</p>
0166      *
0167      * <p>If the iterator is out of sync with its service, status is set
0168      * to U_ENUM_OUT_OF_SYNC_ERROR and nullptr is returned.</p>
0169      *
0170      * Starting with ICU 2.8, the default implementation calls next()
0171      * and handles the conversion.
0172      * Either next() or snext() must be implemented differently by a subclass.
0173      *
0174      * @param status the error code.
0175      * @return a pointer to the string, or nullptr.
0176      *
0177      * @stable ICU 2.4 
0178      */
0179     virtual const UnicodeString* snext(UErrorCode& status);
0180 
0181     /**
0182      * <p>Resets the iterator.  This re-establishes sync with the
0183      * service and rewinds the iterator to start at the first
0184      * element.</p>
0185      *
0186      * <p>Previous pointers returned by next, unext, or snext become
0187      * invalid, and the value returned by count might change.</p>
0188      *
0189      * @param status the error code.
0190      *
0191      * @stable ICU 2.4 
0192      */
0193     virtual void reset(UErrorCode& status) = 0;
0194 
0195     /**
0196      * Compares this enumeration to other to check if both are equal
0197      *
0198      * @param that The other string enumeration to compare this object to
0199      * @return true if the enumerations are equal. false if not.
0200      * @stable ICU 3.6 
0201      */
0202     virtual bool operator==(const StringEnumeration& that)const;
0203     /**
0204      * Compares this enumeration to other to check if both are not equal
0205      *
0206      * @param that The other string enumeration to compare this object to
0207      * @return true if the enumerations are equal. false if not.
0208      * @stable ICU 3.6 
0209      */
0210     virtual bool operator!=(const StringEnumeration& that)const;
0211 
0212 protected:
0213     /**
0214      * UnicodeString field for use with default implementations and subclasses.
0215      * @stable ICU 2.8
0216      */
0217     UnicodeString unistr;
0218     /**
0219      * char * default buffer for use with default implementations and subclasses.
0220      * @stable ICU 2.8
0221      */
0222     char charsBuffer[32];
0223     /**
0224      * char * buffer for use with default implementations and subclasses.
0225      * Allocated in constructor and in ensureCharsCapacity().
0226      * @stable ICU 2.8
0227      */
0228     char *chars;
0229     /**
0230      * Capacity of chars, for use with default implementations and subclasses.
0231      * @stable ICU 2.8
0232      */
0233     int32_t charsCapacity;
0234 
0235     /**
0236      * Default constructor for use with default implementations and subclasses.
0237      * @stable ICU 2.8
0238      */
0239     StringEnumeration();
0240 
0241     /**
0242      * Ensures that chars is at least as large as the requested capacity.
0243      * For use with default implementations and subclasses.
0244      *
0245      * @param capacity Requested capacity.
0246      * @param status ICU in/out error code.
0247      * @stable ICU 2.8
0248      */
0249     void ensureCharsCapacity(int32_t capacity, UErrorCode &status);
0250 
0251     /**
0252      * Converts s to Unicode and sets unistr to the result.
0253      * For use with default implementations and subclasses,
0254      * especially for implementations of snext() in terms of next().
0255      * This is provided with a helper function instead of a default implementation
0256      * of snext() to avoid potential infinite loops between next() and snext().
0257      *
0258      * For example:
0259      * \code
0260      * const UnicodeString* snext(UErrorCode& status) {
0261      *   int32_t resultLength=0;
0262      *   const char *s=next(&resultLength, status);
0263      *   return setChars(s, resultLength, status);
0264      * }
0265      * \endcode
0266      *
0267      * @param s String to be converted to Unicode.
0268      * @param length Length of the string.
0269      * @param status ICU in/out error code.
0270      * @return A pointer to unistr.
0271      * @stable ICU 2.8
0272      */
0273     UnicodeString *setChars(const char *s, int32_t length, UErrorCode &status);
0274 };
0275 
0276 U_NAMESPACE_END
0277 
0278 #endif /* U_SHOW_CPLUSPLUS_API */
0279 
0280 /* STRENUM_H */
0281 #endif