| 1 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 |
|
| 5 |
#ifndef intl_components_MeasureUnit_h_ |
| 6 |
#define intl_components_MeasureUnit_h_ |
| 7 |
|
| 8 |
#include "mozilla/Assertions.h" |
| 9 |
#include "mozilla/intl/ICU4CGlue.h" |
| 10 |
#include "mozilla/intl/ICUError.h" |
| 11 |
#include "mozilla/Result.h" |
| 12 |
#include "mozilla/Span.h" |
| 13 |
#include "mozilla/UniquePtr.h" |
| 14 |
|
| 15 |
#include <iterator> |
| 16 |
#include <stddef.h> |
| 17 |
#include <stdint.h> |
| 18 |
#include <utility> |
| 19 |
|
| 20 |
struct UResourceBundle; |
| 21 |
|
| 22 |
namespace mozilla::intl { |
| 23 |
|
| 24 |
/** |
| 25 |
* This component is a Mozilla-focused API for working with measurement units in |
| 26 |
* internationalization code. It is used in coordination with other operations |
| 27 |
* such as number formatting. |
| 28 |
*/ |
| 29 |
class MeasureUnit final { |
| 30 |
class UResourceBundleDeleter { |
| 31 |
public: |
| 32 |
void operator()(UResourceBundle* aPtr); |
| 33 |
}; |
| 34 |
|
| 35 |
using UniqueUResourceBundle = |
| 36 |
UniquePtr<UResourceBundle, UResourceBundleDeleter>; |
| 37 |
|
| 38 |
public: |
| 39 |
MeasureUnit() = delete; |
| 40 |
|
| 41 |
class Enumeration final { |
| 42 |
// Resource bundle for the root locale. |
| 43 |
UniqueUResourceBundle mRootLocale = nullptr; |
| 44 |
|
| 45 |
// Resource bundle for the root locale's "units" resource table. |
| 46 |
UniqueUResourceBundle mUnits = nullptr; |
| 47 |
|
| 48 |
// The overall amount of available units. |
| 49 |
int32_t mUnitsSize = 0; |
| 50 |
|
| 51 |
public: |
| 52 |
Enumeration(UniqueUResourceBundle aRootLocale, |
| 53 |
UniqueUResourceBundle aUnits); |
| 54 |
|
| 55 |
class Iterator { |
| 56 |
public: |
| 57 |
// std::iterator traits. |
| 58 |
using iterator_category = std::input_iterator_tag; |
| 59 |
using value_type = SpanResult<char>; |
| 60 |
using difference_type = ptrdiff_t; |
| 61 |
using pointer = value_type*; |
| 62 |
using reference = value_type&; |
| 63 |
|
| 64 |
private: |
| 65 |
const Enumeration& mEnumeration; |
| 66 |
|
| 67 |
// Resource bundle to a measurement type within the "units" table. |
| 68 |
// |
| 69 |
// Measurement types describe various categories, like "area", "length", |
| 70 |
// or "mass". |
| 71 |
UniqueUResourceBundle mType = nullptr; |
| 72 |
|
| 73 |
// Resource bundle to a specific subtype within the type table. |
| 74 |
// |
| 75 |
// Measurement subtypes describe concrete measure units, like "acre", |
| 76 |
// "meter", or "kilogram". |
| 77 |
UniqueUResourceBundle mSubtype = nullptr; |
| 78 |
|
| 79 |
// The next position within the "units" table. |
| 80 |
int32_t mUnitsPos = 0; |
| 81 |
|
| 82 |
// The overall amount of types within the |mType| table. |
| 83 |
int32_t mTypeSize = 0; |
| 84 |
|
| 85 |
// The next position within the |mType| table. |
| 86 |
int32_t mTypePos = 0; |
| 87 |
|
| 88 |
// Flag set when an ICU error has occurred. All further operations on this |
| 89 |
// iterator will return an error result when this flag is set. |
| 90 |
bool mHasError = false; |
| 91 |
|
| 92 |
void advance(); |
| 93 |
|
| 94 |
public: |
| 95 |
Iterator(const Enumeration& aEnumeration, int32_t aUnitsPos) |
| 96 |
: mEnumeration(aEnumeration), mUnitsPos(aUnitsPos) { |
| 97 |
advance(); |
| 98 |
} |
| 99 |
|
| 100 |
Iterator& operator++() { |
| 101 |
advance(); |
| 102 |
return *this; |
| 103 |
} |
| 104 |
|
| 105 |
// The post-increment operator would return an invalid iterator, so it's |
| 106 |
// not implemented. |
| 107 |
Iterator operator++(int) = delete; |
| 108 |
|
| 109 |
bool operator==(const Iterator& aOther) const { |
| 110 |
// It's an error to compare an iterator against an iterator from a |
| 111 |
// different enumeration. |
| 112 |
MOZ_ASSERT(&mEnumeration == &aOther.mEnumeration); |
| 113 |
|
| 114 |
return mUnitsPos == aOther.mUnitsPos && mTypeSize == aOther.mTypeSize && |
| 115 |
mTypePos == aOther.mTypePos && mHasError == aOther.mHasError; |
| 116 |
} |
| 117 |
|
| 118 |
bool operator!=(const Iterator& aOther) const { |
| 119 |
return !(*this == aOther); |
| 120 |
} |
| 121 |
|
| 122 |
value_type operator*() const; |
| 123 |
}; |
| 124 |
|
| 125 |
friend class Iterator; |
| 126 |
|
| 127 |
// std::iterator begin() and end() methods. |
| 128 |
|
| 129 |
/** |
| 130 |
* Return an iterator pointing to the start of the "units" table. |
| 131 |
*/ |
| 132 |
Iterator begin() { return Iterator(*this, 0); } |
| 133 |
|
| 134 |
/** |
| 135 |
* Return an iterator pointing to the end of the "units" table. |
| 136 |
*/ |
| 137 |
Iterator end() { return Iterator(*this, mUnitsSize); } |
| 138 |
|
| 139 |
/** |
| 140 |
* Create a new measurement unit enumeration. |
| 141 |
*/ |
| 142 |
static Result<Enumeration, ICUError> TryCreate(); |
| 143 |
}; |
| 144 |
|
| 145 |
/** |
| 146 |
* Return an enumeration over all available measurement units. |
| 147 |
*/ |
| 148 |
static Result<Enumeration, ICUError> GetAvailable() { |
| 149 |
return Enumeration::TryCreate(); |
| 150 |
} |
| 151 |
}; |
| 152 |
|
| 153 |
} // namespace mozilla::intl |
| 154 |
|
| 155 |
#endif |