measure.h 4.7 KB

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