From eaf07e23f93813fa2c2e6b4d67e69fc6f4c48673 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 28 Feb 2012 12:42:00 +0200 Subject: Support for C++11 std::shared_ptr/weak_ptr as object pointers This includes odb::lazy_shared_ptr and odb::lazy_weak_ptr implementations. --- odb/pointer-traits.hxx | 84 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 83 insertions(+), 1 deletion(-) (limited to 'odb/pointer-traits.hxx') 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 #include // operators new/delete -#include // std::auto_ptr +#include // std::auto_ptr, std::shared_ptr (C++11) #include // std::size_t +#include // ODB_CXX11 #include namespace odb @@ -189,6 +190,87 @@ namespace odb operator delete (p); } }; + +#ifdef ODB_CXX11 + + // Specialization for C++11 std::shared_ptr. + // + template + class pointer_traits > + { + public: + static const pointer_kind kind = pk_shared; + static const bool lazy = false; + + typedef T element_type; + typedef std::shared_ptr pointer_type; + typedef std::shared_ptr const_pointer_type; + typedef typename odb::details::meta::remove_const::result + unrestricted_element_type; + typedef std::shared_ptr + unrestricted_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; + } + + static unrestricted_pointer_type + cast (const pointer_type& p) + { + return std::const_pointer_cast (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 + class pointer_traits > + { + public: + static const pointer_kind kind = pk_weak; + static const bool lazy = false; + + typedef T element_type; + typedef std::weak_ptr pointer_type; + typedef std::shared_ptr strong_pointer_type; + + static strong_pointer_type + lock (const pointer_type& p) + { + return p.lock (); + } + }; + +#endif // ODB_CXX11 + } #include -- cgit v1.1