localpointer.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*
  2. *******************************************************************************
  3. *
  4. * Copyright (C) 2009-2014, International Business Machines
  5. * Corporation and others. All Rights Reserved.
  6. *
  7. *******************************************************************************
  8. * file name: localpointer.h
  9. * encoding: US-ASCII
  10. * tab size: 8 (not used)
  11. * indentation:4
  12. *
  13. * created on: 2009nov13
  14. * created by: Markus W. Scherer
  15. */
  16. #ifndef __LOCALPOINTER_H__
  17. #define __LOCALPOINTER_H__
  18. /**
  19. * \file
  20. * \brief C++ API: "Smart pointers" for use with and in ICU4C C++ code.
  21. *
  22. * These classes are inspired by
  23. * - std::auto_ptr
  24. * - boost::scoped_ptr & boost::scoped_array
  25. * - Taligent Safe Pointers (TOnlyPointerTo)
  26. *
  27. * but none of those provide for all of the goals for ICU smart pointers:
  28. * - Smart pointer owns the object and releases it when it goes out of scope.
  29. * - No transfer of ownership via copy/assignment to reduce misuse. Simpler & more robust.
  30. * - ICU-compatible: No exceptions.
  31. * - Need to be able to orphan/release the pointer and its ownership.
  32. * - Need variants for normal C++ object pointers, C++ arrays, and ICU C service objects.
  33. *
  34. * For details see http://site.icu-project.org/design/cpp/scoped_ptr
  35. */
  36. #include "utypes.h"
  37. #if U_SHOW_CPLUSPLUS_API
  38. U_NAMESPACE_BEGIN
  39. /**
  40. * "Smart pointer" base class; do not use directly: use LocalPointer etc.
  41. *
  42. * Base class for smart pointer classes that do not throw exceptions.
  43. *
  44. * Do not use this base class directly, since it does not delete its pointer.
  45. * A subclass must implement methods that delete the pointer:
  46. * Destructor and adoptInstead().
  47. *
  48. * There is no operator T *() provided because the programmer must decide
  49. * whether to use getAlias() (without transfer of ownership) or orpan()
  50. * (with transfer of ownership and NULLing of the pointer).
  51. *
  52. * @see LocalPointer
  53. * @see LocalArray
  54. * @see U_DEFINE_LOCAL_OPEN_POINTER
  55. * @stable ICU 4.4
  56. */
  57. template<typename T>
  58. class LocalPointerBase {
  59. public:
  60. /**
  61. * Constructor takes ownership.
  62. * @param p simple pointer to an object that is adopted
  63. * @stable ICU 4.4
  64. */
  65. explicit LocalPointerBase(T *p=NULL) : ptr(p) {}
  66. /**
  67. * Destructor deletes the object it owns.
  68. * Subclass must override: Base class does nothing.
  69. * @stable ICU 4.4
  70. */
  71. ~LocalPointerBase() { /* delete ptr; */ }
  72. /**
  73. * NULL check.
  74. * @return TRUE if ==NULL
  75. * @stable ICU 4.4
  76. */
  77. UBool isNull() const { return ptr==NULL; }
  78. /**
  79. * NULL check.
  80. * @return TRUE if !=NULL
  81. * @stable ICU 4.4
  82. */
  83. UBool isValid() const { return ptr!=NULL; }
  84. /**
  85. * Comparison with a simple pointer, so that existing code
  86. * with ==NULL need not be changed.
  87. * @param other simple pointer for comparison
  88. * @return true if this pointer value equals other
  89. * @stable ICU 4.4
  90. */
  91. bool operator==(const T *other) const { return ptr==other; }
  92. /**
  93. * Comparison with a simple pointer, so that existing code
  94. * with !=NULL need not be changed.
  95. * @param other simple pointer for comparison
  96. * @return true if this pointer value differs from other
  97. * @stable ICU 4.4
  98. */
  99. bool operator!=(const T *other) const { return ptr!=other; }
  100. /**
  101. * Access without ownership change.
  102. * @return the pointer value
  103. * @stable ICU 4.4
  104. */
  105. T *getAlias() const { return ptr; }
  106. /**
  107. * Access without ownership change.
  108. * @return the pointer value as a reference
  109. * @stable ICU 4.4
  110. */
  111. T &operator*() const { return *ptr; }
  112. /**
  113. * Access without ownership change.
  114. * @return the pointer value
  115. * @stable ICU 4.4
  116. */
  117. T *operator->() const { return ptr; }
  118. /**
  119. * Gives up ownership; the internal pointer becomes NULL.
  120. * @return the pointer value;
  121. * caller becomes responsible for deleting the object
  122. * @stable ICU 4.4
  123. */
  124. T *orphan() {
  125. T *p=ptr;
  126. ptr=NULL;
  127. return p;
  128. }
  129. /**
  130. * Deletes the object it owns,
  131. * and adopts (takes ownership of) the one passed in.
  132. * Subclass must override: Base class does not delete the object.
  133. * @param p simple pointer to an object that is adopted
  134. * @stable ICU 4.4
  135. */
  136. void adoptInstead(T *p) {
  137. // delete ptr;
  138. ptr=p;
  139. }
  140. protected:
  141. /**
  142. * Actual pointer.
  143. * @internal
  144. */
  145. T *ptr;
  146. private:
  147. // No comparison operators with other LocalPointerBases.
  148. bool operator==(const LocalPointerBase &other);
  149. bool operator!=(const LocalPointerBase &other);
  150. // No ownership transfer: No copy constructor, no assignment operator.
  151. LocalPointerBase(const LocalPointerBase &other);
  152. void operator=(const LocalPointerBase &other);
  153. // No heap allocation. Use only on the stack.
  154. static void * U_EXPORT2 operator new(size_t size);
  155. static void * U_EXPORT2 operator new[](size_t size);
  156. #if U_HAVE_PLACEMENT_NEW
  157. static void * U_EXPORT2 operator new(size_t, void *ptr);
  158. #endif
  159. };
  160. /**
  161. * "Smart pointer" class, deletes objects via the standard C++ delete operator.
  162. * For most methods see the LocalPointerBase base class.
  163. *
  164. * Usage example:
  165. * \code
  166. * LocalPointer<UnicodeString> s(new UnicodeString((UChar32)0x50005));
  167. * int32_t length=s->length(); // 2
  168. * UChar lead=s->charAt(0); // 0xd900
  169. * if(some condition) { return; } // no need to explicitly delete the pointer
  170. * s.adoptInstead(new UnicodeString((UChar)0xfffc));
  171. * length=s->length(); // 1
  172. * // no need to explicitly delete the pointer
  173. * \endcode
  174. *
  175. * @see LocalPointerBase
  176. * @stable ICU 4.4
  177. */
  178. template<typename T>
  179. class LocalPointer : public LocalPointerBase<T> {
  180. public:
  181. /**
  182. * Constructor takes ownership.
  183. * @param p simple pointer to an object that is adopted
  184. * @stable ICU 4.4
  185. */
  186. explicit LocalPointer(T *p=NULL) : LocalPointerBase<T>(p) {}
  187. #ifndef U_HIDE_DRAFT_API
  188. /**
  189. * Constructor takes ownership and reports an error if NULL.
  190. *
  191. * This constructor is intended to be used with other-class constructors
  192. * that may report a failure UErrorCode,
  193. * so that callers need to check only for U_FAILURE(errorCode)
  194. * and not also separately for isNull().
  195. *
  196. * @param p simple pointer to an object that is adopted
  197. * @param errorCode in/out UErrorCode, set to U_MEMORY_ALLOCATION_ERROR
  198. * if p==NULL and no other failure code had been set
  199. * @draft ICU 55
  200. */
  201. LocalPointer(T *p, UErrorCode &errorCode) : LocalPointerBase<T>(p) {
  202. if(p==NULL && U_SUCCESS(errorCode)) {
  203. errorCode=U_MEMORY_ALLOCATION_ERROR;
  204. }
  205. }
  206. #endif /* U_HIDE_DRAFT_API */
  207. /**
  208. * Destructor deletes the object it owns.
  209. * @stable ICU 4.4
  210. */
  211. ~LocalPointer() {
  212. delete LocalPointerBase<T>::ptr;
  213. }
  214. /**
  215. * Deletes the object it owns,
  216. * and adopts (takes ownership of) the one passed in.
  217. * @param p simple pointer to an object that is adopted
  218. * @stable ICU 4.4
  219. */
  220. void adoptInstead(T *p) {
  221. delete LocalPointerBase<T>::ptr;
  222. LocalPointerBase<T>::ptr=p;
  223. }
  224. #ifndef U_HIDE_DRAFT_API
  225. /**
  226. * Deletes the object it owns,
  227. * and adopts (takes ownership of) the one passed in.
  228. *
  229. * If U_FAILURE(errorCode), then the current object is retained and the new one deleted.
  230. *
  231. * If U_SUCCESS(errorCode) but the input pointer is NULL,
  232. * then U_MEMORY_ALLOCATION_ERROR is set,
  233. * the current object is deleted, and NULL is set.
  234. *
  235. * @param p simple pointer to an object that is adopted
  236. * @param errorCode in/out UErrorCode, set to U_MEMORY_ALLOCATION_ERROR
  237. * if p==NULL and no other failure code had been set
  238. * @draft ICU 55
  239. */
  240. void adoptInsteadAndCheckErrorCode(T *p, UErrorCode &errorCode) {
  241. if(U_SUCCESS(errorCode)) {
  242. delete LocalPointerBase<T>::ptr;
  243. LocalPointerBase<T>::ptr=p;
  244. if(p==NULL) {
  245. errorCode=U_MEMORY_ALLOCATION_ERROR;
  246. }
  247. } else {
  248. delete p;
  249. }
  250. }
  251. #endif /* U_HIDE_DRAFT_API */
  252. };
  253. /**
  254. * "Smart pointer" class, deletes objects via the C++ array delete[] operator.
  255. * For most methods see the LocalPointerBase base class.
  256. * Adds operator[] for array item access.
  257. *
  258. * Usage example:
  259. * \code
  260. * LocalArray<UnicodeString> a(new UnicodeString[2]);
  261. * a[0].append((UChar)0x61);
  262. * if(some condition) { return; } // no need to explicitly delete the array
  263. * a.adoptInstead(new UnicodeString[4]);
  264. * a[3].append((UChar)0x62).append((UChar)0x63).reverse();
  265. * // no need to explicitly delete the array
  266. * \endcode
  267. *
  268. * @see LocalPointerBase
  269. * @stable ICU 4.4
  270. */
  271. template<typename T>
  272. class LocalArray : public LocalPointerBase<T> {
  273. public:
  274. /**
  275. * Constructor takes ownership.
  276. * @param p simple pointer to an array of T objects that is adopted
  277. * @stable ICU 4.4
  278. */
  279. explicit LocalArray(T *p=NULL) : LocalPointerBase<T>(p) {}
  280. /**
  281. * Destructor deletes the array it owns.
  282. * @stable ICU 4.4
  283. */
  284. ~LocalArray() {
  285. delete[] LocalPointerBase<T>::ptr;
  286. }
  287. /**
  288. * Deletes the array it owns,
  289. * and adopts (takes ownership of) the one passed in.
  290. * @param p simple pointer to an array of T objects that is adopted
  291. * @stable ICU 4.4
  292. */
  293. void adoptInstead(T *p) {
  294. delete[] LocalPointerBase<T>::ptr;
  295. LocalPointerBase<T>::ptr=p;
  296. }
  297. /**
  298. * Array item access (writable).
  299. * No index bounds check.
  300. * @param i array index
  301. * @return reference to the array item
  302. * @stable ICU 4.4
  303. */
  304. T &operator[](ptrdiff_t i) const { return LocalPointerBase<T>::ptr[i]; }
  305. };
  306. /**
  307. * \def U_DEFINE_LOCAL_OPEN_POINTER
  308. * "Smart pointer" definition macro, deletes objects via the closeFunction.
  309. * Defines a subclass of LocalPointerBase which works just
  310. * like LocalPointer<Type> except that this subclass will use the closeFunction
  311. * rather than the C++ delete operator.
  312. *
  313. * Requirement: The closeFunction must tolerate a NULL pointer.
  314. * (We could add a NULL check here but it is normally redundant.)
  315. *
  316. * Usage example:
  317. * \code
  318. * LocalUCaseMapPointer csm(ucasemap_open(localeID, options, &errorCode));
  319. * utf8OutLength=ucasemap_utf8ToLower(csm.getAlias(),
  320. * utf8Out, (int32_t)sizeof(utf8Out),
  321. * utf8In, utf8InLength, &errorCode);
  322. * if(U_FAILURE(errorCode)) { return; } // no need to explicitly delete the UCaseMap
  323. * \endcode
  324. *
  325. * @see LocalPointerBase
  326. * @see LocalPointer
  327. * @stable ICU 4.4
  328. */
  329. #define U_DEFINE_LOCAL_OPEN_POINTER(LocalPointerClassName, Type, closeFunction) \
  330. class LocalPointerClassName : public LocalPointerBase<Type> { \
  331. public: \
  332. explicit LocalPointerClassName(Type *p=NULL) : LocalPointerBase<Type>(p) {} \
  333. ~LocalPointerClassName() { closeFunction(ptr); } \
  334. void adoptInstead(Type *p) { \
  335. closeFunction(ptr); \
  336. ptr=p; \
  337. } \
  338. }
  339. U_NAMESPACE_END
  340. #endif /* U_SHOW_CPLUSPLUS_API */
  341. #endif /* __LOCALPOINTER_H__ */