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