uobject.h 12 KB

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