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