diff options
-rw-r--r-- | odb/lazy-pointer-traits.hxx | 47 | ||||
-rw-r--r-- | odb/lazy-ptr-impl.hxx | 32 | ||||
-rw-r--r-- | odb/lazy-ptr-impl.ixx | 62 | ||||
-rw-r--r-- | odb/lazy-ptr.hxx | 264 | ||||
-rw-r--r-- | odb/lazy-ptr.ixx | 784 | ||||
-rw-r--r-- | odb/lazy-ptr.txx | 66 | ||||
-rw-r--r-- | odb/pointer-traits.hxx | 84 | ||||
-rw-r--r-- | odb/tr1/lazy-ptr.hxx | 9 |
8 files changed, 1344 insertions, 4 deletions
diff --git a/odb/lazy-pointer-traits.hxx b/odb/lazy-pointer-traits.hxx index cd2b535..910e1e4 100644 --- a/odb/lazy-pointer-traits.hxx +++ b/odb/lazy-pointer-traits.hxx @@ -9,6 +9,7 @@ #include <odb/pointer-traits.hxx> #include <odb/lazy-ptr.hxx> +#include <odb/details/config.hxx> // ODB_CXX11 namespace odb { @@ -61,6 +62,52 @@ namespace odb return p.object_id<O> (); } }; + +#ifdef ODB_CXX11 + template <typename T> + class pointer_traits<lazy_shared_ptr<T> > + { + public: + static const pointer_kind kind = pk_shared; + static const bool lazy = true; + + typedef T element_type; + typedef lazy_shared_ptr<element_type> pointer_type; + typedef std::shared_ptr<element_type> eager_pointer_type; + + static bool + null_ptr (const pointer_type& p) + { + return !p; + } + + template <class O /* = T */> + static typename object_traits<O>::id_type + object_id (const pointer_type& p) + { + return p.object_id<O> (); + } + }; + + template <typename T> + class pointer_traits<lazy_weak_ptr<T> > + { + public: + static const pointer_kind kind = pk_weak; + static const bool lazy = true; + + typedef T element_type; + typedef lazy_weak_ptr<element_type> pointer_type; + typedef lazy_shared_ptr<element_type> strong_pointer_type; + typedef std::weak_ptr<element_type> eager_pointer_type; + + static strong_pointer_type + lock (const pointer_type& p) + { + return p.lock (); + } + }; +#endif // ODB_CXX11 } #include <odb/post.hxx> diff --git a/odb/lazy-ptr-impl.hxx b/odb/lazy-ptr-impl.hxx index fd11ff8..257cef8 100644 --- a/odb/lazy-ptr-impl.hxx +++ b/odb/lazy-ptr-impl.hxx @@ -7,9 +7,12 @@ #include <odb/pre.hxx> +#include <utility> // std::move + #include <odb/forward.hxx> // odb::database #include <odb/traits.hxx> +#include <odb/details/config.hxx> // ODB_CXX11 #include <odb/details/export.hxx> namespace odb @@ -38,6 +41,17 @@ namespace odb lazy_ptr_base& operator= (const lazy_ptr_impl_ref&); + // C++11 support. + // + public: +#ifdef ODB_CXX11 + lazy_ptr_base (lazy_ptr_base&&); + + lazy_ptr_base& + operator= (lazy_ptr_base&&); +#endif + + public: // Reset both the id and database. // void @@ -119,6 +133,24 @@ namespace odb lazy_ptr_impl& operator= (const lazy_ptr_impl_ref&); + // C++11 support. + // + public: +#ifdef ODB_CXX11 + lazy_ptr_impl (lazy_ptr_impl&&); + + template <typename Y> + lazy_ptr_impl (lazy_ptr_impl<Y>&&); + + lazy_ptr_impl& + operator= (lazy_ptr_impl&&); + + template <typename Y> + lazy_ptr_impl& + operator= (lazy_ptr_impl<Y>&&); +#endif + + public: using lazy_ptr_base::reset; using lazy_ptr_base::reset_id; diff --git a/odb/lazy-ptr-impl.ixx b/odb/lazy-ptr-impl.ixx index ac11d59..cd49682 100644 --- a/odb/lazy-ptr-impl.ixx +++ b/odb/lazy-ptr-impl.ixx @@ -28,6 +28,32 @@ namespace odb { } +#ifdef ODB_CXX11 + inline lazy_ptr_base:: + lazy_ptr_base (lazy_ptr_base&& r) + : id_ (r.id_), db_ (r.db_), free_ (r.free_), copy_ (r.copy_) + { + r.id_ = 0; + } + + inline lazy_ptr_base& lazy_ptr_base:: + operator= (lazy_ptr_base&& r) + { + if (id_ != r.id_) + { + reset_id (); + id_ = r.id_; + db_ = r.db_; + free_ = r.free_; + copy_ = r.copy_; + + r.id_ = 0; + } + + return *this; + } +#endif + inline void lazy_ptr_base:: reset_id () { @@ -214,6 +240,42 @@ namespace odb return *this; } +#ifdef ODB_CXX11 + template <typename T> + inline lazy_ptr_impl<T>:: + lazy_ptr_impl (lazy_ptr_impl&& r) + : lazy_ptr_base (std::move (r)) + { + } + + template <typename T> + template <typename Y> + inline lazy_ptr_impl<T>:: + lazy_ptr_impl (lazy_ptr_impl<Y>&& r) + : lazy_ptr_base (std::move (r)) + { + } + + template <typename T> + inline lazy_ptr_impl<T>& lazy_ptr_impl<T>:: + operator= (lazy_ptr_impl&& r) + { + lazy_ptr_base& b (*this); + b = std::move (r); + return *this; + } + + template <typename T> + template <typename Y> + inline lazy_ptr_impl<T>& lazy_ptr_impl<T>:: + operator= (lazy_ptr_impl<Y>&& r) + { + lazy_ptr_base& b (*this); + b = std::move (r); + return *this; + } +#endif + template <typename T> template <typename ID> inline void lazy_ptr_impl<T>:: diff --git a/odb/lazy-ptr.hxx b/odb/lazy-ptr.hxx index 88cda2d..c663d4a 100644 --- a/odb/lazy-ptr.hxx +++ b/odb/lazy-ptr.hxx @@ -7,14 +7,18 @@ #include <odb/pre.hxx> -#include <memory> // std::auto_ptr +#include <memory> // std::auto_ptr, std::shared_ptr/weak_ptr +#include <utility> // std::move -#include <odb/forward.hxx> // odb::database +#include <odb/forward.hxx> // odb::database #include <odb/traits.hxx> #include <odb/lazy-ptr-impl.hxx> +#include <odb/details/config.hxx> // ODB_CXX11 namespace odb { + // Raw pointer lazy version. + // template <class T> class lazy_ptr { @@ -74,7 +78,11 @@ namespace odb template <class ID> void reset (database_type&, const ID&); template <class Y> void reset (database_type&, Y*); +#ifdef ODB_CXX11 + template <class O = T> +#else template <class O /* = T */> +#endif typename object_traits<O>::id_type object_id () const; database_type& database () const; @@ -101,7 +109,7 @@ namespace odb template<class T> void swap (lazy_ptr<T>&, lazy_ptr<T>&); - // + // std::auto_ptr lazy version. // template <class T> struct lazy_auto_ptr_ref @@ -185,7 +193,11 @@ namespace odb void reset (database_type&, T*); template <class Y> void reset (database_type&, std::auto_ptr<Y>&); +#ifdef ODB_CXX11 + template <class O = T> +#else template <class O /* = T */> +#endif typename object_traits<O>::id_type object_id () const; database_type& database () const; @@ -202,10 +214,256 @@ namespace odb mutable lazy_ptr_impl<T> i_; }; +#ifdef ODB_CXX11 + + // C++11 std::shared_ptr lazy version. + // + template <class T> + class lazy_weak_ptr; + + template <class T> + class lazy_shared_ptr + { + // The standard shared_ptr interface. + // + public: + typedef T element_type; + + /*constexpr*/ lazy_shared_ptr () /*noexcept*/; + /*constexpr*/ lazy_shared_ptr (std::nullptr_t) /*noexcept*/; + template <class Y> explicit lazy_shared_ptr (Y*); + template <class Y, class D> lazy_shared_ptr (Y*, D); + template <class Y, class D, class A> lazy_shared_ptr (Y*, D, A); + template <class D> lazy_shared_ptr (std::nullptr_t, D); + template <class D, class A> lazy_shared_ptr (std::nullptr_t, D, A); + template <class Y> lazy_shared_ptr (const lazy_shared_ptr<Y>&, T*) /*noexcept*/; + + lazy_shared_ptr (const lazy_shared_ptr&) /*noexcept*/; + template <class Y> lazy_shared_ptr (const lazy_shared_ptr<Y>&) /*noexcept*/; + lazy_shared_ptr (lazy_shared_ptr&&) /*noexcept*/; + template <class Y> lazy_shared_ptr (lazy_shared_ptr<Y>&&) /*noexcept*/; + template <class Y> explicit lazy_shared_ptr (const lazy_weak_ptr<Y>&); + template <class Y> explicit lazy_shared_ptr (std::auto_ptr<Y>&&); + template <class Y, class D> lazy_shared_ptr (std::unique_ptr<Y, D>&&); + + ~lazy_shared_ptr (); + + lazy_shared_ptr& operator= (const lazy_shared_ptr&) /*noexcept*/; + template <class Y> lazy_shared_ptr& operator= (const lazy_shared_ptr<Y>&) /*noexcept*/; + lazy_shared_ptr& operator= (lazy_shared_ptr&&) /*noexcept*/; + template <class Y> lazy_shared_ptr& operator= (lazy_shared_ptr<Y>&&) /*noexcept*/; + template <class Y> lazy_shared_ptr& operator= (std::auto_ptr<Y>&&); + template <class Y, class D> lazy_shared_ptr& operator= (std::unique_ptr<Y, D>&&); + + void swap (lazy_shared_ptr&) /*noexcept*/; + void reset () /*noexcept*/; + template <class Y> void reset (Y*); + template <class Y, class D> void reset (Y*, D); + template <class Y, class D, class A> void reset (Y*, D, A); + + T* get () const /*noexcept*/; + T& operator* () const /*noexcept*/; + T* operator-> () const /*noexcept*/; + long use_count () const /*noexcept*/; + bool unique () const /*noexcept*/; + + explicit operator bool () const /*noexcept*/; + + // owner_before () is not provded. + + // Initialization/assignment from shared_ptr and weak_ptr. + // + public: + template <class Y> lazy_shared_ptr (const std::shared_ptr<Y>&); + template <class Y> lazy_shared_ptr (std::shared_ptr<Y>&&); + template <class Y> explicit lazy_shared_ptr (const std::weak_ptr<Y>&); + + template <class Y> lazy_shared_ptr& operator= (const std::shared_ptr<Y>&); + template <class Y> lazy_shared_ptr& operator= (std::shared_ptr<Y>&&); + + // Lazy loading interface. + // + public: + typedef odb::database database_type; + + // NULL loaded() + // + // true true NULL pointer to transient object + // false true valid pointer to persistent object + // true false unloaded pointer to persistent object + // false false valid pointer to transient object + // + bool loaded () const; + + std::shared_ptr<T> load () const; + + // Unload the pointer. For transient objects this function is + // equivalent to reset(). + // + void unload () const; + + template <class ID> lazy_shared_ptr (database_type&, const ID&); + template <class Y> lazy_shared_ptr (database_type&, Y*); + template <class Y, class D> lazy_shared_ptr (database_type&, Y*, D); + template <class Y, class D, class A> lazy_shared_ptr (database_type&, Y*, D, A); + template <class Y> lazy_shared_ptr (database_type&, std::auto_ptr<Y>&&); + template <class Y> lazy_shared_ptr (database_type&, const std::shared_ptr<Y>&); + template <class Y> lazy_shared_ptr (database_type&, std::shared_ptr<Y>&&); + template <class Y> lazy_shared_ptr (database_type&, const std::weak_ptr<Y>&); + + template <class ID> void reset (database_type&, const ID&); + template <class Y> void reset (database_type&, Y*); + template <class Y, class D> void reset (database_type&, Y*, D); + template <class Y, class D, class A> void reset (database_type&, Y*, D, A); + template <class Y> void reset (database_type&, std::auto_ptr<Y>&&); + template <class Y> void reset (database_type&, const std::shared_ptr<Y>&); + template <class Y> void reset (database_type&, std::shared_ptr<Y>&&); + + template <class O = T> + typename object_traits<O>::id_type object_id () const; + + database_type& database () const; + + // Helpers. + // + public: + template <class Y> bool equal (const lazy_shared_ptr<Y>&) const; + + private: + template <class Y> friend class lazy_shared_ptr; + template <class Y> friend class lazy_weak_ptr; + + mutable std::shared_ptr<T> p_; + mutable lazy_ptr_impl<T> i_; + }; + + template<class T> void swap (lazy_shared_ptr<T>&, lazy_shared_ptr<T>&) /*noexcept*/; + + template<class D, class T> + D* get_deleter (const lazy_shared_ptr<T>&) /*noexcept*/; + + // operator< and operator<< are not provided. + // + template<class T, class Y> + bool operator== (const lazy_shared_ptr<T>&, const lazy_shared_ptr<Y>&) /*noexcept*/; + + template<class T> + bool operator== (const lazy_shared_ptr<T>&, std::nullptr_t) /*noexcept*/; + + template<class T> + bool operator== (std::nullptr_t, const lazy_shared_ptr<T>&) /*noexcept*/; + + template<class T, class Y> + bool operator!= (const lazy_shared_ptr<T>&, const lazy_shared_ptr<Y>&) /*noexcept*/; + + template<class T> + bool operator!= (const lazy_shared_ptr<T>&, std::nullptr_t) /*noexcept*/; + + template<class T> + bool operator!= (std::nullptr_t, const lazy_shared_ptr<T>&) /*noexcept*/; + + // C++11 std::weak_ptr lazy version. + // + template <class T> + class lazy_weak_ptr + { + // The standard weak_ptr interface. + // + public: + typedef T element_type; + + /*constexpr*/ lazy_weak_ptr () /*noexcept*/; + template <class Y> lazy_weak_ptr (const lazy_shared_ptr<Y>&) /*noexcept*/; + lazy_weak_ptr (const lazy_weak_ptr&) /*noexcept*/; + template <class Y> lazy_weak_ptr (const lazy_weak_ptr<Y>&) /*noexcept*/; + + ~lazy_weak_ptr (); + + lazy_weak_ptr& operator= (const lazy_weak_ptr&) /*noexcept*/; + template <class Y> lazy_weak_ptr& operator= (const lazy_weak_ptr<Y>&) /*noexcept*/; + template <class Y> lazy_weak_ptr& operator= (const lazy_shared_ptr<Y>&) /*noexcept*/; + + void swap (lazy_weak_ptr<T>&) /*noexcept*/; + void reset () /*noexcept*/; + + long use_count () const /*noexcept*/; + bool expired () const /*noexcept*/; + + lazy_shared_ptr<T> lock () const /*noexcept*/; + + // owner_before () is not provded. + + // Initialization/assignment from shared_ptr and weak_ptr. + // + public: + template <class Y> lazy_weak_ptr (const std::weak_ptr<Y>&); + template <class Y> lazy_weak_ptr (const std::shared_ptr<Y>&); + + template <class Y> lazy_weak_ptr& operator= (const std::weak_ptr<Y>&); + template <class Y> lazy_weak_ptr& operator= (const std::shared_ptr<Y>&); + + // Lazy loading interface. + // + public: + typedef odb::database database_type; + + // expired() loaded() + // + // true true expired pointer to transient object + // false true valid pointer to persistent object + // true false expired pointer to persistent object + // false false valid pointer to transient object + // + bool loaded () const; + + // Performs both lock and load. + // + std::shared_ptr<T> load () const; + + // Unload the pointer. For transient objects this function is + // equivalent to reset(). + // + void unload () const; + + template <class ID> lazy_weak_ptr (database_type&, const ID&); + template <class Y> lazy_weak_ptr (database_type&, const std::shared_ptr<Y>&); + template <class Y> lazy_weak_ptr (database_type&, const std::weak_ptr<Y>&); + + template <class ID> void reset (database_type&, const ID&); + template <class Y> void reset (database_type&, const std::shared_ptr<Y>&); + template <class Y> void reset (database_type&, const std::weak_ptr<Y>&); + + // The object_id() function can only be called when the object is + // persistent, or: expired() XOR loaded() (can use != for XOR). + // + template <class O = T> + typename object_traits<O>::id_type object_id () const; + + database_type& database () const; + + private: + template <class Y> friend class lazy_shared_ptr; + template <class Y> friend class lazy_weak_ptr; + + mutable std::weak_ptr<T> p_; + mutable lazy_ptr_impl<T> i_; + }; + + // operator< is not provided. + // + template<class T> void swap (lazy_weak_ptr<T>&, lazy_weak_ptr<T>&); + +#endif // ODB_CXX11 + namespace core { using odb::lazy_ptr; using odb::lazy_auto_ptr; + +#ifdef ODB_CXX11 + using odb::lazy_shared_ptr; + using odb::lazy_weak_ptr; +#endif } } 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 + } diff --git a/odb/lazy-ptr.txx b/odb/lazy-ptr.txx index 4bb511c..2265fec 100644 --- a/odb/lazy-ptr.txx +++ b/odb/lazy-ptr.txx @@ -38,4 +38,70 @@ namespace odb return i_.database () == r.i_.database () && object_id<object_type1> () == r.object_id<object_type2> (); } + +#ifdef ODB_CXX11 + + // + // lazy_shared_ptr + // + + template <class T> + template <class Y> + bool lazy_shared_ptr<T>:: + equal (const lazy_shared_ptr<Y>& r) const + { + bool t1 (!p_ == loaded ()); + bool t2 (!r.p_ == r.loaded ()); + + // If both are transient, then compare the underlying pointers. + // + if (t1 && t2) + return p_ == r.p_; + + // If one is transient and the other is persistent, then compare + // the underlying pointers but only if they are non NULL. Note + // that an unloaded persistent object is always unequal to a + // transient object. + // + if (t1 || t2) + return p_ == r.p_ && p_; + + // If both objects are persistent, then we compare databases and + // object ids. + // + typedef typename object_traits<T>::object_type object_type1; + typedef typename object_traits<Y>::object_type object_type2; + + return i_.database () == r.i_.database () && + object_id<object_type1> () == r.object_id<object_type2> (); + } + + // + // lazy_weak_ptr + // + + template <class T> + lazy_shared_ptr<T> lazy_weak_ptr<T>:: + lock () const + { + std::shared_ptr<T> sp (p_.lock ()); + + if (sp) + { + if (database_type* db = i_.database ()) + return lazy_shared_ptr<T> (*db, sp); + else + return lazy_shared_ptr<T> (sp); + } + else + { + if (i_) + return lazy_shared_ptr<T> (*i_.database (), i_.object_id<T> ()); + else + return lazy_shared_ptr<T> (); + } + } + +#endif // ODB_CXX11 + } diff --git a/odb/pointer-traits.hxx b/odb/pointer-traits.hxx index 94be0d2..07b396a 100644 --- a/odb/pointer-traits.hxx +++ b/odb/pointer-traits.hxx @@ -8,9 +8,10 @@ #include <odb/pre.hxx> #include <new> // operators new/delete -#include <memory> // std::auto_ptr +#include <memory> // std::auto_ptr, std::shared_ptr (C++11) #include <cstddef> // std::size_t +#include <odb/details/config.hxx> // ODB_CXX11 #include <odb/details/meta/remove-const.hxx> namespace odb @@ -189,6 +190,87 @@ namespace odb operator delete (p); } }; + +#ifdef ODB_CXX11 + + // Specialization for C++11 std::shared_ptr. + // + template <typename T> + class pointer_traits<std::shared_ptr<T> > + { + public: + static const pointer_kind kind = pk_shared; + static const bool lazy = false; + + typedef T element_type; + typedef std::shared_ptr<element_type> pointer_type; + typedef std::shared_ptr<const element_type> const_pointer_type; + typedef typename odb::details::meta::remove_const<element_type>::result + unrestricted_element_type; + typedef std::shared_ptr<unrestricted_element_type> + unrestricted_pointer_type; + typedef smart_ptr_guard<pointer_type> guard; + + static element_type* + get_ptr (const pointer_type& p) + { + return p.get (); + } + + static element_type& + get_ref (const pointer_type& p) + { + return *p; + } + + static bool + null_ptr (const pointer_type& p) + { + return !p; + } + + static unrestricted_pointer_type + cast (const pointer_type& p) + { + return std::const_pointer_cast<unrestricted_element_type> (p); + } + + public: + static void* + allocate (std::size_t n) + { + return operator new (n); + } + + static void + free (void* p) + { + operator delete (p); + } + }; + + // Specialization for C++11 std::weak_ptr. + // + template <typename T> + class pointer_traits<std::weak_ptr<T> > + { + public: + static const pointer_kind kind = pk_weak; + static const bool lazy = false; + + typedef T element_type; + typedef std::weak_ptr<element_type> pointer_type; + typedef std::shared_ptr<element_type> strong_pointer_type; + + static strong_pointer_type + lock (const pointer_type& p) + { + return p.lock (); + } + }; + +#endif // ODB_CXX11 + } #include <odb/post.hxx> diff --git a/odb/tr1/lazy-ptr.hxx b/odb/tr1/lazy-ptr.hxx index 0dea3f1..fade039 100644 --- a/odb/tr1/lazy-ptr.hxx +++ b/odb/tr1/lazy-ptr.hxx @@ -17,6 +17,7 @@ #include <odb/forward.hxx> // odb::database #include <odb/traits.hxx> #include <odb/lazy-ptr-impl.hxx> +#include <odb/details/config.hxx> // ODB_CXX11 namespace odb { @@ -114,7 +115,11 @@ namespace odb template <class Y> void reset (database_type&, const std::auto_ptr<Y>&); template <class Y> void reset (database_type&, const std::tr1::shared_ptr<Y>&); +#ifdef ODB_CXX11 + template <class O = T> +#else template <class O /* = T */> +#endif typename object_traits<O>::id_type object_id () const; database_type& database () const; @@ -217,7 +222,11 @@ namespace odb // The object_id() function can only be called when the object is // persistent, or: expired() XOR loaded() (can use != for XOR). // +#ifdef ODB_CXX11 + template <class O = T> +#else template <class O /* = T */> +#endif typename object_traits<O>::id_type object_id () const; database_type& database () const; |