michael@0: /* michael@0: ****************************************************************************** michael@0: * michael@0: * Copyright (C) 2002-2012, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: * michael@0: ****************************************************************************** michael@0: * file name: uobject.h michael@0: * encoding: US-ASCII michael@0: * tab size: 8 (not used) michael@0: * indentation:4 michael@0: * michael@0: * created on: 2002jun26 michael@0: * created by: Markus W. Scherer michael@0: */ michael@0: michael@0: #ifndef __UOBJECT_H__ michael@0: #define __UOBJECT_H__ michael@0: michael@0: #include "unicode/utypes.h" michael@0: michael@0: /** michael@0: * \file michael@0: * \brief C++ API: Common ICU base class UObject. michael@0: */ michael@0: michael@0: /** michael@0: * @{ michael@0: * \def U_NO_THROW michael@0: * Define this to define the throw() specification so michael@0: * certain functions do not throw any exceptions michael@0: * michael@0: * UMemory operator new methods should have the throw() specification michael@0: * appended to them, so that the compiler adds the additional NULL check michael@0: * before calling constructors. Without, if operator new returns NULL the michael@0: * constructor is still called, and if the constructor references member michael@0: * data, (which it typically does), the result is a segmentation violation. michael@0: * michael@0: * @stable ICU 4.2 michael@0: */ michael@0: #ifndef U_NO_THROW michael@0: #define U_NO_THROW throw() michael@0: #endif michael@0: michael@0: /** @} */ michael@0: michael@0: /*===========================================================================*/ michael@0: /* UClassID-based RTTI */ michael@0: /*===========================================================================*/ michael@0: michael@0: /** michael@0: * UClassID is used to identify classes without using the compiler's RTTI. michael@0: * This was used before C++ compilers consistently supported RTTI. michael@0: * ICU 4.6 requires compiler RTTI to be turned on. michael@0: * michael@0: * Each class hierarchy which needs michael@0: * to implement polymorphic clone() or operator==() defines two methods, michael@0: * described in detail below. UClassID values can be compared using michael@0: * operator==(). Nothing else should be done with them. michael@0: * michael@0: * \par michael@0: * In class hierarchies that implement "poor man's RTTI", michael@0: * each concrete subclass implements getDynamicClassID() in the same way: michael@0: * michael@0: * \code michael@0: * class Derived { michael@0: * public: michael@0: * virtual UClassID getDynamicClassID() const michael@0: * { return Derived::getStaticClassID(); } michael@0: * } michael@0: * \endcode michael@0: * michael@0: * Each concrete class implements getStaticClassID() as well, which allows michael@0: * clients to test for a specific type. michael@0: * michael@0: * \code michael@0: * class Derived { michael@0: * public: michael@0: * static UClassID U_EXPORT2 getStaticClassID(); michael@0: * private: michael@0: * static char fgClassID; michael@0: * } michael@0: * michael@0: * // In Derived.cpp: michael@0: * UClassID Derived::getStaticClassID() michael@0: * { return (UClassID)&Derived::fgClassID; } michael@0: * char Derived::fgClassID = 0; // Value is irrelevant michael@0: * \endcode michael@0: * @stable ICU 2.0 michael@0: */ michael@0: typedef void* UClassID; michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: michael@0: /** michael@0: * UMemory is the common ICU base class. michael@0: * All other ICU C++ classes are derived from UMemory (starting with ICU 2.4). michael@0: * michael@0: * This is primarily to make it possible and simple to override the michael@0: * C++ memory management by adding new/delete operators to this base class. michael@0: * michael@0: * To override ALL ICU memory management, including that from plain C code, michael@0: * replace the allocation functions declared in cmemory.h michael@0: * michael@0: * UMemory does not contain any virtual functions. michael@0: * Common "boilerplate" functions are defined in UObject. michael@0: * michael@0: * @stable ICU 2.4 michael@0: */ michael@0: class U_COMMON_API UMemory { michael@0: public: michael@0: michael@0: /* test versions for debugging shaper heap memory problems */ michael@0: #ifdef SHAPER_MEMORY_DEBUG michael@0: static void * NewArray(int size, int count); michael@0: static void * GrowArray(void * array, int newSize ); michael@0: static void FreeArray(void * array ); michael@0: #endif michael@0: michael@0: #if U_OVERRIDE_CXX_ALLOCATION michael@0: /** michael@0: * Override for ICU4C C++ memory management. michael@0: * simple, non-class types are allocated using the macros in common/cmemory.h michael@0: * (uprv_malloc(), uprv_free(), uprv_realloc()); michael@0: * they or something else could be used here to implement C++ new/delete michael@0: * for ICU4C C++ classes michael@0: * @stable ICU 2.4 michael@0: */ michael@0: static void * U_EXPORT2 operator new(size_t size) U_NO_THROW; michael@0: michael@0: /** michael@0: * Override for ICU4C C++ memory management. michael@0: * See new(). michael@0: * @stable ICU 2.4 michael@0: */ michael@0: static void * U_EXPORT2 operator new[](size_t size) U_NO_THROW; michael@0: michael@0: /** michael@0: * Override for ICU4C C++ memory management. michael@0: * simple, non-class types are allocated using the macros in common/cmemory.h michael@0: * (uprv_malloc(), uprv_free(), uprv_realloc()); michael@0: * they or something else could be used here to implement C++ new/delete michael@0: * for ICU4C C++ classes michael@0: * @stable ICU 2.4 michael@0: */ michael@0: static void U_EXPORT2 operator delete(void *p) U_NO_THROW; michael@0: michael@0: /** michael@0: * Override for ICU4C C++ memory management. michael@0: * See delete(). michael@0: * @stable ICU 2.4 michael@0: */ michael@0: static void U_EXPORT2 operator delete[](void *p) U_NO_THROW; michael@0: michael@0: #if U_HAVE_PLACEMENT_NEW michael@0: /** michael@0: * Override for ICU4C C++ memory management for STL. michael@0: * See new(). michael@0: * @stable ICU 2.6 michael@0: */ michael@0: static inline void * U_EXPORT2 operator new(size_t, void *ptr) U_NO_THROW { return ptr; } michael@0: michael@0: /** michael@0: * Override for ICU4C C++ memory management for STL. michael@0: * See delete(). michael@0: * @stable ICU 2.6 michael@0: */ michael@0: static inline void U_EXPORT2 operator delete(void *, void *) U_NO_THROW {} michael@0: #endif /* U_HAVE_PLACEMENT_NEW */ michael@0: #if U_HAVE_DEBUG_LOCATION_NEW michael@0: /** michael@0: * This method overrides the MFC debug version of the operator new michael@0: * michael@0: * @param size The requested memory size michael@0: * @param file The file where the allocation was requested michael@0: * @param line The line where the allocation was requested michael@0: */ michael@0: static void * U_EXPORT2 operator new(size_t size, const char* file, int line) U_NO_THROW; michael@0: /** michael@0: * This method provides a matching delete for the MFC debug new michael@0: * michael@0: * @param p The pointer to the allocated memory michael@0: * @param file The file where the allocation was requested michael@0: * @param line The line where the allocation was requested michael@0: */ michael@0: static void U_EXPORT2 operator delete(void* p, const char* file, int line) U_NO_THROW; michael@0: #endif /* U_HAVE_DEBUG_LOCATION_NEW */ michael@0: #endif /* U_OVERRIDE_CXX_ALLOCATION */ michael@0: michael@0: /* michael@0: * Assignment operator not declared. The compiler will provide one michael@0: * which does nothing since this class does not contain any data members. michael@0: * API/code coverage may show the assignment operator as present and michael@0: * untested - ignore. michael@0: * Subclasses need this assignment operator if they use compiler-provided michael@0: * assignment operators of their own. An alternative to not declaring one michael@0: * here would be to declare and empty-implement a protected or public one. michael@0: UMemory &UMemory::operator=(const UMemory &); michael@0: */ michael@0: }; michael@0: michael@0: /** michael@0: * UObject is the common ICU "boilerplate" class. michael@0: * UObject inherits UMemory (starting with ICU 2.4), michael@0: * and all other public ICU C++ classes michael@0: * are derived from UObject (starting with ICU 2.2). michael@0: * michael@0: * UObject contains common virtual functions, in particular a virtual destructor. michael@0: * michael@0: * The clone() function is not available in UObject because it is not michael@0: * implemented by all ICU classes. michael@0: * Many ICU services provide a clone() function for their class trees, michael@0: * defined on the service's C++ base class, and all subclasses within that michael@0: * service class tree return a pointer to the service base class michael@0: * (which itself is a subclass of UObject). michael@0: * This is because some compilers do not support covariant (same-as-this) michael@0: * return types; cast to the appropriate subclass if necessary. michael@0: * michael@0: * @stable ICU 2.2 michael@0: */ michael@0: class U_COMMON_API UObject : public UMemory { michael@0: public: michael@0: /** michael@0: * Destructor. michael@0: * michael@0: * @stable ICU 2.2 michael@0: */ michael@0: virtual ~UObject(); michael@0: michael@0: /** michael@0: * ICU4C "poor man's RTTI", returns a UClassID for the actual ICU class. michael@0: * The base class implementation returns a dummy value. michael@0: * michael@0: * Use compiler RTTI rather than ICU's "poor man's RTTI". michael@0: * Since ICU 4.6, new ICU C++ class hierarchies do not implement "poor man's RTTI". michael@0: * michael@0: * @stable ICU 2.2 michael@0: */ michael@0: virtual UClassID getDynamicClassID() const; michael@0: michael@0: protected: michael@0: // the following functions are protected to prevent instantiation and michael@0: // direct use of UObject itself michael@0: michael@0: // default constructor michael@0: // inline UObject() {} michael@0: michael@0: // copy constructor michael@0: // inline UObject(const UObject &other) {} michael@0: michael@0: #if 0 michael@0: // TODO Sometime in the future. Implement operator==(). michael@0: // (This comment inserted in 2.2) michael@0: // some or all of the following "boilerplate" functions may be made public michael@0: // in a future ICU4C release when all subclasses implement them michael@0: michael@0: // assignment operator michael@0: // (not virtual, see "Taligent's Guide to Designing Programs" pp.73..74) michael@0: // commented out because the implementation is the same as a compiler's default michael@0: // UObject &operator=(const UObject &other) { return *this; } michael@0: michael@0: // comparison operators michael@0: virtual inline UBool operator==(const UObject &other) const { return this==&other; } michael@0: inline UBool operator!=(const UObject &other) const { return !operator==(other); } michael@0: michael@0: // clone() commented out from the base class: michael@0: // some compilers do not support co-variant return types michael@0: // (i.e., subclasses would have to return UObject * as well, instead of SubClass *) michael@0: // see also UObject class documentation. michael@0: // virtual UObject *clone() const; michael@0: #endif michael@0: michael@0: /* michael@0: * Assignment operator not declared. The compiler will provide one michael@0: * which does nothing since this class does not contain any data members. michael@0: * API/code coverage may show the assignment operator as present and michael@0: * untested - ignore. michael@0: * Subclasses need this assignment operator if they use compiler-provided michael@0: * assignment operators of their own. An alternative to not declaring one michael@0: * here would be to declare and empty-implement a protected or public one. michael@0: UObject &UObject::operator=(const UObject &); michael@0: */ michael@0: }; michael@0: michael@0: #ifndef U_HIDE_INTERNAL_API michael@0: /** michael@0: * This is a simple macro to add ICU RTTI to an ICU object implementation. michael@0: * This does not go into the header. This should only be used in *.cpp files. michael@0: * michael@0: * @param myClass The name of the class that needs RTTI defined. michael@0: * @internal michael@0: */ michael@0: #define UOBJECT_DEFINE_RTTI_IMPLEMENTATION(myClass) \ michael@0: UClassID U_EXPORT2 myClass::getStaticClassID() { \ michael@0: static char classID = 0; \ michael@0: return (UClassID)&classID; \ michael@0: } \ michael@0: UClassID myClass::getDynamicClassID() const \ michael@0: { return myClass::getStaticClassID(); } michael@0: michael@0: michael@0: /** michael@0: * This macro adds ICU RTTI to an ICU abstract class implementation. michael@0: * This macro should be invoked in *.cpp files. The corresponding michael@0: * header should declare getStaticClassID. michael@0: * michael@0: * @param myClass The name of the class that needs RTTI defined. michael@0: * @internal michael@0: */ michael@0: #define UOBJECT_DEFINE_ABSTRACT_RTTI_IMPLEMENTATION(myClass) \ michael@0: UClassID U_EXPORT2 myClass::getStaticClassID() { \ michael@0: static char classID = 0; \ michael@0: return (UClassID)&classID; \ michael@0: } michael@0: michael@0: #endif /* U_HIDE_INTERNAL_API */ michael@0: michael@0: U_NAMESPACE_END michael@0: michael@0: #endif