]>
Commit | Line | Data |
---|---|---|
f3c0d7a5 A |
1 | // © 2016 and later: Unicode, Inc. and others. |
2 | // License & terms of use: http://www.unicode.org/copyright.html | |
b75a7d8f A |
3 | /* |
4 | ****************************************************************************** | |
5 | * | |
4388f060 | 6 | * Copyright (C) 2002-2012, International Business Machines |
b75a7d8f A |
7 | * Corporation and others. All Rights Reserved. |
8 | * | |
9 | ****************************************************************************** | |
10 | * file name: uobject.h | |
f3c0d7a5 | 11 | * encoding: UTF-8 |
b75a7d8f A |
12 | * tab size: 8 (not used) |
13 | * indentation:4 | |
14 | * | |
15 | * created on: 2002jun26 | |
16 | * created by: Markus W. Scherer | |
17 | */ | |
18 | ||
19 | #ifndef __UOBJECT_H__ | |
20 | #define __UOBJECT_H__ | |
21 | ||
22 | #include "unicode/utypes.h" | |
3d1f044b | 23 | #include "unicode/platform.h" |
b75a7d8f | 24 | |
b75a7d8f A |
25 | /** |
26 | * \file | |
27 | * \brief C++ API: Common ICU base class UObject. | |
28 | */ | |
29 | ||
729e4ab9 | 30 | /** |
729e4ab9 | 31 | * \def U_NO_THROW |
3d1f044b A |
32 | * Since ICU 64, use U_NOEXCEPT instead. |
33 | * | |
34 | * Previously, define this to define the throw() specification so | |
729e4ab9 A |
35 | * certain functions do not throw any exceptions |
36 | * | |
37 | * UMemory operator new methods should have the throw() specification | |
38 | * appended to them, so that the compiler adds the additional NULL check | |
39 | * before calling constructors. Without, if <code>operator new</code> returns NULL the | |
40 | * constructor is still called, and if the constructor references member | |
41 | * data, (which it typically does), the result is a segmentation violation. | |
42 | * | |
3d1f044b | 43 | * @stable ICU 4.2. Since ICU 64, Use U_NOEXCEPT instead. See ICU-20422. |
4388f060 | 44 | */ |
729e4ab9 A |
45 | #ifndef U_NO_THROW |
46 | #define U_NO_THROW throw() | |
47 | #endif | |
48 | ||
4388f060 A |
49 | /*===========================================================================*/ |
50 | /* UClassID-based RTTI */ | |
51 | /*===========================================================================*/ | |
52 | ||
53 | /** | |
54 | * UClassID is used to identify classes without using the compiler's RTTI. | |
55 | * This was used before C++ compilers consistently supported RTTI. | |
56 | * ICU 4.6 requires compiler RTTI to be turned on. | |
57 | * | |
58 | * Each class hierarchy which needs | |
59 | * to implement polymorphic clone() or operator==() defines two methods, | |
60 | * described in detail below. UClassID values can be compared using | |
61 | * operator==(). Nothing else should be done with them. | |
62 | * | |
63 | * \par | |
51004dcb A |
64 | * In class hierarchies that implement "poor man's RTTI", |
65 | * each concrete subclass implements getDynamicClassID() in the same way: | |
4388f060 A |
66 | * |
67 | * \code | |
4388f060 A |
68 | * class Derived { |
69 | * public: | |
70 | * virtual UClassID getDynamicClassID() const | |
71 | * { return Derived::getStaticClassID(); } | |
72 | * } | |
73 | * \endcode | |
74 | * | |
75 | * Each concrete class implements getStaticClassID() as well, which allows | |
76 | * clients to test for a specific type. | |
77 | * | |
78 | * \code | |
79 | * class Derived { | |
80 | * public: | |
81 | * static UClassID U_EXPORT2 getStaticClassID(); | |
82 | * private: | |
83 | * static char fgClassID; | |
84 | * } | |
85 | * | |
86 | * // In Derived.cpp: | |
87 | * UClassID Derived::getStaticClassID() | |
88 | * { return (UClassID)&Derived::fgClassID; } | |
89 | * char Derived::fgClassID = 0; // Value is irrelevant | |
90 | * \endcode | |
91 | * @stable ICU 2.0 | |
92 | */ | |
93 | typedef void* UClassID; | |
94 | ||
f3c0d7a5 | 95 | #if U_SHOW_CPLUSPLUS_API |
4388f060 A |
96 | U_NAMESPACE_BEGIN |
97 | ||
b75a7d8f A |
98 | /** |
99 | * UMemory is the common ICU base class. | |
100 | * All other ICU C++ classes are derived from UMemory (starting with ICU 2.4). | |
101 | * | |
102 | * This is primarily to make it possible and simple to override the | |
103 | * C++ memory management by adding new/delete operators to this base class. | |
104 | * | |
105 | * To override ALL ICU memory management, including that from plain C code, | |
106 | * replace the allocation functions declared in cmemory.h | |
107 | * | |
108 | * UMemory does not contain any virtual functions. | |
109 | * Common "boilerplate" functions are defined in UObject. | |
110 | * | |
374ca955 | 111 | * @stable ICU 2.4 |
b75a7d8f A |
112 | */ |
113 | class U_COMMON_API UMemory { | |
114 | public: | |
115 | ||
729e4ab9 A |
116 | /* test versions for debugging shaper heap memory problems */ |
117 | #ifdef SHAPER_MEMORY_DEBUG | |
118 | static void * NewArray(int size, int count); | |
119 | static void * GrowArray(void * array, int newSize ); | |
120 | static void FreeArray(void * array ); | |
121 | #endif | |
122 | ||
b75a7d8f A |
123 | #if U_OVERRIDE_CXX_ALLOCATION |
124 | /** | |
125 | * Override for ICU4C C++ memory management. | |
126 | * simple, non-class types are allocated using the macros in common/cmemory.h | |
127 | * (uprv_malloc(), uprv_free(), uprv_realloc()); | |
128 | * they or something else could be used here to implement C++ new/delete | |
129 | * for ICU4C C++ classes | |
374ca955 | 130 | * @stable ICU 2.4 |
b75a7d8f | 131 | */ |
3d1f044b | 132 | static void * U_EXPORT2 operator new(size_t size) U_NOEXCEPT; |
b75a7d8f A |
133 | |
134 | /** | |
135 | * Override for ICU4C C++ memory management. | |
136 | * See new(). | |
374ca955 | 137 | * @stable ICU 2.4 |
b75a7d8f | 138 | */ |
3d1f044b | 139 | static void * U_EXPORT2 operator new[](size_t size) U_NOEXCEPT; |
b75a7d8f A |
140 | |
141 | /** | |
142 | * Override for ICU4C C++ memory management. | |
143 | * simple, non-class types are allocated using the macros in common/cmemory.h | |
144 | * (uprv_malloc(), uprv_free(), uprv_realloc()); | |
145 | * they or something else could be used here to implement C++ new/delete | |
146 | * for ICU4C C++ classes | |
374ca955 | 147 | * @stable ICU 2.4 |
b75a7d8f | 148 | */ |
3d1f044b | 149 | static void U_EXPORT2 operator delete(void *p) U_NOEXCEPT; |
b75a7d8f A |
150 | |
151 | /** | |
152 | * Override for ICU4C C++ memory management. | |
153 | * See delete(). | |
374ca955 | 154 | * @stable ICU 2.4 |
b75a7d8f | 155 | */ |
3d1f044b | 156 | static void U_EXPORT2 operator delete[](void *p) U_NOEXCEPT; |
b75a7d8f A |
157 | |
158 | #if U_HAVE_PLACEMENT_NEW | |
159 | /** | |
160 | * Override for ICU4C C++ memory management for STL. | |
161 | * See new(). | |
374ca955 | 162 | * @stable ICU 2.6 |
b75a7d8f | 163 | */ |
3d1f044b | 164 | static inline void * U_EXPORT2 operator new(size_t, void *ptr) U_NOEXCEPT { return ptr; } |
b75a7d8f A |
165 | |
166 | /** | |
167 | * Override for ICU4C C++ memory management for STL. | |
168 | * See delete(). | |
374ca955 | 169 | * @stable ICU 2.6 |
b75a7d8f | 170 | */ |
3d1f044b | 171 | static inline void U_EXPORT2 operator delete(void *, void *) U_NOEXCEPT {} |
b75a7d8f | 172 | #endif /* U_HAVE_PLACEMENT_NEW */ |
73c04bcf A |
173 | #if U_HAVE_DEBUG_LOCATION_NEW |
174 | /** | |
175 | * This method overrides the MFC debug version of the operator new | |
176 | * | |
177 | * @param size The requested memory size | |
178 | * @param file The file where the allocation was requested | |
179 | * @param line The line where the allocation was requested | |
180 | */ | |
3d1f044b | 181 | static void * U_EXPORT2 operator new(size_t size, const char* file, int line) U_NOEXCEPT; |
73c04bcf A |
182 | /** |
183 | * This method provides a matching delete for the MFC debug new | |
184 | * | |
185 | * @param p The pointer to the allocated memory | |
186 | * @param file The file where the allocation was requested | |
187 | * @param line The line where the allocation was requested | |
188 | */ | |
3d1f044b | 189 | static void U_EXPORT2 operator delete(void* p, const char* file, int line) U_NOEXCEPT; |
73c04bcf | 190 | #endif /* U_HAVE_DEBUG_LOCATION_NEW */ |
b75a7d8f A |
191 | #endif /* U_OVERRIDE_CXX_ALLOCATION */ |
192 | ||
193 | /* | |
194 | * Assignment operator not declared. The compiler will provide one | |
195 | * which does nothing since this class does not contain any data members. | |
196 | * API/code coverage may show the assignment operator as present and | |
197 | * untested - ignore. | |
198 | * Subclasses need this assignment operator if they use compiler-provided | |
199 | * assignment operators of their own. An alternative to not declaring one | |
200 | * here would be to declare and empty-implement a protected or public one. | |
201 | UMemory &UMemory::operator=(const UMemory &); | |
202 | */ | |
203 | }; | |
204 | ||
205 | /** | |
206 | * UObject is the common ICU "boilerplate" class. | |
207 | * UObject inherits UMemory (starting with ICU 2.4), | |
208 | * and all other public ICU C++ classes | |
209 | * are derived from UObject (starting with ICU 2.2). | |
210 | * | |
51004dcb | 211 | * UObject contains common virtual functions, in particular a virtual destructor. |
b75a7d8f A |
212 | * |
213 | * The clone() function is not available in UObject because it is not | |
214 | * implemented by all ICU classes. | |
215 | * Many ICU services provide a clone() function for their class trees, | |
216 | * defined on the service's C++ base class, and all subclasses within that | |
217 | * service class tree return a pointer to the service base class | |
218 | * (which itself is a subclass of UObject). | |
219 | * This is because some compilers do not support covariant (same-as-this) | |
220 | * return types; cast to the appropriate subclass if necessary. | |
221 | * | |
374ca955 | 222 | * @stable ICU 2.2 |
b75a7d8f A |
223 | */ |
224 | class U_COMMON_API UObject : public UMemory { | |
225 | public: | |
226 | /** | |
227 | * Destructor. | |
228 | * | |
374ca955 | 229 | * @stable ICU 2.2 |
b75a7d8f | 230 | */ |
374ca955 | 231 | virtual ~UObject(); |
b75a7d8f A |
232 | |
233 | /** | |
234 | * ICU4C "poor man's RTTI", returns a UClassID for the actual ICU class. | |
51004dcb A |
235 | * The base class implementation returns a dummy value. |
236 | * | |
237 | * Use compiler RTTI rather than ICU's "poor man's RTTI". | |
238 | * Since ICU 4.6, new ICU C++ class hierarchies do not implement "poor man's RTTI". | |
b75a7d8f | 239 | * |
374ca955 | 240 | * @stable ICU 2.2 |
b75a7d8f | 241 | */ |
51004dcb | 242 | virtual UClassID getDynamicClassID() const; |
b75a7d8f A |
243 | |
244 | protected: | |
245 | // the following functions are protected to prevent instantiation and | |
246 | // direct use of UObject itself | |
247 | ||
248 | // default constructor | |
b75a7d8f A |
249 | // inline UObject() {} |
250 | ||
251 | // copy constructor | |
b75a7d8f A |
252 | // inline UObject(const UObject &other) {} |
253 | ||
374ca955 A |
254 | #if 0 |
255 | // TODO Sometime in the future. Implement operator==(). | |
256 | // (This comment inserted in 2.2) | |
b75a7d8f A |
257 | // some or all of the following "boilerplate" functions may be made public |
258 | // in a future ICU4C release when all subclasses implement them | |
259 | ||
260 | // assignment operator | |
261 | // (not virtual, see "Taligent's Guide to Designing Programs" pp.73..74) | |
262 | // commented out because the implementation is the same as a compiler's default | |
263 | // UObject &operator=(const UObject &other) { return *this; } | |
264 | ||
265 | // comparison operators | |
266 | virtual inline UBool operator==(const UObject &other) const { return this==&other; } | |
267 | inline UBool operator!=(const UObject &other) const { return !operator==(other); } | |
268 | ||
269 | // clone() commented out from the base class: | |
270 | // some compilers do not support co-variant return types | |
271 | // (i.e., subclasses would have to return UObject * as well, instead of SubClass *) | |
272 | // see also UObject class documentation. | |
273 | // virtual UObject *clone() const; | |
274 | #endif | |
275 | ||
276 | /* | |
277 | * Assignment operator not declared. The compiler will provide one | |
278 | * which does nothing since this class does not contain any data members. | |
279 | * API/code coverage may show the assignment operator as present and | |
280 | * untested - ignore. | |
281 | * Subclasses need this assignment operator if they use compiler-provided | |
282 | * assignment operators of their own. An alternative to not declaring one | |
283 | * here would be to declare and empty-implement a protected or public one. | |
284 | UObject &UObject::operator=(const UObject &); | |
285 | */ | |
286 | }; | |
287 | ||
4388f060 | 288 | #ifndef U_HIDE_INTERNAL_API |
374ca955 A |
289 | /** |
290 | * This is a simple macro to add ICU RTTI to an ICU object implementation. | |
291 | * This does not go into the header. This should only be used in *.cpp files. | |
292 | * | |
293 | * @param myClass The name of the class that needs RTTI defined. | |
294 | * @internal | |
295 | */ | |
296 | #define UOBJECT_DEFINE_RTTI_IMPLEMENTATION(myClass) \ | |
297 | UClassID U_EXPORT2 myClass::getStaticClassID() { \ | |
73c04bcf | 298 | static char classID = 0; \ |
374ca955 A |
299 | return (UClassID)&classID; \ |
300 | } \ | |
301 | UClassID myClass::getDynamicClassID() const \ | |
302 | { return myClass::getStaticClassID(); } | |
303 | ||
304 | ||
305 | /** | |
306 | * This macro adds ICU RTTI to an ICU abstract class implementation. | |
307 | * This macro should be invoked in *.cpp files. The corresponding | |
308 | * header should declare getStaticClassID. | |
309 | * | |
310 | * @param myClass The name of the class that needs RTTI defined. | |
311 | * @internal | |
312 | */ | |
313 | #define UOBJECT_DEFINE_ABSTRACT_RTTI_IMPLEMENTATION(myClass) \ | |
314 | UClassID U_EXPORT2 myClass::getStaticClassID() { \ | |
73c04bcf | 315 | static char classID = 0; \ |
374ca955 A |
316 | return (UClassID)&classID; \ |
317 | } | |
318 | ||
4388f060 | 319 | #endif /* U_HIDE_INTERNAL_API */ |
374ca955 | 320 | |
b75a7d8f | 321 | U_NAMESPACE_END |
f3c0d7a5 | 322 | #endif // U_SHOW_CPLUSPLUS_API |
b75a7d8f A |
323 | |
324 | #endif |