| 1 | // |
| 2 | // Copyright (c) 2009-2011 Artyom Beilis (Tonkikh) |
| 3 | // Copyright (c) 2021-2022 Alexander Grund |
| 4 | // |
| 5 | // Distributed under the Boost Software License, Version 1.0. |
| 6 | // https://www.boost.org/LICENSE_1_0.txt |
| 7 | |
| 8 | #ifndef BOOST_SRC_ICU_UTIL_HPP |
| 9 | #define BOOST_SRC_ICU_UTIL_HPP |
| 10 | |
| 11 | #include <boost/locale/config.hpp> |
| 12 | #include <cstdint> // Avoid ICU defining e.g. INT8_MIN causing macro redefinition warnings |
| 13 | #include <stdexcept> |
| 14 | #include <string> |
| 15 | #include <unicode/utypes.h> |
| 16 | #include <unicode/uversion.h> |
| 17 | |
| 18 | #define BOOST_LOCALE_ICU_VERSION (U_ICU_VERSION_MAJOR_NUM * 100 + U_ICU_VERSION_MINOR_NUM) |
| 19 | |
| 20 | namespace boost { namespace locale { namespace impl_icu { |
| 21 | |
| 22 | inline void throw_icu_error(UErrorCode err, std::string desc) // LCOV_EXCL_LINE |
| 23 | { |
| 24 | if(!desc.empty()) // LCOV_EXCL_LINE |
| 25 | desc += ": " ; // LCOV_EXCL_LINE |
| 26 | throw std::runtime_error(desc + u_errorName(code: err)); // LCOV_EXCL_LINE |
| 27 | } |
| 28 | |
| 29 | inline void check_and_throw_icu_error(UErrorCode err, const char* desc = "" ) |
| 30 | { |
| 31 | if(U_FAILURE(code: err)) |
| 32 | throw_icu_error(err, desc); // LCOV_EXCL_LINE |
| 33 | } |
| 34 | |
| 35 | /// Cast a pointer to an ICU object to a pointer to TargetType |
| 36 | /// using RTTI or ICUs "poor man's RTTI" to make it work with e.g. libc++ and hidden visibility |
| 37 | template<class TargetType, class SourceType> |
| 38 | TargetType* icu_cast(SourceType* p) |
| 39 | { |
| 40 | TargetType* result = dynamic_cast<TargetType*>(p); |
| 41 | if(!result && p && p->getDynamicClassID() == TargetType::getStaticClassID()) |
| 42 | result = static_cast<TargetType*>(p); |
| 43 | return result; |
| 44 | } |
| 45 | }}} // namespace boost::locale::impl_icu |
| 46 | |
| 47 | #endif |
| 48 | |