From 40466e02c3ab7ef31183158103e3ef7536248753 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Wed, 29 Feb 2012 10:57:43 +0200 Subject: Support for C++11 std::unique_ptr as object pointer This includes the odb::lazy_unique_ptr implementation. --- odb/lazy-ptr.ixx | 423 ++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 387 insertions(+), 36 deletions(-) (limited to 'odb/lazy-ptr.ixx') 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 + template inline bool operator== (const lazy_ptr& a, const lazy_ptr& b) { return a.equal (b); } - template + template inline bool operator!= (const lazy_ptr& a, const lazy_ptr& b) { return !a.equal (b); } - template + template inline void swap (lazy_ptr& a, lazy_ptr& b) { @@ -215,7 +215,7 @@ namespace odb // lazy_auto_ptr_ref // - template + template inline lazy_auto_ptr_ref:: 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 + template inline lazy_auto_ptr:: lazy_auto_ptr (T* p): p_ (p) {} - template + template inline lazy_auto_ptr:: lazy_auto_ptr (lazy_auto_ptr& r) : p_ (r.p_), i_ (static_cast (r.i_)) { } - template - template + template + template inline lazy_auto_ptr:: lazy_auto_ptr (lazy_auto_ptr& r) : p_ (r.p_), i_ (static_cast (r.i_)) { } - template + template inline lazy_auto_ptr& lazy_auto_ptr:: operator= (lazy_auto_ptr& r) { @@ -251,8 +251,8 @@ namespace odb return *this; } - template - template + template + template inline lazy_auto_ptr& lazy_auto_ptr:: operator= (lazy_auto_ptr& r) { @@ -261,28 +261,28 @@ namespace odb return *this; } - template + template inline T& lazy_auto_ptr:: operator* () const { return *p_; } - template + template inline T* lazy_auto_ptr:: operator-> () const { return p_.operator-> (); } - template + template inline T* lazy_auto_ptr:: get () const { return p_.get (); } - template + template inline T* lazy_auto_ptr:: release () { @@ -290,7 +290,7 @@ namespace odb return p_.release (); } - template + template inline void lazy_auto_ptr:: reset (T* p) { @@ -298,11 +298,11 @@ namespace odb p_.reset (p); } - template + template inline lazy_auto_ptr:: lazy_auto_ptr (const lazy_auto_ptr_ref& r): p_ (r.p_), i_ (r.i_) {} - template + template inline lazy_auto_ptr& lazy_auto_ptr:: operator= (const lazy_auto_ptr_ref& r) { @@ -313,32 +313,32 @@ namespace odb return *this; } - template - template + template + template inline lazy_auto_ptr:: operator lazy_auto_ptr_ref () { return lazy_auto_ptr_ref (p_.release (), i_); } - template - template + template + template inline lazy_auto_ptr:: operator lazy_auto_ptr () { return lazy_auto_ptr (*this); } - template + template template inline lazy_auto_ptr:: lazy_auto_ptr (std::auto_ptr& r): p_ (r) {} - template + template inline lazy_auto_ptr:: lazy_auto_ptr (std::auto_ptr_ref r): p_ (r) {} - template + template template inline lazy_auto_ptr& lazy_auto_ptr:: operator= (std::auto_ptr& r) @@ -348,7 +348,7 @@ namespace odb return *this; } - template + template inline lazy_auto_ptr& lazy_auto_ptr:: operator= (std::auto_ptr_ref r) { @@ -413,7 +413,7 @@ namespace odb lazy_auto_ptr (database_type& db, std::auto_ptr& 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 + lazy_unique_ptr:: + lazy_unique_ptr () {} + + template + lazy_unique_ptr:: + lazy_unique_ptr (std::nullptr_t) {} + + template + lazy_unique_ptr:: + lazy_unique_ptr (pointer p): p_ (p) {} + + template + lazy_unique_ptr:: + lazy_unique_ptr (pointer p, const deleter_type& d): p_ (p, d) {} + + template + lazy_unique_ptr:: + lazy_unique_ptr (pointer p, deleter_type&& d): p_ (p, std::move (d)) {} + + template + lazy_unique_ptr:: + lazy_unique_ptr (lazy_unique_ptr&& r) + : p_ (std::move (r.p_)), i_ (std::move (r.i_)) {} + + template + template + lazy_unique_ptr:: + lazy_unique_ptr (lazy_unique_ptr&& r) + : p_ (std::move (r.p_)), i_ (std::move (r.i_)) {} + + template + template + lazy_unique_ptr:: + lazy_unique_ptr (std::auto_ptr&& r): p_ (std::move (r)) {} + + template + lazy_unique_ptr& lazy_unique_ptr:: + operator= (std::nullptr_t) + { + reset (); + return *this; + } + + template + lazy_unique_ptr& lazy_unique_ptr:: + operator= (lazy_unique_ptr&& r) + { + p_ = std::move (r.p_); + i_ = std::move (r.i_); + return *this; + } + + template + template + lazy_unique_ptr& lazy_unique_ptr:: + operator= (lazy_unique_ptr&& r) + { + p_ = std::move (r.p_); + i_ = std::move (r.i_); + return *this; + } + + template + T& lazy_unique_ptr:: + operator* () const + { + return *p_; + } + + template + typename lazy_unique_ptr::pointer lazy_unique_ptr:: + operator-> () const + { + return p_.operator-> (); + } + + template + typename lazy_unique_ptr::pointer lazy_unique_ptr:: + get () const + { + return p_.get (); + } + + template + lazy_unique_ptr:: + operator bool() const + { + return p_ || i_; + } + + template + typename lazy_unique_ptr::pointer lazy_unique_ptr:: + release () + { + i_.reset (); + return p_.release (); + } + + template + void lazy_unique_ptr:: + reset (pointer p) + { + p_.reset (p); + i_.reset (); + } + + template + void lazy_unique_ptr:: + swap (lazy_unique_ptr& b) + { + p_.swap (b.p_); + i_.swap (b.i_); + } + + template + typename lazy_unique_ptr::deleter_type& lazy_unique_ptr:: + get_deleter () + { + return p_.get_deleter (); + } + + template + const typename lazy_unique_ptr::deleter_type& lazy_unique_ptr:: + get_deleter () const + { + return p_.get_deleter (); + } + + template + template + inline lazy_unique_ptr:: + lazy_unique_ptr (std::unique_ptr&& p) + : p_ (std::move (p)) + { + } + + template + template + inline lazy_unique_ptr& lazy_unique_ptr:: + operator= (std::unique_ptr&& p) + { + p_ = std::move (p); + i_.reset (); + return *this; + } + + template + inline bool lazy_unique_ptr:: + loaded () const + { + bool i (i_); + return !p_ != i; // !p_ XOR i_ + } + + template + inline std::unique_ptr& lazy_unique_ptr:: + load () const + { + if (!loaded ()) + p_ = std::unique_ptr (i_.template load (true)); // Reset id. + + return p_; + } + + template + inline void lazy_unique_ptr:: + unload () const + { + typedef typename object_traits::object_type object_type; + + if (p_) + { + if (i_.database () != 0) + i_.reset_id (object_traits::id (*p_)); + + p_.reset (); + } + } + + template + template + inline lazy_unique_ptr:: + lazy_unique_ptr (database_type& db, const ID& id): i_ (db, id) {} + + template + inline lazy_unique_ptr:: + lazy_unique_ptr (database_type& db, T* p) + : p_ (p) + { + if (p_) + i_.reset (db); + } + + template + inline lazy_unique_ptr:: + lazy_unique_ptr (database_type& db, T* p, const deleter_type& d) + : p_ (p, d) + { + if (p_) + i_.reset (db); + } + + template + inline lazy_unique_ptr:: + lazy_unique_ptr (database_type& db, T* p, deleter_type&& d) + : p_ (p, std::move (d)) + { + if (p_) + i_.reset (db); + } + + template + template + inline lazy_unique_ptr:: + lazy_unique_ptr (database_type& db, std::unique_ptr&& p) + : p_ (std::move (p)) + { + if (p_) + i_.reset (db); + } + + template + template + inline lazy_unique_ptr:: + lazy_unique_ptr (database_type& db, std::auto_ptr&& p) + : p_ (std::move (p)) + { + if (p_) + i_.reset (db); + } + + template + template + inline void lazy_unique_ptr:: + reset (database_type& db, const ID& id) + { + p_.reset (); + i_.reset (db, id); + } + + template + inline void lazy_unique_ptr:: + reset (database_type& db, T* p) + { + p_.reset (p); + + if (p) + i_.reset (db); + else + i_.reset (); + } + + template + template + inline void lazy_unique_ptr:: + reset (database_type& db, std::unique_ptr&& p) + { + p_ = std::move (p); + + if (p_) + i_.reset (db); + else + i_.reset (); + } + + template + template + inline void lazy_unique_ptr:: + reset (database_type& db, std::auto_ptr&& p) + { + p_ = std::unique_ptr (std::move (p)); + + if (p_) + i_.reset (db); + else + i_.reset (); + } + + template + template + inline typename object_traits::id_type lazy_unique_ptr:: + object_id () const + { + typedef typename object_traits::object_type object_type; + + return p_ ? object_traits::id (*p_) : i_.object_id (); + } + + template + inline typename lazy_unique_ptr::database_type& lazy_unique_ptr:: + database () const + { + return *i_.database (); + } + + template + inline void + swap (lazy_unique_ptr& a, lazy_unique_ptr& b) + { + a.swap (b); + } + + template + inline bool + operator== (const lazy_unique_ptr& a, + const lazy_unique_ptr& b) + { + return a.equal (b); + } + + template + inline bool + operator== (const lazy_unique_ptr& a, std::nullptr_t) + { + return !a; + } + + template + inline bool + operator== (std::nullptr_t, const lazy_unique_ptr& b) + { + return !b; + } + + template + inline bool + operator!= (const lazy_unique_ptr& a, + const lazy_unique_ptr& b) + { + return !a.equal (b); + } + + template + inline bool + operator!= (const lazy_unique_ptr& a, std::nullptr_t) + { + return a; + } + + template + inline bool + operator!= (std::nullptr_t, const lazy_unique_ptr& b) + { + return b; + } + + // // lazy_shared_ptr // @@ -953,56 +1304,56 @@ namespace odb return *i_.database (); } - template + template inline bool operator== (const lazy_shared_ptr& a, const lazy_shared_ptr& b) { return a.equal (b); } - template + template inline bool operator== (const lazy_shared_ptr& p, std::nullptr_t) { return !p; } - template + template inline bool operator== (std::nullptr_t, const lazy_shared_ptr& p) { return !p; } - template + template inline bool operator!= (const lazy_shared_ptr& a, const lazy_shared_ptr& b) { return !a.equal (b); } - template + template inline bool operator!= (const lazy_shared_ptr& p, std::nullptr_t) { return p; } - template + template inline bool operator!= (std::nullptr_t, const lazy_shared_ptr& p) { return p; } - template + template inline void swap (lazy_shared_ptr& a, lazy_shared_ptr& b) { a.swap (b); } - template + template inline D* get_deleter (const lazy_shared_ptr& p) { @@ -1244,7 +1595,7 @@ namespace odb return *i_.database (); } - template + template inline void swap (lazy_weak_ptr& a, lazy_weak_ptr& b) { -- cgit v1.1