uobject.h 11 KB

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