]>
git.saurik.com Git - apple/icu.git/blob - icuSources/common/unicode/uobject.h
2 ******************************************************************************
4 * Copyright (C) 2002-2012, 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"
24 * \brief C++ API: Common ICU base class UObject.
30 * Define this to define the throw() specification so
31 * certain functions do not throw any exceptions
33 * UMemory operator new methods should have the throw() specification
34 * appended to them, so that the compiler adds the additional NULL check
35 * before calling constructors. Without, if <code>operator new</code> returns NULL the
36 * constructor is still called, and if the constructor references member
37 * data, (which it typically does), the result is a segmentation violation.
42 #define U_NO_THROW throw()
47 /*===========================================================================*/
48 /* UClassID-based RTTI */
49 /*===========================================================================*/
52 * UClassID is used to identify classes without using the compiler's RTTI.
53 * This was used before C++ compilers consistently supported RTTI.
54 * ICU 4.6 requires compiler RTTI to be turned on.
56 * Each class hierarchy which needs
57 * to implement polymorphic clone() or operator==() defines two methods,
58 * described in detail below. UClassID values can be compared using
59 * operator==(). Nothing else should be done with them.
62 * getDynamicClassID() is declared in the base class of the hierarchy as
63 * a pure virtual. Each concrete subclass implements it in the same way:
68 * virtual UClassID getDynamicClassID() const = 0;
73 * virtual UClassID getDynamicClassID() const
74 * { return Derived::getStaticClassID(); }
78 * Each concrete class implements getStaticClassID() as well, which allows
79 * clients to test for a specific type.
84 * static UClassID U_EXPORT2 getStaticClassID();
86 * static char fgClassID;
90 * UClassID Derived::getStaticClassID()
91 * { return (UClassID)&Derived::fgClassID; }
92 * char Derived::fgClassID = 0; // Value is irrelevant
96 typedef void* UClassID
;
101 * UMemory is the common ICU base class.
102 * All other ICU C++ classes are derived from UMemory (starting with ICU 2.4).
104 * This is primarily to make it possible and simple to override the
105 * C++ memory management by adding new/delete operators to this base class.
107 * To override ALL ICU memory management, including that from plain C code,
108 * replace the allocation functions declared in cmemory.h
110 * UMemory does not contain any virtual functions.
111 * Common "boilerplate" functions are defined in UObject.
115 class U_COMMON_API UMemory
{
118 /* test versions for debugging shaper heap memory problems */
119 #ifdef SHAPER_MEMORY_DEBUG
120 static void * NewArray(int size
, int count
);
121 static void * GrowArray(void * array
, int newSize
);
122 static void FreeArray(void * array
);
125 #if U_OVERRIDE_CXX_ALLOCATION
127 * Override for ICU4C C++ memory management.
128 * simple, non-class types are allocated using the macros in common/cmemory.h
129 * (uprv_malloc(), uprv_free(), uprv_realloc());
130 * they or something else could be used here to implement C++ new/delete
131 * for ICU4C C++ classes
134 static void * U_EXPORT2
operator new(size_t size
) U_NO_THROW
;
137 * Override for ICU4C C++ memory management.
141 static void * U_EXPORT2
operator new[](size_t size
) U_NO_THROW
;
144 * Override for ICU4C C++ memory management.
145 * simple, non-class types are allocated using the macros in common/cmemory.h
146 * (uprv_malloc(), uprv_free(), uprv_realloc());
147 * they or something else could be used here to implement C++ new/delete
148 * for ICU4C C++ classes
151 static void U_EXPORT2
operator delete(void *p
) U_NO_THROW
;
154 * Override for ICU4C C++ memory management.
158 static void U_EXPORT2
operator delete[](void *p
) U_NO_THROW
;
160 #if U_HAVE_PLACEMENT_NEW
162 * Override for ICU4C C++ memory management for STL.
166 static inline void * U_EXPORT2
operator new(size_t, void *ptr
) U_NO_THROW
{ return ptr
; }
169 * Override for ICU4C C++ memory management for STL.
173 static inline void U_EXPORT2
operator delete(void *, void *) U_NO_THROW
{}
174 #endif /* U_HAVE_PLACEMENT_NEW */
175 #if U_HAVE_DEBUG_LOCATION_NEW
177 * This method overrides the MFC debug version of the operator new
179 * @param size The requested memory size
180 * @param file The file where the allocation was requested
181 * @param line The line where the allocation was requested
183 static void * U_EXPORT2
operator new(size_t size
, const char* file
, int line
) U_NO_THROW
;
185 * This method provides a matching delete for the MFC debug new
187 * @param p The pointer to the allocated memory
188 * @param file The file where the allocation was requested
189 * @param line The line where the allocation was requested
191 static void U_EXPORT2
operator delete(void* p
, const char* file
, int line
) U_NO_THROW
;
192 #endif /* U_HAVE_DEBUG_LOCATION_NEW */
193 #endif /* U_OVERRIDE_CXX_ALLOCATION */
196 * Assignment operator not declared. The compiler will provide one
197 * which does nothing since this class does not contain any data members.
198 * API/code coverage may show the assignment operator as present and
200 * Subclasses need this assignment operator if they use compiler-provided
201 * assignment operators of their own. An alternative to not declaring one
202 * here would be to declare and empty-implement a protected or public one.
203 UMemory &UMemory::operator=(const UMemory &);
208 * UObject is the common ICU "boilerplate" class.
209 * UObject inherits UMemory (starting with ICU 2.4),
210 * and all other public ICU C++ classes
211 * are derived from UObject (starting with ICU 2.2).
213 * UObject contains common virtual functions like for ICU's "poor man's RTTI".
214 * It does not contain default implementations of virtual methods
215 * like getDynamicClassID to allow derived classes such as Format
216 * to declare these as pure virtual.
218 * The clone() function is not available in UObject because it is not
219 * implemented by all ICU classes.
220 * Many ICU services provide a clone() function for their class trees,
221 * defined on the service's C++ base class, and all subclasses within that
222 * service class tree return a pointer to the service base class
223 * (which itself is a subclass of UObject).
224 * This is because some compilers do not support covariant (same-as-this)
225 * return types; cast to the appropriate subclass if necessary.
229 class U_COMMON_API UObject
: public UMemory
{
239 * ICU4C "poor man's RTTI", returns a UClassID for the actual ICU class.
243 virtual UClassID
getDynamicClassID() const = 0;
246 // the following functions are protected to prevent instantiation and
247 // direct use of UObject itself
249 // default constructor
250 // commented out because UObject is abstract (see getDynamicClassID)
251 // inline UObject() {}
254 // commented out because UObject is abstract (see getDynamicClassID)
255 // inline UObject(const UObject &other) {}
258 // TODO Sometime in the future. Implement operator==().
259 // (This comment inserted in 2.2)
260 // some or all of the following "boilerplate" functions may be made public
261 // in a future ICU4C release when all subclasses implement them
263 // assignment operator
264 // (not virtual, see "Taligent's Guide to Designing Programs" pp.73..74)
265 // commented out because the implementation is the same as a compiler's default
266 // UObject &operator=(const UObject &other) { return *this; }
268 // comparison operators
269 virtual inline UBool
operator==(const UObject
&other
) const { return this==&other
; }
270 inline UBool
operator!=(const UObject
&other
) const { return !operator==(other
); }
272 // clone() commented out from the base class:
273 // some compilers do not support co-variant return types
274 // (i.e., subclasses would have to return UObject * as well, instead of SubClass *)
275 // see also UObject class documentation.
276 // virtual UObject *clone() const;
280 * Assignment operator not declared. The compiler will provide one
281 * which does nothing since this class does not contain any data members.
282 * API/code coverage may show the assignment operator as present and
284 * Subclasses need this assignment operator if they use compiler-provided
285 * assignment operators of their own. An alternative to not declaring one
286 * here would be to declare and empty-implement a protected or public one.
287 UObject &UObject::operator=(const UObject &);
290 // Future implementation for RTTI that support subtyping. [alan]
296 // static UClassID getStaticClassID();
301 // UBool instanceOf(UClassID type) const;
304 #ifndef U_HIDE_INTERNAL_API
306 * This is a simple macro to add ICU RTTI to an ICU object implementation.
307 * This does not go into the header. This should only be used in *.cpp files.
309 * @param myClass The name of the class that needs RTTI defined.
312 #define UOBJECT_DEFINE_RTTI_IMPLEMENTATION(myClass) \
313 UClassID U_EXPORT2 myClass::getStaticClassID() { \
314 static char classID = 0; \
315 return (UClassID)&classID; \
317 UClassID myClass::getDynamicClassID() const \
318 { return myClass::getStaticClassID(); }
322 * This macro adds ICU RTTI to an ICU abstract class implementation.
323 * This macro should be invoked in *.cpp files. The corresponding
324 * header should declare getStaticClassID.
326 * @param myClass The name of the class that needs RTTI defined.
329 #define UOBJECT_DEFINE_ABSTRACT_RTTI_IMPLEMENTATION(myClass) \
330 UClassID U_EXPORT2 myClass::getStaticClassID() { \
331 static char classID = 0; \
332 return (UClassID)&classID; \
336 * This is a simple macro to express that a class and its subclasses do not offer
337 * ICU's "poor man's RTTI".
338 * Beginning with ICU 4.6, ICU requires C++ compiler RTTI.
339 * This does not go into the header. This should only be used in *.cpp files.
340 * Use this with a private getDynamicClassID() in an immediate subclass of UObject.
342 * @param myClass The name of the class that needs RTTI defined.
345 #define UOBJECT_DEFINE_NO_RTTI_IMPLEMENTATION(myClass) \
346 UClassID myClass::getDynamicClassID() const { return NULL; }
349 // * This macro adds ICU RTTI to an ICU concrete class implementation.
350 // * This macro should be invoked in *.cpp files. The corresponding
351 // * header should declare getDynamicClassID and getStaticClassID.
353 // * @param myClass The name of the class that needs RTTI defined.
354 // * @param myParent The name of the myClass's parent.
357 /*#define UOBJECT_DEFINE_RTTI_IMPLEMENTATION(myClass, myParent) \
358 UOBJECT_DEFINE_ABSTRACT_RTTI_IMPLEMENTATION(myClass, myParent) \
359 UClassID myClass::getDynamicClassID() const { \
360 return myClass::getStaticClassID(); \
363 #endif /* U_HIDE_INTERNAL_API */