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