1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
5 * Copyright (C) 2011-2012, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
8 * file name: appendable.h
10 * tab size: 8 (not used)
13 * created on: 2010dec07
14 * created by: Markus W. Scherer
17 #ifndef __APPENDABLE_H__
18 #define __APPENDABLE_H__
22 * \brief C++ API: Appendable class: Sink for Unicode code points and 16-bit code units (char16_ts).
25 #include "unicode/utypes.h"
26 #include "unicode/uobject.h"
28 #if U_SHOW_CPLUSPLUS_API
34 * Base class for objects to which Unicode characters and strings can be appended.
35 * Combines elements of Java Appendable and ICU4C ByteSink.
37 * This class can be used in APIs where it does not matter whether the actual destination is
38 * a UnicodeString, a char16_t[] array, a UnicodeSet, or any other object
39 * that receives and processes characters and/or strings.
41 * Implementation classes must implement at least appendCodeUnit(char16_t).
42 * The base class provides default implementations for the other methods.
44 * The methods do not take UErrorCode parameters.
45 * If an error occurs (e.g., out-of-memory),
46 * in addition to returning FALSE from failing operations,
47 * the implementation must prevent unexpected behavior (e.g., crashes)
48 * from further calls and should make the error condition available separately
49 * (e.g., store a UErrorCode, make/keep a UnicodeString bogus).
52 class U_COMMON_API Appendable
: public UObject
{
61 * Appends a 16-bit code unit.
63 * @return TRUE if the operation succeeded
66 virtual UBool
appendCodeUnit(char16_t c
) = 0;
69 * Appends a code point.
70 * The default implementation calls appendCodeUnit(char16_t) once or twice.
71 * @param c code point 0..0x10ffff
72 * @return TRUE if the operation succeeded
75 virtual UBool
appendCodePoint(UChar32 c
);
79 * The default implementation calls appendCodeUnit(char16_t) for each code unit.
80 * @param s string, must not be NULL if length!=0
81 * @param length string length, or -1 if NUL-terminated
82 * @return TRUE if the operation succeeded
85 virtual UBool
appendString(const char16_t *s
, int32_t length
);
88 * Tells the object that the caller is going to append roughly
89 * appendCapacity char16_ts. A subclass might use this to pre-allocate
90 * a larger buffer if necessary.
91 * The default implementation does nothing. (It always returns TRUE.)
92 * @param appendCapacity estimated number of char16_ts that will be appended
93 * @return TRUE if the operation succeeded
96 virtual UBool
reserveAppendCapacity(int32_t appendCapacity
);
99 * Returns a writable buffer for appending and writes the buffer's capacity to
100 * *resultCapacity. Guarantees *resultCapacity>=minCapacity.
101 * May return a pointer to the caller-owned scratch buffer which must have
102 * scratchCapacity>=minCapacity.
103 * The returned buffer is only valid until the next operation
104 * on this Appendable.
106 * After writing at most *resultCapacity char16_ts, call appendString() with the
107 * pointer returned from this function and the number of char16_ts written.
108 * Many appendString() implementations will avoid copying char16_ts if this function
109 * returned an internal buffer.
111 * Partial usage example:
114 * char16_t* buffer = app.getAppendBuffer(..., &capacity);
115 * ... Write n char16_ts into buffer, with n <= capacity.
116 * app.appendString(buffer, n);
118 * In many implementations, that call to append will avoid copying char16_ts.
120 * If the Appendable allocates or reallocates an internal buffer, it should use
121 * the desiredCapacityHint if appropriate.
122 * If a caller cannot provide a reasonable guess at the desired capacity,
123 * it should pass desiredCapacityHint=0.
125 * If a non-scratch buffer is returned, the caller may only pass
126 * a prefix to it to appendString().
127 * That is, it is not correct to pass an interior pointer to appendString().
129 * The default implementation always returns the scratch buffer.
131 * @param minCapacity required minimum capacity of the returned buffer;
132 * must be non-negative
133 * @param desiredCapacityHint desired capacity of the returned buffer;
134 * must be non-negative
135 * @param scratch default caller-owned buffer
136 * @param scratchCapacity capacity of the scratch buffer
137 * @param resultCapacity pointer to an integer which will be set to the
138 * capacity of the returned buffer
139 * @return a buffer with *resultCapacity>=minCapacity
142 virtual char16_t *getAppendBuffer(int32_t minCapacity
,
143 int32_t desiredCapacityHint
,
144 char16_t *scratch
, int32_t scratchCapacity
,
145 int32_t *resultCapacity
);
149 * An Appendable implementation which writes to a UnicodeString.
151 * This class is not intended for public subclassing.
154 class U_COMMON_API UnicodeStringAppendable
: public Appendable
{
157 * Aliases the UnicodeString (keeps its reference) for writing.
158 * @param s The UnicodeString to which this Appendable will write.
161 explicit UnicodeStringAppendable(UnicodeString
&s
) : str(s
) {}
167 ~UnicodeStringAppendable();
170 * Appends a 16-bit code unit to the string.
172 * @return TRUE if the operation succeeded
175 virtual UBool
appendCodeUnit(char16_t c
);
178 * Appends a code point to the string.
179 * @param c code point 0..0x10ffff
180 * @return TRUE if the operation succeeded
183 virtual UBool
appendCodePoint(UChar32 c
);
186 * Appends a string to the UnicodeString.
187 * @param s string, must not be NULL if length!=0
188 * @param length string length, or -1 if NUL-terminated
189 * @return TRUE if the operation succeeded
192 virtual UBool
appendString(const char16_t *s
, int32_t length
);
195 * Tells the UnicodeString that the caller is going to append roughly
196 * appendCapacity char16_ts.
197 * @param appendCapacity estimated number of char16_ts that will be appended
198 * @return TRUE if the operation succeeded
201 virtual UBool
reserveAppendCapacity(int32_t appendCapacity
);
204 * Returns a writable buffer for appending and writes the buffer's capacity to
205 * *resultCapacity. Guarantees *resultCapacity>=minCapacity.
206 * May return a pointer to the caller-owned scratch buffer which must have
207 * scratchCapacity>=minCapacity.
208 * The returned buffer is only valid until the next write operation
209 * on the UnicodeString.
211 * For details see Appendable::getAppendBuffer().
213 * @param minCapacity required minimum capacity of the returned buffer;
214 * must be non-negative
215 * @param desiredCapacityHint desired capacity of the returned buffer;
216 * must be non-negative
217 * @param scratch default caller-owned buffer
218 * @param scratchCapacity capacity of the scratch buffer
219 * @param resultCapacity pointer to an integer which will be set to the
220 * capacity of the returned buffer
221 * @return a buffer with *resultCapacity>=minCapacity
224 virtual char16_t *getAppendBuffer(int32_t minCapacity
,
225 int32_t desiredCapacityHint
,
226 char16_t *scratch
, int32_t scratchCapacity
,
227 int32_t *resultCapacity
);
234 #endif // U_SHOW_CPLUSPLUS_API
236 #endif // __APPENDABLE_H__