1*0e209d39SAndroid Build Coastguard Worker // © 2018 and later: Unicode, Inc. and others.
2*0e209d39SAndroid Build Coastguard Worker // License & terms of use: http://www.unicode.org/copyright.html
3*0e209d39SAndroid Build Coastguard Worker
4*0e209d39SAndroid Build Coastguard Worker #ifndef __CAPI_HELPER_H__
5*0e209d39SAndroid Build Coastguard Worker #define __CAPI_HELPER_H__
6*0e209d39SAndroid Build Coastguard Worker
7*0e209d39SAndroid Build Coastguard Worker #include "unicode/utypes.h"
8*0e209d39SAndroid Build Coastguard Worker
9*0e209d39SAndroid Build Coastguard Worker U_NAMESPACE_BEGIN
10*0e209d39SAndroid Build Coastguard Worker
11*0e209d39SAndroid Build Coastguard Worker /**
12*0e209d39SAndroid Build Coastguard Worker * An internal helper class to help convert between C and C++ APIs.
13*0e209d39SAndroid Build Coastguard Worker */
14*0e209d39SAndroid Build Coastguard Worker template<typename CType, typename CPPType, int32_t kMagic>
15*0e209d39SAndroid Build Coastguard Worker class IcuCApiHelper {
16*0e209d39SAndroid Build Coastguard Worker public:
17*0e209d39SAndroid Build Coastguard Worker /**
18*0e209d39SAndroid Build Coastguard Worker * Convert from the C type to the C++ type (const version).
19*0e209d39SAndroid Build Coastguard Worker */
20*0e209d39SAndroid Build Coastguard Worker static const CPPType* validate(const CType* input, UErrorCode& status);
21*0e209d39SAndroid Build Coastguard Worker
22*0e209d39SAndroid Build Coastguard Worker /**
23*0e209d39SAndroid Build Coastguard Worker * Convert from the C type to the C++ type (non-const version).
24*0e209d39SAndroid Build Coastguard Worker */
25*0e209d39SAndroid Build Coastguard Worker static CPPType* validate(CType* input, UErrorCode& status);
26*0e209d39SAndroid Build Coastguard Worker
27*0e209d39SAndroid Build Coastguard Worker /**
28*0e209d39SAndroid Build Coastguard Worker * Convert from the C++ type to the C type (const version).
29*0e209d39SAndroid Build Coastguard Worker */
30*0e209d39SAndroid Build Coastguard Worker const CType* exportConstForC() const;
31*0e209d39SAndroid Build Coastguard Worker
32*0e209d39SAndroid Build Coastguard Worker /**
33*0e209d39SAndroid Build Coastguard Worker * Convert from the C++ type to the C type (non-const version).
34*0e209d39SAndroid Build Coastguard Worker */
35*0e209d39SAndroid Build Coastguard Worker CType* exportForC();
36*0e209d39SAndroid Build Coastguard Worker
37*0e209d39SAndroid Build Coastguard Worker /**
38*0e209d39SAndroid Build Coastguard Worker * Invalidates the object.
39*0e209d39SAndroid Build Coastguard Worker */
40*0e209d39SAndroid Build Coastguard Worker ~IcuCApiHelper();
41*0e209d39SAndroid Build Coastguard Worker
42*0e209d39SAndroid Build Coastguard Worker private:
43*0e209d39SAndroid Build Coastguard Worker /**
44*0e209d39SAndroid Build Coastguard Worker * While the object is valid, fMagic equals kMagic.
45*0e209d39SAndroid Build Coastguard Worker */
46*0e209d39SAndroid Build Coastguard Worker int32_t fMagic = kMagic;
47*0e209d39SAndroid Build Coastguard Worker };
48*0e209d39SAndroid Build Coastguard Worker
49*0e209d39SAndroid Build Coastguard Worker
50*0e209d39SAndroid Build Coastguard Worker template<typename CType, typename CPPType, int32_t kMagic>
51*0e209d39SAndroid Build Coastguard Worker const CPPType*
validate(const CType * input,UErrorCode & status)52*0e209d39SAndroid Build Coastguard Worker IcuCApiHelper<CType, CPPType, kMagic>::validate(const CType* input, UErrorCode& status) {
53*0e209d39SAndroid Build Coastguard Worker if (U_FAILURE(status)) {
54*0e209d39SAndroid Build Coastguard Worker return nullptr;
55*0e209d39SAndroid Build Coastguard Worker }
56*0e209d39SAndroid Build Coastguard Worker if (input == nullptr) {
57*0e209d39SAndroid Build Coastguard Worker status = U_ILLEGAL_ARGUMENT_ERROR;
58*0e209d39SAndroid Build Coastguard Worker return nullptr;
59*0e209d39SAndroid Build Coastguard Worker }
60*0e209d39SAndroid Build Coastguard Worker auto* impl = reinterpret_cast<const CPPType*>(input);
61*0e209d39SAndroid Build Coastguard Worker if (static_cast<const IcuCApiHelper<CType, CPPType, kMagic>*>(impl)->fMagic != kMagic) {
62*0e209d39SAndroid Build Coastguard Worker status = U_INVALID_FORMAT_ERROR;
63*0e209d39SAndroid Build Coastguard Worker return nullptr;
64*0e209d39SAndroid Build Coastguard Worker }
65*0e209d39SAndroid Build Coastguard Worker return impl;
66*0e209d39SAndroid Build Coastguard Worker }
67*0e209d39SAndroid Build Coastguard Worker
68*0e209d39SAndroid Build Coastguard Worker template<typename CType, typename CPPType, int32_t kMagic>
69*0e209d39SAndroid Build Coastguard Worker CPPType*
validate(CType * input,UErrorCode & status)70*0e209d39SAndroid Build Coastguard Worker IcuCApiHelper<CType, CPPType, kMagic>::validate(CType* input, UErrorCode& status) {
71*0e209d39SAndroid Build Coastguard Worker auto* constInput = static_cast<const CType*>(input);
72*0e209d39SAndroid Build Coastguard Worker auto* validated = validate(constInput, status);
73*0e209d39SAndroid Build Coastguard Worker return const_cast<CPPType*>(validated);
74*0e209d39SAndroid Build Coastguard Worker }
75*0e209d39SAndroid Build Coastguard Worker
76*0e209d39SAndroid Build Coastguard Worker template<typename CType, typename CPPType, int32_t kMagic>
77*0e209d39SAndroid Build Coastguard Worker const CType*
exportConstForC()78*0e209d39SAndroid Build Coastguard Worker IcuCApiHelper<CType, CPPType, kMagic>::exportConstForC() const {
79*0e209d39SAndroid Build Coastguard Worker return reinterpret_cast<const CType*>(static_cast<const CPPType*>(this));
80*0e209d39SAndroid Build Coastguard Worker }
81*0e209d39SAndroid Build Coastguard Worker
82*0e209d39SAndroid Build Coastguard Worker template<typename CType, typename CPPType, int32_t kMagic>
83*0e209d39SAndroid Build Coastguard Worker CType*
exportForC()84*0e209d39SAndroid Build Coastguard Worker IcuCApiHelper<CType, CPPType, kMagic>::exportForC() {
85*0e209d39SAndroid Build Coastguard Worker return reinterpret_cast<CType*>(static_cast<CPPType*>(this));
86*0e209d39SAndroid Build Coastguard Worker }
87*0e209d39SAndroid Build Coastguard Worker
88*0e209d39SAndroid Build Coastguard Worker template<typename CType, typename CPPType, int32_t kMagic>
~IcuCApiHelper()89*0e209d39SAndroid Build Coastguard Worker IcuCApiHelper<CType, CPPType, kMagic>::~IcuCApiHelper() {
90*0e209d39SAndroid Build Coastguard Worker // head off application errors by preventing use of of deleted objects.
91*0e209d39SAndroid Build Coastguard Worker fMagic = 0;
92*0e209d39SAndroid Build Coastguard Worker }
93*0e209d39SAndroid Build Coastguard Worker
94*0e209d39SAndroid Build Coastguard Worker
95*0e209d39SAndroid Build Coastguard Worker U_NAMESPACE_END
96*0e209d39SAndroid Build Coastguard Worker
97*0e209d39SAndroid Build Coastguard Worker #endif // __CAPI_HELPER_H__
98