1 // © 2024 and later: Unicode, Inc. and others. 2 // License & terms of use: http://www.unicode.org/copyright.html 3 4 #include "unicode/utypes.h" 5 6 #ifndef U_HIDE_DEPRECATED_API 7 8 #ifndef MESSAGEFORMAT2_UTILS_H 9 #define MESSAGEFORMAT2_UTILS_H 10 11 #if U_SHOW_CPLUSPLUS_API 12 13 #if !UCONFIG_NO_FORMATTING 14 15 #if !UCONFIG_NO_MF2 16 17 #include "unicode/unistr.h" 18 #include "uvector.h" 19 20 U_NAMESPACE_BEGIN 21 22 namespace message2 { 23 24 // Helpers 25 26 template<typename T> copyArray(const T * source,int32_t len,UErrorCode & status)27 static T* copyArray(const T* source, int32_t len, UErrorCode& status) { 28 if (U_FAILURE(status)) { 29 return nullptr; 30 } 31 U_ASSERT(source != nullptr); 32 U_ASSERT(len >= 0); 33 T* dest = new T[len]; 34 if (dest == nullptr) { 35 status = U_MEMORY_ALLOCATION_ERROR; 36 } else { 37 for (int32_t i = 0; i < len; i++) { 38 dest[i] = source[i]; 39 } 40 } 41 return dest; 42 } 43 44 template<typename T> copyVectorToArray(const UVector & source,UErrorCode & status)45 static T* copyVectorToArray(const UVector& source, UErrorCode& status) { 46 if (U_FAILURE(status)) { 47 return nullptr; 48 } 49 int32_t len = source.size(); 50 T* dest = new T[len]; 51 if (dest == nullptr) { 52 status = U_MEMORY_ALLOCATION_ERROR; 53 } else { 54 for (int32_t i = 0; i < len; i++) { 55 dest[i] = *(static_cast<T*>(source.elementAt(i))); 56 } 57 } 58 return dest; 59 } 60 61 template<typename T> moveVectorToArray(UVector & source,UErrorCode & status)62 static T* moveVectorToArray(UVector& source, UErrorCode& status) { 63 if (U_FAILURE(status)) { 64 return nullptr; 65 } 66 67 int32_t len = source.size(); 68 T* dest = new T[len]; 69 if (dest == nullptr) { 70 status = U_MEMORY_ALLOCATION_ERROR; 71 } else { 72 for (int32_t i = 0; i < len; i++) { 73 dest[i] = std::move(*static_cast<T*>(source.elementAt(i))); 74 } 75 source.removeAllElements(); 76 } 77 return dest; 78 } 79 createUVectorNoAdopt(UErrorCode & status)80 inline UVector* createUVectorNoAdopt(UErrorCode& status) { 81 if (U_FAILURE(status)) { 82 return nullptr; 83 } 84 LocalPointer<UVector> result(new UVector(status)); 85 if (U_FAILURE(status)) { 86 return nullptr; 87 } 88 return result.orphan(); 89 } 90 createUVector(UErrorCode & status)91 inline UVector* createUVector(UErrorCode& status) { 92 UVector* result = createUVectorNoAdopt(status); 93 if (U_FAILURE(status)) { 94 return nullptr; 95 } 96 result->setDeleter(uprv_deleteUObject); 97 return result; 98 } 99 stringsEqual(const UElement s1,const UElement s2)100 static UBool stringsEqual(const UElement s1, const UElement s2) { 101 return (*static_cast<UnicodeString*>(s1.pointer) == *static_cast<UnicodeString*>(s2.pointer)); 102 } 103 createStringUVector(UErrorCode & status)104 inline UVector* createStringUVector(UErrorCode& status) { 105 UVector* v = createUVector(status); 106 if (U_FAILURE(status)) { 107 return nullptr; 108 } 109 v->setComparer(stringsEqual); 110 return v; 111 } 112 createStringVectorNoAdopt(UErrorCode & status)113 inline UVector* createStringVectorNoAdopt(UErrorCode& status) { 114 UVector* v = createUVectorNoAdopt(status); 115 if (U_FAILURE(status)) { 116 return nullptr; 117 } 118 v->setComparer(stringsEqual); 119 return v; 120 } 121 122 template<typename T> create(T && node,UErrorCode & status)123 inline T* create(T&& node, UErrorCode& status) { 124 if (U_FAILURE(status)) { 125 return nullptr; 126 } 127 T* result = new T(std::move(node)); 128 if (result == nullptr) { 129 status = U_MEMORY_ALLOCATION_ERROR; 130 } 131 return result; 132 } 133 134 } // namespace message2 135 136 U_NAMESPACE_END 137 138 #endif /* #if !UCONFIG_NO_MF2 */ 139 140 #endif /* #if !UCONFIG_NO_FORMATTING */ 141 142 #endif /* U_SHOW_CPLUSPLUS_API */ 143 144 #endif // MESSAGEFORMAT2_UTILS_H 145 146 #endif // U_HIDE_DEPRECATED_API 147 // eof 148