00001 #ifndef PROTON_INTERNAL_UNIQUE_PTR_HPP
00002 #define PROTON_INTERNAL_UNIQUE_PTR_HPP
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "./config.hpp"
00026
00027 #include <memory>
00028
00029 namespace proton {
00030 namespace internal {
00031
00040 template <class T> class pn_unique_ptr {
00041 public:
00042 pn_unique_ptr(T* p=0) : ptr_(p) {}
00043 #if PN_CPP_HAS_RVALUE_REFERENCES
00044 pn_unique_ptr(pn_unique_ptr&& x) : ptr_(0) { std::swap(ptr_, x.ptr_); }
00045 #else
00046 pn_unique_ptr(const pn_unique_ptr& x) : ptr_() { std::swap(ptr_, const_cast<pn_unique_ptr&>(x).ptr_); }
00047 #endif
00048 ~pn_unique_ptr() { delete(ptr_); }
00049 T& operator*() const { return *ptr_; }
00050 T* operator->() const { return ptr_; }
00051 T* get() const { return ptr_; }
00052 void reset(T* p = 0) { pn_unique_ptr<T> tmp(p); std::swap(ptr_, tmp.ptr_); }
00053 T* release() { T *p = ptr_; ptr_ = 0; return p; }
00054 #if PN_CPP_HAS_EXPLICIT_CONVERSIONS
00055 explicit operator bool() const { return get(); }
00056 #endif
00057 bool operator !() const { return !get(); }
00058
00059 #if PN_CPP_HAS_UNIQUE_PTR
00060 operator std::unique_ptr<T>() { T *p = ptr_; ptr_ = 0; return std::unique_ptr<T>(p); }
00061 #endif
00062
00063 private:
00064 T* ptr_;
00065 };
00066
00067 }
00068 }
00069
00070 #endif // PROTON_INTERNAL_UNIQUE_PTR_HPP