From ec355c48c49074bebeb597f6e5dcedfeb9d52fae Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 9 Dec 2010 10:36:15 +0200 Subject: Add lazy pointer support Built-in support is provided for raw, auto, and tr1 shared/weak pointers. New test: common/lazy-ptr. --- odb/tr1/lazy-pointer-traits.hxx | 63 ++++ odb/tr1/lazy-ptr.hxx | 239 +++++++++++++++ odb/tr1/lazy-ptr.ixx | 649 ++++++++++++++++++++++++++++++++++++++++ odb/tr1/lazy-ptr.txx | 59 ++++ 4 files changed, 1010 insertions(+) create mode 100644 odb/tr1/lazy-pointer-traits.hxx create mode 100644 odb/tr1/lazy-ptr.hxx create mode 100644 odb/tr1/lazy-ptr.ixx create mode 100644 odb/tr1/lazy-ptr.txx (limited to 'odb/tr1') diff --git a/odb/tr1/lazy-pointer-traits.hxx b/odb/tr1/lazy-pointer-traits.hxx new file mode 100644 index 0000000..74276c5 --- /dev/null +++ b/odb/tr1/lazy-pointer-traits.hxx @@ -0,0 +1,63 @@ +// file : odb/tr1/lazy-pointer-traits.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_TR1_LAZY_POINTER_TRAITS_HXX +#define ODB_TR1_LAZY_POINTER_TRAITS_HXX + +#include + +#include +#include + +namespace odb +{ + template + class pointer_traits< tr1::lazy_shared_ptr > + { + public: + static pointer_kind const kind = pk_shared; + static bool const lazy = true; + + typedef T element_type; + typedef tr1::lazy_shared_ptr pointer_type; + typedef std::tr1::shared_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< tr1::lazy_weak_ptr > + { + public: + static pointer_kind const kind = pk_weak; + static bool const lazy = true; + + typedef T element_type; + typedef tr1::lazy_weak_ptr pointer_type; + typedef tr1::lazy_shared_ptr strong_pointer_type; + typedef std::tr1::weak_ptr eager_pointer_type; + + static strong_pointer_type + lock (const pointer_type& p) + { + return p.lock (); + } + }; +} + +#include + +#endif // ODB_TR1_LAZY_POINTER_TRAITS_HXX diff --git a/odb/tr1/lazy-ptr.hxx b/odb/tr1/lazy-ptr.hxx new file mode 100644 index 0000000..ea9bf78 --- /dev/null +++ b/odb/tr1/lazy-ptr.hxx @@ -0,0 +1,239 @@ +// file : odb/tr1/lazy-ptr.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_TR1_LAZY_PTR_HXX +#define ODB_TR1_LAZY_PTR_HXX + +#include + +// +// This header assumes that the necessary TR1 header has already +// been included. +// + +#include // std::auto_ptr + +#include // odb::database +#include +#include + +namespace odb +{ + namespace tr1 + { + template + class lazy_weak_ptr; + + // + // + template + class lazy_shared_ptr + { + // The standard shared_ptr interface. + // + public: + typedef T element_type; + + lazy_shared_ptr (); + template explicit lazy_shared_ptr (Y*); + template lazy_shared_ptr (Y*, D); + template lazy_shared_ptr (Y*, D, A); + + lazy_shared_ptr (const lazy_shared_ptr&); + template lazy_shared_ptr (const lazy_shared_ptr&); + template explicit lazy_shared_ptr (const lazy_weak_ptr&); + template explicit lazy_shared_ptr (std::auto_ptr&); + + ~lazy_shared_ptr (); + + lazy_shared_ptr& operator= (const lazy_shared_ptr&); + template lazy_shared_ptr& operator= (const lazy_shared_ptr&); + template lazy_shared_ptr& operator= (std::auto_ptr&); + + void swap (lazy_shared_ptr&); + void reset (); + template void reset (Y*); + template void reset (Y*, D); + template void reset (Y*, D, A); + + T& operator* () const; + T* operator-> () const; + T* get () const; + + bool unique () const; + long use_count () const; + + typedef std::tr1::shared_ptr 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 lazy_shared_ptr (const std::tr1::shared_ptr&); + template explicit lazy_shared_ptr (const std::tr1::weak_ptr&); + + template lazy_shared_ptr& operator= (const std::tr1::shared_ptr&); + + // Lazy loading interface. + // + public: + typedef odb::database database_type; + + bool loaded () const; + std::tr1::shared_ptr load () const; + + // Unload the pointer. For transient objects this function is + // equivalent to reset(). + // + void unload () const; + + template lazy_shared_ptr (database_type&, const ID&); + template lazy_shared_ptr (database_type&, Y*); + template lazy_shared_ptr (database_type&, Y*, D); + template lazy_shared_ptr (database_type&, Y*, D, A); + template lazy_shared_ptr (database_type&, std::auto_ptr&); + template lazy_shared_ptr (database_type&, const std::tr1::shared_ptr&); + template lazy_shared_ptr (database_type&, const std::tr1::weak_ptr&); + + template void reset (database_type&, const ID&); + template void reset (database_type&, Y*); + template void reset (database_type&, Y*, D); + template void reset (database_type&, Y*, D, A); + template void reset (database_type&, const std::auto_ptr&); + template void reset (database_type&, const std::tr1::shared_ptr&); + + template + typename object_traits::id_type object_id () const; + + database_type& database () const; + + // Helpers. + // + public: + template bool equal (const lazy_shared_ptr&) const; + + private: + template friend class lazy_shared_ptr; + template friend class lazy_weak_ptr; + + mutable std::tr1::shared_ptr p_; + mutable lazy_ptr_impl i_; + }; + + // operator< and operator<< are not provided. + // + template + bool operator== (const lazy_shared_ptr&, const lazy_shared_ptr&); + + template + bool operator!= (const lazy_shared_ptr&, const lazy_shared_ptr&); + + template void swap (lazy_shared_ptr&, lazy_shared_ptr&); + + template + D* get_deleter (const lazy_shared_ptr&); + + // + // + template + class lazy_weak_ptr + { + // The standard weak_ptr interface. + // + public: + typedef T element_type; + + lazy_weak_ptr (); + template lazy_weak_ptr (const lazy_shared_ptr&); + lazy_weak_ptr (const lazy_weak_ptr&); + template lazy_weak_ptr (const lazy_weak_ptr&); + + ~lazy_weak_ptr (); + + lazy_weak_ptr& operator= (const lazy_weak_ptr&); + template lazy_weak_ptr& operator= (const lazy_weak_ptr&); + template lazy_weak_ptr& operator= (const lazy_shared_ptr&); + + void swap (lazy_weak_ptr&); + void reset (); + + long use_count () const; + bool expired () const; + + lazy_shared_ptr lock () const; + + // Initialization/assignment from shared_ptr and weak_ptr. + // + public: + template lazy_weak_ptr (const std::tr1::weak_ptr&); + template lazy_weak_ptr (const std::tr1::shared_ptr&); + + template lazy_weak_ptr& operator= (const std::tr1::weak_ptr&); + template lazy_weak_ptr& operator= (const std::tr1::shared_ptr&); + + // 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. + // + std::tr1::shared_ptr load () const; + + // Unload the pointer. For transient objects this function is + // equivalent to reset(). + // + void unload () const; + + template lazy_weak_ptr (database_type&, const ID&); + template lazy_weak_ptr (database_type&, const std::tr1::shared_ptr&); + template lazy_weak_ptr (database_type&, const std::tr1::weak_ptr&); + + template void reset (database_type&, const ID&); + template void reset (database_type&, const std::tr1::shared_ptr&); + template void reset (database_type&, const std::tr1::weak_ptr&); + + // The object_id() function can only be called when the object is + // persistent, or: expired() XOR loaded() (can use != for XOR). + // + template + typename object_traits::id_type object_id () const; + + database_type& database () const; + + private: + template friend class lazy_shared_ptr; + template friend class lazy_weak_ptr; + + mutable std::tr1::weak_ptr p_; + mutable lazy_ptr_impl i_; + }; + + // operator< is not provided. + // + template void swap (lazy_weak_ptr&, lazy_weak_ptr&); + } +} + +#include +#include + +#include + +#include + +#endif // ODB_TR1_LAZY_PTR_HXX diff --git a/odb/tr1/lazy-ptr.ixx b/odb/tr1/lazy-ptr.ixx new file mode 100644 index 0000000..3e217b1 --- /dev/null +++ b/odb/tr1/lazy-ptr.ixx @@ -0,0 +1,649 @@ +// file : odb/tr1/lazy-ptr.ixx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +namespace odb +{ + namespace tr1 + { + // + // lazy_shared_ptr + // + + template + inline lazy_shared_ptr:: + lazy_shared_ptr () {} + + template + template + inline lazy_shared_ptr:: + lazy_shared_ptr (Y* p): p_ (p) {} + + template + template + inline lazy_shared_ptr:: + lazy_shared_ptr (Y* p, D d): p_ (p, d) {} + + template + template + inline lazy_shared_ptr:: + lazy_shared_ptr (Y* p, D d, A a): p_ (p, d, a) {} + + template + inline lazy_shared_ptr:: + lazy_shared_ptr (const lazy_shared_ptr& r): p_ (r.p_), i_ (r.i_) {} + + template + template + inline lazy_shared_ptr:: + lazy_shared_ptr (const lazy_shared_ptr& r): p_ (r.p_), i_ (r.i_) {} + + template + template + inline lazy_shared_ptr:: + lazy_shared_ptr (const lazy_weak_ptr& 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 std::tr1::bad_weak_ptr (); + } + + template + template + inline lazy_shared_ptr:: + lazy_shared_ptr (std::auto_ptr& r): p_ (r) {} + + template + inline lazy_shared_ptr:: + ~lazy_shared_ptr () {} + + template + inline lazy_shared_ptr& lazy_shared_ptr:: + operator= (const lazy_shared_ptr& r) + { + p_ = r.p_; + i_ = r.i_; + return *this; + } + + template + template + inline lazy_shared_ptr& lazy_shared_ptr:: + operator= (const lazy_shared_ptr& r) + { + p_ = r.p_; + i_ = r.i_; + return *this; + } + + template + template + inline lazy_shared_ptr& lazy_shared_ptr:: + operator= (std::auto_ptr& r) + { + p_ = r; + i_.reset (); + return *this; + } + + template + inline void lazy_shared_ptr:: + swap (lazy_shared_ptr& b) + { + p_.swap (b.p_); + i_.swap (b.i_); + } + + template + inline void lazy_shared_ptr:: + reset () + { + p_.reset (); + i_.reset (); + } + + template + template + inline void lazy_shared_ptr:: + reset (Y* p) + { + p_.reset (p); + i_.reset (); + } + + template + template + inline void lazy_shared_ptr:: + reset (Y* p, D d) + { + p_.reset (p, d); + i_.reset (); + } + + template + template + inline void lazy_shared_ptr:: + reset (Y* p, D d, A a) + { + p_.reset (p, d, a); + i_.reset (); + } + + template + inline T& lazy_shared_ptr:: + operator* () const + { + return *p_; + } + + template + inline T* lazy_shared_ptr:: + operator-> () const + { + return p_.operator-> (); + } + + template + inline T* lazy_shared_ptr:: + get () const + { + return p_.get (); + } + + template + inline bool lazy_shared_ptr:: + unique () const + { + return p_.unique (); + } + + template + inline long lazy_shared_ptr:: + use_count () const + { + return p_.use_count (); + } + + template + template + inline lazy_shared_ptr:: + lazy_shared_ptr (const std::tr1::shared_ptr& r): p_ (r) {} + + template + template + inline lazy_shared_ptr:: + lazy_shared_ptr (const std::tr1::weak_ptr& r): p_ (r) {} + + template + template + inline lazy_shared_ptr& lazy_shared_ptr:: + operator= (const std::tr1::shared_ptr& r) + { + p_ = r; + i_.reset (); + return *this; + } + + template + inline bool lazy_shared_ptr:: + loaded () const + { + return p_ || !i_; + } + + template + inline std::tr1::shared_ptr lazy_shared_ptr:: + load () const + { + if (!loaded ()) + p_ = i_.template load (true); // Reset id. + + return p_; + } + + template + inline void lazy_shared_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_shared_ptr:: + lazy_shared_ptr (database_type& db, const ID& id): i_ (db, id) {} + + template + template + inline lazy_shared_ptr:: + lazy_shared_ptr (database_type& db, Y* p) + : p_ (p) + { + if (p_) + i_.reset (db); + } + + template + template + inline lazy_shared_ptr:: + lazy_shared_ptr (database_type& db, Y* p, D d) + : p_ (p, d) + { + if (p_) + i_.reset (db); + } + + template + template + inline lazy_shared_ptr:: + lazy_shared_ptr (database_type& db, Y* p, D d, A a) + : p_ (p, d, a) + { + if (p_) + i_.reset (db); + } + + template + template + inline lazy_shared_ptr:: + lazy_shared_ptr (database_type& db, std::auto_ptr& r) + : p_ (r) + { + if (p_) + i_.reset (db); + } + + template + template + inline lazy_shared_ptr:: + lazy_shared_ptr (database_type& db, const std::tr1::shared_ptr& r) + : p_ (r) + { + if (p_) + i_.reset (db); + } + + template + template + inline lazy_shared_ptr:: + lazy_shared_ptr (database_type& db, const std::tr1::weak_ptr& r) + : p_ (r) + { + if (p_) + i_.reset (db); + } + + template + template + inline void lazy_shared_ptr:: + reset (database_type& db, const ID& id) + { + p_.reset (); + i_.reset (db, id); + } + + template + template + inline void lazy_shared_ptr:: + reset (database_type& db, Y* p) + { + p_.reset (p); + + if (p_) + i_.reset (db); + else + i_.reset (); + } + + template + template + inline void lazy_shared_ptr:: + reset (database_type& db, Y* p, D d) + { + p_.reset (p, d); + + if (p_) + i_.reset (db); + else + i_.reset (); + } + + template + template + inline void lazy_shared_ptr:: + reset (database_type& db, Y* p, D d, A a) + { + p_.reset (p, d, a); + + if (p_) + i_.reset (db); + else + i_.reset (); + } + + template + template + inline void lazy_shared_ptr:: + reset (database_type& db, const std::auto_ptr& r) + { + p_ = r; + + if (p_) + i_.reset (db); + else + i_.reset (); + } + + template + template + inline void lazy_shared_ptr:: + reset (database_type& db, const std::tr1::shared_ptr& r) + { + p_ = r; + + if (p_) + i_.reset (db); + else + i_.reset (); + } + + template + template + inline typename object_traits::id_type lazy_shared_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_shared_ptr::database_type& lazy_shared_ptr:: + database () const + { + return *i_.database (); + } + + template + inline bool + operator== (const lazy_shared_ptr& a, const lazy_shared_ptr& b) + { + return a.equal (b); + } + + template + inline bool + operator!= (const lazy_shared_ptr& a, const lazy_shared_ptr& b) + { + return !a.equal (b); + } + + template + inline void + swap (lazy_shared_ptr& a, lazy_shared_ptr& b) + { + a.swap (b); + } + + template + inline D* + get_deleter (const lazy_shared_ptr& p) + { + return std::tr1::get_deleter (p.p_); + } + + + // + // lazy_weak_ptr + // + + template + inline lazy_weak_ptr:: + lazy_weak_ptr () {} + + template + template + inline lazy_weak_ptr:: + lazy_weak_ptr (const lazy_shared_ptr& r): p_ (r.p_), i_ (r.i_) {} + + template + inline lazy_weak_ptr:: + lazy_weak_ptr (const lazy_weak_ptr& r): p_ (r.p_), i_ (r.i_) {} + + template + template + inline lazy_weak_ptr:: + lazy_weak_ptr (const lazy_weak_ptr& r): p_ (r.p_), i_ (r.i_) {} + + template + inline lazy_weak_ptr:: + ~lazy_weak_ptr () {} + + template + inline lazy_weak_ptr& lazy_weak_ptr:: + operator= (const lazy_weak_ptr& r) + { + p_ = r.p_; + i_ = r.i_; + return *this; + } + + template + template + inline lazy_weak_ptr& lazy_weak_ptr:: + operator= (const lazy_weak_ptr& r) + { + p_ = r.p_; + i_ = r.i_; + return *this; + } + + template + template + inline lazy_weak_ptr& lazy_weak_ptr:: + operator= (const lazy_shared_ptr& r) + { + p_ = r.p_; + i_ = r.i_; + return *this; + } + + template + inline void lazy_weak_ptr:: + swap (lazy_weak_ptr& r) + { + p_.swap (r.p_); + i_.swap (r.i_); + } + + template + inline void lazy_weak_ptr:: + reset () + { + p_.reset (); + i_.reset (); + } + + template + inline long lazy_weak_ptr:: + use_count () const + { + return p_.use_count (); + } + + template + inline bool lazy_weak_ptr:: + expired () const + { + return p_.expired (); + } + + template + template + inline lazy_weak_ptr:: + lazy_weak_ptr (const std::tr1::weak_ptr& r): p_ (r) {} + + template + template + inline lazy_weak_ptr:: + lazy_weak_ptr (const std::tr1::shared_ptr& r): p_ (r) {} + + template + template + inline lazy_weak_ptr& lazy_weak_ptr:: + operator= (const std::tr1::weak_ptr& r) + { + p_ = r; + i_.reset (); + return this; + } + + template + template + inline lazy_weak_ptr& lazy_weak_ptr:: + operator= (const std::tr1::shared_ptr& r) + { + p_ = r; + i_.reset (); + return this; + } + + template + inline bool lazy_weak_ptr:: + loaded () const + { + return !expired () || !i_; + } + + template + inline std::tr1::shared_ptr lazy_weak_ptr:: + load () const + { + std::tr1::shared_ptr r (p_.lock ()); + + if (r || !i_) + return r; + + r = i_.template load (false); // Keep id. + p_ = r; + return r; + } + + template + inline void lazy_weak_ptr:: + unload () const + { + // With weak pointer we always keep i_ up to date. + // + p_.reset (); + } + + template + template + inline lazy_weak_ptr:: + lazy_weak_ptr (database_type& db, const ID& id): i_ (db, id) {} + + template + template + inline lazy_weak_ptr:: + lazy_weak_ptr (database_type& db, const std::tr1::shared_ptr& r) + : p_ (r) + { + typedef typename object_traits::object_type object_type; + + if (r) + i_.reset (db, object_traits::id (*r)); + } + + template + template + inline lazy_weak_ptr:: + lazy_weak_ptr (database_type& db, const std::tr1::weak_ptr& r) + : p_ (r) + { + typedef typename object_traits::object_type object_type; + + std::tr1::shared_ptr sp (p_.lock ()); + + if (sp) + i_.reset (db, object_traits::id (*sp)); + } + + template + template + inline void lazy_weak_ptr:: + reset (database_type& db, const ID& id) + { + p_.reset (); + i_.reset (db, id); + } + + template + template + inline void lazy_weak_ptr:: + reset (database_type& db, const std::tr1::shared_ptr& r) + { + typedef typename object_traits::object_type object_type; + + p_ = r; + + if (r) + i_.reset (db, object_traits::id (*r)); + else + i_.reset (); + } + + template + template + inline void lazy_weak_ptr:: + reset (database_type& db, const std::tr1::weak_ptr& r) + { + typedef typename object_traits::object_type object_type; + + p_ = r; + std::tr1::shared_ptr sp (p_.lock ()); + + if (sp) + i_.reset (db, object_traits::id (*sp)); + else + i_.reset (); + } + + template + template + inline typename object_traits::id_type lazy_weak_ptr:: + object_id () const + { + typedef typename object_traits::object_type object_type; + + std::tr1::shared_ptr sp (p_.lock ()); + return sp ? object_traits::id (*sp) : i_.object_id (); + } + + template + inline typename lazy_weak_ptr::database_type& lazy_weak_ptr:: + database () const + { + return *i_.database (); + } + + template + inline void + swap (lazy_weak_ptr& a, lazy_weak_ptr& b) + { + a.swap (b); + } + } +} diff --git a/odb/tr1/lazy-ptr.txx b/odb/tr1/lazy-ptr.txx new file mode 100644 index 0000000..b7b5bc5 --- /dev/null +++ b/odb/tr1/lazy-ptr.txx @@ -0,0 +1,59 @@ +// file : odb/tr1/lazy-ptr.txx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +namespace odb +{ + namespace tr1 + { + // + // lazy_shared_ptr + // + + template + template + bool lazy_shared_ptr:: + equal (const lazy_shared_ptr& 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::object_type object_type1; + typedef typename object_traits::object_type object_type2; + + return i_.database () == r.i_.database () && + object_id () == r.object_id (); + } + + // + // lazy_weak_ptr + // + + template + lazy_shared_ptr lazy_weak_ptr:: + lock () const + { + std::tr1::shared_ptr sp (p_.lock ()); + + if (sp) + { + if (database_type* db = i_.database ()) + return lazy_shared_ptr (*db, sp); + else + return lazy_shared_ptr (sp); + } + else + { + if (i_) + return lazy_shared_ptr (*i_.database (), i_.object_id ()); + else + return lazy_shared_ptr (); + } + } + } +} -- cgit v1.1