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/lazy-ptr.txx | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) (limited to 'odb/lazy-ptr.txx') diff --git a/odb/lazy-ptr.txx b/odb/lazy-ptr.txx index 4bb511c..2265fec 100644 --- a/odb/lazy-ptr.txx +++ b/odb/lazy-ptr.txx @@ -38,4 +38,70 @@ namespace odb return i_.database () == r.i_.database () && object_id () == r.object_id (); } + +#ifdef ODB_CXX11 + + // + // lazy_shared_ptr + // + + template + template + bool lazy_shared_ptr:: + equal (const lazy_shared_ptr& r) const + { + bool t1 (!p_ == loaded ()); + bool t2 (!r.p_ == r.loaded ()); + + // If both are transient, then compare the underlying pointers. + // + if (t1 && t2) + return p_ == r.p_; + + // If one is transient and the other is persistent, then compare + // the underlying pointers but only if they are non NULL. Note + // that an unloaded persistent object is always unequal to a + // transient object. + // + if (t1 || t2) + return p_ == r.p_ && p_; + + // If both objects are persistent, then we compare databases and + // object ids. + // + 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::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 (); + } + } + +#endif // ODB_CXX11 + } -- cgit v1.1