]>
git.saurik.com Git - apple/icu.git/blob - icuSources/common/unicode/uobject.h
2 ******************************************************************************
4 * Copyright (C) 2002-2006, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 ******************************************************************************
10 * tab size: 8 (not used)
13 * created on: 2002jun26
14 * created by: Markus W. Scherer
20 #include "unicode/utypes.h"
26 * \brief C++ API: Common ICU base class UObject.
29 /** U_OVERRIDE_CXX_ALLOCATION - Define this to override operator new and
30 * delete in UMemory. Enabled by default for ICU.
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.
39 #ifndef U_OVERRIDE_CXX_ALLOCATION
40 #define U_OVERRIDE_CXX_ALLOCATION 1
43 /** U_HAVE_PLACEMENT_NEW - Define this to define the placement new and
44 * delete in UMemory for STL.
48 #ifndef U_HAVE_PLACEMENT_NEW
49 #define U_HAVE_PLACEMENT_NEW 1
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.
59 #ifndef U_HAVE_DEBUG_LOCATION_NEW
60 #define U_HAVE_DEBUG_LOCATION_NEW 0
62 #endif /*U_HIDE_DRAFT_API*/
65 * UMemory is the common ICU base class.
66 * All other ICU C++ classes are derived from UMemory (starting with ICU 2.4).
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.
71 * To override ALL ICU memory management, including that from plain C code,
72 * replace the allocation functions declared in cmemory.h
74 * UMemory does not contain any virtual functions.
75 * Common "boilerplate" functions are defined in UObject.
79 class U_COMMON_API UMemory
{
82 #if U_OVERRIDE_CXX_ALLOCATION
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
91 static void * U_EXPORT2
operator new(size_t size
);
94 * Override for ICU4C C++ memory management.
98 static void * U_EXPORT2
operator new[](size_t size
);
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
108 static void U_EXPORT2
operator delete(void *p
);
111 * Override for ICU4C C++ memory management.
115 static void U_EXPORT2
operator delete[](void *p
);
117 #if U_HAVE_PLACEMENT_NEW
119 * Override for ICU4C C++ memory management for STL.
123 static inline void * U_EXPORT2
operator new(size_t, void *ptr
) { return ptr
; }
126 * Override for ICU4C C++ memory management for STL.
130 static inline void U_EXPORT2
operator delete(void *, void *) {}
131 #endif /* U_HAVE_PLACEMENT_NEW */
132 #if U_HAVE_DEBUG_LOCATION_NEW
134 * This method overrides the MFC debug version of the operator new
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
140 static void * U_EXPORT2
operator new(size_t size
, const char* file
, int line
);
142 * This method provides a matching delete for the MFC debug new
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
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 */
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
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 &);
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).
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.
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.
186 class U_COMMON_API UObject
: public UMemory
{
196 * ICU4C "poor man's RTTI", returns a UClassID for the actual ICU class.
200 virtual UClassID
getDynamicClassID() const = 0;
203 // the following functions are protected to prevent instantiation and
204 // direct use of UObject itself
206 // default constructor
207 // commented out because UObject is abstract (see getDynamicClassID)
208 // inline UObject() {}
211 // commented out because UObject is abstract (see getDynamicClassID)
212 // inline UObject(const UObject &other) {}
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
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; }
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
); }
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;
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
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 &);
247 // Future implementation for RTTI that support subtyping. [alan]
253 // static UClassID getStaticClassID();
258 // UBool instanceOf(UClassID type) const;
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.
265 * @param myClass The name of the class that needs RTTI defined.
268 #define UOBJECT_DEFINE_RTTI_IMPLEMENTATION(myClass) \
269 UClassID U_EXPORT2 myClass::getStaticClassID() { \
270 static char classID = 0; \
271 return (UClassID)&classID; \
273 UClassID myClass::getDynamicClassID() const \
274 { return myClass::getStaticClassID(); }
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.
282 * @param myClass The name of the class that needs RTTI defined.
285 #define UOBJECT_DEFINE_ABSTRACT_RTTI_IMPLEMENTATION(myClass) \
286 UClassID U_EXPORT2 myClass::getStaticClassID() { \
287 static char classID = 0; \
288 return (UClassID)&classID; \
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.
296 // * @param myClass The name of the class that needs RTTI defined.
297 // * @param myParent The name of the myClass's parent.
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(); \