diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2012-02-29 10:57:43 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2012-02-29 10:57:43 +0200 |
commit | 40466e02c3ab7ef31183158103e3ef7536248753 (patch) | |
tree | db4d542aae19b94e9be00636e9cf5b75c5c8cd7a | |
parent | 35a2460893ddfd006d9d85dcc6ab7a01c1ab9848 (diff) |
Support for C++11 std::unique_ptr as object pointer
This includes the odb::lazy_unique_ptr implementation.
-rw-r--r-- | odb/lazy-pointer-traits.hxx | 29 | ||||
-rw-r--r-- | odb/lazy-ptr.hxx | 160 | ||||
-rw-r--r-- | odb/lazy-ptr.ixx | 423 | ||||
-rw-r--r-- | odb/lazy-ptr.txx | 35 | ||||
-rw-r--r-- | odb/pointer-traits.hxx | 55 |
5 files changed, 645 insertions, 57 deletions
diff --git a/odb/lazy-pointer-traits.hxx b/odb/lazy-pointer-traits.hxx index 910e1e4..5273db1 100644 --- a/odb/lazy-pointer-traits.hxx +++ b/odb/lazy-pointer-traits.hxx @@ -64,8 +64,33 @@ namespace odb }; #ifdef ODB_CXX11 + template <typename T, typename D> + class pointer_traits<lazy_unique_ptr<T, D>> + { + public: + static const pointer_kind kind = pk_unique; + static const bool lazy = true; + + typedef T element_type; + typedef lazy_unique_ptr<element_type, D> pointer_type; + typedef std::unique_ptr<element_type, D> eager_pointer_type; + + static bool + null_ptr (const pointer_type& p) + { + return !p; + } + + template <class O /* = T */> + static typename object_traits<O>::id_type + object_id (const pointer_type& p) + { + return p.object_id<O> (); + } + }; + template <typename T> - class pointer_traits<lazy_shared_ptr<T> > + class pointer_traits<lazy_shared_ptr<T>> { public: static const pointer_kind kind = pk_shared; @@ -90,7 +115,7 @@ namespace odb }; template <typename T> - class pointer_traits<lazy_weak_ptr<T> > + class pointer_traits<lazy_weak_ptr<T>> { public: static const pointer_kind kind = pk_weak; diff --git a/odb/lazy-ptr.hxx b/odb/lazy-ptr.hxx index c663d4a..0e44066 100644 --- a/odb/lazy-ptr.hxx +++ b/odb/lazy-ptr.hxx @@ -101,13 +101,13 @@ namespace odb // operator< and operator<< are not provided. // - template<class T, class Y> + template <class T, class Y> bool operator== (const lazy_ptr<T>&, const lazy_ptr<Y>&); - template<class T, class Y> + template <class T, class Y> bool operator!= (const lazy_ptr<T>&, const lazy_ptr<Y>&); - template<class T> void swap (lazy_ptr<T>&, lazy_ptr<T>&); + template <class T> void swap (lazy_ptr<T>&, lazy_ptr<T>&); // std::auto_ptr lazy version. // @@ -130,10 +130,10 @@ namespace odb explicit lazy_auto_ptr (T* = 0); lazy_auto_ptr (lazy_auto_ptr&); - template<class Y> lazy_auto_ptr (lazy_auto_ptr<Y>&); + template <class Y> lazy_auto_ptr (lazy_auto_ptr<Y>&); lazy_auto_ptr& operator= (lazy_auto_ptr&); - template<class Y> lazy_auto_ptr& operator= (lazy_auto_ptr<Y>&); + template <class Y> lazy_auto_ptr& operator= (lazy_auto_ptr<Y>&); T& operator* () const; T* operator-> () const; @@ -143,8 +143,8 @@ namespace odb lazy_auto_ptr (const lazy_auto_ptr_ref<T>&); lazy_auto_ptr& operator= (const lazy_auto_ptr_ref<T>&); - template<class Y> operator lazy_auto_ptr_ref<Y> (); - template<class Y> operator lazy_auto_ptr<Y> (); + template <class Y> operator lazy_auto_ptr_ref<Y> (); + template <class Y> operator lazy_auto_ptr<Y> (); // Extension: conversion to bool. // @@ -216,6 +216,133 @@ namespace odb #ifdef ODB_CXX11 + // C++11 std::unique_ptr lazy version. + // + template <class T, class D = std::default_delete<T>> + class lazy_unique_ptr + { + // Standard lazy_unique_ptr interface. + // + public: + typedef T* pointer; // For now assume it is T*. + typedef T element_type; + typedef D deleter_type; + + /*constexpr*/ lazy_unique_ptr () /*noexcept*/; + /*constexpr*/ lazy_unique_ptr (std::nullptr_t) /*noexcept*/; + explicit lazy_unique_ptr (pointer) /*noexcept*/; + + // For now assume D is non-reference. + // + lazy_unique_ptr (pointer, const deleter_type&) /*noexcept*/; + lazy_unique_ptr (pointer, deleter_type&&) /*noexcept*/; + + lazy_unique_ptr (lazy_unique_ptr&&) /*noexcept*/; + template <class T1, class D1> lazy_unique_ptr (lazy_unique_ptr<T1, D1>&&) /*noexcept*/; + template <class T1> lazy_unique_ptr (std::auto_ptr<T1>&&) /*noexcept*/; + + lazy_unique_ptr& operator= (std::nullptr_t) /*noexcept*/; + lazy_unique_ptr& operator= (lazy_unique_ptr&&) /*noexcept*/; + template <class T1, class D1> lazy_unique_ptr& operator= (lazy_unique_ptr<T1, D1>&&) /*noexcept*/; + + T& operator* () const; + pointer operator-> () const /*noexcept*/; + pointer get () const /*noexcept*/; + explicit operator bool() const /*noexcept*/; + + pointer release () /*noexcept*/; + void reset (pointer = pointer ()) /*noexcept*/; + void swap (lazy_unique_ptr&) /*noexcept*/; + + deleter_type& get_deleter () /*noexcept*/; + const deleter_type& get_deleter () const /*noexcept*/; + + lazy_unique_ptr (const lazy_unique_ptr&) = delete; + lazy_unique_ptr& operator= (const lazy_unique_ptr&) = delete; + + // Initialization/assignment from unique_ptr. + // + public: + template <class T1, class D1> lazy_unique_ptr (std::unique_ptr<T1, D1>&&) /*noexcept*/; + template <class T1, class D1> lazy_unique_ptr& operator= (std::unique_ptr<T1, D1>&&) /*noexcept*/; + + // Lazy loading interface. + // + public: + typedef odb::database database_type; + + // NULL loaded() + // + // true true NULL pointer to transient object + // false true valid pointer to persistent object + // true false unloaded pointer to persistent object + // false false valid pointer to transient object + // + bool loaded () const; + + std::unique_ptr<T, D>& load () const; + + // Unload the pointer. For transient objects this function is + // equivalent to reset(). + // + void unload () const; + + template <class ID> lazy_unique_ptr (database_type&, const ID&); + lazy_unique_ptr (database_type&, pointer); + lazy_unique_ptr (database_type&, pointer, const deleter_type&); + lazy_unique_ptr (database_type&, pointer, deleter_type&&); + template <class T1, class D1> lazy_unique_ptr (database_type&, std::unique_ptr<T1, D1>&&); + template <class T1> lazy_unique_ptr (database_type&, std::auto_ptr<T1>&&); + + template <class ID> void reset (database_type&, const ID&); + void reset (database_type&, pointer); + template <class T1, class D1> void reset (database_type&, std::unique_ptr<T1, D1>&&); + template <class T1> void reset (database_type&, std::auto_ptr<T1>&&); + + template <class O = T> + typename object_traits<O>::id_type object_id () const; + + database_type& database () const; + + // Helpers. + // + public: + template <class T1, class D1> bool equal (const lazy_unique_ptr<T1, D1>&) const; + + private: + template <class T1, class D1> friend class lazy_unique_ptr; + + // Note that it is possible to have a situation where p_ is NULL, + // i_.id is NULL and i_.db is not NULL. This will happen if the + // unique_ptr reference returned by load() is transferred to + // another pointer or reset. + // + mutable std::unique_ptr<T, D> p_; + mutable lazy_ptr_impl<T> i_; + }; + + template <class T> void swap (lazy_unique_ptr<T>&, lazy_unique_ptr<T>&) /*noexcept*/; + + // operator< and operator<< are not provided. + // + template <class T1, class D1, class T2, class D2> + bool operator== (const lazy_unique_ptr<T1, D1>&, const lazy_unique_ptr<T2, D2>&); + + template <class T, class D> + bool operator== (const lazy_unique_ptr<T, D>&, std::nullptr_t) /*noexcept*/; + + template <class T, class D> + bool operator== (std::nullptr_t, const lazy_unique_ptr<T, D>&) /*noexcept*/; + + template <class T1, class D1, class T2, class D2> + bool operator!= (const lazy_unique_ptr<T1, D1>&, const lazy_unique_ptr<T2, D2>&); + + template <class T, class D> + bool operator!= (const lazy_unique_ptr<T, D>&, std::nullptr_t) /*noexcept*/; + + template <class T, class D> + bool operator!= (std::nullptr_t, const lazy_unique_ptr<T, D>&) /*noexcept*/; + // C++11 std::shared_ptr lazy version. // template <class T> @@ -337,29 +464,29 @@ namespace odb mutable lazy_ptr_impl<T> i_; }; - template<class T> void swap (lazy_shared_ptr<T>&, lazy_shared_ptr<T>&) /*noexcept*/; + template <class T> void swap (lazy_shared_ptr<T>&, lazy_shared_ptr<T>&) /*noexcept*/; - template<class D, class T> + template <class D, class T> D* get_deleter (const lazy_shared_ptr<T>&) /*noexcept*/; // operator< and operator<< are not provided. // - template<class T, class Y> + template <class T, class Y> bool operator== (const lazy_shared_ptr<T>&, const lazy_shared_ptr<Y>&) /*noexcept*/; - template<class T> + template <class T> bool operator== (const lazy_shared_ptr<T>&, std::nullptr_t) /*noexcept*/; - template<class T> + template <class T> bool operator== (std::nullptr_t, const lazy_shared_ptr<T>&) /*noexcept*/; - template<class T, class Y> + template <class T, class Y> bool operator!= (const lazy_shared_ptr<T>&, const lazy_shared_ptr<Y>&) /*noexcept*/; - template<class T> + template <class T> bool operator!= (const lazy_shared_ptr<T>&, std::nullptr_t) /*noexcept*/; - template<class T> + template <class T> bool operator!= (std::nullptr_t, const lazy_shared_ptr<T>&) /*noexcept*/; // C++11 std::weak_ptr lazy version. @@ -451,7 +578,7 @@ namespace odb // operator< is not provided. // - template<class T> void swap (lazy_weak_ptr<T>&, lazy_weak_ptr<T>&); + template <class T> void swap (lazy_weak_ptr<T>&, lazy_weak_ptr<T>&); #endif // ODB_CXX11 @@ -461,6 +588,7 @@ namespace odb using odb::lazy_auto_ptr; #ifdef ODB_CXX11 + using odb::lazy_unique_ptr; using odb::lazy_shared_ptr; using odb::lazy_weak_ptr; #endif diff --git a/odb/lazy-ptr.ixx b/odb/lazy-ptr.ixx index de82a89..5c67845 100644 --- a/odb/lazy-ptr.ixx +++ b/odb/lazy-ptr.ixx @@ -190,21 +190,21 @@ namespace odb return *i_.database (); } - template<class T, class Y> + template <class T, class Y> inline bool operator== (const lazy_ptr<T>& a, const lazy_ptr<Y>& b) { return a.equal (b); } - template<class T, class Y> + template <class T, class Y> inline bool operator!= (const lazy_ptr<T>& a, const lazy_ptr<Y>& b) { return !a.equal (b); } - template<class T> + template <class T> inline void swap (lazy_ptr<T>& a, lazy_ptr<T>& b) { @@ -215,7 +215,7 @@ namespace odb // lazy_auto_ptr_ref // - template<class T> + template <class T> inline lazy_auto_ptr_ref<T>:: lazy_auto_ptr_ref (T* p, const lazy_ptr_impl_ref& i): p_ (p), i_ (i) {} @@ -223,26 +223,26 @@ namespace odb // lazy_auto_ptr // - template<class T> + template <class T> inline lazy_auto_ptr<T>:: lazy_auto_ptr (T* p): p_ (p) {} - template<class T> + template <class T> inline lazy_auto_ptr<T>:: lazy_auto_ptr (lazy_auto_ptr& r) : p_ (r.p_), i_ (static_cast<lazy_ptr_impl_ref> (r.i_)) { } - template<class T> - template<class Y> + template <class T> + template <class Y> inline lazy_auto_ptr<T>:: lazy_auto_ptr (lazy_auto_ptr<Y>& r) : p_ (r.p_), i_ (static_cast<lazy_ptr_impl_ref> (r.i_)) { } - template<class T> + template <class T> inline lazy_auto_ptr<T>& lazy_auto_ptr<T>:: operator= (lazy_auto_ptr& r) { @@ -251,8 +251,8 @@ namespace odb return *this; } - template<class T> - template<class Y> + template <class T> + template <class Y> inline lazy_auto_ptr<T>& lazy_auto_ptr<T>:: operator= (lazy_auto_ptr<Y>& r) { @@ -261,28 +261,28 @@ namespace odb return *this; } - template<class T> + template <class T> inline T& lazy_auto_ptr<T>:: operator* () const { return *p_; } - template<class T> + template <class T> inline T* lazy_auto_ptr<T>:: operator-> () const { return p_.operator-> (); } - template<class T> + template <class T> inline T* lazy_auto_ptr<T>:: get () const { return p_.get (); } - template<class T> + template <class T> inline T* lazy_auto_ptr<T>:: release () { @@ -290,7 +290,7 @@ namespace odb return p_.release (); } - template<class T> + template <class T> inline void lazy_auto_ptr<T>:: reset (T* p) { @@ -298,11 +298,11 @@ namespace odb p_.reset (p); } - template<class T> + template <class T> inline lazy_auto_ptr<T>:: lazy_auto_ptr (const lazy_auto_ptr_ref<T>& r): p_ (r.p_), i_ (r.i_) {} - template<class T> + template <class T> inline lazy_auto_ptr<T>& lazy_auto_ptr<T>:: operator= (const lazy_auto_ptr_ref<T>& r) { @@ -313,32 +313,32 @@ namespace odb return *this; } - template<class T> - template<class Y> + template <class T> + template <class Y> inline lazy_auto_ptr<T>:: operator lazy_auto_ptr_ref<Y> () { return lazy_auto_ptr_ref<Y> (p_.release (), i_); } - template<class T> - template<class Y> + template <class T> + template <class Y> inline lazy_auto_ptr<T>:: operator lazy_auto_ptr<Y> () { return lazy_auto_ptr<Y> (*this); } - template<class T> + template <class T> template <class Y> inline lazy_auto_ptr<T>:: lazy_auto_ptr (std::auto_ptr<Y>& r): p_ (r) {} - template<class T> + template <class T> inline lazy_auto_ptr<T>:: lazy_auto_ptr (std::auto_ptr_ref<T> r): p_ (r) {} - template<class T> + template <class T> template <class Y> inline lazy_auto_ptr<T>& lazy_auto_ptr<T>:: operator= (std::auto_ptr<Y>& r) @@ -348,7 +348,7 @@ namespace odb return *this; } - template<class T> + template <class T> inline lazy_auto_ptr<T>& lazy_auto_ptr<T>:: operator= (std::auto_ptr_ref<T> r) { @@ -413,7 +413,7 @@ namespace odb lazy_auto_ptr (database_type& db, std::auto_ptr<Y>& p) : p_ (p) { - if (p) + if (p_.get () != 0) i_.reset (db); } @@ -473,6 +473,357 @@ namespace odb #ifdef ODB_CXX11 // + // lazy_unique_ptr + // + + template <class T, class D> + lazy_unique_ptr<T, D>:: + lazy_unique_ptr () {} + + template <class T, class D> + lazy_unique_ptr<T, D>:: + lazy_unique_ptr (std::nullptr_t) {} + + template <class T, class D> + lazy_unique_ptr<T, D>:: + lazy_unique_ptr (pointer p): p_ (p) {} + + template <class T, class D> + lazy_unique_ptr<T, D>:: + lazy_unique_ptr (pointer p, const deleter_type& d): p_ (p, d) {} + + template <class T, class D> + lazy_unique_ptr<T, D>:: + lazy_unique_ptr (pointer p, deleter_type&& d): p_ (p, std::move (d)) {} + + template <class T, class D> + lazy_unique_ptr<T, D>:: + lazy_unique_ptr (lazy_unique_ptr&& r) + : p_ (std::move (r.p_)), i_ (std::move (r.i_)) {} + + template <class T, class D> + template <class T1, class D1> + lazy_unique_ptr<T, D>:: + lazy_unique_ptr (lazy_unique_ptr<T1, D1>&& r) + : p_ (std::move (r.p_)), i_ (std::move (r.i_)) {} + + template <class T, class D> + template <class T1> + lazy_unique_ptr<T, D>:: + lazy_unique_ptr (std::auto_ptr<T1>&& r): p_ (std::move (r)) {} + + template <class T, class D> + lazy_unique_ptr<T, D>& lazy_unique_ptr<T, D>:: + operator= (std::nullptr_t) + { + reset (); + return *this; + } + + template <class T, class D> + lazy_unique_ptr<T, D>& lazy_unique_ptr<T, D>:: + operator= (lazy_unique_ptr&& r) + { + p_ = std::move (r.p_); + i_ = std::move (r.i_); + return *this; + } + + template <class T, class D> + template <class T1, class D1> + lazy_unique_ptr<T, D>& lazy_unique_ptr<T, D>:: + operator= (lazy_unique_ptr<T1, D1>&& r) + { + p_ = std::move (r.p_); + i_ = std::move (r.i_); + return *this; + } + + template <class T, class D> + T& lazy_unique_ptr<T, D>:: + operator* () const + { + return *p_; + } + + template <class T, class D> + typename lazy_unique_ptr<T, D>::pointer lazy_unique_ptr<T, D>:: + operator-> () const + { + return p_.operator-> (); + } + + template <class T, class D> + typename lazy_unique_ptr<T, D>::pointer lazy_unique_ptr<T, D>:: + get () const + { + return p_.get (); + } + + template <class T, class D> + lazy_unique_ptr<T, D>:: + operator bool() const + { + return p_ || i_; + } + + template <class T, class D> + typename lazy_unique_ptr<T, D>::pointer lazy_unique_ptr<T, D>:: + release () + { + i_.reset (); + return p_.release (); + } + + template <class T, class D> + void lazy_unique_ptr<T, D>:: + reset (pointer p) + { + p_.reset (p); + i_.reset (); + } + + template <class T, class D> + void lazy_unique_ptr<T, D>:: + swap (lazy_unique_ptr& b) + { + p_.swap (b.p_); + i_.swap (b.i_); + } + + template <class T, class D> + typename lazy_unique_ptr<T, D>::deleter_type& lazy_unique_ptr<T, D>:: + get_deleter () + { + return p_.get_deleter (); + } + + template <class T, class D> + const typename lazy_unique_ptr<T, D>::deleter_type& lazy_unique_ptr<T, D>:: + get_deleter () const + { + return p_.get_deleter (); + } + + template <class T, class D> + template <class T1, class D1> + inline lazy_unique_ptr<T, D>:: + lazy_unique_ptr (std::unique_ptr<T1, D1>&& p) + : p_ (std::move (p)) + { + } + + template <class T, class D> + template <class T1, class D1> + inline lazy_unique_ptr<T, D>& lazy_unique_ptr<T, D>:: + operator= (std::unique_ptr<T1, D1>&& p) + { + p_ = std::move (p); + i_.reset (); + return *this; + } + + template <class T, class D> + inline bool lazy_unique_ptr<T, D>:: + loaded () const + { + bool i (i_); + return !p_ != i; // !p_ XOR i_ + } + + template <class T, class D> + inline std::unique_ptr<T, D>& lazy_unique_ptr<T, D>:: + load () const + { + if (!loaded ()) + p_ = std::unique_ptr<T, D> (i_.template load<T> (true)); // Reset id. + + return p_; + } + + template <class T, class D> + inline void lazy_unique_ptr<T, D>:: + unload () const + { + typedef typename object_traits<T>::object_type object_type; + + if (p_) + { + if (i_.database () != 0) + i_.reset_id (object_traits<object_type>::id (*p_)); + + p_.reset (); + } + } + + template <class T, class D> + template <class ID> + inline lazy_unique_ptr<T, D>:: + lazy_unique_ptr (database_type& db, const ID& id): i_ (db, id) {} + + template <class T, class D> + inline lazy_unique_ptr<T, D>:: + lazy_unique_ptr (database_type& db, T* p) + : p_ (p) + { + if (p_) + i_.reset (db); + } + + template <class T, class D> + inline lazy_unique_ptr<T, D>:: + lazy_unique_ptr (database_type& db, T* p, const deleter_type& d) + : p_ (p, d) + { + if (p_) + i_.reset (db); + } + + template <class T, class D> + inline lazy_unique_ptr<T, D>:: + lazy_unique_ptr (database_type& db, T* p, deleter_type&& d) + : p_ (p, std::move (d)) + { + if (p_) + i_.reset (db); + } + + template <class T, class D> + template <class T1, class D1> + inline lazy_unique_ptr<T, D>:: + lazy_unique_ptr (database_type& db, std::unique_ptr<T1, D1>&& p) + : p_ (std::move (p)) + { + if (p_) + i_.reset (db); + } + + template <class T, class D> + template <class T1> + inline lazy_unique_ptr<T, D>:: + lazy_unique_ptr (database_type& db, std::auto_ptr<T1>&& p) + : p_ (std::move (p)) + { + if (p_) + i_.reset (db); + } + + template <class T, class D> + template <class ID> + inline void lazy_unique_ptr<T, D>:: + reset (database_type& db, const ID& id) + { + p_.reset (); + i_.reset (db, id); + } + + template <class T, class D> + inline void lazy_unique_ptr<T, D>:: + reset (database_type& db, T* p) + { + p_.reset (p); + + if (p) + i_.reset (db); + else + i_.reset (); + } + + template <class T, class D> + template <class T1, class D1> + inline void lazy_unique_ptr<T, D>:: + reset (database_type& db, std::unique_ptr<T1, D1>&& p) + { + p_ = std::move (p); + + if (p_) + i_.reset (db); + else + i_.reset (); + } + + template <class T, class D> + template <class T1> + inline void lazy_unique_ptr<T, D>:: + reset (database_type& db, std::auto_ptr<T1>&& p) + { + p_ = std::unique_ptr<T, D> (std::move (p)); + + if (p_) + i_.reset (db); + else + i_.reset (); + } + + template <class T, class D> + template <class O> + inline typename object_traits<O>::id_type lazy_unique_ptr<T, D>:: + object_id () const + { + typedef typename object_traits<T>::object_type object_type; + + return p_ ? object_traits<object_type>::id (*p_) : i_.object_id<O> (); + } + + template <class T, class D> + inline typename lazy_unique_ptr<T, D>::database_type& lazy_unique_ptr<T, D>:: + database () const + { + return *i_.database (); + } + + template <class T> + inline void + swap (lazy_unique_ptr<T>& a, lazy_unique_ptr<T>& b) + { + a.swap (b); + } + + template <class T1, class D1, class T2, class D2> + inline bool + operator== (const lazy_unique_ptr<T1, D1>& a, + const lazy_unique_ptr<T2, D2>& b) + { + return a.equal (b); + } + + template <class T, class D> + inline bool + operator== (const lazy_unique_ptr<T, D>& a, std::nullptr_t) + { + return !a; + } + + template <class T, class D> + inline bool + operator== (std::nullptr_t, const lazy_unique_ptr<T, D>& b) + { + return !b; + } + + template <class T1, class D1, class T2, class D2> + inline bool + operator!= (const lazy_unique_ptr<T1, D1>& a, + const lazy_unique_ptr<T2, D2>& b) + { + return !a.equal (b); + } + + template <class T, class D> + inline bool + operator!= (const lazy_unique_ptr<T, D>& a, std::nullptr_t) + { + return a; + } + + template <class T, class D> + inline bool + operator!= (std::nullptr_t, const lazy_unique_ptr<T, D>& b) + { + return b; + } + + // // lazy_shared_ptr // @@ -953,56 +1304,56 @@ namespace odb return *i_.database (); } - template<class T, class Y> + template <class T, class Y> inline bool operator== (const lazy_shared_ptr<T>& a, const lazy_shared_ptr<Y>& b) { return a.equal (b); } - template<class T> + template <class T> inline bool operator== (const lazy_shared_ptr<T>& p, std::nullptr_t) { return !p; } - template<class T> + template <class T> inline bool operator== (std::nullptr_t, const lazy_shared_ptr<T>& p) { return !p; } - template<class T, class Y> + template <class T, class Y> inline bool operator!= (const lazy_shared_ptr<T>& a, const lazy_shared_ptr<Y>& b) { return !a.equal (b); } - template<class T> + template <class T> inline bool operator!= (const lazy_shared_ptr<T>& p, std::nullptr_t) { return p; } - template<class T> + template <class T> inline bool operator!= (std::nullptr_t, const lazy_shared_ptr<T>& p) { return p; } - template<class T> + template <class T> inline void swap (lazy_shared_ptr<T>& a, lazy_shared_ptr<T>& b) { a.swap (b); } - template<class D, class T> + template <class D, class T> inline D* get_deleter (const lazy_shared_ptr<T>& p) { @@ -1244,7 +1595,7 @@ namespace odb return *i_.database (); } - template<class T> + template <class T> inline void swap (lazy_weak_ptr<T>& a, lazy_weak_ptr<T>& b) { diff --git a/odb/lazy-ptr.txx b/odb/lazy-ptr.txx index 2265fec..fdc304b 100644 --- a/odb/lazy-ptr.txx +++ b/odb/lazy-ptr.txx @@ -42,6 +42,41 @@ namespace odb #ifdef ODB_CXX11 // + // lazy_unique_ptr + // + + template <class T, class D> + template <class T1, class D1> + bool lazy_unique_ptr<T, D>:: + equal (const lazy_unique_ptr<T1, D1>& r) const + { + bool t1 (!p_ == loaded ()); + bool t2 (!r.p_ == r.loaded ()); + + // If both are transient, then compare the underlying pointers. + // + if (t1 && t2) + return p_ == r.p_; + + // If one is transient and the other is persistent, then compare + // the underlying pointers but only if they are non NULL. Note + // that an unloaded persistent object is always unequal to a + // transient object. + // + if (t1 || t2) + return p_ == r.p_ && p_; + + // If both objects are persistent, then we compare databases and + // object ids. + // + typedef typename object_traits<T>::object_type object_type1; + typedef typename object_traits<T1>::object_type object_type2; + + return i_.database () == r.i_.database () && + object_id<object_type1> () == r.object_id<object_type2> (); + } + + // // lazy_shared_ptr // diff --git a/odb/pointer-traits.hxx b/odb/pointer-traits.hxx index 07b396a..fbfa721 100644 --- a/odb/pointer-traits.hxx +++ b/odb/pointer-traits.hxx @@ -8,7 +8,7 @@ #include <odb/pre.hxx> #include <new> // operators new/delete -#include <memory> // std::auto_ptr, std::shared_ptr (C++11) +#include <memory> // std::auto_ptr, std::unique_ptr, std::shared_ptr/weak_ptr #include <cstddef> // std::size_t #include <odb/details/config.hxx> // ODB_CXX11 @@ -193,10 +193,59 @@ namespace odb #ifdef ODB_CXX11 + // Specialization for C++11 std::unique_ptr. + // + template <typename T, typename D> + class pointer_traits<std::unique_ptr<T, D>> + { + public: + static const pointer_kind kind = pk_unique; + static const bool lazy = false; + + typedef T element_type; + typedef std::unique_ptr<element_type, D> pointer_type; + typedef std::unique_ptr<const element_type, D> const_pointer_type; + typedef smart_ptr_guard<pointer_type> guard; + + static element_type* + get_ptr (const pointer_type& p) + { + return p.get (); + } + + static element_type& + get_ref (const pointer_type& p) + { + return *p; + } + + static bool + null_ptr (const pointer_type& p) + { + return !p; + } + + // cast() is not provided since it transfers the ownership. + // + + public: + static void* + allocate (std::size_t n) + { + return operator new (n); + } + + static void + free (void* p) + { + operator delete (p); + } + }; + // Specialization for C++11 std::shared_ptr. // template <typename T> - class pointer_traits<std::shared_ptr<T> > + class pointer_traits<std::shared_ptr<T>> { public: static const pointer_kind kind = pk_shared; @@ -252,7 +301,7 @@ namespace odb // Specialization for C++11 std::weak_ptr. // template <typename T> - class pointer_traits<std::weak_ptr<T> > + class pointer_traits<std::weak_ptr<T>> { public: static const pointer_kind kind = pk_weak; |