1 // © 2018 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 #ifndef __FORMVAL_IMPL_H__
5 #define __FORMVAL_IMPL_H__
7 #include "unicode/utypes.h"
8 #if !UCONFIG_NO_FORMATTING
10 // This file contains compliant implementations of FormattedValue which can be
11 // leveraged by ICU formatters.
13 // Each implementation is defined in its own cpp file in order to split
14 // dependencies more modularly.
16 #include "unicode/formattedvalue.h"
17 #include "capi_helper.h"
21 #include "number_stringbuilder.h"
25 * Represents the type of constraint for ConstrainedFieldPosition.
27 * Constraints are used to control the behavior of iteration in FormattedValue.
31 typedef enum UCFPosConstraintType
{
33 * Represents the lack of a constraint.
35 * This is the value of fConstraint if no "constrain" methods were called.
39 UCFPOS_CONSTRAINT_NONE
= 0,
42 * Represents that the field category is constrained.
44 * This is the value of fConstraint if constraintCategory was called.
46 * FormattedValue implementations should not change the field category
47 * while this constraint is active.
51 UCFPOS_CONSTRAINT_CATEGORY
,
54 * Represents that the field and field category are constrained.
56 * This is the value of fConstraint if constraintField was called.
58 * FormattedValue implementations should not change the field or field category
59 * while this constraint is active.
63 UCFPOS_CONSTRAINT_FIELD
64 } UCFPosConstraintType
;
70 /** Implementation using FieldPositionHandler to accept fields. */
71 class FormattedValueFieldPositionIteratorImpl
: public UMemory
, public FormattedValue
{
74 /** @param initialFieldCapacity Initially allocate space for this many fields. */
75 FormattedValueFieldPositionIteratorImpl(int32_t initialFieldCapacity
, UErrorCode
& status
);
77 virtual ~FormattedValueFieldPositionIteratorImpl();
79 // Implementation of FormattedValue (const):
81 UnicodeString
toString(UErrorCode
& status
) const U_OVERRIDE
;
82 UnicodeString
toTempString(UErrorCode
& status
) const U_OVERRIDE
;
83 Appendable
& appendTo(Appendable
& appendable
, UErrorCode
& status
) const U_OVERRIDE
;
84 UBool
nextPosition(ConstrainedFieldPosition
& cfpos
, UErrorCode
& status
) const U_OVERRIDE
;
86 // Additional methods used during construction phase only (non-const):
88 FieldPositionIteratorHandler
getHandler(UErrorCode
& status
);
89 void appendString(UnicodeString string
, UErrorCode
& status
);
92 * Computes the spans for duplicated values.
93 * For example, if the string has fields:
95 * ...aa..[b.cc]..d.[bb.e.c]..a..
97 * then the spans will be the bracketed regions.
99 * Assumes that the currently known fields are sorted
100 * and all in the same category.
102 void addOverlapSpans(UFieldCategory spanCategory
, int8_t firstIndex
, UErrorCode
& status
);
105 * Sorts the fields: start index first, length second.
110 UnicodeString fString
;
115 class FormattedValueNumberStringBuilderImpl
: public UMemory
, public FormattedValue
{
118 FormattedValueNumberStringBuilderImpl(number::impl::Field numericField
);
120 virtual ~FormattedValueNumberStringBuilderImpl();
122 // Implementation of FormattedValue (const):
124 UnicodeString
toString(UErrorCode
& status
) const U_OVERRIDE
;
125 UnicodeString
toTempString(UErrorCode
& status
) const U_OVERRIDE
;
126 Appendable
& appendTo(Appendable
& appendable
, UErrorCode
& status
) const U_OVERRIDE
;
127 UBool
nextPosition(ConstrainedFieldPosition
& cfpos
, UErrorCode
& status
) const U_OVERRIDE
;
129 inline number::impl::NumberStringBuilder
& getStringRef() {
133 inline const number::impl::NumberStringBuilder
& getStringRef() const {
138 number::impl::NumberStringBuilder fString
;
139 number::impl::Field fNumericField
;
143 // C API Helpers for FormattedValue
144 // Magic number as ASCII == "UFV"
145 struct UFormattedValueImpl
;
146 typedef IcuCApiHelper
<UFormattedValue
, UFormattedValueImpl
, 0x55465600> UFormattedValueApiHelper
;
147 struct UFormattedValueImpl
: public UMemory
, public UFormattedValueApiHelper
{
148 // This pointer should be set by the child class.
149 FormattedValue
* fFormattedValue
= nullptr;
153 /** Boilerplate to check for valid status before dereferencing the fData pointer. */
154 #define UPRV_FORMATTED_VALUE_METHOD_GUARD(returnExpression) \
155 if (U_FAILURE(status)) { \
156 return returnExpression; \
158 if (fData == nullptr) { \
159 status = fErrorCode; \
160 return returnExpression; \
164 /** Implementation of the methods from U_FORMATTED_VALUE_SUBCLASS_AUTO. */
165 #define UPRV_FORMATTED_VALUE_SUBCLASS_AUTO_IMPL(Name) \
166 Name::Name(Name&& src) U_NOEXCEPT \
167 : fData(src.fData), fErrorCode(src.fErrorCode) { \
168 src.fData = nullptr; \
169 src.fErrorCode = U_INVALID_STATE_ERROR; \
175 Name& Name::operator=(Name&& src) U_NOEXCEPT { \
178 src.fData = nullptr; \
179 fErrorCode = src.fErrorCode; \
180 src.fErrorCode = U_INVALID_STATE_ERROR; \
183 UnicodeString Name::toString(UErrorCode& status) const { \
184 UPRV_FORMATTED_VALUE_METHOD_GUARD(ICU_Utility::makeBogusString()) \
185 return fData->toString(status); \
187 UnicodeString Name::toTempString(UErrorCode& status) const { \
188 UPRV_FORMATTED_VALUE_METHOD_GUARD(ICU_Utility::makeBogusString()) \
189 return fData->toTempString(status); \
191 Appendable& Name::appendTo(Appendable& appendable, UErrorCode& status) const { \
192 UPRV_FORMATTED_VALUE_METHOD_GUARD(appendable) \
193 return fData->appendTo(appendable, status); \
195 UBool Name::nextPosition(ConstrainedFieldPosition& cfpos, UErrorCode& status) const { \
196 UPRV_FORMATTED_VALUE_METHOD_GUARD(FALSE) \
197 return fData->nextPosition(cfpos, status); \
201 /** Like UPRV_FORMATTED_VALUE_CAPI_AUTO_IMPL but without impl type declarations. */
202 #define UPRV_FORMATTED_VALUE_CAPI_NO_IMPLTYPE_AUTO_IMPL(CType, ImplType, HelperType, Prefix) \
203 U_CAPI CType* U_EXPORT2 \
204 Prefix ## _openResult (UErrorCode* ec) { \
205 if (U_FAILURE(*ec)) { \
208 ImplType* impl = new ImplType(); \
209 if (impl == nullptr) { \
210 *ec = U_MEMORY_ALLOCATION_ERROR; \
213 return static_cast<HelperType*>(impl)->exportForC(); \
215 U_DRAFT const UFormattedValue* U_EXPORT2 \
216 Prefix ## _resultAsValue (const CType* uresult, UErrorCode* ec) { \
217 const ImplType* result = HelperType::validate(uresult, *ec); \
218 if (U_FAILURE(*ec)) { return nullptr; } \
219 return static_cast<const UFormattedValueApiHelper*>(result)->exportConstForC(); \
221 U_CAPI void U_EXPORT2 \
222 Prefix ## _closeResult (CType* uresult) { \
223 UErrorCode localStatus = U_ZERO_ERROR; \
224 const ImplType* impl = HelperType::validate(uresult, localStatus); \
230 * Implementation of the standard methods for a UFormattedValue "subclass" C API.
231 * @param CPPType The public C++ type, like FormattedList
232 * @param CType The public C type, like UFormattedList
233 * @param ImplType A name to use for the implementation class
234 * @param HelperType A name to use for the "mixin" typedef for C API conversion
235 * @param Prefix The C API prefix, like ulistfmt
236 * @param MagicNumber A unique 32-bit number to use to identify this type
238 #define UPRV_FORMATTED_VALUE_CAPI_AUTO_IMPL(CPPType, CType, ImplType, HelperType, Prefix, MagicNumber) \
241 typedef IcuCApiHelper<CType, ImplType, MagicNumber> HelperType; \
242 class ImplType : public UFormattedValueImpl, public HelperType { \
248 ImplType::ImplType() { \
249 fFormattedValue = &fImpl; \
251 ImplType::~ImplType() {} \
253 UPRV_FORMATTED_VALUE_CAPI_NO_IMPLTYPE_AUTO_IMPL(CType, ImplType, HelperType, Prefix)
258 #endif /* #if !UCONFIG_NO_FORMATTING */
259 #endif // __FORMVAL_IMPL_H__