measure.h 4.3 KB

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