]>
git.saurik.com Git - apple/icu.git/blob - icuSources/common/unicode/uobject.h
2 ******************************************************************************
4 * Copyright (C) 2002-2007, 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 /** U_HAVE_DEBUG_LOCATION_NEW - Define this to define the MFC debug
54 * version of the operator new.
58 #ifndef U_HAVE_DEBUG_LOCATION_NEW
59 #define U_HAVE_DEBUG_LOCATION_NEW 0
63 * UMemory is the common ICU base class.
64 * All other ICU C++ classes are derived from UMemory (starting with ICU 2.4).
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.
69 * To override ALL ICU memory management, including that from plain C code,
70 * replace the allocation functions declared in cmemory.h
72 * UMemory does not contain any virtual functions.
73 * Common "boilerplate" functions are defined in UObject.
77 class U_COMMON_API UMemory
{
80 #if U_OVERRIDE_CXX_ALLOCATION
82 * Override for ICU4C C++ memory management.
83 * simple, non-class types are allocated using the macros in common/cmemory.h
84 * (uprv_malloc(), uprv_free(), uprv_realloc());
85 * they or something else could be used here to implement C++ new/delete
86 * for ICU4C C++ classes
89 static void * U_EXPORT2
operator new(size_t size
);
92 * Override for ICU4C C++ memory management.
96 static void * U_EXPORT2
operator new[](size_t size
);
99 * Override for ICU4C C++ memory management.
100 * simple, non-class types are allocated using the macros in common/cmemory.h
101 * (uprv_malloc(), uprv_free(), uprv_realloc());
102 * they or something else could be used here to implement C++ new/delete
103 * for ICU4C C++ classes
106 static void U_EXPORT2
operator delete(void *p
);
109 * Override for ICU4C C++ memory management.
113 static void U_EXPORT2
operator delete[](void *p
);
115 #if U_HAVE_PLACEMENT_NEW
117 * Override for ICU4C C++ memory management for STL.
121 static inline void * U_EXPORT2
operator new(size_t, void *ptr
) { return ptr
; }
124 * Override for ICU4C C++ memory management for STL.
128 static inline void U_EXPORT2
operator delete(void *, void *) {}
129 #endif /* U_HAVE_PLACEMENT_NEW */
130 #if U_HAVE_DEBUG_LOCATION_NEW
132 * This method overrides the MFC debug version of the operator new
134 * @param size The requested memory size
135 * @param file The file where the allocation was requested
136 * @param line The line where the allocation was requested
138 static void * U_EXPORT2
operator new(size_t size
, const char* file
, int line
);
140 * This method provides a matching delete for the MFC debug new
142 * @param p The pointer to the allocated memory
143 * @param file The file where the allocation was requested
144 * @param line The line where the allocation was requested
146 static void U_EXPORT2
operator delete(void* p
, const char* file
, int line
);
147 #endif /* U_HAVE_DEBUG_LOCATION_NEW */
148 #endif /* U_OVERRIDE_CXX_ALLOCATION */
151 * Assignment operator not declared. The compiler will provide one
152 * which does nothing since this class does not contain any data members.
153 * API/code coverage may show the assignment operator as present and
155 * Subclasses need this assignment operator if they use compiler-provided
156 * assignment operators of their own. An alternative to not declaring one
157 * here would be to declare and empty-implement a protected or public one.
158 UMemory &UMemory::operator=(const UMemory &);
163 * UObject is the common ICU "boilerplate" class.
164 * UObject inherits UMemory (starting with ICU 2.4),
165 * and all other public ICU C++ classes
166 * are derived from UObject (starting with ICU 2.2).
168 * UObject contains common virtual functions like for ICU's "poor man's RTTI".
169 * It does not contain default implementations of virtual methods
170 * like getDynamicClassID to allow derived classes such as Format
171 * to declare these as pure virtual.
173 * The clone() function is not available in UObject because it is not
174 * implemented by all ICU classes.
175 * Many ICU services provide a clone() function for their class trees,
176 * defined on the service's C++ base class, and all subclasses within that
177 * service class tree return a pointer to the service base class
178 * (which itself is a subclass of UObject).
179 * This is because some compilers do not support covariant (same-as-this)
180 * return types; cast to the appropriate subclass if necessary.
184 class U_COMMON_API UObject
: public UMemory
{
194 * ICU4C "poor man's RTTI", returns a UClassID for the actual ICU class.
198 virtual UClassID
getDynamicClassID() const = 0;
201 // the following functions are protected to prevent instantiation and
202 // direct use of UObject itself
204 // default constructor
205 // commented out because UObject is abstract (see getDynamicClassID)
206 // inline UObject() {}
209 // commented out because UObject is abstract (see getDynamicClassID)
210 // inline UObject(const UObject &other) {}
213 // TODO Sometime in the future. Implement operator==().
214 // (This comment inserted in 2.2)
215 // some or all of the following "boilerplate" functions may be made public
216 // in a future ICU4C release when all subclasses implement them
218 // assignment operator
219 // (not virtual, see "Taligent's Guide to Designing Programs" pp.73..74)
220 // commented out because the implementation is the same as a compiler's default
221 // UObject &operator=(const UObject &other) { return *this; }
223 // comparison operators
224 virtual inline UBool
operator==(const UObject
&other
) const { return this==&other
; }
225 inline UBool
operator!=(const UObject
&other
) const { return !operator==(other
); }
227 // clone() commented out from the base class:
228 // some compilers do not support co-variant return types
229 // (i.e., subclasses would have to return UObject * as well, instead of SubClass *)
230 // see also UObject class documentation.
231 // virtual UObject *clone() const;
235 * Assignment operator not declared. The compiler will provide one
236 * which does nothing since this class does not contain any data members.
237 * API/code coverage may show the assignment operator as present and
239 * Subclasses need this assignment operator if they use compiler-provided
240 * assignment operators of their own. An alternative to not declaring one
241 * here would be to declare and empty-implement a protected or public one.
242 UObject &UObject::operator=(const UObject &);
245 // Future implementation for RTTI that support subtyping. [alan]
251 // static UClassID getStaticClassID();
256 // UBool instanceOf(UClassID type) const;
260 * This is a simple macro to add ICU RTTI to an ICU object implementation.
261 * This does not go into the header. This should only be used in *.cpp files.
263 * @param myClass The name of the class that needs RTTI defined.
266 #define UOBJECT_DEFINE_RTTI_IMPLEMENTATION(myClass) \
267 UClassID U_EXPORT2 myClass::getStaticClassID() { \
268 static char classID = 0; \
269 return (UClassID)&classID; \
271 UClassID myClass::getDynamicClassID() const \
272 { return myClass::getStaticClassID(); }
276 * This macro adds ICU RTTI to an ICU abstract class implementation.
277 * This macro should be invoked in *.cpp files. The corresponding
278 * header should declare getStaticClassID.
280 * @param myClass The name of the class that needs RTTI defined.
283 #define UOBJECT_DEFINE_ABSTRACT_RTTI_IMPLEMENTATION(myClass) \
284 UClassID U_EXPORT2 myClass::getStaticClassID() { \
285 static char classID = 0; \
286 return (UClassID)&classID; \
290 // * This macro adds ICU RTTI to an ICU concrete class implementation.
291 // * This macro should be invoked in *.cpp files. The corresponding
292 // * header should declare getDynamicClassID and getStaticClassID.
294 // * @param myClass The name of the class that needs RTTI defined.
295 // * @param myParent The name of the myClass's parent.
298 /*#define UOBJECT_DEFINE_RTTI_IMPLEMENTATION(myClass, myParent) \
299 UOBJECT_DEFINE_ABSTRACT_RTTI_IMPLEMENTATION(myClass, myParent) \
300 UClassID myClass::getDynamicClassID() const { \
301 return myClass::getStaticClassID(); \