uobject.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. // Copyright (C) 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. ******************************************************************************
  5. *
  6. * Copyright (C) 2002-2012, International Business Machines
  7. * Corporation and others. All Rights Reserved.
  8. *
  9. ******************************************************************************
  10. * file name: uobject.h
  11. * encoding: US-ASCII
  12. * tab size: 8 (not used)
  13. * indentation:4
  14. *
  15. * created on: 2002jun26
  16. * created by: Markus W. Scherer
  17. */
  18. #ifndef __UOBJECT_H__
  19. #define __UOBJECT_H__
  20. #include "utypes.h"
  21. /**
  22. * \file
  23. * \brief C++ API: Common ICU base class UObject.
  24. */
  25. /**
  26. * @{
  27. * \def U_NO_THROW
  28. * Define this to define the throw() specification so
  29. * certain functions do not throw any exceptions
  30. *
  31. * UMemory operator new methods should have the throw() specification
  32. * appended to them, so that the compiler adds the additional NULL check
  33. * before calling constructors. Without, if <code>operator new</code> returns NULL the
  34. * constructor is still called, and if the constructor references member
  35. * data, (which it typically does), the result is a segmentation violation.
  36. *
  37. * @stable ICU 4.2
  38. */
  39. #ifndef U_NO_THROW
  40. #define U_NO_THROW throw()
  41. #endif
  42. /** @} */
  43. /*===========================================================================*/
  44. /* UClassID-based RTTI */
  45. /*===========================================================================*/
  46. /**
  47. * UClassID is used to identify classes without using the compiler's RTTI.
  48. * This was used before C++ compilers consistently supported RTTI.
  49. * ICU 4.6 requires compiler RTTI to be turned on.
  50. *
  51. * Each class hierarchy which needs
  52. * to implement polymorphic clone() or operator==() defines two methods,
  53. * described in detail below. UClassID values can be compared using
  54. * operator==(). Nothing else should be done with them.
  55. *
  56. * \par
  57. * In class hierarchies that implement "poor man's RTTI",
  58. * each concrete subclass implements getDynamicClassID() in the same way:
  59. *
  60. * \code
  61. * class Derived {
  62. * public:
  63. * virtual UClassID getDynamicClassID() const
  64. * { return Derived::getStaticClassID(); }
  65. * }
  66. * \endcode
  67. *
  68. * Each concrete class implements getStaticClassID() as well, which allows
  69. * clients to test for a specific type.
  70. *
  71. * \code
  72. * class Derived {
  73. * public:
  74. * static UClassID U_EXPORT2 getStaticClassID();
  75. * private:
  76. * static char fgClassID;
  77. * }
  78. *
  79. * // In Derived.cpp:
  80. * UClassID Derived::getStaticClassID()
  81. * { return (UClassID)&Derived::fgClassID; }
  82. * char Derived::fgClassID = 0; // Value is irrelevant
  83. * \endcode
  84. * @stable ICU 2.0
  85. */
  86. typedef void* UClassID;
  87. U_NAMESPACE_BEGIN
  88. /**
  89. * UMemory is the common ICU base class.
  90. * All other ICU C++ classes are derived from UMemory (starting with ICU 2.4).
  91. *
  92. * This is primarily to make it possible and simple to override the
  93. * C++ memory management by adding new/delete operators to this base class.
  94. *
  95. * To override ALL ICU memory management, including that from plain C code,
  96. * replace the allocation functions declared in cmemory.h
  97. *
  98. * UMemory does not contain any virtual functions.
  99. * Common "boilerplate" functions are defined in UObject.
  100. *
  101. * @stable ICU 2.4
  102. */
  103. class U_COMMON_API UMemory {
  104. public:
  105. /* test versions for debugging shaper heap memory problems */
  106. #ifdef SHAPER_MEMORY_DEBUG
  107. static void * NewArray(int size, int count);
  108. static void * GrowArray(void * array, int newSize );
  109. static void FreeArray(void * array );
  110. #endif
  111. #if U_OVERRIDE_CXX_ALLOCATION
  112. /**
  113. * Override for ICU4C C++ memory management.
  114. * simple, non-class types are allocated using the macros in common/cmemory.h
  115. * (uprv_malloc(), uprv_free(), uprv_realloc());
  116. * they or something else could be used here to implement C++ new/delete
  117. * for ICU4C C++ classes
  118. * @stable ICU 2.4
  119. */
  120. static void * U_EXPORT2 operator new(size_t size) U_NO_THROW;
  121. /**
  122. * Override for ICU4C C++ memory management.
  123. * See new().
  124. * @stable ICU 2.4
  125. */
  126. static void * U_EXPORT2 operator new[](size_t size) U_NO_THROW;
  127. /**
  128. * Override for ICU4C C++ memory management.
  129. * simple, non-class types are allocated using the macros in common/cmemory.h
  130. * (uprv_malloc(), uprv_free(), uprv_realloc());
  131. * they or something else could be used here to implement C++ new/delete
  132. * for ICU4C C++ classes
  133. * @stable ICU 2.4
  134. */
  135. static void U_EXPORT2 operator delete(void *p) U_NO_THROW;
  136. /**
  137. * Override for ICU4C C++ memory management.
  138. * See delete().
  139. * @stable ICU 2.4
  140. */
  141. static void U_EXPORT2 operator delete[](void *p) U_NO_THROW;
  142. #if U_HAVE_PLACEMENT_NEW
  143. /**
  144. * Override for ICU4C C++ memory management for STL.
  145. * See new().
  146. * @stable ICU 2.6
  147. */
  148. static inline void * U_EXPORT2 operator new(size_t, void *ptr) U_NO_THROW { return ptr; }
  149. /**
  150. * Override for ICU4C C++ memory management for STL.
  151. * See delete().
  152. * @stable ICU 2.6
  153. */
  154. static inline void U_EXPORT2 operator delete(void *, void *) U_NO_THROW {}
  155. #endif /* U_HAVE_PLACEMENT_NEW */
  156. #if U_HAVE_DEBUG_LOCATION_NEW
  157. /**
  158. * This method overrides the MFC debug version of the operator new
  159. *
  160. * @param size The requested memory size
  161. * @param file The file where the allocation was requested
  162. * @param line The line where the allocation was requested
  163. */
  164. static void * U_EXPORT2 operator new(size_t size, const char* file, int line) U_NO_THROW;
  165. /**
  166. * This method provides a matching delete for the MFC debug new
  167. *
  168. * @param p The pointer to the allocated memory
  169. * @param file The file where the allocation was requested
  170. * @param line The line where the allocation was requested
  171. */
  172. static void U_EXPORT2 operator delete(void* p, const char* file, int line) U_NO_THROW;
  173. #endif /* U_HAVE_DEBUG_LOCATION_NEW */
  174. #endif /* U_OVERRIDE_CXX_ALLOCATION */
  175. /*
  176. * Assignment operator not declared. The compiler will provide one
  177. * which does nothing since this class does not contain any data members.
  178. * API/code coverage may show the assignment operator as present and
  179. * untested - ignore.
  180. * Subclasses need this assignment operator if they use compiler-provided
  181. * assignment operators of their own. An alternative to not declaring one
  182. * here would be to declare and empty-implement a protected or public one.
  183. UMemory &UMemory::operator=(const UMemory &);
  184. */
  185. };
  186. /**
  187. * UObject is the common ICU "boilerplate" class.
  188. * UObject inherits UMemory (starting with ICU 2.4),
  189. * and all other public ICU C++ classes
  190. * are derived from UObject (starting with ICU 2.2).
  191. *
  192. * UObject contains common virtual functions, in particular a virtual destructor.
  193. *
  194. * The clone() function is not available in UObject because it is not
  195. * implemented by all ICU classes.
  196. * Many ICU services provide a clone() function for their class trees,
  197. * defined on the service's C++ base class, and all subclasses within that
  198. * service class tree return a pointer to the service base class
  199. * (which itself is a subclass of UObject).
  200. * This is because some compilers do not support covariant (same-as-this)
  201. * return types; cast to the appropriate subclass if necessary.
  202. *
  203. * @stable ICU 2.2
  204. */
  205. class U_COMMON_API UObject : public UMemory {
  206. public:
  207. /**
  208. * Destructor.
  209. *
  210. * @stable ICU 2.2
  211. */
  212. virtual ~UObject();
  213. /**
  214. * ICU4C "poor man's RTTI", returns a UClassID for the actual ICU class.
  215. * The base class implementation returns a dummy value.
  216. *
  217. * Use compiler RTTI rather than ICU's "poor man's RTTI".
  218. * Since ICU 4.6, new ICU C++ class hierarchies do not implement "poor man's RTTI".
  219. *
  220. * @stable ICU 2.2
  221. */
  222. virtual UClassID getDynamicClassID() const;
  223. protected:
  224. // the following functions are protected to prevent instantiation and
  225. // direct use of UObject itself
  226. // default constructor
  227. // inline UObject() {}
  228. // copy constructor
  229. // inline UObject(const UObject &other) {}
  230. #if 0
  231. // TODO Sometime in the future. Implement operator==().
  232. // (This comment inserted in 2.2)
  233. // some or all of the following "boilerplate" functions may be made public
  234. // in a future ICU4C release when all subclasses implement them
  235. // assignment operator
  236. // (not virtual, see "Taligent's Guide to Designing Programs" pp.73..74)
  237. // commented out because the implementation is the same as a compiler's default
  238. // UObject &operator=(const UObject &other) { return *this; }
  239. // comparison operators
  240. virtual inline UBool operator==(const UObject &other) const { return this==&other; }
  241. inline UBool operator!=(const UObject &other) const { return !operator==(other); }
  242. // clone() commented out from the base class:
  243. // some compilers do not support co-variant return types
  244. // (i.e., subclasses would have to return UObject * as well, instead of SubClass *)
  245. // see also UObject class documentation.
  246. // virtual UObject *clone() const;
  247. #endif
  248. /*
  249. * Assignment operator not declared. The compiler will provide one
  250. * which does nothing since this class does not contain any data members.
  251. * API/code coverage may show the assignment operator as present and
  252. * untested - ignore.
  253. * Subclasses need this assignment operator if they use compiler-provided
  254. * assignment operators of their own. An alternative to not declaring one
  255. * here would be to declare and empty-implement a protected or public one.
  256. UObject &UObject::operator=(const UObject &);
  257. */
  258. };
  259. #ifndef U_HIDE_INTERNAL_API
  260. /**
  261. * This is a simple macro to add ICU RTTI to an ICU object implementation.
  262. * This does not go into the header. This should only be used in *.cpp files.
  263. *
  264. * @param myClass The name of the class that needs RTTI defined.
  265. * @internal
  266. */
  267. #define UOBJECT_DEFINE_RTTI_IMPLEMENTATION(myClass) \
  268. UClassID U_EXPORT2 myClass::getStaticClassID() { \
  269. static char classID = 0; \
  270. return (UClassID)&classID; \
  271. } \
  272. UClassID myClass::getDynamicClassID() const \
  273. { return myClass::getStaticClassID(); }
  274. /**
  275. * This macro adds ICU RTTI to an ICU abstract class implementation.
  276. * This macro should be invoked in *.cpp files. The corresponding
  277. * header should declare getStaticClassID.
  278. *
  279. * @param myClass The name of the class that needs RTTI defined.
  280. * @internal
  281. */
  282. #define UOBJECT_DEFINE_ABSTRACT_RTTI_IMPLEMENTATION(myClass) \
  283. UClassID U_EXPORT2 myClass::getStaticClassID() { \
  284. static char classID = 0; \
  285. return (UClassID)&classID; \
  286. }
  287. #endif /* U_HIDE_INTERNAL_API */
  288. U_NAMESPACE_END
  289. #endif