sl@0: /* sl@0: ****************************************************************************** sl@0: * sl@0: * Copyright (C) 2002-2005, International Business Machines sl@0: * Corporation and others. All Rights Reserved. sl@0: * sl@0: ****************************************************************************** sl@0: * file name: uobject.h sl@0: * encoding: US-ASCII sl@0: * tab size: 8 (not used) sl@0: * indentation:4 sl@0: * sl@0: * created on: 2002jun26 sl@0: * created by: Markus W. Scherer sl@0: */ sl@0: sl@0: #ifndef __UOBJECT_H__ sl@0: #define __UOBJECT_H__ sl@0: sl@0: #include "unicode/utypes.h" sl@0: sl@0: U_NAMESPACE_BEGIN sl@0: sl@0: /** sl@0: * \file sl@0: * \brief C++ API: Common ICU base class UObject. sl@0: */ sl@0: sl@0: /** U_OVERRIDE_CXX_ALLOCATION - Define this to override operator new and sl@0: * delete in UMemory. Enabled by default for ICU. sl@0: * sl@0: * Enabling forces all allocation of ICU object types to use ICU's sl@0: * memory allocation. On Windows, this allows the ICU DLL to be used by sl@0: * applications that statically link the C Runtime library, meaning that sl@0: * the app and ICU will be using different heaps. sl@0: * sl@0: * @stable ICU 2.2 sl@0: */ sl@0: #ifndef U_OVERRIDE_CXX_ALLOCATION sl@0: #define U_OVERRIDE_CXX_ALLOCATION 1 sl@0: #endif sl@0: sl@0: /** U_HAVE_PLACEMENT_NEW - Define this to define the placement new and sl@0: * delete in UMemory for STL. sl@0: * sl@0: * @stable ICU 2.6 sl@0: */ sl@0: #ifndef U_HAVE_PLACEMENT_NEW sl@0: #define U_HAVE_PLACEMENT_NEW 1 sl@0: #endif sl@0: sl@0: /** U_HAVE_DEBUG_LOCATION_NEW - Define this to define the MFC debug sl@0: * version of the operator new. sl@0: * sl@0: * @draft ICU 3.4 sl@0: */ sl@0: #ifndef U_HAVE_DEBUG_LOCATION_NEW sl@0: #define U_HAVE_DEBUG_LOCATION_NEW 0 sl@0: #endif sl@0: sl@0: sl@0: /** sl@0: * UMemory is the common ICU base class. sl@0: * All other ICU C++ classes are derived from UMemory (starting with ICU 2.4). sl@0: * sl@0: * This is primarily to make it possible and simple to override the sl@0: * C++ memory management by adding new/delete operators to this base class. sl@0: * sl@0: * To override ALL ICU memory management, including that from plain C code, sl@0: * replace the allocation functions declared in cmemory.h sl@0: * sl@0: * UMemory does not contain any virtual functions. sl@0: * Common "boilerplate" functions are defined in UObject. sl@0: * sl@0: * @stable ICU 2.4 sl@0: */ sl@0: class U_COMMON_API UMemory { sl@0: public: sl@0: sl@0: /* test versions for debugging shaper heap memory problems */ sl@0: #ifdef SHAPER_MEMORY_DEBUG sl@0: static void * NewArray(int size, int count); sl@0: static void * GrowArray(void * array, int newSize ); sl@0: static void FreeArray(void * array ); sl@0: #endif sl@0: sl@0: #if U_OVERRIDE_CXX_ALLOCATION sl@0: /** sl@0: * Override for ICU4C C++ memory management. sl@0: * simple, non-class types are allocated using the macros in common/cmemory.h sl@0: * (uprv_malloc(), uprv_free(), uprv_realloc()); sl@0: * they or something else could be used here to implement C++ new/delete sl@0: * for ICU4C C++ classes sl@0: * @stable ICU 2.4 sl@0: */ sl@0: static void * U_EXPORT2 operator new(size_t size); sl@0: sl@0: /** sl@0: * Override for ICU4C C++ memory management. sl@0: * See new(). sl@0: * @stable ICU 2.4 sl@0: */ sl@0: static void * U_EXPORT2 operator new[](size_t size); sl@0: sl@0: /** sl@0: * Override for ICU4C C++ memory management. sl@0: * simple, non-class types are allocated using the macros in common/cmemory.h sl@0: * (uprv_malloc(), uprv_free(), uprv_realloc()); sl@0: * they or something else could be used here to implement C++ new/delete sl@0: * for ICU4C C++ classes sl@0: * @stable ICU 2.4 sl@0: */ sl@0: static void U_EXPORT2 operator delete(void *p); sl@0: sl@0: /** sl@0: * Override for ICU4C C++ memory management. sl@0: * See delete(). sl@0: * @stable ICU 2.4 sl@0: */ sl@0: static void U_EXPORT2 operator delete[](void *p); sl@0: sl@0: #if U_HAVE_PLACEMENT_NEW sl@0: /** sl@0: * Override for ICU4C C++ memory management for STL. sl@0: * See new(). sl@0: * @stable ICU 2.6 sl@0: */ sl@0: static inline void * U_EXPORT2 operator new(size_t, void *ptr) { return ptr; } sl@0: sl@0: /** sl@0: * Override for ICU4C C++ memory management for STL. sl@0: * See delete(). sl@0: * @stable ICU 2.6 sl@0: */ sl@0: static inline void U_EXPORT2 operator delete(void *, void *) {} sl@0: #endif /* U_HAVE_PLACEMENT_NEW */ sl@0: #if U_HAVE_DEBUG_LOCATION_NEW sl@0: /** sl@0: * This method overrides the MFC debug version of the operator new sl@0: * sl@0: * @param size The requested memory size sl@0: * @param file The file where the allocation was requested sl@0: * @param line The line where the allocation was requested sl@0: */ sl@0: static void * U_EXPORT2 operator new(size_t size, const char* file, int line); sl@0: /** sl@0: * This method provides a matching delete for the MFC debug new sl@0: * sl@0: * @param p The pointer to the allocated memory sl@0: * @param file The file where the allocation was requested sl@0: * @param line The line where the allocation was requested sl@0: */ sl@0: static void U_EXPORT2 operator delete(void* p, const char* file, int line); sl@0: #endif /* U_HAVE_DEBUG_LOCATION_NEW */ sl@0: #endif /* U_OVERRIDE_CXX_ALLOCATION */ sl@0: sl@0: /* sl@0: * Assignment operator not declared. The compiler will provide one sl@0: * which does nothing since this class does not contain any data members. sl@0: * API/code coverage may show the assignment operator as present and sl@0: * untested - ignore. sl@0: * Subclasses need this assignment operator if they use compiler-provided sl@0: * assignment operators of their own. An alternative to not declaring one sl@0: * here would be to declare and empty-implement a protected or public one. sl@0: UMemory &UMemory::operator=(const UMemory &); sl@0: */ sl@0: }; sl@0: sl@0: /** sl@0: * UObject is the common ICU "boilerplate" class. sl@0: * UObject inherits UMemory (starting with ICU 2.4), sl@0: * and all other public ICU C++ classes sl@0: * are derived from UObject (starting with ICU 2.2). sl@0: * sl@0: * UObject contains common virtual functions like for ICU's "poor man's RTTI". sl@0: * It does not contain default implementations of virtual methods sl@0: * like getDynamicClassID to allow derived classes such as Format sl@0: * to declare these as pure virtual. sl@0: * sl@0: * The clone() function is not available in UObject because it is not sl@0: * implemented by all ICU classes. sl@0: * Many ICU services provide a clone() function for their class trees, sl@0: * defined on the service's C++ base class, and all subclasses within that sl@0: * service class tree return a pointer to the service base class sl@0: * (which itself is a subclass of UObject). sl@0: * This is because some compilers do not support covariant (same-as-this) sl@0: * return types; cast to the appropriate subclass if necessary. sl@0: * sl@0: * @stable ICU 2.2 sl@0: */ sl@0: class U_COMMON_API UObject : public UMemory { sl@0: public: sl@0: /** sl@0: * Destructor. sl@0: * sl@0: * @stable ICU 2.2 sl@0: */ sl@0: virtual ~UObject(); sl@0: sl@0: /** sl@0: * ICU4C "poor man's RTTI", returns a UClassID for the actual ICU class. sl@0: * sl@0: * @stable ICU 2.2 sl@0: */ sl@0: virtual UClassID getDynamicClassID() const = 0; sl@0: sl@0: protected: sl@0: // the following functions are protected to prevent instantiation and sl@0: // direct use of UObject itself sl@0: sl@0: // default constructor sl@0: // commented out because UObject is abstract (see getDynamicClassID) sl@0: // inline UObject() {} sl@0: sl@0: // copy constructor sl@0: // commented out because UObject is abstract (see getDynamicClassID) sl@0: // inline UObject(const UObject &other) {} sl@0: sl@0: #if 0 sl@0: // TODO Sometime in the future. Implement operator==(). sl@0: // (This comment inserted in 2.2) sl@0: // some or all of the following "boilerplate" functions may be made public sl@0: // in a future ICU4C release when all subclasses implement them sl@0: sl@0: // assignment operator sl@0: // (not virtual, see "Taligent's Guide to Designing Programs" pp.73..74) sl@0: // commented out because the implementation is the same as a compiler's default sl@0: // UObject &operator=(const UObject &other) { return *this; } sl@0: sl@0: // comparison operators sl@0: virtual inline UBool operator==(const UObject &other) const { return this==&other; } sl@0: inline UBool operator!=(const UObject &other) const { return !operator==(other); } sl@0: sl@0: // clone() commented out from the base class: sl@0: // some compilers do not support co-variant return types sl@0: // (i.e., subclasses would have to return UObject * as well, instead of SubClass *) sl@0: // see also UObject class documentation. sl@0: // virtual UObject *clone() const; sl@0: #endif sl@0: sl@0: /* sl@0: * Assignment operator not declared. The compiler will provide one sl@0: * which does nothing since this class does not contain any data members. sl@0: * API/code coverage may show the assignment operator as present and sl@0: * untested - ignore. sl@0: * Subclasses need this assignment operator if they use compiler-provided sl@0: * assignment operators of their own. An alternative to not declaring one sl@0: * here would be to declare and empty-implement a protected or public one. sl@0: UObject &UObject::operator=(const UObject &); sl@0: */ sl@0: sl@0: // Future implementation for RTTI that support subtyping. [alan] sl@0: // sl@0: // public: sl@0: // /** sl@0: // * @internal sl@0: // */ sl@0: // static UClassID getStaticClassID(); sl@0: // sl@0: // /** sl@0: // * @internal sl@0: // */ sl@0: // UBool instanceOf(UClassID type) const; sl@0: }; sl@0: sl@0: /** sl@0: * This is a simple macro to add ICU RTTI to an ICU object implementation. sl@0: * This does not go into the header. This should only be used in *.cpp files. sl@0: * sl@0: * @param myClass The name of the class that needs RTTI defined. sl@0: * @internal sl@0: */ sl@0: #define UOBJECT_DEFINE_RTTI_IMPLEMENTATION(myClass) \ sl@0: UClassID U_EXPORT2 myClass::getStaticClassID() { \ sl@0: static const char classID = 0; \ sl@0: return (UClassID)&classID; \ sl@0: } \ sl@0: UClassID myClass::getDynamicClassID() const \ sl@0: { return myClass::getStaticClassID(); } sl@0: sl@0: sl@0: /** sl@0: * This macro adds ICU RTTI to an ICU abstract class implementation. sl@0: * This macro should be invoked in *.cpp files. The corresponding sl@0: * header should declare getStaticClassID. sl@0: * sl@0: * @param myClass The name of the class that needs RTTI defined. sl@0: * @internal sl@0: */ sl@0: #define UOBJECT_DEFINE_ABSTRACT_RTTI_IMPLEMENTATION(myClass) \ sl@0: UClassID U_EXPORT2 myClass::getStaticClassID() { \ sl@0: static const char classID = 0; \ sl@0: return (UClassID)&classID; \ sl@0: } sl@0: sl@0: // /** sl@0: // * This macro adds ICU RTTI to an ICU concrete class implementation. sl@0: // * This macro should be invoked in *.cpp files. The corresponding sl@0: // * header should declare getDynamicClassID and getStaticClassID. sl@0: // * sl@0: // * @param myClass The name of the class that needs RTTI defined. sl@0: // * @param myParent The name of the myClass's parent. sl@0: // * @internal sl@0: // */ sl@0: /*#define UOBJECT_DEFINE_RTTI_IMPLEMENTATION(myClass, myParent) \ sl@0: UOBJECT_DEFINE_ABSTRACT_RTTI_IMPLEMENTATION(myClass, myParent) \ sl@0: UClassID myClass::getDynamicClassID() const { \ sl@0: return myClass::getStaticClassID(); \ sl@0: } sl@0: */ sl@0: sl@0: sl@0: U_NAMESPACE_END sl@0: sl@0: #endif