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