| 1 |
#ifndef ICU4XUnitsConverterFactory_HPP |
| 2 |
#define ICU4XUnitsConverterFactory_HPP |
| 3 |
#include <stdint.h> |
| 4 |
#include <stddef.h> |
| 5 |
#include <stdbool.h> |
| 6 |
#include <algorithm> |
| 7 |
#include <memory> |
| 8 |
#include <variant> |
| 9 |
#include <optional> |
| 10 |
#include "diplomat_runtime.hpp" |
| 11 |
|
| 12 |
#include "ICU4XUnitsConverterFactory.h" |
| 13 |
|
| 14 |
class ICU4XDataProvider; |
| 15 |
class ICU4XUnitsConverterFactory; |
| 16 |
#include "ICU4XError.hpp" |
| 17 |
class ICU4XMeasureUnit; |
| 18 |
class ICU4XUnitsConverter; |
| 19 |
class ICU4XMeasureUnitParser; |
| 20 |
|
| 21 |
/** |
| 22 |
* A destruction policy for using ICU4XUnitsConverterFactory with std::unique_ptr. |
| 23 |
*/ |
| 24 |
struct ICU4XUnitsConverterFactoryDeleter { |
| 25 |
void operator()(capi::ICU4XUnitsConverterFactory* l) const noexcept { |
| 26 |
capi::ICU4XUnitsConverterFactory_destroy(l); |
| 27 |
} |
| 28 |
}; |
| 29 |
|
| 30 |
/** |
| 31 |
* An ICU4X Units Converter Factory object, capable of creating converters a [`ICU4XUnitsConverter`] |
| 32 |
* for converting between two [`ICU4XMeasureUnit`]s. |
| 33 |
* Also, it can parse the CLDR unit identifier (e.g. `meter-per-square-second`) and get the [`ICU4XMeasureUnit`]. |
| 34 |
* |
| 35 |
* See the [Rust documentation for `ConverterFactory`](https://docs.rs/icu/latest/icu/experimental/units/converter_factory/struct.ConverterFactory.html) for more information. |
| 36 |
*/ |
| 37 |
class ICU4XUnitsConverterFactory { |
| 38 |
public: |
| 39 |
|
| 40 |
/** |
| 41 |
* Construct a new [`ICU4XUnitsConverterFactory`] instance. |
| 42 |
* |
| 43 |
* See the [Rust documentation for `new`](https://docs.rs/icu/latest/icu/experimental/units/converter_factory/struct.ConverterFactory.html#method.new) for more information. |
| 44 |
*/ |
| 45 |
static diplomat::result<ICU4XUnitsConverterFactory, ICU4XError> create(const ICU4XDataProvider& provider); |
| 46 |
|
| 47 |
/** |
| 48 |
* Creates a new [`ICU4XUnitsConverter`] from the input and output [`ICU4XMeasureUnit`]s. |
| 49 |
* Returns nothing if the conversion between the two units is not possible. |
| 50 |
* For example, conversion between `meter` and `second` is not possible. |
| 51 |
* |
| 52 |
* See the [Rust documentation for `converter`](https://docs.rs/icu/latest/icu/experimental/units/converter_factory/struct.ConverterFactory.html#method.converter) for more information. |
| 53 |
*/ |
| 54 |
std::optional<ICU4XUnitsConverter> converter(const ICU4XMeasureUnit& from, const ICU4XMeasureUnit& to) const; |
| 55 |
|
| 56 |
/** |
| 57 |
* Creates a parser to parse the CLDR unit identifier (e.g. `meter-per-square-second`) and get the [`ICU4XMeasureUnit`]. |
| 58 |
* |
| 59 |
* See the [Rust documentation for `parser`](https://docs.rs/icu/latest/icu/experimental/units/converter_factory/struct.ConverterFactory.html#method.parser) for more information. |
| 60 |
* |
| 61 |
* Lifetimes: `this` must live at least as long as the output. |
| 62 |
*/ |
| 63 |
ICU4XMeasureUnitParser parser() const; |
| 64 |
inline const capi::ICU4XUnitsConverterFactory* AsFFI() const { return this->inner.get(); } |
| 65 |
inline capi::ICU4XUnitsConverterFactory* AsFFIMut() { return this->inner.get(); } |
| 66 |
inline explicit ICU4XUnitsConverterFactory(capi::ICU4XUnitsConverterFactory* i) : inner(i) {} |
| 67 |
ICU4XUnitsConverterFactory() = default; |
| 68 |
ICU4XUnitsConverterFactory(ICU4XUnitsConverterFactory&&) noexcept = default; |
| 69 |
ICU4XUnitsConverterFactory& operator=(ICU4XUnitsConverterFactory&& other) noexcept = default; |
| 70 |
private: |
| 71 |
std::unique_ptr<capi::ICU4XUnitsConverterFactory, ICU4XUnitsConverterFactoryDeleter> inner; |
| 72 |
}; |
| 73 |
|
| 74 |
#include "ICU4XDataProvider.hpp" |
| 75 |
#include "ICU4XMeasureUnit.hpp" |
| 76 |
#include "ICU4XUnitsConverter.hpp" |
| 77 |
#include "ICU4XMeasureUnitParser.hpp" |
| 78 |
|
| 79 |
inline diplomat::result<ICU4XUnitsConverterFactory, ICU4XError> ICU4XUnitsConverterFactory::create(const ICU4XDataProvider& provider) { |
| 80 |
auto diplomat_result_raw_out_value = capi::ICU4XUnitsConverterFactory_create(provider.AsFFI()); |
| 81 |
diplomat::result<ICU4XUnitsConverterFactory, ICU4XError> diplomat_result_out_value; |
| 82 |
if (diplomat_result_raw_out_value.is_ok) { |
| 83 |
diplomat_result_out_value = diplomat::Ok<ICU4XUnitsConverterFactory>(ICU4XUnitsConverterFactory(diplomat_result_raw_out_value.ok)); |
| 84 |
} else { |
| 85 |
diplomat_result_out_value = diplomat::Err<ICU4XError>(static_cast<ICU4XError>(diplomat_result_raw_out_value.err)); |
| 86 |
} |
| 87 |
return diplomat_result_out_value; |
| 88 |
} |
| 89 |
inline std::optional<ICU4XUnitsConverter> ICU4XUnitsConverterFactory::converter(const ICU4XMeasureUnit& from, const ICU4XMeasureUnit& to) const { |
| 90 |
auto diplomat_optional_raw_out_value = capi::ICU4XUnitsConverterFactory_converter(this->inner.get(), from.AsFFI(), to.AsFFI()); |
| 91 |
std::optional<ICU4XUnitsConverter> diplomat_optional_out_value; |
| 92 |
if (diplomat_optional_raw_out_value != nullptr) { |
| 93 |
diplomat_optional_out_value = ICU4XUnitsConverter(diplomat_optional_raw_out_value); |
| 94 |
} else { |
| 95 |
diplomat_optional_out_value = std::nullopt; |
| 96 |
} |
| 97 |
return diplomat_optional_out_value; |
| 98 |
} |
| 99 |
inline ICU4XMeasureUnitParser ICU4XUnitsConverterFactory::parser() const { |
| 100 |
return ICU4XMeasureUnitParser(capi::ICU4XUnitsConverterFactory_parser(this->inner.get())); |
| 101 |
} |
| 102 |
#endif |