• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 ******************************************************************************
3 *
4 *   Copyright (C) 2002-2007, 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 U_NAMESPACE_BEGIN
23 
24 /**
25  * \file
26  * \brief C++ API: Common ICU base class UObject.
27  */
28 
29 /**  U_OVERRIDE_CXX_ALLOCATION - Define this to override operator new and
30  *                               delete in UMemory. Enabled by default for ICU.
31  *
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.
36  *
37  * @stable ICU 2.2
38  */
39 #ifndef U_OVERRIDE_CXX_ALLOCATION
40 #define U_OVERRIDE_CXX_ALLOCATION 1
41 #endif
42 
43 /**  U_HAVE_PLACEMENT_NEW - Define this to define the placement new and
44  *                          delete in UMemory for STL.
45  *
46  * @stable ICU 2.6
47  */
48 #ifndef U_HAVE_PLACEMENT_NEW
49 #define U_HAVE_PLACEMENT_NEW 1
50 #endif
51 
52 
53 /**  U_HAVE_DEBUG_LOCATION_NEW - Define this to define the MFC debug
54  * version of the operator new.
55  *
56  * @stable ICU 3.4
57  */
58 #ifndef U_HAVE_DEBUG_LOCATION_NEW
59 #define U_HAVE_DEBUG_LOCATION_NEW 0
60 #endif
61 
62 /**
63  * UMemory is the common ICU base class.
64  * All other ICU C++ classes are derived from UMemory (starting with ICU 2.4).
65  *
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.
68  *
69  * To override ALL ICU memory management, including that from plain C code,
70  * replace the allocation functions declared in cmemory.h
71  *
72  * UMemory does not contain any virtual functions.
73  * Common "boilerplate" functions are defined in UObject.
74  *
75  * @stable ICU 2.4
76  */
77 class U_COMMON_API UMemory {
78 public:
79 
80 #if U_OVERRIDE_CXX_ALLOCATION
81     /**
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
87      * @stable ICU 2.4
88      */
89     static void * U_EXPORT2 operator new(size_t size);
90 
91     /**
92      * Override for ICU4C C++ memory management.
93      * See new().
94      * @stable ICU 2.4
95      */
96     static void * U_EXPORT2 operator new[](size_t size);
97 
98     /**
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
104      * @stable ICU 2.4
105      */
106     static void U_EXPORT2 operator delete(void *p);
107 
108     /**
109      * Override for ICU4C C++ memory management.
110      * See delete().
111      * @stable ICU 2.4
112      */
113     static void U_EXPORT2 operator delete[](void *p);
114 
115 #if U_HAVE_PLACEMENT_NEW
116     /**
117      * Override for ICU4C C++ memory management for STL.
118      * See new().
119      * @stable ICU 2.6
120      */
new(size_t,void * ptr)121     static inline void * U_EXPORT2 operator new(size_t, void *ptr) { return ptr; }
122 
123     /**
124      * Override for ICU4C C++ memory management for STL.
125      * See delete().
126      * @stable ICU 2.6
127      */
delete(void *,void *)128     static inline void U_EXPORT2 operator delete(void *, void *) {}
129 #endif /* U_HAVE_PLACEMENT_NEW */
130 #if U_HAVE_DEBUG_LOCATION_NEW
131     /**
132       * This method overrides the MFC debug version of the operator new
133       *
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
137       */
138     static void * U_EXPORT2 operator new(size_t size, const char* file, int line);
139     /**
140       * This method provides a matching delete for the MFC debug new
141       *
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
145       */
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 */
149 
150     /*
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
154      * untested - ignore.
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 &);
159      */
160 };
161 
162 /**
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).
167  *
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.
172  *
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.
181  *
182  * @stable ICU 2.2
183  */
184 class U_COMMON_API UObject : public UMemory {
185 public:
186     /**
187      * Destructor.
188      *
189      * @stable ICU 2.2
190      */
191     virtual ~UObject();
192 
193     /**
194      * ICU4C "poor man's RTTI", returns a UClassID for the actual ICU class.
195      *
196      * @stable ICU 2.2
197      */
198     virtual UClassID getDynamicClassID() const = 0;
199 
200 protected:
201     // the following functions are protected to prevent instantiation and
202     // direct use of UObject itself
203 
204     // default constructor
205     // commented out because UObject is abstract (see getDynamicClassID)
206     // inline UObject() {}
207 
208     // copy constructor
209     // commented out because UObject is abstract (see getDynamicClassID)
210     // inline UObject(const UObject &other) {}
211 
212 #if 0
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
217 
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; }
222 
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); }
226 
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;
232 #endif
233 
234     /*
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
238      * untested - ignore.
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 &);
243      */
244 
245 // Future implementation for RTTI that support subtyping. [alan]
246 //
247 //  public:
248 //     /**
249 //      * @internal
250 //      */
251 //     static UClassID getStaticClassID();
252 //
253 //     /**
254 //      * @internal
255 //      */
256 //     UBool instanceOf(UClassID type) const;
257 };
258 
259 /**
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.
262  *
263  * @param myClass The name of the class that needs RTTI defined.
264  * @internal
265  */
266 #define UOBJECT_DEFINE_RTTI_IMPLEMENTATION(myClass) \
267     UClassID U_EXPORT2 myClass::getStaticClassID() { \
268         static char classID = 0; \
269         return (UClassID)&classID; \
270     } \
271     UClassID myClass::getDynamicClassID() const \
272     { return myClass::getStaticClassID(); }
273 
274 
275 /**
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.
279  *
280  * @param myClass The name of the class that needs RTTI defined.
281  * @internal
282  */
283 #define UOBJECT_DEFINE_ABSTRACT_RTTI_IMPLEMENTATION(myClass) \
284     UClassID U_EXPORT2 myClass::getStaticClassID() { \
285         static char classID = 0; \
286         return (UClassID)&classID; \
287     }
288 
289 // /**
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.
293 //  *
294 //  * @param myClass The name of the class that needs RTTI defined.
295 //  * @param myParent The name of the myClass's parent.
296 //  * @internal
297 //  */
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(); \
302     }
303 */
304 
305 
306 U_NAMESPACE_END
307 
308 #endif
309