Debian Codegraph @

intl/icu/source/i18n/messageformat2_allocation.h

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