]>
git.saurik.com Git - apple/icu.git/blob - icuSources/common/capi_helper.h
1 // © 2018 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 #ifndef __CAPI_HELPER_H__
5 #define __CAPI_HELPER_H__
7 #include "unicode/utypes.h"
12 * An internal helper class to help convert between C and C++ APIs.
14 template<typename CType
, typename CPPType
, int32_t kMagic
>
18 * Convert from the C type to the C++ type (const version).
20 static const CPPType
* validate(const CType
* input
, UErrorCode
& status
);
23 * Convert from the C type to the C++ type (non-const version).
25 static CPPType
* validate(CType
* input
, UErrorCode
& status
);
28 * Convert from the C++ type to the C type (const version).
30 const CType
* exportConstForC() const;
33 * Convert from the C++ type to the C type (non-const version).
38 * Invalidates the object.
44 * While the object is valid, fMagic equals kMagic.
46 int32_t fMagic
= kMagic
;
50 template<typename CType
, typename CPPType
, int32_t kMagic
>
52 IcuCApiHelper
<CType
, CPPType
, kMagic
>::validate(const CType
* input
, UErrorCode
& status
) {
53 if (U_FAILURE(status
)) {
56 if (input
== nullptr) {
57 status
= U_ILLEGAL_ARGUMENT_ERROR
;
60 auto* impl
= reinterpret_cast<const CPPType
*>(input
);
61 if (static_cast<const IcuCApiHelper
<CType
, CPPType
, kMagic
>*>(impl
)->fMagic
!= kMagic
) {
62 status
= U_INVALID_FORMAT_ERROR
;
68 template<typename CType
, typename CPPType
, int32_t kMagic
>
70 IcuCApiHelper
<CType
, CPPType
, kMagic
>::validate(CType
* input
, UErrorCode
& status
) {
71 auto* constInput
= static_cast<const CType
*>(input
);
72 auto* validated
= validate(constInput
, status
);
73 return const_cast<CPPType
*>(validated
);
76 template<typename CType
, typename CPPType
, int32_t kMagic
>
78 IcuCApiHelper
<CType
, CPPType
, kMagic
>::exportConstForC() const {
79 return reinterpret_cast<const CType
*>(static_cast<const CPPType
*>(this));
82 template<typename CType
, typename CPPType
, int32_t kMagic
>
84 IcuCApiHelper
<CType
, CPPType
, kMagic
>::exportForC() {
85 return reinterpret_cast<CType
*>(static_cast<CPPType
*>(this));
88 template<typename CType
, typename CPPType
, int32_t kMagic
>
89 IcuCApiHelper
<CType
, CPPType
, kMagic
>::~IcuCApiHelper() {
90 // head off application errors by preventing use of of deleted objects.
97 #endif // __CAPI_HELPER_H__