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