// file : odb/lazy-ptr.ixx // copyright : Copyright (c) 2009-2015 Code Synthesis Tools CC // license : GNU GPL v2; see accompanying LICENSE file namespace odb { // // lazy_ptr // template inline lazy_ptr:: lazy_ptr (): p_ (0) {} template template inline lazy_ptr:: lazy_ptr (Y* p): p_ (p) {} template inline lazy_ptr:: lazy_ptr (const lazy_ptr& r): p_ (r.p_), i_ (r.i_) {} template template inline lazy_ptr:: lazy_ptr (const lazy_ptr& r): p_ (r.p_), i_ (r.i_) {} template inline lazy_ptr& lazy_ptr:: operator= (const lazy_ptr& r) { p_ = r.p_; i_ = r.i_; return *this; } template template inline lazy_ptr& lazy_ptr:: operator= (Y* r) { p_ = r; i_.reset (); return *this; } template template inline lazy_ptr& lazy_ptr:: operator= (const lazy_ptr& r) { p_ = r.p_; i_ = r.i_; return *this; } template inline void lazy_ptr:: swap (lazy_ptr& b) { T* p (p_); p_ = b.p_; b.p_ = p; i_.swap (b.i_); } template inline void lazy_ptr:: reset () { p_ = 0; i_.reset (); } template template inline void lazy_ptr:: reset (Y* p) { p_ = p; i_.reset (); } template inline T& lazy_ptr:: operator* () const { return *p_; } template inline T* lazy_ptr:: operator-> () const { return p_; } template inline T* lazy_ptr:: get () const { return p_; } template inline bool lazy_ptr:: loaded () const { bool i (i_); return (p_ == 0) != i; // !p_ XOR i } template inline T* lazy_ptr:: load () const { if (p_ == 0 && i_) p_ = i_.template load (true); // Reset id. return p_; } template inline void lazy_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_ = 0; } } template inline T* lazy_ptr:: get_eager () const { return p_; } template template inline lazy_ptr:: lazy_ptr (DB& db, const ID& id): p_ (0), i_ (db, id) {} template template inline lazy_ptr:: lazy_ptr (DB& db, Y* r) : p_ (r) { if (p_) i_.reset_db (db); } template template inline void lazy_ptr:: reset (DB& db, const ID& id) { p_ = 0; i_.reset (db, id); } template template inline void lazy_ptr:: reset (DB& db, Y* r) { p_ = r; if (p_) i_.reset_db (db); else i_.reset (); } template template inline typename object_traits::id_type lazy_ptr:: object_id () const { typedef typename object_traits::object_type object_type; return p_ ? object_traits::id (*p_) : i_.template object_id (); } template inline typename lazy_ptr::database_type& lazy_ptr:: database () const { return *i_.database (); } template inline bool operator== (const lazy_ptr& a, const lazy_ptr& b) { return a.equal (b); } template inline bool operator!= (const lazy_ptr& a, const lazy_ptr& b) { return !a.equal (b); } template inline void swap (lazy_ptr& a, lazy_ptr& b) { a.swap (b); } // // lazy_auto_ptr_ref // #ifndef ODB_CXX11 template inline lazy_auto_ptr_ref:: lazy_auto_ptr_ref (T* p, const lazy_ptr_impl_ref& i): p_ (p), i_ (i) {} // // lazy_auto_ptr // template inline lazy_auto_ptr:: lazy_auto_ptr (T* p): p_ (p) {} 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 (lazy_auto_ptr& r) : p_ (r.p_), i_ (static_cast (r.i_)) { } template inline lazy_auto_ptr& lazy_auto_ptr:: operator= (lazy_auto_ptr& r) { p_ = r.p_; i_ = static_cast (r.i_); return *this; } template template inline lazy_auto_ptr& lazy_auto_ptr:: operator= (lazy_auto_ptr& r) { p_ = r.p_; i_ = static_cast (r.i_); return *this; } template inline T& lazy_auto_ptr:: operator* () const { return *p_; } template inline T* lazy_auto_ptr:: operator-> () const { return p_.operator-> (); } template inline T* lazy_auto_ptr:: get () const { return p_.get (); } template inline T* lazy_auto_ptr:: release () { i_.reset (); return p_.release (); } template inline void lazy_auto_ptr:: reset (T* p) { i_.reset (); p_.reset (p); } template inline lazy_auto_ptr:: lazy_auto_ptr (const lazy_auto_ptr_ref& r): p_ (r.p_), i_ (r.i_) {} template inline lazy_auto_ptr& lazy_auto_ptr:: operator= (const lazy_auto_ptr_ref& r) { if (p_.get () != r.p_) p_.reset (r.p_); i_ = r.i_; return *this; } template template inline lazy_auto_ptr:: operator lazy_auto_ptr_ref () { return lazy_auto_ptr_ref (p_.release (), i_); } template template inline lazy_auto_ptr:: operator lazy_auto_ptr () { return lazy_auto_ptr (*this); } template template inline lazy_auto_ptr:: lazy_auto_ptr (std::auto_ptr& r): p_ (r) {} template inline lazy_auto_ptr:: lazy_auto_ptr (std::auto_ptr_ref r): p_ (r) {} template template inline lazy_auto_ptr& lazy_auto_ptr:: operator= (std::auto_ptr& r) { p_ = r; i_.reset (); return *this; } template inline lazy_auto_ptr& lazy_auto_ptr:: operator= (std::auto_ptr_ref r) { p_ = r; i_.reset (); return *this; } template inline bool lazy_auto_ptr:: loaded () const { bool i (i_); return (p_.get () == 0) != i; // XOR } template inline std::auto_ptr& lazy_auto_ptr:: load () const { if (p_.get () == 0 && i_) { std::auto_ptr tmp (i_.template load (true)); // Reset id. p_ = tmp; } return p_; } template inline void lazy_auto_ptr:: unload () const { typedef typename object_traits::object_type object_type; if (p_.get () != 0) { if (i_.database () != 0) i_.reset_id (object_traits::id (*p_)); p_.reset (); } } template inline std::auto_ptr& lazy_auto_ptr:: get_eager () const { return p_; } template template inline lazy_auto_ptr:: lazy_auto_ptr (DB& db, const ID& id): i_ (db, id) {} template template inline lazy_auto_ptr:: lazy_auto_ptr (DB& db, T* p) : p_ (p) { if (p) i_.reset_db (db); } template template inline lazy_auto_ptr:: lazy_auto_ptr (DB& db, std::auto_ptr& p) : p_ (p) { if (p_.get () != 0) i_.reset_db (db); } template template inline void lazy_auto_ptr:: reset (DB& db, const ID& id) { p_.reset (); i_.reset (db, id); } template template inline void lazy_auto_ptr:: reset (DB& db, T* p) { p_.reset (p); if (p) i_.reset_db (db); else i_.reset (); } template template inline void lazy_auto_ptr:: reset (DB& db, std::auto_ptr& p) { p_ = p; if (p_.get () != 0) i_.reset_db (db); else i_.reset (); } template template inline typename object_traits::id_type lazy_auto_ptr:: object_id () const { typedef typename object_traits::object_type object_type; return p_.get () != 0 ? object_traits::id (*p_) : i_.template object_id (); } template inline typename lazy_auto_ptr::database_type& lazy_auto_ptr:: database () const { return *i_.database (); } #endif #ifdef ODB_CXX11 // // lazy_unique_ptr // template lazy_unique_ptr:: lazy_unique_ptr () {} #ifdef ODB_CXX11_NULLPTR template lazy_unique_ptr:: lazy_unique_ptr (std::nullptr_t) {} #endif 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)) {} #ifdef ODB_CXX11_NULLPTR template lazy_unique_ptr& lazy_unique_ptr:: operator= (std::nullptr_t) { reset (); return *this; } #endif 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 (); } #ifdef ODB_CXX11_EXPLICIT_CONVERSION_OPERATOR template lazy_unique_ptr:: operator bool() const { return p_ || i_; } #endif 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 (!p_ && i_) 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 inline std::unique_ptr& lazy_unique_ptr:: get_eager () const { return p_; } template template inline lazy_unique_ptr:: lazy_unique_ptr (DB& db, const ID& id): i_ (db, id) {} template template inline lazy_unique_ptr:: lazy_unique_ptr (DB& db, T* p) : p_ (p) { if (p_) i_.reset_db (db); } template template inline lazy_unique_ptr:: lazy_unique_ptr (DB& db, T* p, const deleter_type& d) : p_ (p, d) { if (p_) i_.reset_db (db); } template template inline lazy_unique_ptr:: lazy_unique_ptr (DB& db, T* p, deleter_type&& d) : p_ (p, std::move (d)) { if (p_) i_.reset_db (db); } template template inline lazy_unique_ptr:: lazy_unique_ptr (DB& db, std::unique_ptr&& p) : p_ (std::move (p)) { if (p_) i_.reset_db (db); } // template // template // inline lazy_unique_ptr:: // lazy_unique_ptr (DB& db, std::auto_ptr&& p) // : p_ (std::move (p)) // { // if (p_) // i_.reset_db (db); // } template template inline void lazy_unique_ptr:: reset (DB& db, const ID& id) { p_.reset (); i_.reset (db, id); } template template inline void lazy_unique_ptr:: reset (DB& db, T* p) { p_.reset (p); if (p) i_.reset_db (db); else i_.reset (); } template template inline void lazy_unique_ptr:: reset (DB& db, std::unique_ptr&& p) { p_ = std::move (p); if (p_) i_.reset_db (db); else i_.reset (); } // template // template // inline void lazy_unique_ptr:: // reset (DB& db, std::auto_ptr&& p) // { // p_ = std::unique_ptr (std::move (p)); // // if (p_) // i_.reset_db (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_.template 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, const lazy_unique_ptr& b) { return !a.equal (b); } #ifdef ODB_CXX11_NULLPTR 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, std::nullptr_t) { return bool (a); // Explicit to-bool conversion. } template inline bool operator!= (std::nullptr_t, const lazy_unique_ptr& b) { return bool (b); // Explicit to-bool conversion. } #endif // // lazy_shared_ptr // template inline lazy_shared_ptr:: lazy_shared_ptr () {} #ifdef ODB_CXX11_NULLPTR template inline lazy_shared_ptr:: lazy_shared_ptr (std::nullptr_t) {} #endif 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) {} #ifdef ODB_CXX11_NULLPTR 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) {} #endif 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 ().get_eager (); 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 (); } #ifdef ODB_CXX11_EXPLICIT_CONVERSION_OPERATOR template inline lazy_shared_ptr:: operator bool () const { return p_ || i_; } #endif 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 (!p_ && i_) 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 inline std::shared_ptr lazy_shared_ptr:: get_eager () const { return p_; } template template inline lazy_shared_ptr:: lazy_shared_ptr (DB& db, const ID& id): i_ (db, id) {} template template inline lazy_shared_ptr:: lazy_shared_ptr (DB& db, Y* p) : p_ (p) { if (p_) i_.reset_db (db); } template template inline lazy_shared_ptr:: lazy_shared_ptr (DB& db, Y* p, D d) : p_ (p, d) { if (p_) i_.reset_db (db); } template template inline lazy_shared_ptr:: lazy_shared_ptr (DB& db, Y* p, D d, A a) : p_ (p, d, a) { if (p_) i_.reset_db (db); } // template // template // inline lazy_shared_ptr:: // lazy_shared_ptr (DB& db, std::auto_ptr&& r) // : p_ (std::move (r)) // { // if (p_) // i_.reset_db (db); // } template template inline lazy_shared_ptr:: lazy_shared_ptr (DB& db, const std::shared_ptr& r) : p_ (r) { if (p_) i_.reset_db (db); } template template inline lazy_shared_ptr:: lazy_shared_ptr (DB& db, std::shared_ptr&& r) : p_ (std::move (r)) { if (p_) i_.reset_db (db); } template template inline lazy_shared_ptr:: lazy_shared_ptr (DB& db, const std::weak_ptr& r) : p_ (r) { if (p_) i_.reset_db (db); } template template inline void lazy_shared_ptr:: reset (DB& db, const ID& id) { p_.reset (); i_.reset (db, id); } template template inline void lazy_shared_ptr:: reset (DB& db, Y* p) { p_.reset (p); if (p_) i_.reset_db (db); else i_.reset (); } template template inline void lazy_shared_ptr:: reset (DB& db, Y* p, D d) { p_.reset (p, d); if (p_) i_.reset_db (db); else i_.reset (); } template template inline void lazy_shared_ptr:: reset (DB& db, Y* p, D d, A a) { p_.reset (p, d, a); if (p_) i_.reset_db (db); else i_.reset (); } // template // template // inline void lazy_shared_ptr:: // reset (DB& db, std::auto_ptr&& r) // { // p_ = std::move (r); // // if (p_) // i_.reset_db (db); // else // i_.reset (); // } template template inline void lazy_shared_ptr:: reset (DB& db, const std::shared_ptr& r) { p_ = r; if (p_) i_.reset_db (db); else i_.reset (); } template template inline void lazy_shared_ptr:: reset (DB& db, std::shared_ptr&& r) { p_ = std::move (r); if (p_) i_.reset_db (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_.template 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& a, const lazy_shared_ptr& b) { return !a.equal (b); } #ifdef ODB_CXX11_NULLPTR 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& p, std::nullptr_t) { return bool (p); // Explicit to-bool conversion. } template inline bool operator!= (std::nullptr_t, const lazy_shared_ptr& p) { return bool (p); // Explicit to-bool conversion. } #endif 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 lazy_shared_ptr lazy_weak_ptr:: lock () const { return lazy_shared_ptr (p_.lock (), i_); } template inline std::shared_ptr lazy_weak_ptr:: load () const { std::shared_ptr r (p_.lock ()); if (!r && i_) { 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 inline std::weak_ptr lazy_weak_ptr:: get_eager () const { return p_; } template template inline lazy_weak_ptr:: lazy_weak_ptr (DB& db, const ID& id): i_ (db, id) {} template template inline lazy_weak_ptr:: lazy_weak_ptr (DB& 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 (DB& 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 (DB& db, const ID& id) { p_.reset (); i_.reset (db, id); } template template inline void lazy_weak_ptr:: reset (DB& 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 (DB& 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_.template 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 }