aboutsummaryrefslogtreecommitdiff
path: root/odb/lazy-ptr.ixx
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2012-02-28 12:42:00 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2012-02-28 12:42:00 +0200
commiteaf07e23f93813fa2c2e6b4d67e69fc6f4c48673 (patch)
tree3b6a54db5d3019121b0bbd553322248ce45db4c2 /odb/lazy-ptr.ixx
parent3012187847b2783c49153db3f5486b84074cb2b0 (diff)
Support for C++11 std::shared_ptr/weak_ptr as object pointers
This includes odb::lazy_shared_ptr and odb::lazy_weak_ptr implementations.
Diffstat (limited to 'odb/lazy-ptr.ixx')
-rw-r--r--odb/lazy-ptr.ixx784
1 files changed, 784 insertions, 0 deletions
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 <class T>
+ inline lazy_shared_ptr<T>::
+ lazy_shared_ptr () {}
+
+ template <class T>
+ inline lazy_shared_ptr<T>::
+ lazy_shared_ptr (std::nullptr_t) {}
+
+ template <class T>
+ template <class Y>
+ inline lazy_shared_ptr<T>::
+ lazy_shared_ptr (Y* p): p_ (p) {}
+
+ template <class T>
+ template <class Y, class D>
+ inline lazy_shared_ptr<T>::
+ lazy_shared_ptr (Y* p, D d): p_ (p, d) {}
+
+ template <class T>
+ template <class Y, class D, class A>
+ inline lazy_shared_ptr<T>::
+ lazy_shared_ptr (Y* p, D d, A a): p_ (p, d, a) {}
+
+ template <class T>
+ template <class D>
+ inline lazy_shared_ptr<T>::
+ lazy_shared_ptr (std::nullptr_t p, D d): p_ (p, d) {}
+
+ template <class T>
+ template <class D, class A>
+ inline lazy_shared_ptr<T>::
+ lazy_shared_ptr (std::nullptr_t p, D d, A a): p_ (p, d, a) {}
+
+ template <class T>
+ template <class Y>
+ inline lazy_shared_ptr<T>::
+ lazy_shared_ptr (const lazy_shared_ptr<Y>& r, T* p)
+ // r.p_ has to be loaded
+ : p_ (r.p_, p) {}
+
+ template <class T>
+ inline lazy_shared_ptr<T>::
+ lazy_shared_ptr (const lazy_shared_ptr& r): p_ (r.p_), i_ (r.i_) {}
+
+ template <class T>
+ template <class Y>
+ inline lazy_shared_ptr<T>::
+ lazy_shared_ptr (const lazy_shared_ptr<Y>& r): p_ (r.p_), i_ (r.i_) {}
+
+ template <class T>
+ inline lazy_shared_ptr<T>::
+ lazy_shared_ptr (lazy_shared_ptr&& r)
+ : p_ (std::move (r.p_)), i_ (std::move (r.i_)) {}
+
+ template <class T>
+ template <class Y>
+ inline lazy_shared_ptr<T>::
+ lazy_shared_ptr (lazy_shared_ptr<Y>&& r)
+ : p_ (std::move (r.p_)), i_ (std::move (r.i_)) {}
+
+ template <class T>
+ template <class Y>
+ inline lazy_shared_ptr<T>::
+ lazy_shared_ptr (const lazy_weak_ptr<Y>& 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 <class T>
+ template <class Y>
+ inline lazy_shared_ptr<T>::
+ lazy_shared_ptr (std::auto_ptr<Y>&& r): p_ (std::move (r)) {}
+
+ template <class T>
+ template <class Y, class D>
+ inline lazy_shared_ptr<T>::
+ lazy_shared_ptr (std::unique_ptr<Y, D>&& r): p_ (std::move (r)) {}
+
+ template <class T>
+ inline lazy_shared_ptr<T>::
+ ~lazy_shared_ptr () {}
+
+ template <class T>
+ inline lazy_shared_ptr<T>& lazy_shared_ptr<T>::
+ operator= (const lazy_shared_ptr& r)
+ {
+ p_ = r.p_;
+ i_ = r.i_;
+ return *this;
+ }
+
+ template <class T>
+ template <class Y>
+ inline lazy_shared_ptr<T>& lazy_shared_ptr<T>::
+ operator= (const lazy_shared_ptr<Y>& r)
+ {
+ p_ = r.p_;
+ i_ = r.i_;
+ return *this;
+ }
+
+ template <class T>
+ inline lazy_shared_ptr<T>& lazy_shared_ptr<T>::
+ operator= (lazy_shared_ptr&& r)
+ {
+ p_ = std::move (r.p_);
+ i_ = std::move (r.i_);
+ return *this;
+ }
+
+ template <class T>
+ template <class Y>
+ inline lazy_shared_ptr<T>& lazy_shared_ptr<T>::
+ operator= (lazy_shared_ptr<Y>&& r)
+ {
+ p_ = std::move (r.p_);
+ i_ = std::move (r.i_);
+ return *this;
+ }
+
+ template <class T>
+ template <class Y>
+ inline lazy_shared_ptr<T>& lazy_shared_ptr<T>::
+ operator= (std::auto_ptr<Y>&& r)
+ {
+ p_ = std::move (r);
+ i_.reset ();
+ return *this;
+ }
+
+ template <class T>
+ template <class Y, class D>
+ inline lazy_shared_ptr<T>& lazy_shared_ptr<T>::
+ operator= (std::unique_ptr<Y, D>&& r)
+ {
+ p_ = std::move (r);
+ i_.reset ();
+ return *this;
+ }
+
+ template <class T>
+ inline void lazy_shared_ptr<T>::
+ swap (lazy_shared_ptr& b)
+ {
+ p_.swap (b.p_);
+ i_.swap (b.i_);
+ }
+
+ template <class T>
+ inline void lazy_shared_ptr<T>::
+ reset ()
+ {
+ p_.reset ();
+ i_.reset ();
+ }
+
+ template <class T>
+ template <class Y>
+ inline void lazy_shared_ptr<T>::
+ reset (Y* p)
+ {
+ p_.reset (p);
+ i_.reset ();
+ }
+
+ template <class T>
+ template <class Y, class D>
+ inline void lazy_shared_ptr<T>::
+ reset (Y* p, D d)
+ {
+ p_.reset (p, d);
+ i_.reset ();
+ }
+
+ template <class T>
+ template <class Y, class D, class A>
+ inline void lazy_shared_ptr<T>::
+ reset (Y* p, D d, A a)
+ {
+ p_.reset (p, d, a);
+ i_.reset ();
+ }
+
+ template <class T>
+ inline T& lazy_shared_ptr<T>::
+ operator* () const
+ {
+ return *p_;
+ }
+
+ template <class T>
+ inline T* lazy_shared_ptr<T>::
+ operator-> () const
+ {
+ return p_.operator-> ();
+ }
+
+ template <class T>
+ inline T* lazy_shared_ptr<T>::
+ get () const
+ {
+ return p_.get ();
+ }
+
+ template <class T>
+ inline bool lazy_shared_ptr<T>::
+ unique () const
+ {
+ return p_.unique ();
+ }
+
+ template <class T>
+ inline long lazy_shared_ptr<T>::
+ use_count () const
+ {
+ return p_.use_count ();
+ }
+
+ template <class T>
+ inline lazy_shared_ptr<T>::
+ operator bool () const
+ {
+ return p_ || i_;
+ }
+
+ template <class T>
+ template <class Y>
+ inline lazy_shared_ptr<T>::
+ lazy_shared_ptr (const std::shared_ptr<Y>& r): p_ (r) {}
+
+ template <class T>
+ template <class Y>
+ inline lazy_shared_ptr<T>::
+ lazy_shared_ptr (std::shared_ptr<Y>&& r): p_ (std::move (r)) {}
+
+ template <class T>
+ template <class Y>
+ inline lazy_shared_ptr<T>::
+ lazy_shared_ptr (const std::weak_ptr<Y>& r): p_ (r) {}
+
+ template <class T>
+ template <class Y>
+ inline lazy_shared_ptr<T>& lazy_shared_ptr<T>::
+ operator= (const std::shared_ptr<Y>& r)
+ {
+ p_ = r;
+ i_.reset ();
+ return *this;
+ }
+
+ template <class T>
+ template <class Y>
+ inline lazy_shared_ptr<T>& lazy_shared_ptr<T>::
+ operator= (std::shared_ptr<Y>&& r)
+ {
+ p_ = std::move (r);
+ i_.reset ();
+ return *this;
+ }
+
+ template <class T>
+ inline bool lazy_shared_ptr<T>::
+ loaded () const
+ {
+ bool i (i_);
+ return !p_ != i; // !p_ XOR i_
+ }
+
+ template <class T>
+ inline std::shared_ptr<T> lazy_shared_ptr<T>::
+ load () const
+ {
+ if (!loaded ())
+ p_ = i_.template load<T> (true); // Reset id.
+
+ return p_;
+ }
+
+ template <class T>
+ inline void lazy_shared_ptr<T>::
+ unload () const
+ {
+ typedef typename object_traits<T>::object_type object_type;
+
+ if (p_)
+ {
+ if (i_.database () != 0)
+ i_.reset_id (object_traits<object_type>::id (*p_));
+
+ p_.reset ();
+ }
+ }
+
+ template <class T>
+ template <class ID>
+ inline lazy_shared_ptr<T>::
+ lazy_shared_ptr (database_type& db, const ID& id): i_ (db, id) {}
+
+ template <class T>
+ template <class Y>
+ inline lazy_shared_ptr<T>::
+ lazy_shared_ptr (database_type& db, Y* p)
+ : p_ (p)
+ {
+ if (p_)
+ i_.reset (db);
+ }
+
+ template <class T>
+ template <class Y, class D>
+ inline lazy_shared_ptr<T>::
+ lazy_shared_ptr (database_type& db, Y* p, D d)
+ : p_ (p, d)
+ {
+ if (p_)
+ i_.reset (db);
+ }
+
+ template <class T>
+ template <class Y, class D, class A>
+ inline lazy_shared_ptr<T>::
+ lazy_shared_ptr (database_type& db, Y* p, D d, A a)
+ : p_ (p, d, a)
+ {
+ if (p_)
+ i_.reset (db);
+ }
+
+ template <class T>
+ template <class Y>
+ inline lazy_shared_ptr<T>::
+ lazy_shared_ptr (database_type& db, std::auto_ptr<Y>&& r)
+ : p_ (std::move (r))
+ {
+ if (p_)
+ i_.reset (db);
+ }
+
+ template <class T>
+ template <class Y>
+ inline lazy_shared_ptr<T>::
+ lazy_shared_ptr (database_type& db, const std::shared_ptr<Y>& r)
+ : p_ (r)
+ {
+ if (p_)
+ i_.reset (db);
+ }
+
+ template <class T>
+ template <class Y>
+ inline lazy_shared_ptr<T>::
+ lazy_shared_ptr (database_type& db, std::shared_ptr<Y>&& r)
+ : p_ (std::move (r))
+ {
+ if (p_)
+ i_.reset (db);
+ }
+
+ template <class T>
+ template <class Y>
+ inline lazy_shared_ptr<T>::
+ lazy_shared_ptr (database_type& db, const std::weak_ptr<Y>& r)
+ : p_ (r)
+ {
+ if (p_)
+ i_.reset (db);
+ }
+
+ template <class T>
+ template <class ID>
+ inline void lazy_shared_ptr<T>::
+ reset (database_type& db, const ID& id)
+ {
+ p_.reset ();
+ i_.reset (db, id);
+ }
+
+ template <class T>
+ template <class Y>
+ inline void lazy_shared_ptr<T>::
+ reset (database_type& db, Y* p)
+ {
+ p_.reset (p);
+
+ if (p_)
+ i_.reset (db);
+ else
+ i_.reset ();
+ }
+
+ template <class T>
+ template <class Y, class D>
+ inline void lazy_shared_ptr<T>::
+ reset (database_type& db, Y* p, D d)
+ {
+ p_.reset (p, d);
+
+ if (p_)
+ i_.reset (db);
+ else
+ i_.reset ();
+ }
+
+ template <class T>
+ template <class Y, class D, class A>
+ inline void lazy_shared_ptr<T>::
+ reset (database_type& db, Y* p, D d, A a)
+ {
+ p_.reset (p, d, a);
+
+ if (p_)
+ i_.reset (db);
+ else
+ i_.reset ();
+ }
+
+ template <class T>
+ template <class Y>
+ inline void lazy_shared_ptr<T>::
+ reset (database_type& db, std::auto_ptr<Y>&& r)
+ {
+ p_ = std::move (r);
+
+ if (p_)
+ i_.reset (db);
+ else
+ i_.reset ();
+ }
+
+ template <class T>
+ template <class Y>
+ inline void lazy_shared_ptr<T>::
+ reset (database_type& db, const std::shared_ptr<Y>& r)
+ {
+ p_ = r;
+
+ if (p_)
+ i_.reset (db);
+ else
+ i_.reset ();
+ }
+
+ template <class T>
+ template <class Y>
+ inline void lazy_shared_ptr<T>::
+ reset (database_type& db, std::shared_ptr<Y>&& r)
+ {
+ p_ = std::move (r);
+
+ if (p_)
+ i_.reset (db);
+ else
+ i_.reset ();
+ }
+
+ template <class T>
+ template <class O>
+ inline typename object_traits<O>::id_type lazy_shared_ptr<T>::
+ object_id () const
+ {
+ typedef typename object_traits<T>::object_type object_type;
+
+ return p_ ? object_traits<object_type>::id (*p_) : i_.object_id<O> ();
+ }
+
+ template <class T>
+ inline typename lazy_shared_ptr<T>::database_type& lazy_shared_ptr<T>::
+ database () const
+ {
+ return *i_.database ();
+ }
+
+ template<class T, class Y>
+ inline bool
+ operator== (const lazy_shared_ptr<T>& a, const lazy_shared_ptr<Y>& b)
+ {
+ return a.equal (b);
+ }
+
+ template<class T>
+ inline bool
+ operator== (const lazy_shared_ptr<T>& p, std::nullptr_t)
+ {
+ return !p;
+ }
+
+ template<class T>
+ inline bool
+ operator== (std::nullptr_t, const lazy_shared_ptr<T>& p)
+ {
+ return !p;
+ }
+
+ template<class T, class Y>
+ inline bool
+ operator!= (const lazy_shared_ptr<T>& a, const lazy_shared_ptr<Y>& b)
+ {
+ return !a.equal (b);
+ }
+
+ template<class T>
+ inline bool
+ operator!= (const lazy_shared_ptr<T>& p, std::nullptr_t)
+ {
+ return p;
+ }
+
+ template<class T>
+ inline bool
+ operator!= (std::nullptr_t, const lazy_shared_ptr<T>& p)
+ {
+ return p;
+ }
+
+ template<class T>
+ inline void
+ swap (lazy_shared_ptr<T>& a, lazy_shared_ptr<T>& b)
+ {
+ a.swap (b);
+ }
+
+ template<class D, class T>
+ inline D*
+ get_deleter (const lazy_shared_ptr<T>& p)
+ {
+ return std::get_deleter<D> (p.p_);
+ }
+
+ //
+ // lazy_weak_ptr
+ //
+
+ template <class T>
+ inline lazy_weak_ptr<T>::
+ lazy_weak_ptr () {}
+
+ template <class T>
+ template <class Y>
+ inline lazy_weak_ptr<T>::
+ lazy_weak_ptr (const lazy_shared_ptr<Y>& r): p_ (r.p_), i_ (r.i_) {}
+
+ template <class T>
+ inline lazy_weak_ptr<T>::
+ lazy_weak_ptr (const lazy_weak_ptr& r): p_ (r.p_), i_ (r.i_) {}
+
+ template <class T>
+ template <class Y>
+ inline lazy_weak_ptr<T>::
+ lazy_weak_ptr (const lazy_weak_ptr<Y>& r): p_ (r.p_), i_ (r.i_) {}
+
+ template <class T>
+ inline lazy_weak_ptr<T>::
+ ~lazy_weak_ptr () {}
+
+ template <class T>
+ inline lazy_weak_ptr<T>& lazy_weak_ptr<T>::
+ operator= (const lazy_weak_ptr& r)
+ {
+ p_ = r.p_;
+ i_ = r.i_;
+ return *this;
+ }
+
+ template <class T>
+ template <class Y>
+ inline lazy_weak_ptr<T>& lazy_weak_ptr<T>::
+ operator= (const lazy_weak_ptr<Y>& r)
+ {
+ p_ = r.p_;
+ i_ = r.i_;
+ return *this;
+ }
+
+ template <class T>
+ template <class Y>
+ inline lazy_weak_ptr<T>& lazy_weak_ptr<T>::
+ operator= (const lazy_shared_ptr<Y>& r)
+ {
+ p_ = r.p_;
+ i_ = r.i_;
+ return *this;
+ }
+
+ template <class T>
+ inline void lazy_weak_ptr<T>::
+ swap (lazy_weak_ptr<T>& r)
+ {
+ p_.swap (r.p_);
+ i_.swap (r.i_);
+ }
+
+ template <class T>
+ inline void lazy_weak_ptr<T>::
+ reset ()
+ {
+ p_.reset ();
+ i_.reset ();
+ }
+
+ template <class T>
+ inline long lazy_weak_ptr<T>::
+ use_count () const
+ {
+ return p_.use_count ();
+ }
+
+ template <class T>
+ inline bool lazy_weak_ptr<T>::
+ expired () const
+ {
+ return p_.expired ();
+ }
+
+ template <class T>
+ template <class Y>
+ inline lazy_weak_ptr<T>::
+ lazy_weak_ptr (const std::weak_ptr<Y>& r): p_ (r) {}
+
+ template <class T>
+ template <class Y>
+ inline lazy_weak_ptr<T>::
+ lazy_weak_ptr (const std::shared_ptr<Y>& r): p_ (r) {}
+
+ template <class T>
+ template <class Y>
+ inline lazy_weak_ptr<T>& lazy_weak_ptr<T>::
+ operator= (const std::weak_ptr<Y>& r)
+ {
+ p_ = r;
+ i_.reset ();
+ return this;
+ }
+
+ template <class T>
+ template <class Y>
+ inline lazy_weak_ptr<T>& lazy_weak_ptr<T>::
+ operator= (const std::shared_ptr<Y>& r)
+ {
+ p_ = r;
+ i_.reset ();
+ return this;
+ }
+
+ template <class T>
+ inline bool lazy_weak_ptr<T>::
+ loaded () const
+ {
+ bool i (i_);
+ return expired () != i; // expired () XOR i_
+ }
+
+ template <class T>
+ inline std::shared_ptr<T> lazy_weak_ptr<T>::
+ load () const
+ {
+ std::shared_ptr<T> r (p_.lock ());
+
+ if (r || !i_)
+ return r;
+
+ r = i_.template load<T> (false); // Keep id.
+ p_ = r;
+ return r;
+ }
+
+ template <class T>
+ inline void lazy_weak_ptr<T>::
+ unload () const
+ {
+ // With weak pointer we always keep i_ up to date.
+ //
+ p_.reset ();
+ }
+
+ template <class T>
+ template <class ID>
+ inline lazy_weak_ptr<T>::
+ lazy_weak_ptr (database_type& db, const ID& id): i_ (db, id) {}
+
+ template <class T>
+ template <class Y>
+ inline lazy_weak_ptr<T>::
+ lazy_weak_ptr (database_type& db, const std::shared_ptr<Y>& r)
+ : p_ (r)
+ {
+ typedef typename object_traits<T>::object_type object_type;
+
+ if (r)
+ i_.reset (db, object_traits<object_type>::id (*r));
+ }
+
+ template <class T>
+ template <class Y>
+ inline lazy_weak_ptr<T>::
+ lazy_weak_ptr (database_type& db, const std::weak_ptr<Y>& r)
+ : p_ (r)
+ {
+ typedef typename object_traits<T>::object_type object_type;
+
+ std::shared_ptr<T> sp (p_.lock ());
+
+ if (sp)
+ i_.reset (db, object_traits<object_type>::id (*sp));
+ }
+
+ template <class T>
+ template <class ID>
+ inline void lazy_weak_ptr<T>::
+ reset (database_type& db, const ID& id)
+ {
+ p_.reset ();
+ i_.reset (db, id);
+ }
+
+ template <class T>
+ template <class Y>
+ inline void lazy_weak_ptr<T>::
+ reset (database_type& db, const std::shared_ptr<Y>& r)
+ {
+ typedef typename object_traits<T>::object_type object_type;
+
+ p_ = r;
+
+ if (r)
+ i_.reset (db, object_traits<object_type>::id (*r));
+ else
+ i_.reset ();
+ }
+
+ template <class T>
+ template <class Y>
+ inline void lazy_weak_ptr<T>::
+ reset (database_type& db, const std::weak_ptr<Y>& r)
+ {
+ typedef typename object_traits<T>::object_type object_type;
+
+ p_ = r;
+ std::shared_ptr<T> sp (p_.lock ());
+
+ if (sp)
+ i_.reset (db, object_traits<object_type>::id (*sp));
+ else
+ i_.reset ();
+ }
+
+ template <class T>
+ template <class O>
+ inline typename object_traits<O>::id_type lazy_weak_ptr<T>::
+ object_id () const
+ {
+ typedef typename object_traits<T>::object_type object_type;
+
+ std::shared_ptr<T> sp (p_.lock ());
+ return sp ? object_traits<object_type>::id (*sp) : i_.object_id<O> ();
+ }
+
+ template <class T>
+ inline typename lazy_weak_ptr<T>::database_type& lazy_weak_ptr<T>::
+ database () const
+ {
+ return *i_.database ();
+ }
+
+ template<class T>
+ inline void
+ swap (lazy_weak_ptr<T>& a, lazy_weak_ptr<T>& b)
+ {
+ a.swap (b);
+ }
+
+#endif // ODB_CXX11
+
}