michael@0: /* michael@0: ******************************************************************************* michael@0: * Copyright (C) 2011-2012, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: ******************************************************************************* michael@0: * file name: appendable.h michael@0: * encoding: US-ASCII michael@0: * tab size: 8 (not used) michael@0: * indentation:4 michael@0: * michael@0: * created on: 2010dec07 michael@0: * created by: Markus W. Scherer michael@0: */ michael@0: michael@0: #ifndef __APPENDABLE_H__ michael@0: #define __APPENDABLE_H__ michael@0: michael@0: /** michael@0: * \file michael@0: * \brief C++ API: Appendable class: Sink for Unicode code points and 16-bit code units (UChars). michael@0: */ michael@0: michael@0: #include "unicode/utypes.h" michael@0: #include "unicode/uobject.h" michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: michael@0: class UnicodeString; michael@0: michael@0: /** michael@0: * Base class for objects to which Unicode characters and strings can be appended. michael@0: * Combines elements of Java Appendable and ICU4C ByteSink. michael@0: * michael@0: * This class can be used in APIs where it does not matter whether the actual destination is michael@0: * a UnicodeString, a UChar[] array, a UnicodeSet, or any other object michael@0: * that receives and processes characters and/or strings. michael@0: * michael@0: * Implementation classes must implement at least appendCodeUnit(UChar). michael@0: * The base class provides default implementations for the other methods. michael@0: * michael@0: * The methods do not take UErrorCode parameters. michael@0: * If an error occurs (e.g., out-of-memory), michael@0: * in addition to returning FALSE from failing operations, michael@0: * the implementation must prevent unexpected behavior (e.g., crashes) michael@0: * from further calls and should make the error condition available separately michael@0: * (e.g., store a UErrorCode, make/keep a UnicodeString bogus). michael@0: * @stable ICU 4.8 michael@0: */ michael@0: class U_COMMON_API Appendable : public UObject { michael@0: public: michael@0: /** michael@0: * Destructor. michael@0: * @stable ICU 4.8 michael@0: */ michael@0: ~Appendable(); michael@0: michael@0: /** michael@0: * Appends a 16-bit code unit. michael@0: * @param c code unit michael@0: * @return TRUE if the operation succeeded michael@0: * @stable ICU 4.8 michael@0: */ michael@0: virtual UBool appendCodeUnit(UChar c) = 0; michael@0: michael@0: /** michael@0: * Appends a code point. michael@0: * The default implementation calls appendCodeUnit(UChar) once or twice. michael@0: * @param c code point 0..0x10ffff michael@0: * @return TRUE if the operation succeeded michael@0: * @stable ICU 4.8 michael@0: */ michael@0: virtual UBool appendCodePoint(UChar32 c); michael@0: michael@0: /** michael@0: * Appends a string. michael@0: * The default implementation calls appendCodeUnit(UChar) for each code unit. michael@0: * @param s string, must not be NULL if length!=0 michael@0: * @param length string length, or -1 if NUL-terminated michael@0: * @return TRUE if the operation succeeded michael@0: * @stable ICU 4.8 michael@0: */ michael@0: virtual UBool appendString(const UChar *s, int32_t length); michael@0: michael@0: /** michael@0: * Tells the object that the caller is going to append roughly michael@0: * appendCapacity UChars. A subclass might use this to pre-allocate michael@0: * a larger buffer if necessary. michael@0: * The default implementation does nothing. (It always returns TRUE.) michael@0: * @param appendCapacity estimated number of UChars that will be appended michael@0: * @return TRUE if the operation succeeded michael@0: * @stable ICU 4.8 michael@0: */ michael@0: virtual UBool reserveAppendCapacity(int32_t appendCapacity); michael@0: michael@0: /** michael@0: * Returns a writable buffer for appending and writes the buffer's capacity to michael@0: * *resultCapacity. Guarantees *resultCapacity>=minCapacity. michael@0: * May return a pointer to the caller-owned scratch buffer which must have michael@0: * scratchCapacity>=minCapacity. michael@0: * The returned buffer is only valid until the next operation michael@0: * on this Appendable. michael@0: * michael@0: * After writing at most *resultCapacity UChars, call appendString() with the michael@0: * pointer returned from this function and the number of UChars written. michael@0: * Many appendString() implementations will avoid copying UChars if this function michael@0: * returned an internal buffer. michael@0: * michael@0: * Partial usage example: michael@0: * \code michael@0: * int32_t capacity; michael@0: * UChar* buffer = app.getAppendBuffer(..., &capacity); michael@0: * ... Write n UChars into buffer, with n <= capacity. michael@0: * app.appendString(buffer, n); michael@0: * \endcode michael@0: * In many implementations, that call to append will avoid copying UChars. michael@0: * michael@0: * If the Appendable allocates or reallocates an internal buffer, it should use michael@0: * the desiredCapacityHint if appropriate. michael@0: * If a caller cannot provide a reasonable guess at the desired capacity, michael@0: * it should pass desiredCapacityHint=0. michael@0: * michael@0: * If a non-scratch buffer is returned, the caller may only pass michael@0: * a prefix to it to appendString(). michael@0: * That is, it is not correct to pass an interior pointer to appendString(). michael@0: * michael@0: * The default implementation always returns the scratch buffer. michael@0: * michael@0: * @param minCapacity required minimum capacity of the returned buffer; michael@0: * must be non-negative michael@0: * @param desiredCapacityHint desired capacity of the returned buffer; michael@0: * must be non-negative michael@0: * @param scratch default caller-owned buffer michael@0: * @param scratchCapacity capacity of the scratch buffer michael@0: * @param resultCapacity pointer to an integer which will be set to the michael@0: * capacity of the returned buffer michael@0: * @return a buffer with *resultCapacity>=minCapacity michael@0: * @stable ICU 4.8 michael@0: */ michael@0: virtual UChar *getAppendBuffer(int32_t minCapacity, michael@0: int32_t desiredCapacityHint, michael@0: UChar *scratch, int32_t scratchCapacity, michael@0: int32_t *resultCapacity); michael@0: }; michael@0: michael@0: /** michael@0: * An Appendable implementation which writes to a UnicodeString. michael@0: * michael@0: * This class is not intended for public subclassing. michael@0: * @stable ICU 4.8 michael@0: */ michael@0: class U_COMMON_API UnicodeStringAppendable : public Appendable { michael@0: public: michael@0: /** michael@0: * Aliases the UnicodeString (keeps its reference) for writing. michael@0: * @param s The UnicodeString to which this Appendable will write. michael@0: * @stable ICU 4.8 michael@0: */ michael@0: explicit UnicodeStringAppendable(UnicodeString &s) : str(s) {} michael@0: michael@0: /** michael@0: * Destructor. michael@0: * @stable ICU 4.8 michael@0: */ michael@0: ~UnicodeStringAppendable(); michael@0: michael@0: /** michael@0: * Appends a 16-bit code unit to the string. michael@0: * @param c code unit michael@0: * @return TRUE if the operation succeeded michael@0: * @stable ICU 4.8 michael@0: */ michael@0: virtual UBool appendCodeUnit(UChar c); michael@0: michael@0: /** michael@0: * Appends a code point to the string. michael@0: * @param c code point 0..0x10ffff michael@0: * @return TRUE if the operation succeeded michael@0: * @stable ICU 4.8 michael@0: */ michael@0: virtual UBool appendCodePoint(UChar32 c); michael@0: michael@0: /** michael@0: * Appends a string to the UnicodeString. michael@0: * @param s string, must not be NULL if length!=0 michael@0: * @param length string length, or -1 if NUL-terminated michael@0: * @return TRUE if the operation succeeded michael@0: * @stable ICU 4.8 michael@0: */ michael@0: virtual UBool appendString(const UChar *s, int32_t length); michael@0: michael@0: /** michael@0: * Tells the UnicodeString that the caller is going to append roughly michael@0: * appendCapacity UChars. michael@0: * @param appendCapacity estimated number of UChars that will be appended michael@0: * @return TRUE if the operation succeeded michael@0: * @stable ICU 4.8 michael@0: */ michael@0: virtual UBool reserveAppendCapacity(int32_t appendCapacity); michael@0: michael@0: /** michael@0: * Returns a writable buffer for appending and writes the buffer's capacity to michael@0: * *resultCapacity. Guarantees *resultCapacity>=minCapacity. michael@0: * May return a pointer to the caller-owned scratch buffer which must have michael@0: * scratchCapacity>=minCapacity. michael@0: * The returned buffer is only valid until the next write operation michael@0: * on the UnicodeString. michael@0: * michael@0: * For details see Appendable::getAppendBuffer(). michael@0: * michael@0: * @param minCapacity required minimum capacity of the returned buffer; michael@0: * must be non-negative michael@0: * @param desiredCapacityHint desired capacity of the returned buffer; michael@0: * must be non-negative michael@0: * @param scratch default caller-owned buffer michael@0: * @param scratchCapacity capacity of the scratch buffer michael@0: * @param resultCapacity pointer to an integer which will be set to the michael@0: * capacity of the returned buffer michael@0: * @return a buffer with *resultCapacity>=minCapacity michael@0: * @stable ICU 4.8 michael@0: */ michael@0: virtual UChar *getAppendBuffer(int32_t minCapacity, michael@0: int32_t desiredCapacityHint, michael@0: UChar *scratch, int32_t scratchCapacity, michael@0: int32_t *resultCapacity); michael@0: michael@0: private: michael@0: UnicodeString &str; michael@0: }; michael@0: michael@0: U_NAMESPACE_END michael@0: michael@0: #endif // __APPENDABLE_H__