uobject.h 11 KB

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