measure.h 4.1 KB

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