1/*
2******************************************************************************
3*
4* Copyright (C) 2002-2012, International Business Machines
5* Corporation and others. All Rights Reserved.
6*
7******************************************************************************
8* file name: uobject.h
9* encoding: US-ASCII
10* tab size: 8 (not used)
11* indentation:4
12*
13* created on: 2002jun26
14* created by: Markus W. Scherer
15*/
16
17#ifndef __UOBJECT_H__
18#define __UOBJECT_H__
19
20#include "unicode/utypes.h"
21
22/**
23 * \file
24 * \brief C++ API: Common ICU base class UObject.
25 */
26
27/**
28 * @{
29 * \def U_NO_THROW
30 * Define this to define the throw() specification so
31 * certain functions do not throw any exceptions
32 *
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.
38 *
39 * @stable ICU 4.2
40 */
41#ifndef U_NO_THROW
42#define U_NO_THROW throw()
43#endif
44
45/** @} */
46
47/*===========================================================================*/
48/* UClassID-based RTTI */
49/*===========================================================================*/
50
51/**
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.
55 *
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.
60 *
61 * \par
62 * In class hierarchies that implement "poor man's RTTI",
63 * each concrete subclass implements getDynamicClassID() in the same way:
64 *
65 * \code
66 * class Derived {
67 * public:
68 * virtual UClassID getDynamicClassID() const
69 * { return Derived::getStaticClassID(); }
70 * }
71 * \endcode
72 *
73 * Each concrete class implements getStaticClassID() as well, which allows
74 * clients to test for a specific type.
75 *
76 * \code
77 * class Derived {
78 * public:
79 * static UClassID U_EXPORT2 getStaticClassID();
80 * private:
81 * static char fgClassID;
82 * }
83 *
84 * // In Derived.cpp:
85 * UClassID Derived::getStaticClassID()
86 * { return (UClassID)&Derived::fgClassID; }
87 * char Derived::fgClassID = 0; // Value is irrelevant
88 * \endcode
89 * @stable ICU 2.0
90 */
91typedef void* UClassID;
92
93U_NAMESPACE_BEGIN
94
95/**
96 * UMemory is the common ICU base class.
97 * All other ICU C++ classes are derived from UMemory (starting with ICU 2.4).
98 *
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.
101 *
102 * To override ALL ICU memory management, including that from plain C code,
103 * replace the allocation functions declared in cmemory.h
104 *
105 * UMemory does not contain any virtual functions.
106 * Common "boilerplate" functions are defined in UObject.
107 *
108 * @stable ICU 2.4
109 */
110class U_COMMON_API UMemory {
111public:
112
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 );
118#endif
119
120#if U_OVERRIDE_CXX_ALLOCATION
121 /**
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
127 * @stable ICU 2.4
128 */
129 static void * U_EXPORT2 operator new(size_t size) U_NO_THROW;
130
131 /**
132 * Override for ICU4C C++ memory management.
133 * See new().
134 * @stable ICU 2.4
135 */
136 static void * U_EXPORT2 operator new[](size_t size) U_NO_THROW;
137
138 /**
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
144 * @stable ICU 2.4
145 */
146 static void U_EXPORT2 operator delete(void *p) U_NO_THROW;
147
148 /**
149 * Override for ICU4C C++ memory management.
150 * See delete().
151 * @stable ICU 2.4
152 */
153 static void U_EXPORT2 operator delete[](void *p) U_NO_THROW;
154
155#if U_HAVE_PLACEMENT_NEW
156 /**
157 * Override for ICU4C C++ memory management for STL.
158 * See new().
159 * @stable ICU 2.6
160 */
161 static inline void * U_EXPORT2 operator new(size_t, void *ptr) U_NO_THROW { return ptr; }
162
163 /**
164 * Override for ICU4C C++ memory management for STL.
165 * See delete().
166 * @stable ICU 2.6
167 */
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
171 /**
172 * This method overrides the MFC debug version of the operator new
173 *
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
177 */
178 static void * U_EXPORT2 operator new(size_t size, const char* file, int line) U_NO_THROW;
179 /**
180 * This method provides a matching delete for the MFC debug new
181 *
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
185 */
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 */
189
190 /*
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
194 * untested - ignore.
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 &);
199 */
200};
201
202/**
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).
207 *
208 * UObject contains common virtual functions, in particular a virtual destructor.
209 *
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.
218 *
219 * @stable ICU 2.2
220 */
221class U_COMMON_API UObject : public UMemory {
222public:
223 /**
224 * Destructor.
225 *
226 * @stable ICU 2.2
227 */
228 virtual ~UObject();
229
230 /**
231 * ICU4C "poor man's RTTI", returns a UClassID for the actual ICU class.
232 * The base class implementation returns a dummy value.
233 *
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".
236 *
237 * @stable ICU 2.2
238 */
239 virtual UClassID getDynamicClassID() const;
240
241protected:
242 // the following functions are protected to prevent instantiation and
243 // direct use of UObject itself
244
245 // default constructor
246 // inline UObject() {}
247
248 // copy constructor
249 // inline UObject(const UObject &other) {}
250
251#if 0
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
256
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; }
261
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); }
265
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;
271#endif
272
273 /*
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
277 * untested - ignore.
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 &);
282 */
283};
284
285#ifndef U_HIDE_INTERNAL_API
286/**
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.
289 *
290 * @param myClass The name of the class that needs RTTI defined.
291 * @internal
292 */
293#define UOBJECT_DEFINE_RTTI_IMPLEMENTATION(myClass) \
294 UClassID U_EXPORT2 myClass::getStaticClassID() { \
295 static char classID = 0; \
296 return (UClassID)&classID; \
297 } \
298 UClassID myClass::getDynamicClassID() const \
299 { return myClass::getStaticClassID(); }
300
301
302/**
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.
306 *
307 * @param myClass The name of the class that needs RTTI defined.
308 * @internal
309 */
310#define UOBJECT_DEFINE_ABSTRACT_RTTI_IMPLEMENTATION(myClass) \
311 UClassID U_EXPORT2 myClass::getStaticClassID() { \
312 static char classID = 0; \
313 return (UClassID)&classID; \
314 }
315
316#endif /* U_HIDE_INTERNAL_API */
317
318U_NAMESPACE_END
319
320#endif
321