Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:13:05

0001 // © 2018 and later: Unicode, Inc. and others.
0002 // License & terms of use: http://www.unicode.org/copyright.html
0003 
0004 #ifndef __FORMATTEDVALUE_H__
0005 #define __FORMATTEDVALUE_H__
0006 
0007 #include "unicode/utypes.h"
0008 
0009 #if U_SHOW_CPLUSPLUS_API
0010 
0011 #if !UCONFIG_NO_FORMATTING
0012 
0013 #include "unicode/appendable.h"
0014 #include "unicode/fpositer.h"
0015 #include "unicode/unistr.h"
0016 #include "unicode/uformattedvalue.h"
0017 
0018 U_NAMESPACE_BEGIN
0019 
0020 /**
0021  * \file
0022  * \brief C++ API: Abstract operations for localized strings.
0023  * 
0024  * This file contains declarations for classes that deal with formatted strings. A number
0025  * of APIs throughout ICU use these classes for expressing their localized output.
0026  */
0027 
0028 /**
0029  * Represents a span of a string containing a given field.
0030  *
0031  * This class differs from FieldPosition in the following ways:
0032  *
0033  *   1. It has information on the field category.
0034  *   2. It allows you to set constraints to use when iterating over field positions.
0035  *   3. It is used for the newer FormattedValue APIs.
0036  *
0037  * This class is not intended for public subclassing.
0038  *
0039  * @stable ICU 64
0040  */
0041 class U_I18N_API ConstrainedFieldPosition : public UMemory {
0042   public:
0043 
0044     /**
0045      * Initializes a ConstrainedFieldPosition.
0046      *
0047      * By default, the ConstrainedFieldPosition has no iteration constraints.
0048      *
0049      * @stable ICU 64
0050      */
0051     ConstrainedFieldPosition();
0052 
0053     /** @stable ICU 64 */
0054     ~ConstrainedFieldPosition();
0055 
0056     /**
0057      * Resets this ConstrainedFieldPosition to its initial state, as if it were newly created:
0058      *
0059      * - Removes any constraints that may have been set on the instance.
0060      * - Resets the iteration position.
0061      *
0062      * @stable ICU 64
0063      */
0064     void reset();
0065 
0066     /**
0067      * Sets a constraint on the field category.
0068      * 
0069      * When this instance of ConstrainedFieldPosition is passed to FormattedValue#nextPosition,
0070      * positions are skipped unless they have the given category.
0071      *
0072      * Any previously set constraints are cleared.
0073      *
0074      * For example, to loop over only the number-related fields:
0075      *
0076      *     ConstrainedFieldPosition cfpos;
0077      *     cfpos.constrainCategory(UFIELDCATEGORY_NUMBER_FORMAT);
0078      *     while (fmtval.nextPosition(cfpos, status)) {
0079      *         // handle the number-related field position
0080      *     }
0081      *
0082      * Changing the constraint while in the middle of iterating over a FormattedValue
0083      * does not generally have well-defined behavior.
0084      *
0085      * @param category The field category to fix when iterating.
0086      * @stable ICU 64
0087      */
0088     void constrainCategory(int32_t category);
0089 
0090     /**
0091      * Sets a constraint on the category and field.
0092      * 
0093      * When this instance of ConstrainedFieldPosition is passed to FormattedValue#nextPosition,
0094      * positions are skipped unless they have the given category and field.
0095      *
0096      * Any previously set constraints are cleared.
0097      *
0098      * For example, to loop over all grouping separators:
0099      *
0100      *     ConstrainedFieldPosition cfpos;
0101      *     cfpos.constrainField(UFIELDCATEGORY_NUMBER_FORMAT, UNUM_GROUPING_SEPARATOR_FIELD);
0102      *     while (fmtval.nextPosition(cfpos, status)) {
0103      *         // handle the grouping separator position
0104      *     }
0105      *
0106      * Changing the constraint while in the middle of iterating over a FormattedValue
0107      * does not generally have well-defined behavior.
0108      *
0109      * @param category The field category to fix when iterating.
0110      * @param field The field to fix when iterating.
0111      * @stable ICU 64
0112      */
0113     void constrainField(int32_t category, int32_t field);
0114 
0115     /**
0116      * Gets the field category for the current position.
0117      *
0118      * The return value is well-defined only after
0119      * FormattedValue#nextPosition returns true.
0120      *
0121      * @return The field category saved in the instance.
0122      * @stable ICU 64
0123      */
0124     inline int32_t getCategory() const {
0125         return fCategory;
0126     }
0127 
0128     /**
0129      * Gets the field for the current position.
0130      *
0131      * The return value is well-defined only after
0132      * FormattedValue#nextPosition returns true.
0133      *
0134      * @return The field saved in the instance.
0135      * @stable ICU 64
0136      */
0137     inline int32_t getField() const {
0138         return fField;
0139     }
0140 
0141     /**
0142      * Gets the INCLUSIVE start index for the current position.
0143      *
0144      * The return value is well-defined only after FormattedValue#nextPosition returns true.
0145      *
0146      * @return The start index saved in the instance.
0147      * @stable ICU 64
0148      */
0149     inline int32_t getStart() const {
0150         return fStart;
0151     }
0152 
0153     /**
0154      * Gets the EXCLUSIVE end index stored for the current position.
0155      *
0156      * The return value is well-defined only after FormattedValue#nextPosition returns true.
0157      *
0158      * @return The end index saved in the instance.
0159      * @stable ICU 64
0160      */
0161     inline int32_t getLimit() const {
0162         return fLimit;
0163     }
0164 
0165     ////////////////////////////////////////////////////////////////////
0166     //// The following methods are for FormattedValue implementers; ////
0167     //// most users can ignore them.                                ////
0168     ////////////////////////////////////////////////////////////////////
0169 
0170     /**
0171      * Gets an int64 that FormattedValue implementations may use for storage.
0172      *
0173      * The initial value is zero.
0174      *
0175      * Users of FormattedValue should not need to call this method.
0176      *
0177      * @return The current iteration context from {@link #setInt64IterationContext}.
0178      * @stable ICU 64
0179      */
0180     inline int64_t getInt64IterationContext() const {
0181         return fContext;
0182     }
0183 
0184     /**
0185      * Sets an int64 that FormattedValue implementations may use for storage.
0186      *
0187      * Intended to be used by FormattedValue implementations.
0188      *
0189      * @param context The new iteration context.
0190      * @stable ICU 64
0191      */
0192     void setInt64IterationContext(int64_t context);
0193 
0194     /**
0195      * Determines whether a given field should be included given the
0196      * constraints.
0197      *
0198      * Intended to be used by FormattedValue implementations.
0199      *
0200      * @param category The category to test.
0201      * @param field The field to test.
0202      * @stable ICU 64
0203      */
0204     UBool matchesField(int32_t category, int32_t field) const;
0205 
0206     /**
0207      * Sets new values for the primary public getters.
0208      *
0209      * Intended to be used by FormattedValue implementations.
0210      *
0211      * It is up to the implementation to ensure that the user-requested
0212      * constraints are satisfied. This method does not check!
0213      *
0214      * @param category The new field category.
0215      * @param field The new field.
0216      * @param start The new inclusive start index.
0217      * @param limit The new exclusive end index.
0218      * @stable ICU 64
0219      */
0220     void setState(
0221         int32_t category,
0222         int32_t field,
0223         int32_t start,
0224         int32_t limit);
0225 
0226   private:
0227     int64_t fContext = 0LL;
0228     int32_t fField = 0;
0229     int32_t fStart = 0;
0230     int32_t fLimit = 0;
0231     int32_t fCategory = UFIELD_CATEGORY_UNDEFINED;
0232     int8_t fConstraint = 0;
0233 };
0234 
0235 /**
0236  * An abstract formatted value: a string with associated field attributes.
0237  * Many formatters format to classes implementing FormattedValue.
0238  *
0239  * @stable ICU 64
0240  */
0241 class U_I18N_API FormattedValue /* not : public UObject because this is an interface/mixin class */ {
0242   public:
0243     /** @stable ICU 64 */
0244     virtual ~FormattedValue();
0245 
0246     /**
0247      * Returns the formatted string as a self-contained UnicodeString.
0248      *
0249      * If you need the string within the current scope only, consider #toTempString.
0250      *
0251      * @param status Set if an error occurs.
0252      * @return a UnicodeString containing the formatted string.
0253      *
0254      * @stable ICU 64
0255      */
0256     virtual UnicodeString toString(UErrorCode& status) const = 0;
0257 
0258     /**
0259      * Returns the formatted string as a read-only alias to memory owned by the FormattedValue.
0260      *
0261      * The return value is valid only as long as this FormattedValue is present and unchanged in
0262      * memory. If you need the string outside the current scope, consider #toString.
0263      *
0264      * The buffer returned by calling UnicodeString#getBuffer() on the return value is
0265      * guaranteed to be NUL-terminated.
0266      *
0267      * @param status Set if an error occurs.
0268      * @return a temporary UnicodeString containing the formatted string.
0269      *
0270      * @stable ICU 64
0271      */
0272     virtual UnicodeString toTempString(UErrorCode& status) const = 0;
0273 
0274     /**
0275      * Appends the formatted string to an Appendable.
0276      *
0277      * @param appendable
0278      *         The Appendable to which to append the string output.
0279      * @param status Set if an error occurs.
0280      * @return The same Appendable, for chaining.
0281      *
0282      * @stable ICU 64
0283      * @see Appendable
0284      */
0285     virtual Appendable& appendTo(Appendable& appendable, UErrorCode& status) const = 0;
0286 
0287     /**
0288      * Iterates over field positions in the FormattedValue. This lets you determine the position
0289      * of specific types of substrings, like a month or a decimal separator.
0290      *
0291      * To loop over all field positions:
0292      *
0293      *     ConstrainedFieldPosition cfpos;
0294      *     while (fmtval.nextPosition(cfpos, status)) {
0295      *         // handle the field position; get information from cfpos
0296      *     }
0297      *
0298      * @param cfpos
0299      *         The object used for iteration state. This can provide constraints to iterate over
0300      *         only one specific category or field;
0301      *         see ConstrainedFieldPosition#constrainCategory
0302      *         and ConstrainedFieldPosition#constrainField.
0303      * @param status Set if an error occurs.
0304      * @return true if a new occurrence of the field was found;
0305      *         false otherwise or if an error was set.
0306      *
0307      * @stable ICU 64
0308      */
0309     virtual UBool nextPosition(ConstrainedFieldPosition& cfpos, UErrorCode& status) const = 0;
0310 };
0311 
0312 U_NAMESPACE_END
0313 
0314 #endif /* #if !UCONFIG_NO_FORMATTING */
0315 
0316 #endif /* U_SHOW_CPLUSPLUS_API */
0317 
0318 #endif // __FORMATTEDVALUE_H__