sl@0
|
1 |
/*
|
sl@0
|
2 |
******************************************************************************
|
sl@0
|
3 |
*
|
sl@0
|
4 |
* Copyright (C) 2002-2005, International Business Machines
|
sl@0
|
5 |
* Corporation and others. All Rights Reserved.
|
sl@0
|
6 |
*
|
sl@0
|
7 |
******************************************************************************
|
sl@0
|
8 |
* file name: uobject.h
|
sl@0
|
9 |
* encoding: US-ASCII
|
sl@0
|
10 |
* tab size: 8 (not used)
|
sl@0
|
11 |
* indentation:4
|
sl@0
|
12 |
*
|
sl@0
|
13 |
* created on: 2002jun26
|
sl@0
|
14 |
* created by: Markus W. Scherer
|
sl@0
|
15 |
*/
|
sl@0
|
16 |
|
sl@0
|
17 |
#ifndef __UOBJECT_H__
|
sl@0
|
18 |
#define __UOBJECT_H__
|
sl@0
|
19 |
|
sl@0
|
20 |
#include "unicode/utypes.h"
|
sl@0
|
21 |
|
sl@0
|
22 |
U_NAMESPACE_BEGIN
|
sl@0
|
23 |
|
sl@0
|
24 |
/**
|
sl@0
|
25 |
* \file
|
sl@0
|
26 |
* \brief C++ API: Common ICU base class UObject.
|
sl@0
|
27 |
*/
|
sl@0
|
28 |
|
sl@0
|
29 |
/** U_OVERRIDE_CXX_ALLOCATION - Define this to override operator new and
|
sl@0
|
30 |
* delete in UMemory. Enabled by default for ICU.
|
sl@0
|
31 |
*
|
sl@0
|
32 |
* Enabling forces all allocation of ICU object types to use ICU's
|
sl@0
|
33 |
* memory allocation. On Windows, this allows the ICU DLL to be used by
|
sl@0
|
34 |
* applications that statically link the C Runtime library, meaning that
|
sl@0
|
35 |
* the app and ICU will be using different heaps.
|
sl@0
|
36 |
*
|
sl@0
|
37 |
* @stable ICU 2.2
|
sl@0
|
38 |
*/
|
sl@0
|
39 |
#ifndef U_OVERRIDE_CXX_ALLOCATION
|
sl@0
|
40 |
#define U_OVERRIDE_CXX_ALLOCATION 1
|
sl@0
|
41 |
#endif
|
sl@0
|
42 |
|
sl@0
|
43 |
/** U_HAVE_PLACEMENT_NEW - Define this to define the placement new and
|
sl@0
|
44 |
* delete in UMemory for STL.
|
sl@0
|
45 |
*
|
sl@0
|
46 |
* @stable ICU 2.6
|
sl@0
|
47 |
*/
|
sl@0
|
48 |
#ifndef U_HAVE_PLACEMENT_NEW
|
sl@0
|
49 |
#define U_HAVE_PLACEMENT_NEW 1
|
sl@0
|
50 |
#endif
|
sl@0
|
51 |
|
sl@0
|
52 |
/** U_HAVE_DEBUG_LOCATION_NEW - Define this to define the MFC debug
|
sl@0
|
53 |
* version of the operator new.
|
sl@0
|
54 |
*
|
sl@0
|
55 |
* @draft ICU 3.4
|
sl@0
|
56 |
*/
|
sl@0
|
57 |
#ifndef U_HAVE_DEBUG_LOCATION_NEW
|
sl@0
|
58 |
#define U_HAVE_DEBUG_LOCATION_NEW 0
|
sl@0
|
59 |
#endif
|
sl@0
|
60 |
|
sl@0
|
61 |
|
sl@0
|
62 |
/**
|
sl@0
|
63 |
* UMemory is the common ICU base class.
|
sl@0
|
64 |
* All other ICU C++ classes are derived from UMemory (starting with ICU 2.4).
|
sl@0
|
65 |
*
|
sl@0
|
66 |
* This is primarily to make it possible and simple to override the
|
sl@0
|
67 |
* C++ memory management by adding new/delete operators to this base class.
|
sl@0
|
68 |
*
|
sl@0
|
69 |
* To override ALL ICU memory management, including that from plain C code,
|
sl@0
|
70 |
* replace the allocation functions declared in cmemory.h
|
sl@0
|
71 |
*
|
sl@0
|
72 |
* UMemory does not contain any virtual functions.
|
sl@0
|
73 |
* Common "boilerplate" functions are defined in UObject.
|
sl@0
|
74 |
*
|
sl@0
|
75 |
* @stable ICU 2.4
|
sl@0
|
76 |
*/
|
sl@0
|
77 |
class U_COMMON_API UMemory {
|
sl@0
|
78 |
public:
|
sl@0
|
79 |
|
sl@0
|
80 |
/* test versions for debugging shaper heap memory problems */
|
sl@0
|
81 |
#ifdef SHAPER_MEMORY_DEBUG
|
sl@0
|
82 |
static void * NewArray(int size, int count);
|
sl@0
|
83 |
static void * GrowArray(void * array, int newSize );
|
sl@0
|
84 |
static void FreeArray(void * array );
|
sl@0
|
85 |
#endif
|
sl@0
|
86 |
|
sl@0
|
87 |
#if U_OVERRIDE_CXX_ALLOCATION
|
sl@0
|
88 |
/**
|
sl@0
|
89 |
* Override for ICU4C C++ memory management.
|
sl@0
|
90 |
* simple, non-class types are allocated using the macros in common/cmemory.h
|
sl@0
|
91 |
* (uprv_malloc(), uprv_free(), uprv_realloc());
|
sl@0
|
92 |
* they or something else could be used here to implement C++ new/delete
|
sl@0
|
93 |
* for ICU4C C++ classes
|
sl@0
|
94 |
* @stable ICU 2.4
|
sl@0
|
95 |
*/
|
sl@0
|
96 |
static void * U_EXPORT2 operator new(size_t size);
|
sl@0
|
97 |
|
sl@0
|
98 |
/**
|
sl@0
|
99 |
* Override for ICU4C C++ memory management.
|
sl@0
|
100 |
* See new().
|
sl@0
|
101 |
* @stable ICU 2.4
|
sl@0
|
102 |
*/
|
sl@0
|
103 |
static void * U_EXPORT2 operator new[](size_t size);
|
sl@0
|
104 |
|
sl@0
|
105 |
/**
|
sl@0
|
106 |
* Override for ICU4C C++ memory management.
|
sl@0
|
107 |
* simple, non-class types are allocated using the macros in common/cmemory.h
|
sl@0
|
108 |
* (uprv_malloc(), uprv_free(), uprv_realloc());
|
sl@0
|
109 |
* they or something else could be used here to implement C++ new/delete
|
sl@0
|
110 |
* for ICU4C C++ classes
|
sl@0
|
111 |
* @stable ICU 2.4
|
sl@0
|
112 |
*/
|
sl@0
|
113 |
static void U_EXPORT2 operator delete(void *p);
|
sl@0
|
114 |
|
sl@0
|
115 |
/**
|
sl@0
|
116 |
* Override for ICU4C C++ memory management.
|
sl@0
|
117 |
* See delete().
|
sl@0
|
118 |
* @stable ICU 2.4
|
sl@0
|
119 |
*/
|
sl@0
|
120 |
static void U_EXPORT2 operator delete[](void *p);
|
sl@0
|
121 |
|
sl@0
|
122 |
#if U_HAVE_PLACEMENT_NEW
|
sl@0
|
123 |
/**
|
sl@0
|
124 |
* Override for ICU4C C++ memory management for STL.
|
sl@0
|
125 |
* See new().
|
sl@0
|
126 |
* @stable ICU 2.6
|
sl@0
|
127 |
*/
|
sl@0
|
128 |
static inline void * U_EXPORT2 operator new(size_t, void *ptr) { return ptr; }
|
sl@0
|
129 |
|
sl@0
|
130 |
/**
|
sl@0
|
131 |
* Override for ICU4C C++ memory management for STL.
|
sl@0
|
132 |
* See delete().
|
sl@0
|
133 |
* @stable ICU 2.6
|
sl@0
|
134 |
*/
|
sl@0
|
135 |
static inline void U_EXPORT2 operator delete(void *, void *) {}
|
sl@0
|
136 |
#endif /* U_HAVE_PLACEMENT_NEW */
|
sl@0
|
137 |
#if U_HAVE_DEBUG_LOCATION_NEW
|
sl@0
|
138 |
/**
|
sl@0
|
139 |
* This method overrides the MFC debug version of the operator new
|
sl@0
|
140 |
*
|
sl@0
|
141 |
* @param size The requested memory size
|
sl@0
|
142 |
* @param file The file where the allocation was requested
|
sl@0
|
143 |
* @param line The line where the allocation was requested
|
sl@0
|
144 |
*/
|
sl@0
|
145 |
static void * U_EXPORT2 operator new(size_t size, const char* file, int line);
|
sl@0
|
146 |
/**
|
sl@0
|
147 |
* This method provides a matching delete for the MFC debug new
|
sl@0
|
148 |
*
|
sl@0
|
149 |
* @param p The pointer to the allocated memory
|
sl@0
|
150 |
* @param file The file where the allocation was requested
|
sl@0
|
151 |
* @param line The line where the allocation was requested
|
sl@0
|
152 |
*/
|
sl@0
|
153 |
static void U_EXPORT2 operator delete(void* p, const char* file, int line);
|
sl@0
|
154 |
#endif /* U_HAVE_DEBUG_LOCATION_NEW */
|
sl@0
|
155 |
#endif /* U_OVERRIDE_CXX_ALLOCATION */
|
sl@0
|
156 |
|
sl@0
|
157 |
/*
|
sl@0
|
158 |
* Assignment operator not declared. The compiler will provide one
|
sl@0
|
159 |
* which does nothing since this class does not contain any data members.
|
sl@0
|
160 |
* API/code coverage may show the assignment operator as present and
|
sl@0
|
161 |
* untested - ignore.
|
sl@0
|
162 |
* Subclasses need this assignment operator if they use compiler-provided
|
sl@0
|
163 |
* assignment operators of their own. An alternative to not declaring one
|
sl@0
|
164 |
* here would be to declare and empty-implement a protected or public one.
|
sl@0
|
165 |
UMemory &UMemory::operator=(const UMemory &);
|
sl@0
|
166 |
*/
|
sl@0
|
167 |
};
|
sl@0
|
168 |
|
sl@0
|
169 |
/**
|
sl@0
|
170 |
* UObject is the common ICU "boilerplate" class.
|
sl@0
|
171 |
* UObject inherits UMemory (starting with ICU 2.4),
|
sl@0
|
172 |
* and all other public ICU C++ classes
|
sl@0
|
173 |
* are derived from UObject (starting with ICU 2.2).
|
sl@0
|
174 |
*
|
sl@0
|
175 |
* UObject contains common virtual functions like for ICU's "poor man's RTTI".
|
sl@0
|
176 |
* It does not contain default implementations of virtual methods
|
sl@0
|
177 |
* like getDynamicClassID to allow derived classes such as Format
|
sl@0
|
178 |
* to declare these as pure virtual.
|
sl@0
|
179 |
*
|
sl@0
|
180 |
* The clone() function is not available in UObject because it is not
|
sl@0
|
181 |
* implemented by all ICU classes.
|
sl@0
|
182 |
* Many ICU services provide a clone() function for their class trees,
|
sl@0
|
183 |
* defined on the service's C++ base class, and all subclasses within that
|
sl@0
|
184 |
* service class tree return a pointer to the service base class
|
sl@0
|
185 |
* (which itself is a subclass of UObject).
|
sl@0
|
186 |
* This is because some compilers do not support covariant (same-as-this)
|
sl@0
|
187 |
* return types; cast to the appropriate subclass if necessary.
|
sl@0
|
188 |
*
|
sl@0
|
189 |
* @stable ICU 2.2
|
sl@0
|
190 |
*/
|
sl@0
|
191 |
class U_COMMON_API UObject : public UMemory {
|
sl@0
|
192 |
public:
|
sl@0
|
193 |
/**
|
sl@0
|
194 |
* Destructor.
|
sl@0
|
195 |
*
|
sl@0
|
196 |
* @stable ICU 2.2
|
sl@0
|
197 |
*/
|
sl@0
|
198 |
virtual ~UObject();
|
sl@0
|
199 |
|
sl@0
|
200 |
/**
|
sl@0
|
201 |
* ICU4C "poor man's RTTI", returns a UClassID for the actual ICU class.
|
sl@0
|
202 |
*
|
sl@0
|
203 |
* @stable ICU 2.2
|
sl@0
|
204 |
*/
|
sl@0
|
205 |
virtual UClassID getDynamicClassID() const = 0;
|
sl@0
|
206 |
|
sl@0
|
207 |
protected:
|
sl@0
|
208 |
// the following functions are protected to prevent instantiation and
|
sl@0
|
209 |
// direct use of UObject itself
|
sl@0
|
210 |
|
sl@0
|
211 |
// default constructor
|
sl@0
|
212 |
// commented out because UObject is abstract (see getDynamicClassID)
|
sl@0
|
213 |
// inline UObject() {}
|
sl@0
|
214 |
|
sl@0
|
215 |
// copy constructor
|
sl@0
|
216 |
// commented out because UObject is abstract (see getDynamicClassID)
|
sl@0
|
217 |
// inline UObject(const UObject &other) {}
|
sl@0
|
218 |
|
sl@0
|
219 |
#if 0
|
sl@0
|
220 |
// TODO Sometime in the future. Implement operator==().
|
sl@0
|
221 |
// (This comment inserted in 2.2)
|
sl@0
|
222 |
// some or all of the following "boilerplate" functions may be made public
|
sl@0
|
223 |
// in a future ICU4C release when all subclasses implement them
|
sl@0
|
224 |
|
sl@0
|
225 |
// assignment operator
|
sl@0
|
226 |
// (not virtual, see "Taligent's Guide to Designing Programs" pp.73..74)
|
sl@0
|
227 |
// commented out because the implementation is the same as a compiler's default
|
sl@0
|
228 |
// UObject &operator=(const UObject &other) { return *this; }
|
sl@0
|
229 |
|
sl@0
|
230 |
// comparison operators
|
sl@0
|
231 |
virtual inline UBool operator==(const UObject &other) const { return this==&other; }
|
sl@0
|
232 |
inline UBool operator!=(const UObject &other) const { return !operator==(other); }
|
sl@0
|
233 |
|
sl@0
|
234 |
// clone() commented out from the base class:
|
sl@0
|
235 |
// some compilers do not support co-variant return types
|
sl@0
|
236 |
// (i.e., subclasses would have to return UObject * as well, instead of SubClass *)
|
sl@0
|
237 |
// see also UObject class documentation.
|
sl@0
|
238 |
// virtual UObject *clone() const;
|
sl@0
|
239 |
#endif
|
sl@0
|
240 |
|
sl@0
|
241 |
/*
|
sl@0
|
242 |
* Assignment operator not declared. The compiler will provide one
|
sl@0
|
243 |
* which does nothing since this class does not contain any data members.
|
sl@0
|
244 |
* API/code coverage may show the assignment operator as present and
|
sl@0
|
245 |
* untested - ignore.
|
sl@0
|
246 |
* Subclasses need this assignment operator if they use compiler-provided
|
sl@0
|
247 |
* assignment operators of their own. An alternative to not declaring one
|
sl@0
|
248 |
* here would be to declare and empty-implement a protected or public one.
|
sl@0
|
249 |
UObject &UObject::operator=(const UObject &);
|
sl@0
|
250 |
*/
|
sl@0
|
251 |
|
sl@0
|
252 |
// Future implementation for RTTI that support subtyping. [alan]
|
sl@0
|
253 |
//
|
sl@0
|
254 |
// public:
|
sl@0
|
255 |
// /**
|
sl@0
|
256 |
// * @internal
|
sl@0
|
257 |
// */
|
sl@0
|
258 |
// static UClassID getStaticClassID();
|
sl@0
|
259 |
//
|
sl@0
|
260 |
// /**
|
sl@0
|
261 |
// * @internal
|
sl@0
|
262 |
// */
|
sl@0
|
263 |
// UBool instanceOf(UClassID type) const;
|
sl@0
|
264 |
};
|
sl@0
|
265 |
|
sl@0
|
266 |
/**
|
sl@0
|
267 |
* This is a simple macro to add ICU RTTI to an ICU object implementation.
|
sl@0
|
268 |
* This does not go into the header. This should only be used in *.cpp files.
|
sl@0
|
269 |
*
|
sl@0
|
270 |
* @param myClass The name of the class that needs RTTI defined.
|
sl@0
|
271 |
* @internal
|
sl@0
|
272 |
*/
|
sl@0
|
273 |
#define UOBJECT_DEFINE_RTTI_IMPLEMENTATION(myClass) \
|
sl@0
|
274 |
UClassID U_EXPORT2 myClass::getStaticClassID() { \
|
sl@0
|
275 |
static const char classID = 0; \
|
sl@0
|
276 |
return (UClassID)&classID; \
|
sl@0
|
277 |
} \
|
sl@0
|
278 |
UClassID myClass::getDynamicClassID() const \
|
sl@0
|
279 |
{ return myClass::getStaticClassID(); }
|
sl@0
|
280 |
|
sl@0
|
281 |
|
sl@0
|
282 |
/**
|
sl@0
|
283 |
* This macro adds ICU RTTI to an ICU abstract class implementation.
|
sl@0
|
284 |
* This macro should be invoked in *.cpp files. The corresponding
|
sl@0
|
285 |
* header should declare getStaticClassID.
|
sl@0
|
286 |
*
|
sl@0
|
287 |
* @param myClass The name of the class that needs RTTI defined.
|
sl@0
|
288 |
* @internal
|
sl@0
|
289 |
*/
|
sl@0
|
290 |
#define UOBJECT_DEFINE_ABSTRACT_RTTI_IMPLEMENTATION(myClass) \
|
sl@0
|
291 |
UClassID U_EXPORT2 myClass::getStaticClassID() { \
|
sl@0
|
292 |
static const char classID = 0; \
|
sl@0
|
293 |
return (UClassID)&classID; \
|
sl@0
|
294 |
}
|
sl@0
|
295 |
|
sl@0
|
296 |
// /**
|
sl@0
|
297 |
// * This macro adds ICU RTTI to an ICU concrete class implementation.
|
sl@0
|
298 |
// * This macro should be invoked in *.cpp files. The corresponding
|
sl@0
|
299 |
// * header should declare getDynamicClassID and getStaticClassID.
|
sl@0
|
300 |
// *
|
sl@0
|
301 |
// * @param myClass The name of the class that needs RTTI defined.
|
sl@0
|
302 |
// * @param myParent The name of the myClass's parent.
|
sl@0
|
303 |
// * @internal
|
sl@0
|
304 |
// */
|
sl@0
|
305 |
/*#define UOBJECT_DEFINE_RTTI_IMPLEMENTATION(myClass, myParent) \
|
sl@0
|
306 |
UOBJECT_DEFINE_ABSTRACT_RTTI_IMPLEMENTATION(myClass, myParent) \
|
sl@0
|
307 |
UClassID myClass::getDynamicClassID() const { \
|
sl@0
|
308 |
return myClass::getStaticClassID(); \
|
sl@0
|
309 |
}
|
sl@0
|
310 |
*/
|
sl@0
|
311 |
|
sl@0
|
312 |
|
sl@0
|
313 |
U_NAMESPACE_END
|
sl@0
|
314 |
|
sl@0
|
315 |
#endif
|