1/* 2****************************************************************************** 3* 4* Copyright (C) 2002-2010, 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 22U_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/** 44 * \def U_HAVE_PLACEMENT_NEW 45 * Define this to define the placement new and 46 * delete in UMemory for STL. 47 * 48 * @stable ICU 2.6 49 */ 50#ifndef U_HAVE_PLACEMENT_NEW 51#define U_HAVE_PLACEMENT_NEW 1 52#endif 53 54 55/** 56 * \def U_HAVE_DEBUG_LOCATION_NEW 57 * Define this to define the MFC debug 58 * version of the operator new. 59 * 60 * @stable ICU 3.4 61 */ 62#ifndef U_HAVE_DEBUG_LOCATION_NEW 63#define U_HAVE_DEBUG_LOCATION_NEW 0 64#endif 65 66/** 67 * @{ 68 * \def U_NO_THROW 69 * Define this to define the throw() specification so 70 * certain functions do not throw any exceptions 71 * 72 * UMemory operator new methods should have the throw() specification 73 * appended to them, so that the compiler adds the additional NULL check 74 * before calling constructors. Without, if <code>operator new</code> returns NULL the 75 * constructor is still called, and if the constructor references member 76 * data, (which it typically does), the result is a segmentation violation. 77 * 78 * @draft ICU 4.2 79 */ 80#ifndef U_NO_THROW 81#define U_NO_THROW throw() 82#endif 83 84/** @} */ 85 86/** 87 * UMemory is the common ICU base class. 88 * All other ICU C++ classes are derived from UMemory (starting with ICU 2.4). 89 * 90 * This is primarily to make it possible and simple to override the 91 * C++ memory management by adding new/delete operators to this base class. 92 * 93 * To override ALL ICU memory management, including that from plain C code, 94 * replace the allocation functions declared in cmemory.h 95 * 96 * UMemory does not contain any virtual functions. 97 * Common "boilerplate" functions are defined in UObject. 98 * 99 * @stable ICU 2.4 100 */ 101class U_COMMON_API UMemory { 102public: 103 104/* test versions for debugging shaper heap memory problems */ 105#ifdef SHAPER_MEMORY_DEBUG 106 static void * NewArray(int size, int count); 107 static void * GrowArray(void * array, int newSize ); 108 static void FreeArray(void * array ); 109#endif 110 111#if U_OVERRIDE_CXX_ALLOCATION 112 /** 113 * Override for ICU4C C++ memory management. 114 * simple, non-class types are allocated using the macros in common/cmemory.h 115 * (uprv_malloc(), uprv_free(), uprv_realloc()); 116 * they or something else could be used here to implement C++ new/delete 117 * for ICU4C C++ classes 118 * @stable ICU 2.4 119 */ 120 static void * U_EXPORT2 operator new(size_t size) U_NO_THROW; 121 122 /** 123 * Override for ICU4C C++ memory management. 124 * See new(). 125 * @stable ICU 2.4 126 */ 127 static void * U_EXPORT2 operator new[](size_t size) U_NO_THROW; 128 129 /** 130 * Override for ICU4C C++ memory management. 131 * simple, non-class types are allocated using the macros in common/cmemory.h 132 * (uprv_malloc(), uprv_free(), uprv_realloc()); 133 * they or something else could be used here to implement C++ new/delete 134 * for ICU4C C++ classes 135 * @stable ICU 2.4 136 */ 137 static void U_EXPORT2 operator delete(void *p) U_NO_THROW; 138 139 /** 140 * Override for ICU4C C++ memory management. 141 * See delete(). 142 * @stable ICU 2.4 143 */ 144 static void U_EXPORT2 operator delete[](void *p) U_NO_THROW; 145 146#if U_HAVE_PLACEMENT_NEW 147 /** 148 * Override for ICU4C C++ memory management for STL. 149 * See new(). 150 * @stable ICU 2.6 151 */ 152 static inline void * U_EXPORT2 operator new(size_t, void *ptr) U_NO_THROW { return ptr; } 153 154 /** 155 * Override for ICU4C C++ memory management for STL. 156 * See delete(). 157 * @stable ICU 2.6 158 */ 159 static inline void U_EXPORT2 operator delete(void *, void *) U_NO_THROW {} 160#endif /* U_HAVE_PLACEMENT_NEW */ 161#if U_HAVE_DEBUG_LOCATION_NEW 162 /** 163 * This method overrides the MFC debug version of the operator new 164 * 165 * @param size The requested memory size 166 * @param file The file where the allocation was requested 167 * @param line The line where the allocation was requested 168 */ 169 static void * U_EXPORT2 operator new(size_t size, const char* file, int line) U_NO_THROW; 170 /** 171 * This method provides a matching delete for the MFC debug new 172 * 173 * @param p The pointer to the allocated memory 174 * @param file The file where the allocation was requested 175 * @param line The line where the allocation was requested 176 */ 177 static void U_EXPORT2 operator delete(void* p, const char* file, int line) U_NO_THROW; 178#endif /* U_HAVE_DEBUG_LOCATION_NEW */ 179#endif /* U_OVERRIDE_CXX_ALLOCATION */ 180 181 /* 182 * Assignment operator not declared. The compiler will provide one 183 * which does nothing since this class does not contain any data members. 184 * API/code coverage may show the assignment operator as present and 185 * untested - ignore. 186 * Subclasses need this assignment operator if they use compiler-provided 187 * assignment operators of their own. An alternative to not declaring one 188 * here would be to declare and empty-implement a protected or public one. 189 UMemory &UMemory::operator=(const UMemory &); 190 */ 191}; 192 193/** 194 * UObject is the common ICU "boilerplate" class. 195 * UObject inherits UMemory (starting with ICU 2.4), 196 * and all other public ICU C++ classes 197 * are derived from UObject (starting with ICU 2.2). 198 * 199 * UObject contains common virtual functions like for ICU's "poor man's RTTI". 200 * It does not contain default implementations of virtual methods 201 * like getDynamicClassID to allow derived classes such as Format 202 * to declare these as pure virtual. 203 * 204 * The clone() function is not available in UObject because it is not 205 * implemented by all ICU classes. 206 * Many ICU services provide a clone() function for their class trees, 207 * defined on the service's C++ base class, and all subclasses within that 208 * service class tree return a pointer to the service base class 209 * (which itself is a subclass of UObject). 210 * This is because some compilers do not support covariant (same-as-this) 211 * return types; cast to the appropriate subclass if necessary. 212 * 213 * @stable ICU 2.2 214 */ 215class U_COMMON_API UObject : public UMemory { 216public: 217 /** 218 * Destructor. 219 * 220 * @stable ICU 2.2 221 */ 222 virtual ~UObject(); 223 224 /** 225 * ICU4C "poor man's RTTI", returns a UClassID for the actual ICU class. 226 * 227 * @stable ICU 2.2 228 */ 229 virtual UClassID getDynamicClassID() const = 0; 230 231protected: 232 // the following functions are protected to prevent instantiation and 233 // direct use of UObject itself 234 235 // default constructor 236 // commented out because UObject is abstract (see getDynamicClassID) 237 // inline UObject() {} 238 239 // copy constructor 240 // commented out because UObject is abstract (see getDynamicClassID) 241 // inline UObject(const UObject &other) {} 242 243#if 0 244 // TODO Sometime in the future. Implement operator==(). 245 // (This comment inserted in 2.2) 246 // some or all of the following "boilerplate" functions may be made public 247 // in a future ICU4C release when all subclasses implement them 248 249 // assignment operator 250 // (not virtual, see "Taligent's Guide to Designing Programs" pp.73..74) 251 // commented out because the implementation is the same as a compiler's default 252 // UObject &operator=(const UObject &other) { return *this; } 253 254 // comparison operators 255 virtual inline UBool operator==(const UObject &other) const { return this==&other; } 256 inline UBool operator!=(const UObject &other) const { return !operator==(other); } 257 258 // clone() commented out from the base class: 259 // some compilers do not support co-variant return types 260 // (i.e., subclasses would have to return UObject * as well, instead of SubClass *) 261 // see also UObject class documentation. 262 // virtual UObject *clone() const; 263#endif 264 265 /* 266 * Assignment operator not declared. The compiler will provide one 267 * which does nothing since this class does not contain any data members. 268 * API/code coverage may show the assignment operator as present and 269 * untested - ignore. 270 * Subclasses need this assignment operator if they use compiler-provided 271 * assignment operators of their own. An alternative to not declaring one 272 * here would be to declare and empty-implement a protected or public one. 273 UObject &UObject::operator=(const UObject &); 274 */ 275 276// Future implementation for RTTI that support subtyping. [alan] 277// 278// public: 279// /** 280// * @internal 281// */ 282// static UClassID getStaticClassID(); 283// 284// /** 285// * @internal 286// */ 287// UBool instanceOf(UClassID type) const; 288}; 289 290/** 291 * This is a simple macro to add ICU RTTI to an ICU object implementation. 292 * This does not go into the header. This should only be used in *.cpp files. 293 * 294 * @param myClass The name of the class that needs RTTI defined. 295 * @internal 296 */ 297#define UOBJECT_DEFINE_RTTI_IMPLEMENTATION(myClass) \ 298 UClassID U_EXPORT2 myClass::getStaticClassID() { \ 299 static char classID = 0; \ 300 return (UClassID)&classID; \ 301 } \ 302 UClassID myClass::getDynamicClassID() const \ 303 { return myClass::getStaticClassID(); } 304 305 306/** 307 * This macro adds ICU RTTI to an ICU abstract class implementation. 308 * This macro should be invoked in *.cpp files. The corresponding 309 * header should declare getStaticClassID. 310 * 311 * @param myClass The name of the class that needs RTTI defined. 312 * @internal 313 */ 314#define UOBJECT_DEFINE_ABSTRACT_RTTI_IMPLEMENTATION(myClass) \ 315 UClassID U_EXPORT2 myClass::getStaticClassID() { \ 316 static char classID = 0; \ 317 return (UClassID)&classID; \ 318 } 319 320/** 321 * This is a simple macro to express that a class and its subclasses do not offer 322 * ICU's "poor man's RTTI". 323 * Beginning with ICU 4.6, ICU requires C++ compiler RTTI. 324 * This does not go into the header. This should only be used in *.cpp files. 325 * Use this with a private getDynamicClassID() in an immediate subclass of UObject. 326 * 327 * @param myClass The name of the class that needs RTTI defined. 328 * @internal 329 */ 330#define UOBJECT_DEFINE_NO_RTTI_IMPLEMENTATION(myClass) \ 331 UClassID myClass::getDynamicClassID() const { return NULL; } 332 333// /** 334// * This macro adds ICU RTTI to an ICU concrete class implementation. 335// * This macro should be invoked in *.cpp files. The corresponding 336// * header should declare getDynamicClassID and getStaticClassID. 337// * 338// * @param myClass The name of the class that needs RTTI defined. 339// * @param myParent The name of the myClass's parent. 340// * @internal 341// */ 342/*#define UOBJECT_DEFINE_RTTI_IMPLEMENTATION(myClass, myParent) \ 343 UOBJECT_DEFINE_ABSTRACT_RTTI_IMPLEMENTATION(myClass, myParent) \ 344 UClassID myClass::getDynamicClassID() const { \ 345 return myClass::getStaticClassID(); \ 346 } 347*/ 348 349 350U_NAMESPACE_END 351 352#endif 353