Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/unicode/uobject.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // © 2016 and later: Unicode, Inc. and others.
0002 // License & terms of use: http://www.unicode.org/copyright.html
0003 /*
0004 ******************************************************************************
0005 *
0006 *   Copyright (C) 2002-2012, International Business Machines
0007 *   Corporation and others.  All Rights Reserved.
0008 *
0009 ******************************************************************************
0010 *   file name:  uobject.h
0011 *   encoding:   UTF-8
0012 *   tab size:   8 (not used)
0013 *   indentation:4
0014 *
0015 *   created on: 2002jun26
0016 *   created by: Markus W. Scherer
0017 */
0018 
0019 #ifndef __UOBJECT_H__
0020 #define __UOBJECT_H__
0021 
0022 #include "unicode/utypes.h"
0023 
0024 #if U_SHOW_CPLUSPLUS_API
0025 
0026 #include "unicode/platform.h"
0027 
0028 /**
0029  * \file
0030  * \brief C++ API: Common ICU base class UObject.
0031  */
0032 
0033 /**
0034  * \def U_NO_THROW
0035  *         Since ICU 64, use noexcept instead.
0036  *
0037  *         Previously, define this to define the throw() specification so
0038  *                 certain functions do not throw any exceptions
0039  *
0040  *         UMemory operator new methods should have the throw() specification 
0041  *         appended to them, so that the compiler adds the additional nullptr check 
0042  *         before calling constructors. Without, if <code>operator new</code> returns nullptr the
0043  *         constructor is still called, and if the constructor references member 
0044  *         data, (which it typically does), the result is a segmentation violation.
0045  *
0046  * @stable ICU 4.2. Since ICU 64, Use noexcept instead. See ICU-20422.
0047  */
0048 #ifndef U_NO_THROW
0049 #define U_NO_THROW noexcept
0050 #endif
0051 
0052 /*===========================================================================*/
0053 /* UClassID-based RTTI */
0054 /*===========================================================================*/
0055 
0056 /**
0057  * UClassID is used to identify classes without using the compiler's RTTI.
0058  * This was used before C++ compilers consistently supported RTTI.
0059  * ICU 4.6 requires compiler RTTI to be turned on.
0060  *
0061  * Each class hierarchy which needs
0062  * to implement polymorphic clone() or operator==() defines two methods,
0063  * described in detail below.  UClassID values can be compared using
0064  * operator==(). Nothing else should be done with them.
0065  *
0066  * \par
0067  * In class hierarchies that implement "poor man's RTTI",
0068  * each concrete subclass implements getDynamicClassID() in the same way:
0069  *
0070  * \code
0071  *      class Derived {
0072  *      public:
0073  *          virtual UClassID getDynamicClassID() const
0074  *            { return Derived::getStaticClassID(); }
0075  *      }
0076  * \endcode
0077  *
0078  * Each concrete class implements getStaticClassID() as well, which allows
0079  * clients to test for a specific type.
0080  *
0081  * \code
0082  *      class Derived {
0083  *      public:
0084  *          static UClassID U_EXPORT2 getStaticClassID();
0085  *      private:
0086  *          static char fgClassID;
0087  *      }
0088  *
0089  *      // In Derived.cpp:
0090  *      UClassID Derived::getStaticClassID()
0091  *        { return (UClassID)&Derived::fgClassID; }
0092  *      char Derived::fgClassID = 0; // Value is irrelevant
0093  * \endcode
0094  * @stable ICU 2.0
0095  */
0096 typedef void* UClassID;
0097 
0098 U_NAMESPACE_BEGIN
0099 
0100 /**
0101  * UMemory is the common ICU base class.
0102  * All other ICU C++ classes are derived from UMemory (starting with ICU 2.4).
0103  *
0104  * This is primarily to make it possible and simple to override the
0105  * C++ memory management by adding new/delete operators to this base class.
0106  *
0107  * To override ALL ICU memory management, including that from plain C code,
0108  * replace the allocation functions declared in cmemory.h
0109  *
0110  * UMemory does not contain any virtual functions.
0111  * Common "boilerplate" functions are defined in UObject.
0112  *
0113  * @stable ICU 2.4
0114  */
0115 class U_COMMON_API UMemory {
0116 public:
0117 
0118 /* test versions for debugging shaper heap memory problems */
0119 #ifdef SHAPER_MEMORY_DEBUG  
0120     static void * NewArray(int size, int count);
0121     static void * GrowArray(void * array, int newSize );
0122     static void   FreeArray(void * array );
0123 #endif
0124 
0125 #if U_OVERRIDE_CXX_ALLOCATION
0126     /**
0127      * Override for ICU4C C++ memory management.
0128      * simple, non-class types are allocated using the macros in common/cmemory.h
0129      * (uprv_malloc(), uprv_free(), uprv_realloc());
0130      * they or something else could be used here to implement C++ new/delete
0131      * for ICU4C C++ classes
0132      * @stable ICU 2.4
0133      */
0134     static void * U_EXPORT2 operator new(size_t size) noexcept;
0135 
0136     /**
0137      * Override for ICU4C C++ memory management.
0138      * See new().
0139      * @stable ICU 2.4
0140      */
0141     static void * U_EXPORT2 operator new[](size_t size) noexcept;
0142 
0143     /**
0144      * Override for ICU4C C++ memory management.
0145      * simple, non-class types are allocated using the macros in common/cmemory.h
0146      * (uprv_malloc(), uprv_free(), uprv_realloc());
0147      * they or something else could be used here to implement C++ new/delete
0148      * for ICU4C C++ classes
0149      * @stable ICU 2.4
0150      */
0151     static void U_EXPORT2 operator delete(void *p) noexcept;
0152 
0153     /**
0154      * Override for ICU4C C++ memory management.
0155      * See delete().
0156      * @stable ICU 2.4
0157      */
0158     static void U_EXPORT2 operator delete[](void *p) noexcept;
0159 
0160 #if U_HAVE_PLACEMENT_NEW
0161     /**
0162      * Override for ICU4C C++ memory management for STL.
0163      * See new().
0164      * @stable ICU 2.6
0165      */
0166     static inline void * U_EXPORT2 operator new(size_t, void *ptr) noexcept { return ptr; }
0167 
0168     /**
0169      * Override for ICU4C C++ memory management for STL.
0170      * See delete().
0171      * @stable ICU 2.6
0172      */
0173     static inline void U_EXPORT2 operator delete(void *, void *) noexcept {}
0174 #endif /* U_HAVE_PLACEMENT_NEW */
0175 #if U_HAVE_DEBUG_LOCATION_NEW
0176     /**
0177       * This method overrides the MFC debug version of the operator new
0178       * 
0179       * @param size   The requested memory size
0180       * @param file   The file where the allocation was requested
0181       * @param line   The line where the allocation was requested 
0182       */ 
0183     static void * U_EXPORT2 operator new(size_t size, const char* file, int line) noexcept;
0184     /**
0185       * This method provides a matching delete for the MFC debug new
0186       * 
0187       * @param p      The pointer to the allocated memory
0188       * @param file   The file where the allocation was requested
0189       * @param line   The line where the allocation was requested 
0190       */ 
0191     static void U_EXPORT2 operator delete(void* p, const char* file, int line) noexcept;
0192 #endif /* U_HAVE_DEBUG_LOCATION_NEW */
0193 #endif /* U_OVERRIDE_CXX_ALLOCATION */
0194 
0195     /*
0196      * Assignment operator not declared. The compiler will provide one
0197      * which does nothing since this class does not contain any data members.
0198      * API/code coverage may show the assignment operator as present and
0199      * untested - ignore.
0200      * Subclasses need this assignment operator if they use compiler-provided
0201      * assignment operators of their own. An alternative to not declaring one
0202      * here would be to declare and empty-implement a protected or public one.
0203     UMemory &UMemory::operator=(const UMemory &);
0204      */
0205 };
0206 
0207 /**
0208  * UObject is the common ICU "boilerplate" class.
0209  * UObject inherits UMemory (starting with ICU 2.4),
0210  * and all other public ICU C++ classes
0211  * are derived from UObject (starting with ICU 2.2).
0212  *
0213  * UObject contains common virtual functions, in particular a virtual destructor.
0214  *
0215  * The clone() function is not available in UObject because it is not
0216  * implemented by all ICU classes.
0217  * Many ICU services provide a clone() function for their class trees,
0218  * defined on the service's C++ base class
0219  * (which itself is a subclass of UObject).
0220  *
0221  * @stable ICU 2.2
0222  */
0223 class U_COMMON_API UObject : public UMemory {
0224 public:
0225     /**
0226      * Destructor.
0227      *
0228      * @stable ICU 2.2
0229      */
0230     virtual ~UObject();
0231 
0232     /**
0233      * ICU4C "poor man's RTTI", returns a UClassID for the actual ICU class.
0234      * The base class implementation returns a dummy value.
0235      *
0236      * Use compiler RTTI rather than ICU's "poor man's RTTI".
0237      * Since ICU 4.6, new ICU C++ class hierarchies do not implement "poor man's RTTI".
0238      *
0239      * @stable ICU 2.2
0240      */
0241     virtual UClassID getDynamicClassID() const;
0242 
0243 protected:
0244     // the following functions are protected to prevent instantiation and
0245     // direct use of UObject itself
0246 
0247     // default constructor
0248     // inline UObject() {}
0249 
0250     // copy constructor
0251     // inline UObject(const UObject &other) {}
0252 
0253 #if 0
0254     // TODO Sometime in the future. Implement operator==().
0255     // (This comment inserted in 2.2)
0256     // some or all of the following "boilerplate" functions may be made public
0257     // in a future ICU4C release when all subclasses implement them
0258 
0259     // assignment operator
0260     // (not virtual, see "Taligent's Guide to Designing Programs" pp.73..74)
0261     // commented out because the implementation is the same as a compiler's default
0262     // UObject &operator=(const UObject &other) { return *this; }
0263 
0264     // comparison operators
0265     virtual inline bool operator==(const UObject &other) const { return this==&other; }
0266     inline bool operator!=(const UObject &other) const { return !operator==(other); }
0267 
0268     // clone() commented out from the base class:
0269     // some compilers do not support co-variant return types
0270     // (i.e., subclasses would have to return UObject * as well, instead of SubClass *)
0271     // see also UObject class documentation.
0272     // virtual UObject *clone() const;
0273 #endif
0274 
0275     /*
0276      * Assignment operator not declared. The compiler will provide one
0277      * which does nothing since this class does not contain any data members.
0278      * API/code coverage may show the assignment operator as present and
0279      * untested - ignore.
0280      * Subclasses need this assignment operator if they use compiler-provided
0281      * assignment operators of their own. An alternative to not declaring one
0282      * here would be to declare and empty-implement a protected or public one.
0283     UObject &UObject::operator=(const UObject &);
0284      */
0285 };
0286 
0287 #ifndef U_HIDE_INTERNAL_API
0288 /**
0289  * This is a simple macro to add ICU RTTI to an ICU object implementation.
0290  * This does not go into the header. This should only be used in *.cpp files.
0291  *
0292  * @param myClass The name of the class that needs RTTI defined.
0293  * @internal
0294  */
0295 #define UOBJECT_DEFINE_RTTI_IMPLEMENTATION(myClass) \
0296     UClassID U_EXPORT2 myClass::getStaticClassID() { \
0297         static char classID = 0; \
0298         return (UClassID)&classID; \
0299     } \
0300     UClassID myClass::getDynamicClassID() const \
0301     { return myClass::getStaticClassID(); }
0302 
0303 
0304 /**
0305  * This macro adds ICU RTTI to an ICU abstract class implementation.
0306  * This macro should be invoked in *.cpp files.  The corresponding
0307  * header should declare getStaticClassID.
0308  *
0309  * @param myClass The name of the class that needs RTTI defined.
0310  * @internal
0311  */
0312 #define UOBJECT_DEFINE_ABSTRACT_RTTI_IMPLEMENTATION(myClass) \
0313     UClassID U_EXPORT2 myClass::getStaticClassID() { \
0314         static char classID = 0; \
0315         return (UClassID)&classID; \
0316     }
0317 
0318 #endif  /* U_HIDE_INTERNAL_API */
0319 
0320 U_NAMESPACE_END
0321 
0322 #endif /* U_SHOW_CPLUSPLUS_API */
0323 
0324 #endif