measure.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. /**
  79. * Inequality operator. Returns true if this object is not equal to the other object.
  80. * @param other the object to compare with
  81. * @return true if the objects are not equal
  82. * @stable ICU 74
  83. */
  84. inline bool operator!=(const UObject& other) const { return !operator==(other); }
  85. /**
  86. * Return a reference to the numeric value of this object. The
  87. * numeric value may be of any numeric type supported by
  88. * Formattable.
  89. * @stable ICU 3.0
  90. */
  91. inline const Formattable& getNumber() const;
  92. /**
  93. * Return a reference to the unit of this object.
  94. * @stable ICU 3.0
  95. */
  96. inline const MeasureUnit& getUnit() const;
  97. /**
  98. * Return the class ID for this class. This is useful only for comparing to
  99. * a return value from getDynamicClassID(). For example:
  100. * <pre>
  101. * . Base* polymorphic_pointer = createPolymorphicObject();
  102. * . if (polymorphic_pointer->getDynamicClassID() ==
  103. * . erived::getStaticClassID()) ...
  104. * </pre>
  105. * @return The class ID for all objects of this class.
  106. * @stable ICU 53
  107. */
  108. static UClassID U_EXPORT2 getStaticClassID();
  109. /**
  110. * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. This
  111. * method is to implement a simple version of RTTI, since not all C++
  112. * compilers support genuine RTTI. Polymorphic operator==() and clone()
  113. * methods call this method.
  114. *
  115. * @return The class ID for this object. All objects of a
  116. * given class have the same class ID. Objects of
  117. * other classes have different class IDs.
  118. * @stable ICU 53
  119. */
  120. virtual UClassID getDynamicClassID() const override;
  121. protected:
  122. /**
  123. * Default constructor.
  124. * @stable ICU 3.0
  125. */
  126. Measure();
  127. private:
  128. /**
  129. * The numeric value of this object, e.g. 2.54 or 100.
  130. */
  131. Formattable number;
  132. /**
  133. * The unit of this object, e.g., "millimeter" or "JPY". This is
  134. * owned by this object.
  135. */
  136. MeasureUnit* unit;
  137. };
  138. inline const Formattable& Measure::getNumber() const {
  139. return number;
  140. }
  141. inline const MeasureUnit& Measure::getUnit() const {
  142. return *unit;
  143. }
  144. U_NAMESPACE_END
  145. #endif // !UCONFIG_NO_FORMATTING
  146. #endif /* U_SHOW_CPLUSPLUS_API */
  147. #endif // __MEASURE_H__