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-ptr.hxx | 160 +++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 144 insertions(+), 16 deletions(-) (limited to 'odb/lazy-ptr.hxx') 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 -- cgit v1.1