From 40466e02c3ab7ef31183158103e3ef7536248753 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Wed, 29 Feb 2012 10:57:43 +0200 Subject: Support for C++11 std::unique_ptr as object pointer This includes the odb::lazy_unique_ptr implementation. --- odb/lazy-pointer-traits.hxx | 29 ++- odb/lazy-ptr.hxx | 160 +++++++++++++++-- odb/lazy-ptr.ixx | 423 ++++++++++++++++++++++++++++++++++++++++---- odb/lazy-ptr.txx | 35 ++++ odb/pointer-traits.hxx | 55 +++++- 5 files changed, 645 insertions(+), 57 deletions(-) (limited to 'odb') diff --git a/odb/lazy-pointer-traits.hxx b/odb/lazy-pointer-traits.hxx index 910e1e4..5273db1 100644 --- a/odb/lazy-pointer-traits.hxx +++ b/odb/lazy-pointer-traits.hxx @@ -64,8 +64,33 @@ namespace odb }; #ifdef ODB_CXX11 + template + class pointer_traits> + { + public: + static const pointer_kind kind = pk_unique; + static const bool lazy = true; + + typedef T element_type; + typedef lazy_unique_ptr pointer_type; + typedef std::unique_ptr eager_pointer_type; + + static bool + null_ptr (const pointer_type& p) + { + return !p; + } + + template + static typename object_traits::id_type + object_id (const pointer_type& p) + { + return p.object_id (); + } + }; + template - class pointer_traits > + class pointer_traits> { public: static const pointer_kind kind = pk_shared; @@ -90,7 +115,7 @@ namespace odb }; template - class pointer_traits > + class pointer_traits> { public: static const pointer_kind kind = pk_weak; diff --git a/odb/lazy-ptr.hxx b/odb/lazy-ptr.hxx index c663d4a..0e44066 100644 --- a/odb/lazy-ptr.hxx +++ b/odb/lazy-ptr.hxx @@ -101,13 +101,13 @@ namespace odb // operator< and operator<< are not provided. // - template + template bool operator== (const lazy_ptr&, const lazy_ptr&); - template + template bool operator!= (const lazy_ptr&, const lazy_ptr&); - template void swap (lazy_ptr&, lazy_ptr&); + template void swap (lazy_ptr&, lazy_ptr&); // std::auto_ptr lazy version. // @@ -130,10 +130,10 @@ namespace odb explicit lazy_auto_ptr (T* = 0); lazy_auto_ptr (lazy_auto_ptr&); - template lazy_auto_ptr (lazy_auto_ptr&); + template lazy_auto_ptr (lazy_auto_ptr&); lazy_auto_ptr& operator= (lazy_auto_ptr&); - template lazy_auto_ptr& operator= (lazy_auto_ptr&); + template lazy_auto_ptr& operator= (lazy_auto_ptr&); T& operator* () const; T* operator-> () const; @@ -143,8 +143,8 @@ namespace odb lazy_auto_ptr (const lazy_auto_ptr_ref&); lazy_auto_ptr& operator= (const lazy_auto_ptr_ref&); - template operator lazy_auto_ptr_ref (); - template operator lazy_auto_ptr (); + template operator lazy_auto_ptr_ref (); + template operator lazy_auto_ptr (); // Extension: conversion to bool. // @@ -216,6 +216,133 @@ namespace odb #ifdef ODB_CXX11 + // C++11 std::unique_ptr lazy version. + // + template > + class lazy_unique_ptr + { + // Standard lazy_unique_ptr interface. + // + public: + typedef T* pointer; // For now assume it is T*. + typedef T element_type; + typedef D deleter_type; + + /*constexpr*/ lazy_unique_ptr () /*noexcept*/; + /*constexpr*/ lazy_unique_ptr (std::nullptr_t) /*noexcept*/; + explicit lazy_unique_ptr (pointer) /*noexcept*/; + + // For now assume D is non-reference. + // + lazy_unique_ptr (pointer, const deleter_type&) /*noexcept*/; + lazy_unique_ptr (pointer, deleter_type&&) /*noexcept*/; + + lazy_unique_ptr (lazy_unique_ptr&&) /*noexcept*/; + template lazy_unique_ptr (lazy_unique_ptr&&) /*noexcept*/; + template lazy_unique_ptr (std::auto_ptr&&) /*noexcept*/; + + lazy_unique_ptr& operator= (std::nullptr_t) /*noexcept*/; + lazy_unique_ptr& operator= (lazy_unique_ptr&&) /*noexcept*/; + template lazy_unique_ptr& operator= (lazy_unique_ptr&&) /*noexcept*/; + + T& operator* () const; + pointer operator-> () const /*noexcept*/; + pointer get () const /*noexcept*/; + explicit operator bool() const /*noexcept*/; + + pointer release () /*noexcept*/; + void reset (pointer = pointer ()) /*noexcept*/; + void swap (lazy_unique_ptr&) /*noexcept*/; + + deleter_type& get_deleter () /*noexcept*/; + const deleter_type& get_deleter () const /*noexcept*/; + + lazy_unique_ptr (const lazy_unique_ptr&) = delete; + lazy_unique_ptr& operator= (const lazy_unique_ptr&) = delete; + + // Initialization/assignment from unique_ptr. + // + public: + template lazy_unique_ptr (std::unique_ptr&&) /*noexcept*/; + template lazy_unique_ptr& operator= (std::unique_ptr&&) /*noexcept*/; + + // 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::unique_ptr& load () const; + + // Unload the pointer. For transient objects this function is + // equivalent to reset(). + // + void unload () const; + + template lazy_unique_ptr (database_type&, const ID&); + lazy_unique_ptr (database_type&, pointer); + lazy_unique_ptr (database_type&, pointer, const deleter_type&); + lazy_unique_ptr (database_type&, pointer, deleter_type&&); + template lazy_unique_ptr (database_type&, std::unique_ptr&&); + template lazy_unique_ptr (database_type&, std::auto_ptr&&); + + template void reset (database_type&, const ID&); + void reset (database_type&, pointer); + template void reset (database_type&, std::unique_ptr&&); + template void reset (database_type&, std::auto_ptr&&); + + template + typename object_traits::id_type object_id () const; + + database_type& database () const; + + // Helpers. + // + public: + template bool equal (const lazy_unique_ptr&) const; + + private: + template friend class lazy_unique_ptr; + + // Note that it is possible to have a situation where p_ is NULL, + // i_.id is NULL and i_.db is not NULL. This will happen if the + // unique_ptr reference returned by load() is transferred to + // another pointer or reset. + // + mutable std::unique_ptr p_; + mutable lazy_ptr_impl i_; + }; + + template void swap (lazy_unique_ptr&, lazy_unique_ptr&) /*noexcept*/; + + // operator< and operator<< are not provided. + // + template + bool operator== (const lazy_unique_ptr&, const lazy_unique_ptr&); + + template + bool operator== (const lazy_unique_ptr&, std::nullptr_t) /*noexcept*/; + + template + bool operator== (std::nullptr_t, const lazy_unique_ptr&) /*noexcept*/; + + template + bool operator!= (const lazy_unique_ptr&, const lazy_unique_ptr&); + + template + bool operator!= (const lazy_unique_ptr&, std::nullptr_t) /*noexcept*/; + + template + bool operator!= (std::nullptr_t, const lazy_unique_ptr&) /*noexcept*/; + // C++11 std::shared_ptr lazy version. // template @@ -337,29 +464,29 @@ namespace odb mutable lazy_ptr_impl i_; }; - template void swap (lazy_shared_ptr&, lazy_shared_ptr&) /*noexcept*/; + template void swap (lazy_shared_ptr&, lazy_shared_ptr&) /*noexcept*/; - template + template D* get_deleter (const lazy_shared_ptr&) /*noexcept*/; // operator< and operator<< are not provided. // - template + template bool operator== (const lazy_shared_ptr&, const lazy_shared_ptr&) /*noexcept*/; - template + template bool operator== (const lazy_shared_ptr&, std::nullptr_t) /*noexcept*/; - template + template bool operator== (std::nullptr_t, const lazy_shared_ptr&) /*noexcept*/; - template + template bool operator!= (const lazy_shared_ptr&, const lazy_shared_ptr&) /*noexcept*/; - template + template bool operator!= (const lazy_shared_ptr&, std::nullptr_t) /*noexcept*/; - template + template bool operator!= (std::nullptr_t, const lazy_shared_ptr&) /*noexcept*/; // C++11 std::weak_ptr lazy version. @@ -451,7 +578,7 @@ namespace odb // operator< is not provided. // - template void swap (lazy_weak_ptr&, lazy_weak_ptr&); + template void swap (lazy_weak_ptr&, lazy_weak_ptr&); #endif // ODB_CXX11 @@ -461,6 +588,7 @@ namespace odb using odb::lazy_auto_ptr; #ifdef ODB_CXX11 + using odb::lazy_unique_ptr; using odb::lazy_shared_ptr; using odb::lazy_weak_ptr; #endif diff --git a/odb/lazy-ptr.ixx b/odb/lazy-ptr.ixx index de82a89..5c67845 100644 --- a/odb/lazy-ptr.ixx +++ b/odb/lazy-ptr.ixx @@ -190,21 +190,21 @@ namespace odb return *i_.database (); } - template + template inline bool operator== (const lazy_ptr& a, const lazy_ptr& b) { return a.equal (b); } - template + template inline bool operator!= (const lazy_ptr& a, const lazy_ptr& b) { return !a.equal (b); } - template + template inline void swap (lazy_ptr& a, lazy_ptr& b) { @@ -215,7 +215,7 @@ namespace odb // lazy_auto_ptr_ref // - template + template inline lazy_auto_ptr_ref:: lazy_auto_ptr_ref (T* p, const lazy_ptr_impl_ref& i): p_ (p), i_ (i) {} @@ -223,26 +223,26 @@ namespace odb // lazy_auto_ptr // - template + template inline lazy_auto_ptr:: lazy_auto_ptr (T* p): p_ (p) {} - template + template inline lazy_auto_ptr:: lazy_auto_ptr (lazy_auto_ptr& r) : p_ (r.p_), i_ (static_cast (r.i_)) { } - template - template + template + 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:: operator= (lazy_auto_ptr& r) { @@ -251,8 +251,8 @@ namespace odb return *this; } - template - template + template + template inline lazy_auto_ptr& lazy_auto_ptr:: operator= (lazy_auto_ptr& r) { @@ -261,28 +261,28 @@ namespace odb return *this; } - template + template inline T& lazy_auto_ptr:: operator* () const { return *p_; } - template + template inline T* lazy_auto_ptr:: operator-> () const { return p_.operator-> (); } - template + template inline T* lazy_auto_ptr:: get () const { return p_.get (); } - template + template inline T* lazy_auto_ptr:: release () { @@ -290,7 +290,7 @@ namespace odb return p_.release (); } - template + template inline void lazy_auto_ptr:: reset (T* p) { @@ -298,11 +298,11 @@ namespace odb p_.reset (p); } - template + template inline lazy_auto_ptr:: lazy_auto_ptr (const lazy_auto_ptr_ref& r): p_ (r.p_), i_ (r.i_) {} - template + template inline lazy_auto_ptr& lazy_auto_ptr:: operator= (const lazy_auto_ptr_ref& r) { @@ -313,32 +313,32 @@ namespace odb return *this; } - template - template + template + template inline lazy_auto_ptr:: operator lazy_auto_ptr_ref () { return lazy_auto_ptr_ref (p_.release (), i_); } - template - template + template + template inline lazy_auto_ptr:: operator lazy_auto_ptr () { return lazy_auto_ptr (*this); } - template + template template inline lazy_auto_ptr:: lazy_auto_ptr (std::auto_ptr& r): p_ (r) {} - template + template inline lazy_auto_ptr:: lazy_auto_ptr (std::auto_ptr_ref r): p_ (r) {} - template + template template inline lazy_auto_ptr& lazy_auto_ptr:: operator= (std::auto_ptr& r) @@ -348,7 +348,7 @@ namespace odb return *this; } - template + template inline lazy_auto_ptr& lazy_auto_ptr:: operator= (std::auto_ptr_ref r) { @@ -413,7 +413,7 @@ namespace odb lazy_auto_ptr (database_type& db, std::auto_ptr& p) : p_ (p) { - if (p) + if (p_.get () != 0) i_.reset (db); } @@ -473,6 +473,357 @@ namespace odb #ifdef ODB_CXX11 // + // lazy_unique_ptr + // + + template + lazy_unique_ptr:: + lazy_unique_ptr () {} + + template + lazy_unique_ptr:: + lazy_unique_ptr (std::nullptr_t) {} + + 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)) {} + + template + lazy_unique_ptr& lazy_unique_ptr:: + operator= (std::nullptr_t) + { + reset (); + return *this; + } + + 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 (); + } + + template + lazy_unique_ptr:: + operator bool() const + { + return p_ || i_; + } + + 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 (!loaded ()) + 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 + template + inline lazy_unique_ptr:: + lazy_unique_ptr (database_type& db, const ID& id): i_ (db, id) {} + + template + inline lazy_unique_ptr:: + lazy_unique_ptr (database_type& db, T* p) + : p_ (p) + { + if (p_) + i_.reset (db); + } + + template + inline lazy_unique_ptr:: + lazy_unique_ptr (database_type& db, T* p, const deleter_type& d) + : p_ (p, d) + { + if (p_) + i_.reset (db); + } + + template + inline lazy_unique_ptr:: + lazy_unique_ptr (database_type& db, T* p, deleter_type&& d) + : p_ (p, std::move (d)) + { + if (p_) + i_.reset (db); + } + + template + template + inline lazy_unique_ptr:: + lazy_unique_ptr (database_type& db, std::unique_ptr&& p) + : p_ (std::move (p)) + { + if (p_) + i_.reset (db); + } + + template + template + inline lazy_unique_ptr:: + lazy_unique_ptr (database_type& db, std::auto_ptr&& p) + : p_ (std::move (p)) + { + if (p_) + i_.reset (db); + } + + template + template + inline void lazy_unique_ptr:: + reset (database_type& db, const ID& id) + { + p_.reset (); + i_.reset (db, id); + } + + template + inline void lazy_unique_ptr:: + reset (database_type& db, T* p) + { + p_.reset (p); + + if (p) + i_.reset (db); + else + i_.reset (); + } + + template + template + inline void lazy_unique_ptr:: + reset (database_type& db, std::unique_ptr&& p) + { + p_ = std::move (p); + + if (p_) + i_.reset (db); + else + i_.reset (); + } + + template + template + inline void lazy_unique_ptr:: + reset (database_type& db, std::auto_ptr&& p) + { + p_ = std::unique_ptr (std::move (p)); + + if (p_) + i_.reset (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_.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, 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, + const lazy_unique_ptr& b) + { + return !a.equal (b); + } + + 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; + } + + // // lazy_shared_ptr // @@ -953,56 +1304,56 @@ namespace odb return *i_.database (); } - template + template inline bool operator== (const lazy_shared_ptr& a, const lazy_shared_ptr& b) { return a.equal (b); } - template + template inline bool operator== (const lazy_shared_ptr& p, std::nullptr_t) { return !p; } - template + template inline bool operator== (std::nullptr_t, const lazy_shared_ptr& p) { return !p; } - template + template inline bool operator!= (const lazy_shared_ptr& a, const lazy_shared_ptr& b) { return !a.equal (b); } - template + template inline bool operator!= (const lazy_shared_ptr& p, std::nullptr_t) { return p; } - template + template inline bool operator!= (std::nullptr_t, const lazy_shared_ptr& p) { return p; } - template + template inline void swap (lazy_shared_ptr& a, lazy_shared_ptr& b) { a.swap (b); } - template + template inline D* get_deleter (const lazy_shared_ptr& p) { @@ -1244,7 +1595,7 @@ namespace odb return *i_.database (); } - template + template inline void swap (lazy_weak_ptr& a, lazy_weak_ptr& b) { diff --git a/odb/lazy-ptr.txx b/odb/lazy-ptr.txx index 2265fec..fdc304b 100644 --- a/odb/lazy-ptr.txx +++ b/odb/lazy-ptr.txx @@ -42,6 +42,41 @@ namespace odb #ifdef ODB_CXX11 // + // lazy_unique_ptr + // + + template + template + bool lazy_unique_ptr:: + equal (const lazy_unique_ptr& 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::object_type object_type1; + typedef typename object_traits::object_type object_type2; + + return i_.database () == r.i_.database () && + object_id () == r.object_id (); + } + + // // lazy_shared_ptr // diff --git a/odb/pointer-traits.hxx b/odb/pointer-traits.hxx index 07b396a..fbfa721 100644 --- a/odb/pointer-traits.hxx +++ b/odb/pointer-traits.hxx @@ -8,7 +8,7 @@ #include #include // operators new/delete -#include // std::auto_ptr, std::shared_ptr (C++11) +#include // std::auto_ptr, std::unique_ptr, std::shared_ptr/weak_ptr #include // std::size_t #include // ODB_CXX11 @@ -193,10 +193,59 @@ namespace odb #ifdef ODB_CXX11 + // Specialization for C++11 std::unique_ptr. + // + template + class pointer_traits> + { + public: + static const pointer_kind kind = pk_unique; + static const bool lazy = false; + + typedef T element_type; + typedef std::unique_ptr pointer_type; + typedef std::unique_ptr const_pointer_type; + typedef smart_ptr_guard 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; + } + + // cast() is not provided since it transfers the ownership. + // + + 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::shared_ptr. // template - class pointer_traits > + class pointer_traits> { public: static const pointer_kind kind = pk_shared; @@ -252,7 +301,7 @@ namespace odb // Specialization for C++11 std::weak_ptr. // template - class pointer_traits > + class pointer_traits> { public: static const pointer_kind kind = pk_weak; -- cgit v1.1