localpointer.h 19 KB

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