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