]>
git.saurik.com Git - apple/icu.git/blob - icuSources/common/unicode/uobject.h
2 ******************************************************************************
4 * Copyright (C) 2002-2010, 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
44 * \def U_HAVE_PLACEMENT_NEW
45 * Define this to define the placement new and
46 * delete in UMemory for STL.
50 #ifndef U_HAVE_PLACEMENT_NEW
51 #define U_HAVE_PLACEMENT_NEW 1
56 * \def U_HAVE_DEBUG_LOCATION_NEW
57 * Define this to define the MFC debug
58 * version of the operator new.
62 #ifndef U_HAVE_DEBUG_LOCATION_NEW
63 #define U_HAVE_DEBUG_LOCATION_NEW 0
69 * Define this to define the throw() specification so
70 * certain functions do not throw any exceptions
72 * UMemory operator new methods should have the throw() specification
73 * appended to them, so that the compiler adds the additional NULL check
74 * before calling constructors. Without, if <code>operator new</code> returns NULL the
75 * constructor is still called, and if the constructor references member
76 * data, (which it typically does), the result is a segmentation violation.
81 #define U_NO_THROW throw()
87 * UMemory is the common ICU base class.
88 * All other ICU C++ classes are derived from UMemory (starting with ICU 2.4).
90 * This is primarily to make it possible and simple to override the
91 * C++ memory management by adding new/delete operators to this base class.
93 * To override ALL ICU memory management, including that from plain C code,
94 * replace the allocation functions declared in cmemory.h
96 * UMemory does not contain any virtual functions.
97 * Common "boilerplate" functions are defined in UObject.
101 class U_COMMON_API UMemory
{
104 /* test versions for debugging shaper heap memory problems */
105 #ifdef SHAPER_MEMORY_DEBUG
106 static void * NewArray(int size
, int count
);
107 static void * GrowArray(void * array
, int newSize
);
108 static void FreeArray(void * array
);
111 #if U_OVERRIDE_CXX_ALLOCATION
113 * Override for ICU4C C++ memory management.
114 * simple, non-class types are allocated using the macros in common/cmemory.h
115 * (uprv_malloc(), uprv_free(), uprv_realloc());
116 * they or something else could be used here to implement C++ new/delete
117 * for ICU4C C++ classes
120 static void * U_EXPORT2
operator new(size_t size
) U_NO_THROW
;
123 * Override for ICU4C C++ memory management.
127 static void * U_EXPORT2
operator new[](size_t size
) U_NO_THROW
;
130 * Override for ICU4C C++ memory management.
131 * simple, non-class types are allocated using the macros in common/cmemory.h
132 * (uprv_malloc(), uprv_free(), uprv_realloc());
133 * they or something else could be used here to implement C++ new/delete
134 * for ICU4C C++ classes
137 static void U_EXPORT2
operator delete(void *p
) U_NO_THROW
;
140 * Override for ICU4C C++ memory management.
144 static void U_EXPORT2
operator delete[](void *p
) U_NO_THROW
;
146 #if U_HAVE_PLACEMENT_NEW
148 * Override for ICU4C C++ memory management for STL.
152 static inline void * U_EXPORT2
operator new(size_t, void *ptr
) U_NO_THROW
{ return ptr
; }
155 * Override for ICU4C C++ memory management for STL.
159 static inline void U_EXPORT2
operator delete(void *, void *) U_NO_THROW
{}
160 #endif /* U_HAVE_PLACEMENT_NEW */
161 #if U_HAVE_DEBUG_LOCATION_NEW
163 * This method overrides the MFC debug version of the operator new
165 * @param size The requested memory size
166 * @param file The file where the allocation was requested
167 * @param line The line where the allocation was requested
169 static void * U_EXPORT2
operator new(size_t size
, const char* file
, int line
) U_NO_THROW
;
171 * This method provides a matching delete for the MFC debug new
173 * @param p The pointer to the allocated memory
174 * @param file The file where the allocation was requested
175 * @param line The line where the allocation was requested
177 static void U_EXPORT2
operator delete(void* p
, const char* file
, int line
) U_NO_THROW
;
178 #endif /* U_HAVE_DEBUG_LOCATION_NEW */
179 #endif /* U_OVERRIDE_CXX_ALLOCATION */
182 * Assignment operator not declared. The compiler will provide one
183 * which does nothing since this class does not contain any data members.
184 * API/code coverage may show the assignment operator as present and
186 * Subclasses need this assignment operator if they use compiler-provided
187 * assignment operators of their own. An alternative to not declaring one
188 * here would be to declare and empty-implement a protected or public one.
189 UMemory &UMemory::operator=(const UMemory &);
194 * UObject is the common ICU "boilerplate" class.
195 * UObject inherits UMemory (starting with ICU 2.4),
196 * and all other public ICU C++ classes
197 * are derived from UObject (starting with ICU 2.2).
199 * UObject contains common virtual functions like for ICU's "poor man's RTTI".
200 * It does not contain default implementations of virtual methods
201 * like getDynamicClassID to allow derived classes such as Format
202 * to declare these as pure virtual.
204 * The clone() function is not available in UObject because it is not
205 * implemented by all ICU classes.
206 * Many ICU services provide a clone() function for their class trees,
207 * defined on the service's C++ base class, and all subclasses within that
208 * service class tree return a pointer to the service base class
209 * (which itself is a subclass of UObject).
210 * This is because some compilers do not support covariant (same-as-this)
211 * return types; cast to the appropriate subclass if necessary.
215 class U_COMMON_API UObject
: public UMemory
{
225 * ICU4C "poor man's RTTI", returns a UClassID for the actual ICU class.
229 virtual UClassID
getDynamicClassID() const = 0;
232 // the following functions are protected to prevent instantiation and
233 // direct use of UObject itself
235 // default constructor
236 // commented out because UObject is abstract (see getDynamicClassID)
237 // inline UObject() {}
240 // commented out because UObject is abstract (see getDynamicClassID)
241 // inline UObject(const UObject &other) {}
244 // TODO Sometime in the future. Implement operator==().
245 // (This comment inserted in 2.2)
246 // some or all of the following "boilerplate" functions may be made public
247 // in a future ICU4C release when all subclasses implement them
249 // assignment operator
250 // (not virtual, see "Taligent's Guide to Designing Programs" pp.73..74)
251 // commented out because the implementation is the same as a compiler's default
252 // UObject &operator=(const UObject &other) { return *this; }
254 // comparison operators
255 virtual inline UBool
operator==(const UObject
&other
) const { return this==&other
; }
256 inline UBool
operator!=(const UObject
&other
) const { return !operator==(other
); }
258 // clone() commented out from the base class:
259 // some compilers do not support co-variant return types
260 // (i.e., subclasses would have to return UObject * as well, instead of SubClass *)
261 // see also UObject class documentation.
262 // virtual UObject *clone() const;
266 * Assignment operator not declared. The compiler will provide one
267 * which does nothing since this class does not contain any data members.
268 * API/code coverage may show the assignment operator as present and
270 * Subclasses need this assignment operator if they use compiler-provided
271 * assignment operators of their own. An alternative to not declaring one
272 * here would be to declare and empty-implement a protected or public one.
273 UObject &UObject::operator=(const UObject &);
276 // Future implementation for RTTI that support subtyping. [alan]
282 // static UClassID getStaticClassID();
287 // UBool instanceOf(UClassID type) const;
291 * This is a simple macro to add ICU RTTI to an ICU object implementation.
292 * This does not go into the header. This should only be used in *.cpp files.
294 * @param myClass The name of the class that needs RTTI defined.
297 #define UOBJECT_DEFINE_RTTI_IMPLEMENTATION(myClass) \
298 UClassID U_EXPORT2 myClass::getStaticClassID() { \
299 static char classID = 0; \
300 return (UClassID)&classID; \
302 UClassID myClass::getDynamicClassID() const \
303 { return myClass::getStaticClassID(); }
307 * This macro adds ICU RTTI to an ICU abstract class implementation.
308 * This macro should be invoked in *.cpp files. The corresponding
309 * header should declare getStaticClassID.
311 * @param myClass The name of the class that needs RTTI defined.
314 #define UOBJECT_DEFINE_ABSTRACT_RTTI_IMPLEMENTATION(myClass) \
315 UClassID U_EXPORT2 myClass::getStaticClassID() { \
316 static char classID = 0; \
317 return (UClassID)&classID; \
321 * This is a simple macro to express that a class and its subclasses do not offer
322 * ICU's "poor man's RTTI".
323 * Beginning with ICU 4.6, ICU requires C++ compiler RTTI.
324 * This does not go into the header. This should only be used in *.cpp files.
325 * Use this with a private getDynamicClassID() in an immediate subclass of UObject.
327 * @param myClass The name of the class that needs RTTI defined.
330 #define UOBJECT_DEFINE_NO_RTTI_IMPLEMENTATION(myClass) \
331 UClassID myClass::getDynamicClassID() const { return NULL; }
334 // * This macro adds ICU RTTI to an ICU concrete class implementation.
335 // * This macro should be invoked in *.cpp files. The corresponding
336 // * header should declare getDynamicClassID and getStaticClassID.
338 // * @param myClass The name of the class that needs RTTI defined.
339 // * @param myParent The name of the myClass's parent.
342 /*#define UOBJECT_DEFINE_RTTI_IMPLEMENTATION(myClass, myParent) \
343 UOBJECT_DEFINE_ABSTRACT_RTTI_IMPLEMENTATION(myClass, myParent) \
344 UClassID myClass::getDynamicClassID() const { \
345 return myClass::getStaticClassID(); \