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