// file : odb/boost/smart-ptr/lazy-ptr.ixx // copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC // license : GNU GPL v2; see accompanying LICENSE file namespace odb { namespace boost { // // lazy_shared_ptr // template inline lazy_shared_ptr:: lazy_shared_ptr () {} 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 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 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 ::boost::bad_weak_ptr (); } template template inline lazy_shared_ptr:: lazy_shared_ptr (std::auto_ptr& r): p_ (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 template inline lazy_shared_ptr& lazy_shared_ptr:: operator= (std::auto_ptr& r) { p_ = 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 template inline lazy_shared_ptr:: lazy_shared_ptr (const ::boost::shared_ptr& r): p_ (r) {} template template inline lazy_shared_ptr:: lazy_shared_ptr (const ::boost::weak_ptr& r): p_ (r) {} template template inline lazy_shared_ptr& lazy_shared_ptr:: operator= (const ::boost::shared_ptr& r) { p_ = r; i_.reset (); return *this; } template inline bool lazy_shared_ptr:: loaded () const { bool i (i_); return !p_ != i; // !p_ XOR i_ } template inline ::boost::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 (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_ (r) { if (p_) i_.reset_db (db); } template template inline lazy_shared_ptr:: lazy_shared_ptr (DB& db, const ::boost::shared_ptr& r) : p_ (r) { if (p_) i_.reset_db (db); } template template inline lazy_shared_ptr:: lazy_shared_ptr (DB& db, const ::boost::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_ = r; if (p_) i_.reset_db (db); else i_.reset (); } template template inline void lazy_shared_ptr:: reset (DB& db, const ::boost::shared_ptr& r) { p_ = 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); } 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 ::boost::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 ::boost::weak_ptr& r): p_ (r) {} template template inline lazy_weak_ptr:: lazy_weak_ptr (const ::boost::shared_ptr& r): p_ (r) {} template template inline lazy_weak_ptr& lazy_weak_ptr:: operator= (const ::boost::weak_ptr& r) { p_ = r; i_.reset (); return *this; } template template inline lazy_weak_ptr& lazy_weak_ptr:: operator= (const ::boost::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 ::boost::shared_ptr lazy_weak_ptr:: load () const { ::boost::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 (DB& db, const ID& id): i_ (db, id) {} template template inline lazy_weak_ptr:: lazy_weak_ptr (DB& db, const ::boost::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 ::boost::weak_ptr& r) : p_ (r) { typedef typename object_traits::object_type object_type; ::boost::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 ::boost::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 ::boost::weak_ptr& r) { typedef typename object_traits::object_type object_type; p_ = r; ::boost::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; ::boost::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); } } }