From eaf07e23f93813fa2c2e6b4d67e69fc6f4c48673 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 28 Feb 2012 12:42:00 +0200 Subject: Support for C++11 std::shared_ptr/weak_ptr as object pointers This includes odb::lazy_shared_ptr and odb::lazy_weak_ptr implementations. --- odb/lazy-ptr.ixx | 784 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 784 insertions(+) (limited to 'odb/lazy-ptr.ixx') diff --git a/odb/lazy-ptr.ixx b/odb/lazy-ptr.ixx index 1926055..de82a89 100644 --- a/odb/lazy-ptr.ixx +++ b/odb/lazy-ptr.ixx @@ -469,4 +469,788 @@ namespace odb { return *i_.database (); } + +#ifdef ODB_CXX11 + + // + // lazy_shared_ptr + // + + template + inline lazy_shared_ptr:: + lazy_shared_ptr () {} + + template + inline lazy_shared_ptr:: + lazy_shared_ptr (std::nullptr_t) {} + + template + template + inline lazy_shared_ptr:: + lazy_shared_ptr (Y* p): p_ (p) {} + + template + template + inline lazy_shared_ptr:: + lazy_shared_ptr (Y* p, D d): p_ (p, d) {} + + template + template + inline lazy_shared_ptr:: + lazy_shared_ptr (Y* p, D d, A a): p_ (p, d, a) {} + + template + template + inline lazy_shared_ptr:: + lazy_shared_ptr (std::nullptr_t p, D d): p_ (p, d) {} + + template + template + inline lazy_shared_ptr:: + lazy_shared_ptr (std::nullptr_t p, D d, A a): p_ (p, d, a) {} + + template + template + inline lazy_shared_ptr:: + lazy_shared_ptr (const lazy_shared_ptr& r, T* p) + // r.p_ has to be loaded + : p_ (r.p_, p) {} + + template + inline lazy_shared_ptr:: + lazy_shared_ptr (const lazy_shared_ptr& r): p_ (r.p_), i_ (r.i_) {} + + template + template + inline lazy_shared_ptr:: + lazy_shared_ptr (const lazy_shared_ptr& r): p_ (r.p_), i_ (r.i_) {} + + template + inline lazy_shared_ptr:: + lazy_shared_ptr (lazy_shared_ptr&& r) + : p_ (std::move (r.p_)), i_ (std::move (r.i_)) {} + + template + template + inline lazy_shared_ptr:: + lazy_shared_ptr (lazy_shared_ptr&& r) + : p_ (std::move (r.p_)), i_ (std::move (r.i_)) {} + + template + template + inline lazy_shared_ptr:: + lazy_shared_ptr (const lazy_weak_ptr& r): i_ (r.i_) + { + // If the pointer has expired but can be re-loaded, then don't throw. + // + p_ = r.lock (); + + if (!p_ && !i_) + throw std::bad_weak_ptr (); + } + + template + template + inline lazy_shared_ptr:: + lazy_shared_ptr (std::auto_ptr&& r): p_ (std::move (r)) {} + + template + template + inline lazy_shared_ptr:: + lazy_shared_ptr (std::unique_ptr&& r): p_ (std::move (r)) {} + + template + inline lazy_shared_ptr:: + ~lazy_shared_ptr () {} + + template + inline lazy_shared_ptr& lazy_shared_ptr:: + operator= (const lazy_shared_ptr& r) + { + p_ = r.p_; + i_ = r.i_; + return *this; + } + + template + template + inline lazy_shared_ptr& lazy_shared_ptr:: + operator= (const lazy_shared_ptr& r) + { + p_ = r.p_; + i_ = r.i_; + return *this; + } + + template + inline lazy_shared_ptr& lazy_shared_ptr:: + operator= (lazy_shared_ptr&& r) + { + p_ = std::move (r.p_); + i_ = std::move (r.i_); + return *this; + } + + template + template + inline lazy_shared_ptr& lazy_shared_ptr:: + operator= (lazy_shared_ptr&& r) + { + p_ = std::move (r.p_); + i_ = std::move (r.i_); + return *this; + } + + template + template + inline lazy_shared_ptr& lazy_shared_ptr:: + operator= (std::auto_ptr&& r) + { + p_ = std::move (r); + i_.reset (); + return *this; + } + + template + template + inline lazy_shared_ptr& lazy_shared_ptr:: + operator= (std::unique_ptr&& r) + { + p_ = std::move (r); + i_.reset (); + return *this; + } + + template + inline void lazy_shared_ptr:: + swap (lazy_shared_ptr& b) + { + p_.swap (b.p_); + i_.swap (b.i_); + } + + template + inline void lazy_shared_ptr:: + reset () + { + p_.reset (); + i_.reset (); + } + + template + template + inline void lazy_shared_ptr:: + reset (Y* p) + { + p_.reset (p); + i_.reset (); + } + + template + template + inline void lazy_shared_ptr:: + reset (Y* p, D d) + { + p_.reset (p, d); + i_.reset (); + } + + template + template + inline void lazy_shared_ptr:: + reset (Y* p, D d, A a) + { + p_.reset (p, d, a); + i_.reset (); + } + + template + inline T& lazy_shared_ptr:: + operator* () const + { + return *p_; + } + + template + inline T* lazy_shared_ptr:: + operator-> () const + { + return p_.operator-> (); + } + + template + inline T* lazy_shared_ptr:: + get () const + { + return p_.get (); + } + + template + inline bool lazy_shared_ptr:: + unique () const + { + return p_.unique (); + } + + template + inline long lazy_shared_ptr:: + use_count () const + { + return p_.use_count (); + } + + template + inline lazy_shared_ptr:: + operator bool () const + { + return p_ || i_; + } + + template + template + inline lazy_shared_ptr:: + lazy_shared_ptr (const std::shared_ptr& r): p_ (r) {} + + template + template + inline lazy_shared_ptr:: + lazy_shared_ptr (std::shared_ptr&& r): p_ (std::move (r)) {} + + template + template + inline lazy_shared_ptr:: + lazy_shared_ptr (const std::weak_ptr& r): p_ (r) {} + + template + template + inline lazy_shared_ptr& lazy_shared_ptr:: + operator= (const std::shared_ptr& r) + { + p_ = r; + i_.reset (); + return *this; + } + + template + template + inline lazy_shared_ptr& lazy_shared_ptr:: + operator= (std::shared_ptr&& r) + { + p_ = std::move (r); + i_.reset (); + return *this; + } + + template + inline bool lazy_shared_ptr:: + loaded () const + { + bool i (i_); + return !p_ != i; // !p_ XOR i_ + } + + template + inline std::shared_ptr lazy_shared_ptr:: + load () const + { + if (!loaded ()) + p_ = i_.template load (true); // Reset id. + + return p_; + } + + template + inline void lazy_shared_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_shared_ptr:: + lazy_shared_ptr (database_type& db, const ID& id): i_ (db, id) {} + + template + template + inline lazy_shared_ptr:: + lazy_shared_ptr (database_type& db, Y* p) + : p_ (p) + { + if (p_) + i_.reset (db); + } + + template + template + inline lazy_shared_ptr:: + lazy_shared_ptr (database_type& db, Y* p, D d) + : p_ (p, d) + { + if (p_) + i_.reset (db); + } + + template + template + inline lazy_shared_ptr:: + lazy_shared_ptr (database_type& db, Y* p, D d, A a) + : p_ (p, d, a) + { + if (p_) + i_.reset (db); + } + + template + template + inline lazy_shared_ptr:: + lazy_shared_ptr (database_type& db, std::auto_ptr&& r) + : p_ (std::move (r)) + { + if (p_) + i_.reset (db); + } + + template + template + inline lazy_shared_ptr:: + lazy_shared_ptr (database_type& db, const std::shared_ptr& r) + : p_ (r) + { + if (p_) + i_.reset (db); + } + + template + template + inline lazy_shared_ptr:: + lazy_shared_ptr (database_type& db, std::shared_ptr&& r) + : p_ (std::move (r)) + { + if (p_) + i_.reset (db); + } + + template + template + inline lazy_shared_ptr:: + lazy_shared_ptr (database_type& db, const std::weak_ptr& r) + : p_ (r) + { + if (p_) + i_.reset (db); + } + + template + template + inline void lazy_shared_ptr:: + reset (database_type& db, const ID& id) + { + p_.reset (); + i_.reset (db, id); + } + + template + template + inline void lazy_shared_ptr:: + reset (database_type& db, Y* p) + { + p_.reset (p); + + if (p_) + i_.reset (db); + else + i_.reset (); + } + + template + template + inline void lazy_shared_ptr:: + reset (database_type& db, Y* p, D d) + { + p_.reset (p, d); + + if (p_) + i_.reset (db); + else + i_.reset (); + } + + template + template + inline void lazy_shared_ptr:: + reset (database_type& db, Y* p, D d, A a) + { + p_.reset (p, d, a); + + if (p_) + i_.reset (db); + else + i_.reset (); + } + + template + template + inline void lazy_shared_ptr:: + reset (database_type& db, std::auto_ptr&& r) + { + p_ = std::move (r); + + if (p_) + i_.reset (db); + else + i_.reset (); + } + + template + template + inline void lazy_shared_ptr:: + reset (database_type& db, const std::shared_ptr& r) + { + p_ = r; + + if (p_) + i_.reset (db); + else + i_.reset (); + } + + template + template + inline void lazy_shared_ptr:: + reset (database_type& db, std::shared_ptr&& r) + { + p_ = std::move (r); + + if (p_) + i_.reset (db); + else + i_.reset (); + } + + template + template + inline typename object_traits::id_type lazy_shared_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_shared_ptr::database_type& lazy_shared_ptr:: + database () const + { + return *i_.database (); + } + + template + inline bool + operator== (const lazy_shared_ptr& a, const lazy_shared_ptr& b) + { + return a.equal (b); + } + + template + inline bool + operator== (const lazy_shared_ptr& p, std::nullptr_t) + { + return !p; + } + + template + inline bool + operator== (std::nullptr_t, const lazy_shared_ptr& p) + { + return !p; + } + + template + inline bool + operator!= (const lazy_shared_ptr& a, const lazy_shared_ptr& b) + { + return !a.equal (b); + } + + template + inline bool + operator!= (const lazy_shared_ptr& p, std::nullptr_t) + { + return p; + } + + template + inline bool + operator!= (std::nullptr_t, const lazy_shared_ptr& p) + { + return p; + } + + template + inline void + swap (lazy_shared_ptr& a, lazy_shared_ptr& b) + { + a.swap (b); + } + + template + inline D* + get_deleter (const lazy_shared_ptr& p) + { + return std::get_deleter (p.p_); + } + + // + // lazy_weak_ptr + // + + template + inline lazy_weak_ptr:: + lazy_weak_ptr () {} + + template + template + inline lazy_weak_ptr:: + lazy_weak_ptr (const lazy_shared_ptr& r): p_ (r.p_), i_ (r.i_) {} + + template + inline lazy_weak_ptr:: + lazy_weak_ptr (const lazy_weak_ptr& r): p_ (r.p_), i_ (r.i_) {} + + template + template + inline lazy_weak_ptr:: + lazy_weak_ptr (const lazy_weak_ptr& r): p_ (r.p_), i_ (r.i_) {} + + template + inline lazy_weak_ptr:: + ~lazy_weak_ptr () {} + + template + inline lazy_weak_ptr& lazy_weak_ptr:: + operator= (const lazy_weak_ptr& r) + { + p_ = r.p_; + i_ = r.i_; + return *this; + } + + template + template + inline lazy_weak_ptr& lazy_weak_ptr:: + operator= (const lazy_weak_ptr& r) + { + p_ = r.p_; + i_ = r.i_; + return *this; + } + + template + template + inline lazy_weak_ptr& lazy_weak_ptr:: + operator= (const lazy_shared_ptr& r) + { + p_ = r.p_; + i_ = r.i_; + return *this; + } + + template + inline void lazy_weak_ptr:: + swap (lazy_weak_ptr& r) + { + p_.swap (r.p_); + i_.swap (r.i_); + } + + template + inline void lazy_weak_ptr:: + reset () + { + p_.reset (); + i_.reset (); + } + + template + inline long lazy_weak_ptr:: + use_count () const + { + return p_.use_count (); + } + + template + inline bool lazy_weak_ptr:: + expired () const + { + return p_.expired (); + } + + template + template + inline lazy_weak_ptr:: + lazy_weak_ptr (const std::weak_ptr& r): p_ (r) {} + + template + template + inline lazy_weak_ptr:: + lazy_weak_ptr (const std::shared_ptr& r): p_ (r) {} + + template + template + inline lazy_weak_ptr& lazy_weak_ptr:: + operator= (const std::weak_ptr& r) + { + p_ = r; + i_.reset (); + return this; + } + + template + template + inline lazy_weak_ptr& lazy_weak_ptr:: + operator= (const std::shared_ptr& r) + { + p_ = r; + i_.reset (); + return this; + } + + template + inline bool lazy_weak_ptr:: + loaded () const + { + bool i (i_); + return expired () != i; // expired () XOR i_ + } + + template + inline std::shared_ptr lazy_weak_ptr:: + load () const + { + std::shared_ptr r (p_.lock ()); + + if (r || !i_) + return r; + + r = i_.template load (false); // Keep id. + p_ = r; + return r; + } + + template + inline void lazy_weak_ptr:: + unload () const + { + // With weak pointer we always keep i_ up to date. + // + p_.reset (); + } + + template + template + inline lazy_weak_ptr:: + lazy_weak_ptr (database_type& db, const ID& id): i_ (db, id) {} + + template + template + inline lazy_weak_ptr:: + lazy_weak_ptr (database_type& db, const std::shared_ptr& r) + : p_ (r) + { + typedef typename object_traits::object_type object_type; + + if (r) + i_.reset (db, object_traits::id (*r)); + } + + template + template + inline lazy_weak_ptr:: + lazy_weak_ptr (database_type& db, const std::weak_ptr& r) + : p_ (r) + { + typedef typename object_traits::object_type object_type; + + std::shared_ptr sp (p_.lock ()); + + if (sp) + i_.reset (db, object_traits::id (*sp)); + } + + template + template + inline void lazy_weak_ptr:: + reset (database_type& db, const ID& id) + { + p_.reset (); + i_.reset (db, id); + } + + template + template + inline void lazy_weak_ptr:: + reset (database_type& db, const std::shared_ptr& r) + { + typedef typename object_traits::object_type object_type; + + p_ = r; + + if (r) + i_.reset (db, object_traits::id (*r)); + else + i_.reset (); + } + + template + template + inline void lazy_weak_ptr:: + reset (database_type& db, const std::weak_ptr& r) + { + typedef typename object_traits::object_type object_type; + + p_ = r; + std::shared_ptr sp (p_.lock ()); + + if (sp) + i_.reset (db, object_traits::id (*sp)); + else + i_.reset (); + } + + template + template + inline typename object_traits::id_type lazy_weak_ptr:: + object_id () const + { + typedef typename object_traits::object_type object_type; + + std::shared_ptr sp (p_.lock ()); + return sp ? object_traits::id (*sp) : i_.object_id (); + } + + template + inline typename lazy_weak_ptr::database_type& lazy_weak_ptr:: + database () const + { + return *i_.database (); + } + + template + inline void + swap (lazy_weak_ptr& a, lazy_weak_ptr& b) + { + a.swap (b); + } + +#endif // ODB_CXX11 + } -- cgit v1.1