// file : odb/qt/smart-ptr/lazy-ptr.ixx // copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC // license : GNU GPL v2; see accompanying LICENSE file // // QLazySharedPointer definition. // template inline QLazySharedPointer:: QLazySharedPointer () {} template inline QLazySharedPointer:: QLazySharedPointer (T* p): p_ (p) {} template template inline QLazySharedPointer:: QLazySharedPointer (T* p, Deleter d): p_ (p, d) {} template inline QLazySharedPointer:: QLazySharedPointer (const QLazySharedPointer& r): p_ (r.p_), i_ (r.i_) {} template template inline QLazySharedPointer:: QLazySharedPointer (const QLazySharedPointer& r): p_ (r.p_), i_ (r.i_) {} template template inline QLazySharedPointer:: QLazySharedPointer (const QLazyWeakPointer& r): p_ (r.p_), i_ (r.i_) {} template inline QLazySharedPointer:: ~QLazySharedPointer () {} template inline QLazySharedPointer& QLazySharedPointer:: operator= (const QLazySharedPointer& r) { p_ = r.p_; i_ = r.i_; return *this; } template template inline QLazySharedPointer& QLazySharedPointer:: operator= (const QLazySharedPointer& r) { p_ = r.p_; i_ = r.i_; return *this; } template template inline QLazySharedPointer& QLazySharedPointer:: operator= (const QLazyWeakPointer& r) { p_ = r.p_; i_ = r.i_; return *this; } template inline bool QLazySharedPointer:: operator! () const { return isNull (); } template inline T& QLazySharedPointer:: operator* () const { return *p_; } template inline T* QLazySharedPointer:: operator-> () const { return p_.operator-> (); } template inline void QLazySharedPointer:: swap (QLazySharedPointer& x) { p_.swap (x.p_); i_.swap (x.i_); } template inline bool QLazySharedPointer:: isNull () const { return !(p_ || i_); } template inline T* QLazySharedPointer:: data () const { return p_.data (); } template inline QLazyWeakPointer QLazySharedPointer:: toWeakRef () const { return QLazyWeakPointer (*this); } template inline void QLazySharedPointer:: clear () { p_.clear (); i_.reset (); } template template inline QLazySharedPointer QLazySharedPointer:: staticCast () const { QLazySharedPointer c (p_.template staticCast ()); c.i_ = i_; return c; } template template inline QLazySharedPointer QLazySharedPointer:: dynamicCast () const { QLazySharedPointer c (p_.template dynamicCast ()); if (c) c.i_ = i_; return c; } template template inline QLazySharedPointer QLazySharedPointer:: constCast () const { QLazySharedPointer c (p_.template constCast ()); c.i_ = i_; return c; } template template inline QLazySharedPointer:: QLazySharedPointer (const QSharedPointer& r): p_ (r) {} template template inline QLazySharedPointer:: QLazySharedPointer (const QWeakPointer& r): p_ (r) {} template template inline QLazySharedPointer& QLazySharedPointer:: operator= (const QSharedPointer& r) { p_ = r; i_.reset (); return *this; } template template inline QLazySharedPointer& QLazySharedPointer:: operator= (const QWeakPointer& r) { p_ = r; i_.reset (); return *this; } template inline bool QLazySharedPointer:: loaded () const { bool i (i_); return !p_ != i; // !p_ XOR i_ } template inline QSharedPointer QLazySharedPointer:: load () const { if (!loaded ()) p_ = i_.template load (true); // Reset id. return p_; } template inline void QLazySharedPointer:: unload () const { typedef typename odb::object_traits::object_type object_type; if (p_) { if (i_.database () != 0) i_.reset_id (odb::object_traits::id (*p_)); p_.clear (); } } template template inline QLazySharedPointer:: QLazySharedPointer (DB& db, const ID& id): i_ (db, id) {} template template inline QLazySharedPointer:: QLazySharedPointer (DB& db, T* p) : p_ (p) { if (p_) i_.reset_db (db); } template template inline QLazySharedPointer:: QLazySharedPointer (DB& db, T* p, Deleter d) : p_ (p, d) { if (p_) i_.reset_db (db); } template template inline QLazySharedPointer:: QLazySharedPointer (DB& db, const QSharedPointer& r) : p_ (r) { if (p_) i_.reset_db (db); } template template inline QLazySharedPointer:: QLazySharedPointer (DB& db, const QWeakPointer& r) : p_ (r) { if (p_) i_.reset_db (db); } template inline typename QLazySharedPointer::database_type& QLazySharedPointer:: database () const { return *i_.database (); } template template inline typename odb::object_traits::id_type QLazySharedPointer:: objectId () const { typedef typename odb::object_traits::object_type object_type; return p_ ? odb::object_traits::id (*p_) : i_.template object_id (); } // // QLazySharedPointer related non-member function definitions. // template inline QLazySharedPointer qSharedPointerCast (const QLazySharedPointer& r) { return r.template staticCast (); } template inline QLazySharedPointer qSharedPointerConstCast (const QLazySharedPointer& r) { return r.template constCast (); } template inline QLazySharedPointer qSharedPointerDynamicCast (const QLazySharedPointer& r) { return r.template dynamicCast (); } template inline bool operator== (const QLazySharedPointer& a, const QLazySharedPointer& b) { return a.equal (b); } template inline bool operator!= (const QLazySharedPointer& a, const QLazySharedPointer& b) { return !a.equal (b); } // // QLazyWeakPointer definition. // template inline QLazyWeakPointer:: QLazyWeakPointer () {} template inline QLazyWeakPointer:: QLazyWeakPointer (const QLazyWeakPointer& r): p_ (r.p_), i_ (r.i_) {} template template inline QLazyWeakPointer:: QLazyWeakPointer (const QLazyWeakPointer& r): p_ (r.p_), i_ (r.i_) {} template template inline QLazyWeakPointer:: QLazyWeakPointer (const QLazySharedPointer& r): p_ (r.p_), i_ (r.i_) {} template inline QLazyWeakPointer:: ~QLazyWeakPointer () {} template inline QLazyWeakPointer& QLazyWeakPointer:: operator= (const QLazyWeakPointer& r) { p_ = r.p_; i_ = r.i_; return *this; } template template inline QLazyWeakPointer& QLazyWeakPointer:: operator= (const QLazyWeakPointer& r) { p_ = r.p_; i_ = r.i_; return *this; } template template inline QLazyWeakPointer& QLazyWeakPointer:: operator= (const QLazySharedPointer& r) { p_ = r.p_; i_ = r.i_; return *this; } template inline bool QLazyWeakPointer:: operator! () const { return isNull (); } #ifdef QWEAKPOINTER_ENABLE_ARROW template inline T* QLazyWeakPointer:: operator-> () const { return p_.operator-> (); } #endif template inline void QLazyWeakPointer:: clear () { p_.clear (); i_.reset (); } template inline T* QLazyWeakPointer:: data () const { return p_.data (); } template inline bool QLazyWeakPointer:: isNull () const { return !(p_ || i_); } template template inline QLazyWeakPointer:: QLazyWeakPointer (const QWeakPointer& r): p_ (r) {} template template inline QLazyWeakPointer:: QLazyWeakPointer (const QSharedPointer& r): p_ (r) {} template template inline QLazyWeakPointer& QLazyWeakPointer:: operator= (const QWeakPointer& r) { p_ = r; i_.reset (); return *this; } template template inline QLazyWeakPointer& QLazyWeakPointer:: operator= (const QSharedPointer& r) { p_ = r; i_.reset (); return *this; } template inline bool QLazyWeakPointer:: loaded () const { bool i (i_); return p_.toStrongRef ().isNull () != i; // expired () XOR i_ } template inline QSharedPointer QLazyWeakPointer:: load () const { QSharedPointer r (p_.toStrongRef ()); if (r || !i_) return r; r = i_.template load (false); // Keep id. p_ = r; return r; } template inline void QLazyWeakPointer:: unload () const { // With weak pointer we always keep i_ up to date. // p_.clear (); } template template inline QLazyWeakPointer:: QLazyWeakPointer (DB& db, const ID& id): i_ (db, id) {} template template inline QLazyWeakPointer:: QLazyWeakPointer (DB& db, const QSharedPointer& r) : p_ (r) { typedef typename odb::object_traits::object_type object_type; if (r) i_.reset (db, odb::object_traits::id (*r)); } template template inline QLazyWeakPointer:: QLazyWeakPointer (DB& db, const QWeakPointer& r) : p_ (r) { typedef typename odb::object_traits::object_type object_type; QSharedPointer sp (p_.toStrongRef ()); if (sp) i_.reset (db, odb::object_traits::id (*sp)); } template template inline typename odb::object_traits::id_type QLazyWeakPointer:: objectId () const { typedef typename odb::object_traits::object_type object_type; QSharedPointer sp (p_.toStrongRef ()); return sp ? odb::object_traits::id (*sp) : i_.template object_id (); } template inline typename QLazyWeakPointer::database_type& QLazyWeakPointer:: database () const { return *i_.database (); } // // QLazyWeakPointer related non-member functions. // template inline QLazySharedPointer qSharedPointerCast (const QLazyWeakPointer& r) { return QLazySharedPointer (r).template staticCast (); } template inline QLazySharedPointer qSharedPointerConstCast (const QLazyWeakPointer& r) { return QLazySharedPointer (r).template constCast (); } template inline QLazySharedPointer qSharedPointerDynamicCast (const QLazyWeakPointer& r) { return QLazySharedPointer (r).template dynamicCast (); } template inline QLazyWeakPointer qWeakPointerCast (const QLazyWeakPointer& r) { return QLazySharedPointer (r).template staticCast ().toWeakRef (); } template inline bool operator== (const QLazyWeakPointer& t, const QLazyWeakPointer& x) { return t.equal (x); } template inline bool operator== (const QLazyWeakPointer& t, const QLazySharedPointer& x) { return t.equal (x); } template inline bool operator== (const QLazySharedPointer& t, const QLazyWeakPointer& x) { return x.equal (t); } template inline bool operator!= (const QLazyWeakPointer& t, const QLazyWeakPointer& x) { return !t.equal (x); } template inline bool operator!= (const QLazyWeakPointer& t, const QLazySharedPointer& x) { return !t.equal (x); } template inline bool operator!= (const QLazySharedPointer& t, const QLazyWeakPointer& x) { return !x.equal (t); }