intl/icu/source/common/unicode/uobject.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial