localpointer.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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) 2009-2016, International Business Machines
  7. * Corporation and others. All Rights Reserved.
  8. *
  9. *******************************************************************************
  10. * file name: localpointer.h
  11. * encoding: US-ASCII
  12. * tab size: 8 (not used)
  13. * indentation:4
  14. *
  15. * created on: 2009nov13
  16. * created by: Markus W. Scherer
  17. */
  18. #ifndef __LOCALPOINTER_H__
  19. #define __LOCALPOINTER_H__
  20. /**
  21. * \file
  22. * \brief C++ API: "Smart pointers" for use with and in ICU4C C++ code.
  23. *
  24. * These classes are inspired by
  25. * - std::auto_ptr
  26. * - boost::scoped_ptr & boost::scoped_array
  27. * - Taligent Safe Pointers (TOnlyPointerTo)
  28. *
  29. * but none of those provide for all of the goals for ICU smart pointers:
  30. * - Smart pointer owns the object and releases it when it goes out of scope.
  31. * - No transfer of ownership via copy/assignment to reduce misuse. Simpler & more robust.
  32. * - ICU-compatible: No exceptions.
  33. * - Need to be able to orphan/release the pointer and its ownership.
  34. * - Need variants for normal C++ object pointers, C++ arrays, and ICU C service objects.
  35. *
  36. * For details see http://site.icu-project.org/design/cpp/scoped_ptr
  37. */
  38. #include "unicode/utypes.h"
  39. #if U_SHOW_CPLUSPLUS_API
  40. U_NAMESPACE_BEGIN
  41. /**
  42. * "Smart pointer" base class; do not use directly: use LocalPointer etc.
  43. *
  44. * Base class for smart pointer classes that do not throw exceptions.
  45. *
  46. * Do not use this base class directly, since it does not delete its pointer.
  47. * A subclass must implement methods that delete the pointer:
  48. * Destructor and adoptInstead().
  49. *
  50. * There is no operator T *() provided because the programmer must decide
  51. * whether to use getAlias() (without transfer of ownership) or orphan()
  52. * (with transfer of ownership and NULLing of the pointer).
  53. *
  54. * @see LocalPointer
  55. * @see LocalArray
  56. * @see U_DEFINE_LOCAL_OPEN_POINTER
  57. * @stable ICU 4.4
  58. */
  59. template<typename T>
  60. class LocalPointerBase {
  61. public:
  62. /**
  63. * Constructor takes ownership.
  64. * @param p simple pointer to an object that is adopted
  65. * @stable ICU 4.4
  66. */
  67. explicit LocalPointerBase(T *p=NULL) : ptr(p) {}
  68. /**
  69. * Destructor deletes the object it owns.
  70. * Subclass must override: Base class does nothing.
  71. * @stable ICU 4.4
  72. */
  73. ~LocalPointerBase() { /* delete ptr; */ }
  74. /**
  75. * NULL check.
  76. * @return TRUE if ==NULL
  77. * @stable ICU 4.4
  78. */
  79. UBool isNull() const { return ptr==NULL; }
  80. /**
  81. * NULL check.
  82. * @return TRUE if !=NULL
  83. * @stable ICU 4.4
  84. */
  85. UBool isValid() const { return ptr!=NULL; }
  86. /**
  87. * Comparison with a simple pointer, so that existing code
  88. * with ==NULL need not be changed.
  89. * @param other simple pointer for comparison
  90. * @return true if this pointer value equals other
  91. * @stable ICU 4.4
  92. */
  93. bool operator==(const T *other) const { return ptr==other; }
  94. /**
  95. * Comparison with a simple pointer, so that existing code
  96. * with !=NULL need not be changed.
  97. * @param other simple pointer for comparison
  98. * @return true if this pointer value differs from other
  99. * @stable ICU 4.4
  100. */
  101. bool operator!=(const T *other) const { return ptr!=other; }
  102. /**
  103. * Access without ownership change.
  104. * @return the pointer value
  105. * @stable ICU 4.4
  106. */
  107. T *getAlias() const { return ptr; }
  108. /**
  109. * Access without ownership change.
  110. * @return the pointer value as a reference
  111. * @stable ICU 4.4
  112. */
  113. T &operator*() const { return *ptr; }
  114. /**
  115. * Access without ownership change.
  116. * @return the pointer value
  117. * @stable ICU 4.4
  118. */
  119. T *operator->() const { return ptr; }
  120. /**
  121. * Gives up ownership; the internal pointer becomes NULL.
  122. * @return the pointer value;
  123. * caller becomes responsible for deleting the object
  124. * @stable ICU 4.4
  125. */
  126. T *orphan() {
  127. T *p=ptr;
  128. ptr=NULL;
  129. return p;
  130. }
  131. /**
  132. * Deletes the object it owns,
  133. * and adopts (takes ownership of) the one passed in.
  134. * Subclass must override: Base class does not delete the object.
  135. * @param p simple pointer to an object that is adopted
  136. * @stable ICU 4.4
  137. */
  138. void adoptInstead(T *p) {
  139. // delete ptr;
  140. ptr=p;
  141. }
  142. protected:
  143. /**
  144. * Actual pointer.
  145. * @internal
  146. */
  147. T *ptr;
  148. private:
  149. // No comparison operators with other LocalPointerBases.
  150. bool operator==(const LocalPointerBase<T> &other);
  151. bool operator!=(const LocalPointerBase<T> &other);
  152. // No ownership sharing: No copy constructor, no assignment operator.
  153. LocalPointerBase(const LocalPointerBase<T> &other);
  154. void operator=(const LocalPointerBase<T> &other);
  155. // No heap allocation. Use only on the stack.
  156. static void * U_EXPORT2 operator new(size_t size);
  157. static void * U_EXPORT2 operator new[](size_t size);
  158. #if U_HAVE_PLACEMENT_NEW
  159. static void * U_EXPORT2 operator new(size_t, void *ptr);
  160. #endif
  161. };
  162. /**
  163. * "Smart pointer" class, deletes objects via the standard C++ delete operator.
  164. * For most methods see the LocalPointerBase base class.
  165. *
  166. * Usage example:
  167. * \code
  168. * LocalPointer<UnicodeString> s(new UnicodeString((UChar32)0x50005));
  169. * int32_t length=s->length(); // 2
  170. * UChar lead=s->charAt(0); // 0xd900
  171. * if(some condition) { return; } // no need to explicitly delete the pointer
  172. * s.adoptInstead(new UnicodeString((UChar)0xfffc));
  173. * length=s->length(); // 1
  174. * // no need to explicitly delete the pointer
  175. * \endcode
  176. *
  177. * @see LocalPointerBase
  178. * @stable ICU 4.4
  179. */
  180. template<typename T>
  181. class LocalPointer : public LocalPointerBase<T> {
  182. public:
  183. using LocalPointerBase<T>::operator*;
  184. using LocalPointerBase<T>::operator->;
  185. /**
  186. * Constructor takes ownership.
  187. * @param p simple pointer to an object that is adopted
  188. * @stable ICU 4.4
  189. */
  190. explicit LocalPointer(T *p=NULL) : LocalPointerBase<T>(p) {}
  191. /**
  192. * Constructor takes ownership and reports an error if NULL.
  193. *
  194. * This constructor is intended to be used with other-class constructors
  195. * that may report a failure UErrorCode,
  196. * so that callers need to check only for U_FAILURE(errorCode)
  197. * and not also separately for isNull().
  198. *
  199. * @param p simple pointer to an object that is adopted
  200. * @param errorCode in/out UErrorCode, set to U_MEMORY_ALLOCATION_ERROR
  201. * if p==NULL and no other failure code had been set
  202. * @stable ICU 55
  203. */
  204. LocalPointer(T *p, UErrorCode &errorCode) : LocalPointerBase<T>(p) {
  205. if(p==NULL && U_SUCCESS(errorCode)) {
  206. errorCode=U_MEMORY_ALLOCATION_ERROR;
  207. }
  208. }
  209. #if U_HAVE_RVALUE_REFERENCES
  210. /**
  211. * Move constructor, leaves src with isNull().
  212. * @param src source smart pointer
  213. * @stable ICU 56
  214. */
  215. LocalPointer(LocalPointer<T> &&src) U_NOEXCEPT : LocalPointerBase<T>(src.ptr) {
  216. src.ptr=NULL;
  217. }
  218. #endif
  219. /**
  220. * Destructor deletes the object it owns.
  221. * @stable ICU 4.4
  222. */
  223. ~LocalPointer() {
  224. delete LocalPointerBase<T>::ptr;
  225. }
  226. #if U_HAVE_RVALUE_REFERENCES
  227. /**
  228. * Move assignment operator, leaves src with isNull().
  229. * The behavior is undefined if *this and src are the same object.
  230. * @param src source smart pointer
  231. * @return *this
  232. * @stable ICU 56
  233. */
  234. LocalPointer<T> &operator=(LocalPointer<T> &&src) U_NOEXCEPT {
  235. return moveFrom(src);
  236. }
  237. #endif
  238. // do not use #ifndef U_HIDE_DRAFT_API for moveFrom, needed by non-draft API
  239. /**
  240. * Move assignment, leaves src with isNull().
  241. * The behavior is undefined if *this and src are the same object.
  242. *
  243. * Can be called explicitly, does not need C++11 support.
  244. * @param src source smart pointer
  245. * @return *this
  246. * @draft ICU 56
  247. */
  248. LocalPointer<T> &moveFrom(LocalPointer<T> &src) U_NOEXCEPT {
  249. delete LocalPointerBase<T>::ptr;
  250. LocalPointerBase<T>::ptr=src.ptr;
  251. src.ptr=NULL;
  252. return *this;
  253. }
  254. /**
  255. * Swap pointers.
  256. * @param other other smart pointer
  257. * @stable ICU 56
  258. */
  259. void swap(LocalPointer<T> &other) U_NOEXCEPT {
  260. T *temp=LocalPointerBase<T>::ptr;
  261. LocalPointerBase<T>::ptr=other.ptr;
  262. other.ptr=temp;
  263. }
  264. /**
  265. * Non-member LocalPointer swap function.
  266. * @param p1 will get p2's pointer
  267. * @param p2 will get p1's pointer
  268. * @stable ICU 56
  269. */
  270. friend inline void swap(LocalPointer<T> &p1, LocalPointer<T> &p2) U_NOEXCEPT {
  271. p1.swap(p2);
  272. }
  273. /**
  274. * Deletes the object it owns,
  275. * and adopts (takes ownership of) the one passed in.
  276. * @param p simple pointer to an object that is adopted
  277. * @stable ICU 4.4
  278. */
  279. void adoptInstead(T *p) {
  280. delete LocalPointerBase<T>::ptr;
  281. LocalPointerBase<T>::ptr=p;
  282. }
  283. /**
  284. * Deletes the object it owns,
  285. * and adopts (takes ownership of) the one passed in.
  286. *
  287. * If U_FAILURE(errorCode), then the current object is retained and the new one deleted.
  288. *
  289. * If U_SUCCESS(errorCode) but the input pointer is NULL,
  290. * then U_MEMORY_ALLOCATION_ERROR is set,
  291. * the current object is deleted, and NULL is set.
  292. *
  293. * @param p simple pointer to an object that is adopted
  294. * @param errorCode in/out UErrorCode, set to U_MEMORY_ALLOCATION_ERROR
  295. * if p==NULL and no other failure code had been set
  296. * @stable ICU 55
  297. */
  298. void adoptInsteadAndCheckErrorCode(T *p, UErrorCode &errorCode) {
  299. if(U_SUCCESS(errorCode)) {
  300. delete LocalPointerBase<T>::ptr;
  301. LocalPointerBase<T>::ptr=p;
  302. if(p==NULL) {
  303. errorCode=U_MEMORY_ALLOCATION_ERROR;
  304. }
  305. } else {
  306. delete p;
  307. }
  308. }
  309. };
  310. /**
  311. * "Smart pointer" class, deletes objects via the C++ array delete[] operator.
  312. * For most methods see the LocalPointerBase base class.
  313. * Adds operator[] for array item access.
  314. *
  315. * Usage example:
  316. * \code
  317. * LocalArray<UnicodeString> a(new UnicodeString[2]);
  318. * a[0].append((UChar)0x61);
  319. * if(some condition) { return; } // no need to explicitly delete the array
  320. * a.adoptInstead(new UnicodeString[4]);
  321. * a[3].append((UChar)0x62).append((UChar)0x63).reverse();
  322. * // no need to explicitly delete the array
  323. * \endcode
  324. *
  325. * @see LocalPointerBase
  326. * @stable ICU 4.4
  327. */
  328. template<typename T>
  329. class LocalArray : public LocalPointerBase<T> {
  330. public:
  331. using LocalPointerBase<T>::operator*;
  332. using LocalPointerBase<T>::operator->;
  333. /**
  334. * Constructor takes ownership.
  335. * @param p simple pointer to an array of T objects that is adopted
  336. * @stable ICU 4.4
  337. */
  338. explicit LocalArray(T *p=NULL) : LocalPointerBase<T>(p) {}
  339. /**
  340. * Constructor takes ownership and reports an error if NULL.
  341. *
  342. * This constructor is intended to be used with other-class constructors
  343. * that may report a failure UErrorCode,
  344. * so that callers need to check only for U_FAILURE(errorCode)
  345. * and not also separately for isNull().
  346. *
  347. * @param p simple pointer to an array of T objects that is adopted
  348. * @param errorCode in/out UErrorCode, set to U_MEMORY_ALLOCATION_ERROR
  349. * if p==NULL and no other failure code had been set
  350. * @stable ICU 56
  351. */
  352. LocalArray(T *p, UErrorCode &errorCode) : LocalPointerBase<T>(p) {
  353. if(p==NULL && U_SUCCESS(errorCode)) {
  354. errorCode=U_MEMORY_ALLOCATION_ERROR;
  355. }
  356. }
  357. #if U_HAVE_RVALUE_REFERENCES
  358. /**
  359. * Move constructor, leaves src with isNull().
  360. * @param src source smart pointer
  361. * @stable ICU 56
  362. */
  363. LocalArray(LocalArray<T> &&src) U_NOEXCEPT : LocalPointerBase<T>(src.ptr) {
  364. src.ptr=NULL;
  365. }
  366. #endif
  367. /**
  368. * Destructor deletes the array it owns.
  369. * @stable ICU 4.4
  370. */
  371. ~LocalArray() {
  372. delete[] LocalPointerBase<T>::ptr;
  373. }
  374. #if U_HAVE_RVALUE_REFERENCES
  375. /**
  376. * Move assignment operator, leaves src with isNull().
  377. * The behavior is undefined if *this and src are the same object.
  378. * @param src source smart pointer
  379. * @return *this
  380. * @stable ICU 56
  381. */
  382. LocalArray<T> &operator=(LocalArray<T> &&src) U_NOEXCEPT {
  383. return moveFrom(src);
  384. }
  385. #endif
  386. // do not use #ifndef U_HIDE_DRAFT_API for moveFrom, needed by non-draft API
  387. /**
  388. * Move assignment, leaves src with isNull().
  389. * The behavior is undefined if *this and src are the same object.
  390. *
  391. * Can be called explicitly, does not need C++11 support.
  392. * @param src source smart pointer
  393. * @return *this
  394. * @draft ICU 56
  395. */
  396. LocalArray<T> &moveFrom(LocalArray<T> &src) U_NOEXCEPT {
  397. delete[] LocalPointerBase<T>::ptr;
  398. LocalPointerBase<T>::ptr=src.ptr;
  399. src.ptr=NULL;
  400. return *this;
  401. }
  402. /**
  403. * Swap pointers.
  404. * @param other other smart pointer
  405. * @stable ICU 56
  406. */
  407. void swap(LocalArray<T> &other) U_NOEXCEPT {
  408. T *temp=LocalPointerBase<T>::ptr;
  409. LocalPointerBase<T>::ptr=other.ptr;
  410. other.ptr=temp;
  411. }
  412. /**
  413. * Non-member LocalArray swap function.
  414. * @param p1 will get p2's pointer
  415. * @param p2 will get p1's pointer
  416. * @stable ICU 56
  417. */
  418. friend inline void swap(LocalArray<T> &p1, LocalArray<T> &p2) U_NOEXCEPT {
  419. p1.swap(p2);
  420. }
  421. /**
  422. * Deletes the array it owns,
  423. * and adopts (takes ownership of) the one passed in.
  424. * @param p simple pointer to an array of T objects that is adopted
  425. * @stable ICU 4.4
  426. */
  427. void adoptInstead(T *p) {
  428. delete[] LocalPointerBase<T>::ptr;
  429. LocalPointerBase<T>::ptr=p;
  430. }
  431. /**
  432. * Deletes the array it owns,
  433. * and adopts (takes ownership of) the one passed in.
  434. *
  435. * If U_FAILURE(errorCode), then the current array is retained and the new one deleted.
  436. *
  437. * If U_SUCCESS(errorCode) but the input pointer is NULL,
  438. * then U_MEMORY_ALLOCATION_ERROR is set,
  439. * the current array is deleted, and NULL is set.
  440. *
  441. * @param p simple pointer to an array of T objects that is adopted
  442. * @param errorCode in/out UErrorCode, set to U_MEMORY_ALLOCATION_ERROR
  443. * if p==NULL and no other failure code had been set
  444. * @stable ICU 56
  445. */
  446. void adoptInsteadAndCheckErrorCode(T *p, UErrorCode &errorCode) {
  447. if(U_SUCCESS(errorCode)) {
  448. delete[] LocalPointerBase<T>::ptr;
  449. LocalPointerBase<T>::ptr=p;
  450. if(p==NULL) {
  451. errorCode=U_MEMORY_ALLOCATION_ERROR;
  452. }
  453. } else {
  454. delete[] p;
  455. }
  456. }
  457. /**
  458. * Array item access (writable).
  459. * No index bounds check.
  460. * @param i array index
  461. * @return reference to the array item
  462. * @stable ICU 4.4
  463. */
  464. T &operator[](ptrdiff_t i) const { return LocalPointerBase<T>::ptr[i]; }
  465. };
  466. /**
  467. * \def U_DEFINE_LOCAL_OPEN_POINTER
  468. * "Smart pointer" definition macro, deletes objects via the closeFunction.
  469. * Defines a subclass of LocalPointerBase which works just
  470. * like LocalPointer<Type> except that this subclass will use the closeFunction
  471. * rather than the C++ delete operator.
  472. *
  473. * Usage example:
  474. * \code
  475. * LocalUCaseMapPointer csm(ucasemap_open(localeID, options, &errorCode));
  476. * utf8OutLength=ucasemap_utf8ToLower(csm.getAlias(),
  477. * utf8Out, (int32_t)sizeof(utf8Out),
  478. * utf8In, utf8InLength, &errorCode);
  479. * if(U_FAILURE(errorCode)) { return; } // no need to explicitly delete the UCaseMap
  480. * \endcode
  481. *
  482. * @see LocalPointerBase
  483. * @see LocalPointer
  484. * @stable ICU 4.4
  485. */
  486. #if U_HAVE_RVALUE_REFERENCES
  487. #define U_DEFINE_LOCAL_OPEN_POINTER(LocalPointerClassName, Type, closeFunction) \
  488. class LocalPointerClassName : public LocalPointerBase<Type> { \
  489. public: \
  490. using LocalPointerBase<Type>::operator*; \
  491. using LocalPointerBase<Type>::operator->; \
  492. explicit LocalPointerClassName(Type *p=NULL) : LocalPointerBase<Type>(p) {} \
  493. LocalPointerClassName(LocalPointerClassName &&src) U_NOEXCEPT \
  494. : LocalPointerBase<Type>(src.ptr) { \
  495. src.ptr=NULL; \
  496. } \
  497. ~LocalPointerClassName() { if (ptr != NULL) { closeFunction(ptr); } } \
  498. LocalPointerClassName &operator=(LocalPointerClassName &&src) U_NOEXCEPT { \
  499. return moveFrom(src); \
  500. } \
  501. LocalPointerClassName &moveFrom(LocalPointerClassName &src) U_NOEXCEPT { \
  502. if (ptr != NULL) { closeFunction(ptr); } \
  503. LocalPointerBase<Type>::ptr=src.ptr; \
  504. src.ptr=NULL; \
  505. return *this; \
  506. } \
  507. void swap(LocalPointerClassName &other) U_NOEXCEPT { \
  508. Type *temp=LocalPointerBase<Type>::ptr; \
  509. LocalPointerBase<Type>::ptr=other.ptr; \
  510. other.ptr=temp; \
  511. } \
  512. friend inline void swap(LocalPointerClassName &p1, LocalPointerClassName &p2) U_NOEXCEPT { \
  513. p1.swap(p2); \
  514. } \
  515. void adoptInstead(Type *p) { \
  516. if (ptr != NULL) { closeFunction(ptr); } \
  517. ptr=p; \
  518. } \
  519. }
  520. #else
  521. #define U_DEFINE_LOCAL_OPEN_POINTER(LocalPointerClassName, Type, closeFunction) \
  522. class LocalPointerClassName : public LocalPointerBase<Type> { \
  523. public: \
  524. using LocalPointerBase<Type>::operator*; \
  525. using LocalPointerBase<Type>::operator->; \
  526. explicit LocalPointerClassName(Type *p=NULL) : LocalPointerBase<Type>(p) {} \
  527. ~LocalPointerClassName() { closeFunction(ptr); } \
  528. LocalPointerClassName &moveFrom(LocalPointerClassName &src) U_NOEXCEPT { \
  529. if (ptr != NULL) { closeFunction(ptr); } \
  530. LocalPointerBase<Type>::ptr=src.ptr; \
  531. src.ptr=NULL; \
  532. return *this; \
  533. } \
  534. void swap(LocalPointerClassName &other) U_NOEXCEPT { \
  535. Type *temp=LocalPointerBase<Type>::ptr; \
  536. LocalPointerBase<Type>::ptr=other.ptr; \
  537. other.ptr=temp; \
  538. } \
  539. friend inline void swap(LocalPointerClassName &p1, LocalPointerClassName &p2) U_NOEXCEPT { \
  540. p1.swap(p2); \
  541. } \
  542. void adoptInstead(Type *p) { \
  543. if (ptr != NULL) { closeFunction(ptr); } \
  544. ptr=p; \
  545. } \
  546. }
  547. #endif
  548. U_NAMESPACE_END
  549. #endif /* U_SHOW_CPLUSPLUS_API */
  550. #endif /* __LOCALPOINTER_H__ */