diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2012-02-28 12:42:00 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2012-02-28 12:42:00 +0200 |
commit | eaf07e23f93813fa2c2e6b4d67e69fc6f4c48673 (patch) | |
tree | 3b6a54db5d3019121b0bbd553322248ce45db4c2 /odb/pointer-traits.hxx | |
parent | 3012187847b2783c49153db3f5486b84074cb2b0 (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/pointer-traits.hxx')
-rw-r--r-- | odb/pointer-traits.hxx | 84 |
1 files changed, 83 insertions, 1 deletions
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> |