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 * In class hierarchies that implement "poor man's RTTI",
63 * each concrete subclass implements getDynamicClassID() in the same way:
68 * virtual UClassID getDynamicClassID() const
69 * { return Derived::getStaticClassID(); }
73 * Each concrete class implements getStaticClassID() as well, which allows
74 * clients to test for a specific type.
79 * static UClassID U_EXPORT2 getStaticClassID();
81 * static char fgClassID;
85 * UClassID Derived::getStaticClassID()
86 * { return (UClassID)&Derived::fgClassID; }
87 * char Derived::fgClassID = 0; // Value is irrelevant
91 typedef void* UClassID
;
96 * UMemory is the common ICU base class.
97 * All other ICU C++ classes are derived from UMemory (starting with ICU 2.4).
99 * This is primarily to make it possible and simple to override the
100 * C++ memory management by adding new/delete operators to this base class.
102 * To override ALL ICU memory management, including that from plain C code,
103 * replace the allocation functions declared in cmemory.h
105 * UMemory does not contain any virtual functions.
106 * Common "boilerplate" functions are defined in UObject.
110 class U_COMMON_API UMemory
{
113 /* test versions for debugging shaper heap memory problems */
114 #ifdef SHAPER_MEMORY_DEBUG
115 static void * NewArray(int size
, int count
);
116 static void * GrowArray(void * array
, int newSize
);
117 static void FreeArray(void * array
);
120 #if U_OVERRIDE_CXX_ALLOCATION
122 * Override for ICU4C C++ memory management.
123 * simple, non-class types are allocated using the macros in common/cmemory.h
124 * (uprv_malloc(), uprv_free(), uprv_realloc());
125 * they or something else could be used here to implement C++ new/delete
126 * for ICU4C C++ classes
129 static void * U_EXPORT2
operator new(size_t size
) U_NO_THROW
;
132 * Override for ICU4C C++ memory management.
136 static void * U_EXPORT2
operator new[](size_t size
) U_NO_THROW
;
139 * Override for ICU4C C++ memory management.
140 * simple, non-class types are allocated using the macros in common/cmemory.h
141 * (uprv_malloc(), uprv_free(), uprv_realloc());
142 * they or something else could be used here to implement C++ new/delete
143 * for ICU4C C++ classes
146 static void U_EXPORT2
operator delete(void *p
) U_NO_THROW
;
149 * Override for ICU4C C++ memory management.
153 static void U_EXPORT2
operator delete[](void *p
) U_NO_THROW
;
155 #if U_HAVE_PLACEMENT_NEW
157 * Override for ICU4C C++ memory management for STL.
161 static inline void * U_EXPORT2
operator new(size_t, void *ptr
) U_NO_THROW
{ return ptr
; }
164 * Override for ICU4C C++ memory management for STL.
168 static inline void U_EXPORT2
operator delete(void *, void *) U_NO_THROW
{}
169 #endif /* U_HAVE_PLACEMENT_NEW */
170 #if U_HAVE_DEBUG_LOCATION_NEW
172 * This method overrides the MFC debug version of the operator new
174 * @param size The requested memory size
175 * @param file The file where the allocation was requested
176 * @param line The line where the allocation was requested
178 static void * U_EXPORT2
operator new(size_t size
, const char* file
, int line
) U_NO_THROW
;
180 * This method provides a matching delete for the MFC debug new
182 * @param p The pointer to the allocated memory
183 * @param file The file where the allocation was requested
184 * @param line The line where the allocation was requested
186 static void U_EXPORT2
operator delete(void* p
, const char* file
, int line
) U_NO_THROW
;
187 #endif /* U_HAVE_DEBUG_LOCATION_NEW */
188 #endif /* U_OVERRIDE_CXX_ALLOCATION */
191 * Assignment operator not declared. The compiler will provide one
192 * which does nothing since this class does not contain any data members.
193 * API/code coverage may show the assignment operator as present and
195 * Subclasses need this assignment operator if they use compiler-provided
196 * assignment operators of their own. An alternative to not declaring one
197 * here would be to declare and empty-implement a protected or public one.
198 UMemory &UMemory::operator=(const UMemory &);
203 * UObject is the common ICU "boilerplate" class.
204 * UObject inherits UMemory (starting with ICU 2.4),
205 * and all other public ICU C++ classes
206 * are derived from UObject (starting with ICU 2.2).
208 * UObject contains common virtual functions, in particular a virtual destructor.
210 * The clone() function is not available in UObject because it is not
211 * implemented by all ICU classes.
212 * Many ICU services provide a clone() function for their class trees,
213 * defined on the service's C++ base class, and all subclasses within that
214 * service class tree return a pointer to the service base class
215 * (which itself is a subclass of UObject).
216 * This is because some compilers do not support covariant (same-as-this)
217 * return types; cast to the appropriate subclass if necessary.
221 class U_COMMON_API UObject
: public UMemory
{
231 * ICU4C "poor man's RTTI", returns a UClassID for the actual ICU class.
232 * The base class implementation returns a dummy value.
234 * Use compiler RTTI rather than ICU's "poor man's RTTI".
235 * Since ICU 4.6, new ICU C++ class hierarchies do not implement "poor man's RTTI".
239 virtual UClassID
getDynamicClassID() const;
242 // the following functions are protected to prevent instantiation and
243 // direct use of UObject itself
245 // default constructor
246 // inline UObject() {}
249 // inline UObject(const UObject &other) {}
252 // TODO Sometime in the future. Implement operator==().
253 // (This comment inserted in 2.2)
254 // some or all of the following "boilerplate" functions may be made public
255 // in a future ICU4C release when all subclasses implement them
257 // assignment operator
258 // (not virtual, see "Taligent's Guide to Designing Programs" pp.73..74)
259 // commented out because the implementation is the same as a compiler's default
260 // UObject &operator=(const UObject &other) { return *this; }
262 // comparison operators
263 virtual inline UBool
operator==(const UObject
&other
) const { return this==&other
; }
264 inline UBool
operator!=(const UObject
&other
) const { return !operator==(other
); }
266 // clone() commented out from the base class:
267 // some compilers do not support co-variant return types
268 // (i.e., subclasses would have to return UObject * as well, instead of SubClass *)
269 // see also UObject class documentation.
270 // virtual UObject *clone() const;
274 * Assignment operator not declared. The compiler will provide one
275 * which does nothing since this class does not contain any data members.
276 * API/code coverage may show the assignment operator as present and
278 * Subclasses need this assignment operator if they use compiler-provided
279 * assignment operators of their own. An alternative to not declaring one
280 * here would be to declare and empty-implement a protected or public one.
281 UObject &UObject::operator=(const UObject &);
285 #ifndef U_HIDE_INTERNAL_API
287 * This is a simple macro to add ICU RTTI to an ICU object implementation.
288 * This does not go into the header. This should only be used in *.cpp files.
290 * @param myClass The name of the class that needs RTTI defined.
293 #define UOBJECT_DEFINE_RTTI_IMPLEMENTATION(myClass) \
294 UClassID U_EXPORT2 myClass::getStaticClassID() { \
295 static char classID = 0; \
296 return (UClassID)&classID; \
298 UClassID myClass::getDynamicClassID() const \
299 { return myClass::getStaticClassID(); }
303 * This macro adds ICU RTTI to an ICU abstract class implementation.
304 * This macro should be invoked in *.cpp files. The corresponding
305 * header should declare getStaticClassID.
307 * @param myClass The name of the class that needs RTTI defined.
310 #define UOBJECT_DEFINE_ABSTRACT_RTTI_IMPLEMENTATION(myClass) \
311 UClassID U_EXPORT2 myClass::getStaticClassID() { \
312 static char classID = 0; \
313 return (UClassID)&classID; \
316 #endif /* U_HIDE_INTERNAL_API */