#if U_SHOW_CPLUSPLUS_API
+#include <memory>
+
U_NAMESPACE_BEGIN
/**
template<typename T>
class LocalPointerBase {
public:
+ // No heap allocation. Use only on the stack.
+ static void* U_EXPORT2 operator new(size_t) = delete;
+ static void* U_EXPORT2 operator new[](size_t) = delete;
+#if U_HAVE_PLACEMENT_NEW
+ static void* U_EXPORT2 operator new(size_t, void*) = delete;
+#endif
+
/**
* Constructor takes ownership.
* @param p simple pointer to an object that is adopted
// No ownership sharing: No copy constructor, no assignment operator.
LocalPointerBase(const LocalPointerBase<T> &other);
void operator=(const LocalPointerBase<T> &other);
- // No heap allocation. Use only on the stack.
- static void * U_EXPORT2 operator new(size_t size);
- static void * U_EXPORT2 operator new[](size_t size);
-#if U_HAVE_PLACEMENT_NEW
- static void * U_EXPORT2 operator new(size_t, void *ptr);
-#endif
};
/**
errorCode=U_MEMORY_ALLOCATION_ERROR;
}
}
-#if U_HAVE_RVALUE_REFERENCES
/**
* Move constructor, leaves src with isNull().
* @param src source smart pointer
LocalPointer(LocalPointer<T> &&src) U_NOEXCEPT : LocalPointerBase<T>(src.ptr) {
src.ptr=NULL;
}
-#endif
+
+#ifndef U_HIDE_DRAFT_API
+ /**
+ * Constructs a LocalPointer from a C++11 std::unique_ptr.
+ * The LocalPointer steals the object owned by the std::unique_ptr.
+ *
+ * This constructor works via move semantics. If your std::unique_ptr is
+ * in a local variable, you must use std::move.
+ *
+ * @param p The std::unique_ptr from which the pointer will be stolen.
+ * @draft ICU 64
+ */
+ explicit LocalPointer(std::unique_ptr<T> &&p)
+ : LocalPointerBase<T>(p.release()) {}
+#endif /* U_HIDE_DRAFT_API */
+
/**
* Destructor deletes the object it owns.
* @stable ICU 4.4
~LocalPointer() {
delete LocalPointerBase<T>::ptr;
}
-#if U_HAVE_RVALUE_REFERENCES
/**
* Move assignment operator, leaves src with isNull().
* The behavior is undefined if *this and src are the same object.
* @stable ICU 56
*/
LocalPointer<T> &operator=(LocalPointer<T> &&src) U_NOEXCEPT {
- return moveFrom(src);
+ delete LocalPointerBase<T>::ptr;
+ LocalPointerBase<T>::ptr=src.ptr;
+ src.ptr=NULL;
+ return *this;
}
-#endif
- // do not use #ifndef U_HIDE_DRAFT_API for moveFrom, needed by non-draft API
+
+#ifndef U_HIDE_DRAFT_API
/**
- * Move assignment, leaves src with isNull().
- * The behavior is undefined if *this and src are the same object.
+ * Move-assign from an std::unique_ptr to this LocalPointer.
+ * Steals the pointer from the std::unique_ptr.
*
- * Can be called explicitly, does not need C++11 support.
- * @param src source smart pointer
+ * @param p The std::unique_ptr from which the pointer will be stolen.
* @return *this
- * @draft ICU 56
+ * @draft ICU 64
*/
- LocalPointer<T> &moveFrom(LocalPointer<T> &src) U_NOEXCEPT {
- delete LocalPointerBase<T>::ptr;
- LocalPointerBase<T>::ptr=src.ptr;
- src.ptr=NULL;
+ LocalPointer<T> &operator=(std::unique_ptr<T> &&p) U_NOEXCEPT {
+ adoptInstead(p.release());
return *this;
}
+#endif /* U_HIDE_DRAFT_API */
+
/**
* Swap pointers.
* @param other other smart pointer
delete p;
}
}
+
+#ifndef U_HIDE_DRAFT_API
+ /**
+ * Conversion operator to a C++11 std::unique_ptr.
+ * Disowns the object and gives it to the returned std::unique_ptr.
+ *
+ * This operator works via move semantics. If your LocalPointer is
+ * in a local variable, you must use std::move.
+ *
+ * @return An std::unique_ptr owning the pointer previously owned by this
+ * icu::LocalPointer.
+ * @draft ICU 64
+ */
+ operator std::unique_ptr<T> () && {
+ return std::unique_ptr<T>(LocalPointerBase<T>::orphan());
+ }
+#endif /* U_HIDE_DRAFT_API */
};
/**
errorCode=U_MEMORY_ALLOCATION_ERROR;
}
}
-#if U_HAVE_RVALUE_REFERENCES
/**
* Move constructor, leaves src with isNull().
* @param src source smart pointer
LocalArray(LocalArray<T> &&src) U_NOEXCEPT : LocalPointerBase<T>(src.ptr) {
src.ptr=NULL;
}
-#endif
+
+#ifndef U_HIDE_DRAFT_API
+ /**
+ * Constructs a LocalArray from a C++11 std::unique_ptr of an array type.
+ * The LocalPointer steals the array owned by the std::unique_ptr.
+ *
+ * This constructor works via move semantics. If your std::unique_ptr is
+ * in a local variable, you must use std::move.
+ *
+ * @param p The std::unique_ptr from which the array will be stolen.
+ * @draft ICU 64
+ */
+ explicit LocalArray(std::unique_ptr<T[]> &&p)
+ : LocalPointerBase<T>(p.release()) {}
+#endif /* U_HIDE_DRAFT_API */
+
/**
* Destructor deletes the array it owns.
* @stable ICU 4.4
~LocalArray() {
delete[] LocalPointerBase<T>::ptr;
}
-#if U_HAVE_RVALUE_REFERENCES
/**
* Move assignment operator, leaves src with isNull().
* The behavior is undefined if *this and src are the same object.
* @stable ICU 56
*/
LocalArray<T> &operator=(LocalArray<T> &&src) U_NOEXCEPT {
- return moveFrom(src);
+ delete[] LocalPointerBase<T>::ptr;
+ LocalPointerBase<T>::ptr=src.ptr;
+ src.ptr=NULL;
+ return *this;
}
-#endif
- // do not use #ifndef U_HIDE_DRAFT_API for moveFrom, needed by non-draft API
+
+#ifndef U_HIDE_DRAFT_API
/**
- * Move assignment, leaves src with isNull().
- * The behavior is undefined if *this and src are the same object.
+ * Move-assign from an std::unique_ptr to this LocalPointer.
+ * Steals the array from the std::unique_ptr.
*
- * Can be called explicitly, does not need C++11 support.
- * @param src source smart pointer
+ * @param p The std::unique_ptr from which the array will be stolen.
* @return *this
- * @draft ICU 56
+ * @draft ICU 64
*/
- LocalArray<T> &moveFrom(LocalArray<T> &src) U_NOEXCEPT {
- delete[] LocalPointerBase<T>::ptr;
- LocalPointerBase<T>::ptr=src.ptr;
- src.ptr=NULL;
+ LocalArray<T> &operator=(std::unique_ptr<T[]> &&p) U_NOEXCEPT {
+ adoptInstead(p.release());
return *this;
}
+#endif /* U_HIDE_DRAFT_API */
+
/**
* Swap pointers.
* @param other other smart pointer
* @stable ICU 4.4
*/
T &operator[](ptrdiff_t i) const { return LocalPointerBase<T>::ptr[i]; }
+
+#ifndef U_HIDE_DRAFT_API
+ /**
+ * Conversion operator to a C++11 std::unique_ptr.
+ * Disowns the object and gives it to the returned std::unique_ptr.
+ *
+ * This operator works via move semantics. If your LocalPointer is
+ * in a local variable, you must use std::move.
+ *
+ * @return An std::unique_ptr owning the pointer previously owned by this
+ * icu::LocalPointer.
+ * @draft ICU 64
+ */
+ operator std::unique_ptr<T[]> () && {
+ return std::unique_ptr<T[]>(LocalPointerBase<T>::orphan());
+ }
+#endif /* U_HIDE_DRAFT_API */
};
/**
* @see LocalPointer
* @stable ICU 4.4
*/
-#if U_HAVE_RVALUE_REFERENCES
#define U_DEFINE_LOCAL_OPEN_POINTER(LocalPointerClassName, Type, closeFunction) \
class LocalPointerClassName : public LocalPointerBase<Type> { \
public: \
: LocalPointerBase<Type>(src.ptr) { \
src.ptr=NULL; \
} \
+ /* TODO: Be agnostic of the deleter function signature from the user-provided std::unique_ptr? */ \
+ explicit LocalPointerClassName(std::unique_ptr<Type, decltype(&closeFunction)> &&p) \
+ : LocalPointerBase<Type>(p.release()) {} \
~LocalPointerClassName() { if (ptr != NULL) { closeFunction(ptr); } } \
LocalPointerClassName &operator=(LocalPointerClassName &&src) U_NOEXCEPT { \
- return moveFrom(src); \
- } \
- LocalPointerClassName &moveFrom(LocalPointerClassName &src) U_NOEXCEPT { \
if (ptr != NULL) { closeFunction(ptr); } \
LocalPointerBase<Type>::ptr=src.ptr; \
src.ptr=NULL; \
return *this; \
} \
- void swap(LocalPointerClassName &other) U_NOEXCEPT { \
- Type *temp=LocalPointerBase<Type>::ptr; \
- LocalPointerBase<Type>::ptr=other.ptr; \
- other.ptr=temp; \
- } \
- friend inline void swap(LocalPointerClassName &p1, LocalPointerClassName &p2) U_NOEXCEPT { \
- p1.swap(p2); \
- } \
- void adoptInstead(Type *p) { \
- if (ptr != NULL) { closeFunction(ptr); } \
- ptr=p; \
- } \
- }
-#else
-#define U_DEFINE_LOCAL_OPEN_POINTER(LocalPointerClassName, Type, closeFunction) \
- class LocalPointerClassName : public LocalPointerBase<Type> { \
- public: \
- using LocalPointerBase<Type>::operator*; \
- using LocalPointerBase<Type>::operator->; \
- explicit LocalPointerClassName(Type *p=NULL) : LocalPointerBase<Type>(p) {} \
- ~LocalPointerClassName() { closeFunction(ptr); } \
- LocalPointerClassName &moveFrom(LocalPointerClassName &src) U_NOEXCEPT { \
- if (ptr != NULL) { closeFunction(ptr); } \
- LocalPointerBase<Type>::ptr=src.ptr; \
- src.ptr=NULL; \
+ /* TODO: Be agnostic of the deleter function signature from the user-provided std::unique_ptr? */ \
+ LocalPointerClassName &operator=(std::unique_ptr<Type, decltype(&closeFunction)> &&p) { \
+ adoptInstead(p.release()); \
return *this; \
} \
void swap(LocalPointerClassName &other) U_NOEXCEPT { \
if (ptr != NULL) { closeFunction(ptr); } \
ptr=p; \
} \
+ operator std::unique_ptr<Type, decltype(&closeFunction)> () && { \
+ return std::unique_ptr<Type, decltype(&closeFunction)>(LocalPointerBase<Type>::orphan(), closeFunction); \
+ } \
}
-#endif
U_NAMESPACE_END