diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2011-02-04 13:56:09 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2011-02-04 13:56:09 +0200 |
commit | f1c2a621ac2695dfe4a3bc71dbc9ce3079008d2f (patch) | |
tree | d0c45ecd5489cd1b4beb57fc5afda3263ad0d2b2 /odb/boost/smart-ptr | |
parent | 6a07b88cdf6419440b19e7b7dbc9231519c32c62 (diff) |
Add support for smart-ptr, unordered, and date_time (basic)
Diffstat (limited to 'odb/boost/smart-ptr')
-rw-r--r-- | odb/boost/smart-ptr/lazy-pointer-traits.hxx | 63 | ||||
-rw-r--r-- | odb/boost/smart-ptr/lazy-ptr.hxx | 237 | ||||
-rw-r--r-- | odb/boost/smart-ptr/lazy-ptr.ixx | 649 | ||||
-rw-r--r-- | odb/boost/smart-ptr/lazy-ptr.txx | 59 | ||||
-rw-r--r-- | odb/boost/smart-ptr/pointer-traits.hxx | 87 |
5 files changed, 1095 insertions, 0 deletions
diff --git a/odb/boost/smart-ptr/lazy-pointer-traits.hxx b/odb/boost/smart-ptr/lazy-pointer-traits.hxx new file mode 100644 index 0000000..d7a9428 --- /dev/null +++ b/odb/boost/smart-ptr/lazy-pointer-traits.hxx @@ -0,0 +1,63 @@ +// file : odb/boost/smart-ptr/lazy-pointer-traits.hxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_BOOST_SMART_PTR_LAZY_POINTER_TRAITS_HXX +#define ODB_BOOST_SMART_PTR_LAZY_POINTER_TRAITS_HXX + +#include <odb/pre.hxx> + +#include <odb/pointer-traits.hxx> +#include <odb/boost/smart-ptr/lazy-ptr.hxx> + +namespace odb +{ + template <typename T> + class pointer_traits<boost::lazy_shared_ptr<T> > + { + public: + static pointer_kind const kind = pk_shared; + static bool const lazy = true; + + typedef T element_type; + typedef boost::lazy_shared_ptr<element_type> pointer_type; + typedef ::boost::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<boost::lazy_weak_ptr<T> > + { + public: + static pointer_kind const kind = pk_weak; + static bool const lazy = true; + + typedef T element_type; + typedef boost::lazy_weak_ptr<element_type> pointer_type; + typedef boost::lazy_shared_ptr<element_type> strong_pointer_type; + typedef ::boost::weak_ptr<element_type> eager_pointer_type; + + static strong_pointer_type + lock (const pointer_type& p) + { + return p.lock (); + } + }; +} + +#include <odb/post.hxx> + +#endif // ODB_BOOST_SMART_PTR_LAZY_POINTER_TRAITS_HXX diff --git a/odb/boost/smart-ptr/lazy-ptr.hxx b/odb/boost/smart-ptr/lazy-ptr.hxx new file mode 100644 index 0000000..4dbf9bf --- /dev/null +++ b/odb/boost/smart-ptr/lazy-ptr.hxx @@ -0,0 +1,237 @@ +// file : odb/boost/smart-ptr/lazy-ptr.hxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_BOOST_SMART_PTR_LAZY_PTR_HXX +#define ODB_BOOST_SMART_PTR_LAZY_PTR_HXX + +#include <odb/pre.hxx> + +#include <memory> // std::auto_ptr + +#include <boost/shared_ptr.hpp> +#include <boost/weak_ptr.hpp> + +#include <odb/forward.hxx> // odb::database +#include <odb/traits.hxx> +#include <odb/lazy-ptr-impl.hxx> + +namespace odb +{ + namespace boost + { + template <class T> + class lazy_weak_ptr; + + // + // + template <class T> + class lazy_shared_ptr + { + // The standard shared_ptr interface. + // + public: + typedef T element_type; + + lazy_shared_ptr (); + 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); + + lazy_shared_ptr (const lazy_shared_ptr&); + template <class Y> lazy_shared_ptr (const lazy_shared_ptr<Y>&); + template <class Y> explicit lazy_shared_ptr (const lazy_weak_ptr<Y>&); + template <class Y> explicit lazy_shared_ptr (std::auto_ptr<Y>&); + + ~lazy_shared_ptr (); + + lazy_shared_ptr& operator= (const lazy_shared_ptr&); + template <class Y> lazy_shared_ptr& operator= (const lazy_shared_ptr<Y>&); + template <class Y> lazy_shared_ptr& operator= (std::auto_ptr<Y>&); + + void swap (lazy_shared_ptr&); + void reset (); + 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& operator* () const; + T* operator-> () const; + T* get () const; + + bool unique () const; + long use_count () const; + + typedef ::boost::shared_ptr<T> lazy_shared_ptr::*unspecified_bool_type; + operator unspecified_bool_type () const + { + return (p_ || i_) ? &lazy_shared_ptr::p_ : 0; + } + + // Initialization/assignment from shared_ptr and weak_ptr. + // + public: + template <class Y> lazy_shared_ptr (const ::boost::shared_ptr<Y>&); + template <class Y> explicit lazy_shared_ptr (const ::boost::weak_ptr<Y>&); + + template <class Y> lazy_shared_ptr& operator= (const ::boost::shared_ptr<Y>&); + + // Lazy loading interface. + // + public: + typedef odb::database database_type; + + bool loaded () const; + ::boost::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 ::boost::shared_ptr<Y>&); + template <class Y> lazy_shared_ptr (database_type&, const ::boost::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&, const std::auto_ptr<Y>&); + template <class Y> void reset (database_type&, const ::boost::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 ::boost::shared_ptr<T> p_; + mutable lazy_ptr_impl<T> i_; + }; + + // operator< and operator<< are not provided. + // + template<class T, class Y> + bool operator== (const lazy_shared_ptr<T>&, const lazy_shared_ptr<Y>&); + + template<class T, class Y> + bool operator!= (const lazy_shared_ptr<T>&, const lazy_shared_ptr<Y>&); + + template<class T> void swap (lazy_shared_ptr<T>&, lazy_shared_ptr<T>&); + + template<class D, class T> + D* get_deleter (const lazy_shared_ptr<T>&); + + // + // + template <class T> + class lazy_weak_ptr + { + // The standard weak_ptr interface. + // + public: + typedef T element_type; + + lazy_weak_ptr (); + template <class Y> lazy_weak_ptr (const lazy_shared_ptr<Y>&); + lazy_weak_ptr (const lazy_weak_ptr&); + template <class Y> lazy_weak_ptr (const lazy_weak_ptr<Y>&); + + ~lazy_weak_ptr (); + + lazy_weak_ptr& operator= (const lazy_weak_ptr&); + template <class Y> lazy_weak_ptr& operator= (const lazy_weak_ptr<Y>&); + template <class Y> lazy_weak_ptr& operator= (const lazy_shared_ptr<Y>&); + + void swap (lazy_weak_ptr<T>&); + void reset (); + + long use_count () const; + bool expired () const; + + lazy_shared_ptr<T> lock () const; + + // Initialization/assignment from shared_ptr and weak_ptr. + // + public: + template <class Y> lazy_weak_ptr (const ::boost::weak_ptr<Y>&); + template <class Y> lazy_weak_ptr (const ::boost::shared_ptr<Y>&); + + template <class Y> lazy_weak_ptr& operator= (const ::boost::weak_ptr<Y>&); + template <class Y> lazy_weak_ptr& operator= (const ::boost::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 transiend object + // + bool loaded () const; + + // Performs both lock and load. + // + ::boost::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 ::boost::shared_ptr<Y>&); + template <class Y> lazy_weak_ptr (database_type&, const ::boost::weak_ptr<Y>&); + + template <class ID> void reset (database_type&, const ID&); + template <class Y> void reset (database_type&, const ::boost::shared_ptr<Y>&); + template <class Y> void reset (database_type&, const ::boost::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 ::boost::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>&); + } +} + +#include <odb/boost/smart-ptr/lazy-ptr.ixx> +#include <odb/boost/smart-ptr/lazy-ptr.txx> + +#include <odb/boost/smart-ptr/lazy-pointer-traits.hxx> + +#include <odb/post.hxx> + +#endif // ODB_BOOST_SMART_PTR_LAZY_PTR_HXX diff --git a/odb/boost/smart-ptr/lazy-ptr.ixx b/odb/boost/smart-ptr/lazy-ptr.ixx new file mode 100644 index 0000000..2522ab4 --- /dev/null +++ b/odb/boost/smart-ptr/lazy-ptr.ixx @@ -0,0 +1,649 @@ +// file : odb/boost/smart-ptr/lazy-ptr.ixx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +namespace odb +{ + namespace boost + { + // + // lazy_shared_ptr + // + + template <class T> + inline lazy_shared_ptr<T>:: + lazy_shared_ptr () {} + + 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> + 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> + 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 ::boost::bad_weak_ptr (); + } + + template <class T> + template <class Y> + inline lazy_shared_ptr<T>:: + lazy_shared_ptr (std::auto_ptr<Y>& r): p_ (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> + template <class Y> + inline lazy_shared_ptr<T>& lazy_shared_ptr<T>:: + operator= (std::auto_ptr<Y>& r) + { + p_ = 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> + template <class Y> + inline lazy_shared_ptr<T>:: + lazy_shared_ptr (const ::boost::shared_ptr<Y>& r): p_ (r) {} + + template <class T> + template <class Y> + inline lazy_shared_ptr<T>:: + lazy_shared_ptr (const ::boost::weak_ptr<Y>& r): p_ (r) {} + + template <class T> + template <class Y> + inline lazy_shared_ptr<T>& lazy_shared_ptr<T>:: + operator= (const ::boost::shared_ptr<Y>& r) + { + p_ = r; + i_.reset (); + return *this; + } + + template <class T> + inline bool lazy_shared_ptr<T>:: + loaded () const + { + return p_ || !i_; + } + + template <class T> + inline ::boost::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_ (r) + { + if (p_) + i_.reset (db); + } + + template <class T> + template <class Y> + inline lazy_shared_ptr<T>:: + lazy_shared_ptr (database_type& db, const ::boost::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, const ::boost::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, const std::auto_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, const ::boost::shared_ptr<Y>& r) + { + p_ = 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, 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 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 ::boost::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 ::boost::weak_ptr<Y>& r): p_ (r) {} + + template <class T> + template <class Y> + inline lazy_weak_ptr<T>:: + lazy_weak_ptr (const ::boost::shared_ptr<Y>& r): p_ (r) {} + + template <class T> + template <class Y> + inline lazy_weak_ptr<T>& lazy_weak_ptr<T>:: + operator= (const ::boost::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 ::boost::shared_ptr<Y>& r) + { + p_ = r; + i_.reset (); + return this; + } + + template <class T> + inline bool lazy_weak_ptr<T>:: + loaded () const + { + return !expired () || !i_; + } + + template <class T> + inline ::boost::shared_ptr<T> lazy_weak_ptr<T>:: + load () const + { + ::boost::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 ::boost::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 ::boost::weak_ptr<Y>& r) + : p_ (r) + { + typedef typename object_traits<T>::object_type object_type; + + ::boost::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 ::boost::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 ::boost::weak_ptr<Y>& r) + { + typedef typename object_traits<T>::object_type object_type; + + p_ = r; + ::boost::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; + + ::boost::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); + } + } +} diff --git a/odb/boost/smart-ptr/lazy-ptr.txx b/odb/boost/smart-ptr/lazy-ptr.txx new file mode 100644 index 0000000..bca6fdb --- /dev/null +++ b/odb/boost/smart-ptr/lazy-ptr.txx @@ -0,0 +1,59 @@ +// file : odb/boost/smart-ptr/lazy-ptr.txx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +namespace odb +{ + namespace boost + { + // + // lazy_shared_ptr + // + + template <class T> + template <class Y> + bool lazy_shared_ptr<T>:: + equal (const lazy_shared_ptr<Y>& r) const + { + if (loaded () && r.loaded ()) + return p_ == r.p_; + + // If one of the object is not loaded, then we compare databases and + // object ids. Note that NULL pointers cannot have non-NULL databases + // and if both of them are NULL, we wouldn't have gotten here. + // + 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 + { + ::boost::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> (); + } + } + } +} diff --git a/odb/boost/smart-ptr/pointer-traits.hxx b/odb/boost/smart-ptr/pointer-traits.hxx new file mode 100644 index 0000000..62a63a6 --- /dev/null +++ b/odb/boost/smart-ptr/pointer-traits.hxx @@ -0,0 +1,87 @@ +// file : odb/boost/smart-ptr/pointer-traits.hxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_BOOST_SMART_PTR_POINTER_TRAITS_HXX +#define ODB_BOOST_SMART_PTR_POINTER_TRAITS_HXX + +#include <odb/pre.hxx> + +#include <boost/shared_ptr.hpp> +#include <boost/weak_ptr.hpp> + +#include <odb/pointer-traits.hxx> + +namespace odb +{ + // Specialization for boost::shared_ptr. + // + template <typename T> + class pointer_traits< ::boost::shared_ptr<T> > + { + public: + static pointer_kind const kind = pk_shared; + static bool const lazy = false; + + typedef T element_type; + typedef ::boost::shared_ptr<element_type> pointer_type; + typedef ::boost::shared_ptr<const element_type> const_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; + } + + public: + static void* + allocate (std::size_t n) + { + return operator new (n); + } + + static void + free (void* p) + { + operator delete (p); + } + }; + + // Specialization for boost::weak_ptr. + // + template <typename T> + class pointer_traits< ::boost::weak_ptr<T> > + { + public: + static pointer_kind const kind = pk_weak; + static bool const lazy = false; + + typedef T element_type; + typedef ::boost::weak_ptr<element_type> pointer_type; + typedef ::boost::shared_ptr<element_type> strong_pointer_type; + + static strong_pointer_type + lock (const pointer_type& p) + { + return p.lock (); + } + }; +} + +#include <odb/post.hxx> + +#endif // ODB_BOOST_SMART_PTR_POINTER_TRAITS_HXX |