measure.h 4.3 KB

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