xref: /aosp_15_r20/external/icu/libicu/cts_headers/unicode/measure.h (revision 0e209d3975ff4a8c132096b14b0e9364a753506e)
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 **********************************************************************
5 * Copyright (c) 2004-2015, International Business Machines
6 * Corporation and others.  All Rights Reserved.
7 **********************************************************************
8 * Author: Alan Liu
9 * Created: April 26, 2004
10 * Since: ICU 3.0
11 **********************************************************************
12 */
13 #ifndef __MEASURE_H__
14 #define __MEASURE_H__
15 
16 #include "unicode/utypes.h"
17 
18 #if U_SHOW_CPLUSPLUS_API
19 
20 /**
21  * \file
22  * \brief C++ API: MeasureUnit object.
23  */
24 
25 #if !UCONFIG_NO_FORMATTING
26 
27 #include "unicode/fmtable.h"
28 
29 U_NAMESPACE_BEGIN
30 
31 class MeasureUnit;
32 
33 /**
34  * An amount of a specified unit, consisting of a number and a Unit.
35  * For example, a length measure consists of a number and a length
36  * unit, such as feet or meters.
37  *
38  * <p>Measure objects are formatted by MeasureFormat.
39  *
40  * <p>Measure objects are immutable.
41  *
42  * @author Alan Liu
43  * @stable ICU 3.0
44  */
45 class U_I18N_API Measure: public UObject {
46  public:
47     /**
48      * Construct an object with the given numeric amount and the given
49      * unit.  After this call, the caller must not delete the given
50      * unit object.
51      * @param number a numeric object; amount.isNumeric() must be true
52      * @param adoptedUnit the unit object, which must not be nullptr
53      * @param ec input-output error code. If the amount or the unit
54      * is invalid, then this will be set to a failing value.
55      * @stable ICU 3.0
56      */
57     Measure(const Formattable& number, MeasureUnit* adoptedUnit,
58             UErrorCode& ec);
59 
60     /**
61      * Copy constructor
62      * @stable ICU 3.0
63      */
64     Measure(const Measure& other);
65 
66     /**
67      * Assignment operator
68      * @stable ICU 3.0
69      */
70     Measure& operator=(const Measure& other);
71 
72     /**
73      * Return a polymorphic clone of this object.  The result will
74      * have the same class as returned by getDynamicClassID().
75      * @stable ICU 3.0
76      */
77     virtual Measure* clone() const;
78 
79     /**
80      * Destructor
81      * @stable ICU 3.0
82      */
83     virtual ~Measure();
84 
85     /**
86      * Equality operator.  Return true if this object is equal
87      * to the given object.
88      * @stable ICU 3.0
89      */
90     bool operator==(const UObject& other) const;
91 
92 #ifndef U_HIDE_DRAFT_API
93     /**
94      * Inequality operator.  Returns true if this object is not equal to the other object.
95      * @param other the object to compare with
96      * @return true if the objects are not equal
97      * @draft ICU 74
98      */
99     inline bool operator!=(const UObject& other) const { return !operator==(other); }
100 #endif  // U_HIDE_DRAFT_API
101 
102     /**
103      * Return a reference to the numeric value of this object.  The
104      * numeric value may be of any numeric type supported by
105      * Formattable.
106      * @stable ICU 3.0
107      */
108     inline const Formattable& getNumber() const;
109 
110     /**
111      * Return a reference to the unit of this object.
112      * @stable ICU 3.0
113      */
114     inline const MeasureUnit& getUnit() const;
115 
116     /**
117      * Return the class ID for this class. This is useful only for comparing to
118      * a return value from getDynamicClassID(). For example:
119      * <pre>
120      * .   Base* polymorphic_pointer = createPolymorphicObject();
121      * .   if (polymorphic_pointer->getDynamicClassID() ==
122      * .       erived::getStaticClassID()) ...
123      * </pre>
124      * @return          The class ID for all objects of this class.
125      * @stable ICU 53
126      */
127     static UClassID U_EXPORT2 getStaticClassID();
128 
129     /**
130      * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. This
131      * method is to implement a simple version of RTTI, since not all C++
132      * compilers support genuine RTTI. Polymorphic operator==() and clone()
133      * methods call this method.
134      *
135      * @return          The class ID for this object. All objects of a
136      *                  given class have the same class ID.  Objects of
137      *                  other classes have different class IDs.
138      * @stable ICU 53
139      */
140     virtual UClassID getDynamicClassID() const override;
141 
142  protected:
143     /**
144      * Default constructor.
145      * @stable ICU 3.0
146      */
147     Measure();
148 
149  private:
150     /**
151      * The numeric value of this object, e.g. 2.54 or 100.
152      */
153     Formattable number;
154 
155     /**
156      * The unit of this object, e.g., "millimeter" or "JPY".  This is
157      * owned by this object.
158      */
159     MeasureUnit* unit;
160 };
161 
getNumber()162 inline const Formattable& Measure::getNumber() const {
163     return number;
164 }
165 
getUnit()166 inline const MeasureUnit& Measure::getUnit() const {
167     return *unit;
168 }
169 
170 U_NAMESPACE_END
171 
172 #endif // !UCONFIG_NO_FORMATTING
173 
174 #endif /* U_SHOW_CPLUSPLUS_API */
175 
176 #endif // __MEASURE_H__
177