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 NULL |
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 | /** |
93 | * Return a reference to the numeric value of this object. The |
94 | * numeric value may be of any numeric type supported by |
95 | * Formattable. |
96 | * @stable ICU 3.0 |
97 | */ |
98 | inline const Formattable& getNumber() const; |
99 | |
100 | /** |
101 | * Return a reference to the unit of this object. |
102 | * @stable ICU 3.0 |
103 | */ |
104 | inline const MeasureUnit& getUnit() const; |
105 | |
106 | /** |
107 | * Return the class ID for this class. This is useful only for comparing to |
108 | * a return value from getDynamicClassID(). For example: |
109 | * <pre> |
110 | * . Base* polymorphic_pointer = createPolymorphicObject(); |
111 | * . if (polymorphic_pointer->getDynamicClassID() == |
112 | * . erived::getStaticClassID()) ... |
113 | * </pre> |
114 | * @return The class ID for all objects of this class. |
115 | * @stable ICU 53 |
116 | */ |
117 | static UClassID U_EXPORT2 getStaticClassID(void); |
118 | |
119 | /** |
120 | * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. This |
121 | * method is to implement a simple version of RTTI, since not all C++ |
122 | * compilers support genuine RTTI. Polymorphic operator==() and clone() |
123 | * methods call this method. |
124 | * |
125 | * @return The class ID for this object. All objects of a |
126 | * given class have the same class ID. Objects of |
127 | * other classes have different class IDs. |
128 | * @stable ICU 53 |
129 | */ |
130 | virtual UClassID getDynamicClassID(void) const override; |
131 | |
132 | protected: |
133 | /** |
134 | * Default constructor. |
135 | * @stable ICU 3.0 |
136 | */ |
137 | Measure(); |
138 | |
139 | private: |
140 | /** |
141 | * The numeric value of this object, e.g. 2.54 or 100. |
142 | */ |
143 | Formattable number; |
144 | |
145 | /** |
146 | * The unit of this object, e.g., "millimeter" or "JPY". This is |
147 | * owned by this object. |
148 | */ |
149 | MeasureUnit* unit; |
150 | }; |
151 | |
152 | inline const Formattable& Measure::getNumber() const { |
153 | return number; |
154 | } |
155 | |
156 | inline const MeasureUnit& Measure::getUnit() const { |
157 | return *unit; |
158 | } |
159 | |
160 | U_NAMESPACE_END |
161 | |
162 | #endif // !UCONFIG_NO_FORMATTING |
163 | |
164 | #endif /* U_SHOW_CPLUSPLUS_API */ |
165 | |
166 | #endif // __MEASURE_H__ |
167 | |