From 7db38e8c622c2cd23d7a38e3cf70cb1b00dbc838 Mon Sep 17 00:00:00 2001 From: Constantin Michael Date: Tue, 12 Apr 2011 17:52:37 +0200 Subject: Add qt/smart-ptr implementation --- odb/qt.options | 1 + odb/qt/lazy-ptr.hxx | 11 + odb/qt/smart-ptr.options | 16 + odb/qt/smart-ptr/lazy-pointer-traits.hxx | 63 ++++ odb/qt/smart-ptr/lazy-ptr.hxx | 415 +++++++++++++++++++++ odb/qt/smart-ptr/lazy-ptr.ixx | 603 +++++++++++++++++++++++++++++++ odb/qt/smart-ptr/lazy-ptr.txx | 99 +++++ odb/qt/smart-ptr/pointer-traits.hxx | 87 +++++ odb/qt/version.hxx | 6 +- odb/qt/version.options | 2 +- version | 2 +- 11 files changed, 1300 insertions(+), 5 deletions(-) create mode 100644 odb/qt/lazy-ptr.hxx create mode 100644 odb/qt/smart-ptr.options create mode 100644 odb/qt/smart-ptr/lazy-pointer-traits.hxx create mode 100644 odb/qt/smart-ptr/lazy-ptr.hxx create mode 100644 odb/qt/smart-ptr/lazy-ptr.ixx create mode 100644 odb/qt/smart-ptr/lazy-ptr.txx create mode 100644 odb/qt/smart-ptr/pointer-traits.hxx diff --git a/odb/qt.options b/odb/qt.options index 76c25ec..a1360f2 100644 --- a/odb/qt.options +++ b/odb/qt.options @@ -5,3 +5,4 @@ --profile qt/basic --profile qt/date-time +--profile qt/smart-ptr diff --git a/odb/qt/lazy-ptr.hxx b/odb/qt/lazy-ptr.hxx new file mode 100644 index 0000000..4587132 --- /dev/null +++ b/odb/qt/lazy-ptr.hxx @@ -0,0 +1,11 @@ +// file : odb/qt/lazy-ptr.hxx +// author : Constantin Michael +// copyright : Copyright (c) 2005-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_QT_LAZY_PTR_HXX +#define ODB_QT_LAZY_PTR_HXX + +#include + +#endif // ODB_QT_LAZY_PTR_HXX diff --git a/odb/qt/smart-ptr.options b/odb/qt/smart-ptr.options new file mode 100644 index 0000000..f9de219 --- /dev/null +++ b/odb/qt/smart-ptr.options @@ -0,0 +1,16 @@ +# file : odb/qt/smart-ptr.options +# author : Constantin Michael +# copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +# license : GNU GPL v2; see accompanying LICENSE file + +--profile qt/version + +# Make QSharedPointer the default object pointer. +# +--hxx-prologue '#include ' +--default-pointer QSharedPointer + +# Include pointer traits. +# +--odb-epilogue '#include ' +--hxx-prologue '#include ' diff --git a/odb/qt/smart-ptr/lazy-pointer-traits.hxx b/odb/qt/smart-ptr/lazy-pointer-traits.hxx new file mode 100644 index 0000000..89c1222 --- /dev/null +++ b/odb/qt/smart-ptr/lazy-pointer-traits.hxx @@ -0,0 +1,63 @@ +// file : odb/qt/smart-ptr/lazy-pointer-traits.hxx +// author : Constantin Michael +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_QT_SMART_PTR_LAZY_POINTER_TRAITS_HXX +#define ODB_QT_SMART_PTR_LAZY_POINTER_TRAITS_HXX + +#include + +#include +#include + +namespace odb +{ + template + class pointer_traits > + { + public: + static const pointer_kind kind = pk_shared; + static const bool lazy = true; + + typedef T element_type; + typedef QLazySharedPointer pointer_type; + typedef QSharedPointer 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.template objectId (); + } + }; + + template + class pointer_traits > + { + public: + static const pointer_kind kind = pk_weak; + static const bool lazy = true; + + typedef T element_type; + typedef QLazyWeakPointer pointer_type; + typedef QLazySharedPointer strong_pointer_type; + typedef QWeakPointer eager_pointer_type; + + static strong_pointer_type + lock (const pointer_type& p) + { + return p.toStrongRef (); + } + }; +} + +#include + +#endif // ODB_QT_SMART_PTR_LAZY_POINTER_TRAITS_HXX diff --git a/odb/qt/smart-ptr/lazy-ptr.hxx b/odb/qt/smart-ptr/lazy-ptr.hxx new file mode 100644 index 0000000..0d0e60a --- /dev/null +++ b/odb/qt/smart-ptr/lazy-ptr.hxx @@ -0,0 +1,415 @@ +// file : odb/qt/smart-ptr/lazy-ptr.hxx +// author : Constantin Michael +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_QT_SMART_PTR_LAZY_PTR_HXX +#define ODB_QT_SMART_PTR_LAZY_PTR_HXX + +#include + +#include +#include +#include + +#include // odb::database +#include +#include + +template +class QLazyWeakPointer; + +// +// QLazySharedPointer declaration. +// + +template +class QLazySharedPointer +{ + // The standard QSharedPointer interface. + // +public: + // These typedefs are inherited by QSharedPointer indirectly from + // QtSharedPointer::Basic + // + typedef T Type; + typedef T element_type; + typedef T value_type; + typedef value_type* pointer; + typedef const value_type* const_pointer; + typedef value_type& reference; + typedef const value_type& const_reference; + typedef qptrdiff difference_type; + + QLazySharedPointer (); + + explicit + QLazySharedPointer (T*); + + template + QLazySharedPointer (T*, Deleter); + + QLazySharedPointer (const QLazySharedPointer&); + + template + QLazySharedPointer (const QLazySharedPointer&); + + template + QLazySharedPointer (const QLazyWeakPointer&); + + ~QLazySharedPointer (); + + QLazySharedPointer& + operator= (const QLazySharedPointer&); + + template + QLazySharedPointer& + operator= (const QLazySharedPointer&); + + template + QLazySharedPointer& + operator= (const QLazyWeakPointer&); + + typedef QSharedPointer QLazySharedPointer::*unspecified_bool_type; + operator unspecified_bool_type () const + { + return isNull () ? 0 : &QLazySharedPointer::p_; + } + + bool + operator! () const; + + T& + operator* () const; + + T* + operator-> () const; + + void + swap (QLazySharedPointer&); + + bool + isNull () const; + + T* + data () const; + + QLazyWeakPointer + toWeakRef () const; + + void + clear (); + + template + QLazySharedPointer + staticCast () const; + + template + QLazySharedPointer + dynamicCast () const; + + template + QLazySharedPointer + constCast () const; + + // Initialization and assignment from QSharedPointer and QWeakPointer. + // +public: + template + QLazySharedPointer (const QSharedPointer&); + + template + explicit + QLazySharedPointer (const QWeakPointer&); + + template + QLazySharedPointer& + operator= (const QSharedPointer&); + + template + QLazySharedPointer& + operator= (const QWeakPointer&); + + // Lazy loading interface. + // +public: + typedef odb::database database_type; + + // isNull() loaded() + // + // true true NULL pointer to transient object + // false true valid pointer to persistent object + // true false unloaded pointer to persistent object + // false false valid pointer to transient object + // + bool + loaded () const; + + QSharedPointer + load () const; + + // Unload the pointer. For transient objects this function is + // equivalent to clear(). + // + void unload () const; + + template + QLazySharedPointer (database_type&, const ID&); + + QLazySharedPointer (database_type&, T*); + + template + QLazySharedPointer (database_type&, T*, Deleter); + + template + QLazySharedPointer (database_type&, const QSharedPointer&); + + template + QLazySharedPointer (database_type&, const QWeakPointer&); + + template + typename odb::object_traits::id_type + objectId () const; + + database_type& + database () const; + + // Helpers. + // +public: + template + bool + equal (const QLazySharedPointer&) const; + +private: + template friend class QLazySharedPointer; + template friend class QLazyWeakPointer; + + mutable QSharedPointer p_; + mutable odb::lazy_ptr_impl i_; +}; + +// +// QLazySharedPointer related non-members. +// + +template +QLazySharedPointer +qSharedPointerCast (const QLazySharedPointer&); + +template +QLazySharedPointer +qSharedPointerConstCast (const QLazySharedPointer&); + +template +QLazySharedPointer +qSharedPointerDynamicCast (const QLazySharedPointer&); + +template +bool +operator== (const QLazySharedPointer&, const QLazySharedPointer&); + +template +bool +operator!= (const QLazySharedPointer&, const QLazySharedPointer&); + +// +// QLazyWeakPointer declaration +// + +template +class QLazyWeakPointer +{ + // The standard QWeakPointer interface + // +public: + typedef T element_type; + typedef T value_type; + typedef value_type* pointer; + typedef const value_type* const_pointer; + typedef value_type& reference; + typedef const value_type& const_reference; + typedef qptrdiff difference_type; + + QLazyWeakPointer (); + + QLazyWeakPointer (const QLazyWeakPointer&); + + template + QLazyWeakPointer (const QLazyWeakPointer&); + + template + QLazyWeakPointer (const QLazySharedPointer&); + + ~QLazyWeakPointer (); + + QLazyWeakPointer& + operator= (const QLazyWeakPointer&); + + template + QLazyWeakPointer& + operator= (const QLazyWeakPointer&); + + template + QLazyWeakPointer& + operator= (const QLazySharedPointer&); + + typedef QWeakPointer QLazyWeakPointer::*unspecified_bool_type; + operator unspecified_bool_type () const + { + return isNull () ? 0 : &QLazyWeakPointer::p_; + } + + bool + operator! () const; + +#ifdef QWEAKPOINTER_ENABLE_ARROW + T* + operator-> () const; +#endif + + void + clear (); + + T* + data () const; + + bool + isNull () const; + + QLazySharedPointer + toStrongRef () const; + + // Initialization/assignment from QSharedPointer and QWeakPointer. + // +public: + template + QLazyWeakPointer (const QWeakPointer&); + + template + QLazyWeakPointer (const QSharedPointer&); + + template + QLazyWeakPointer& + operator= (const QWeakPointer&); + + template + QLazyWeakPointer& + operator= (const QSharedPointer&); + + // Lazy loading interface. + // +public: + typedef odb::database database_type; + + // toStrongRef().isNull() 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 transient object + // + bool + loaded () const; + + // Create a strong reference using toStrongRef() and load. + // + QSharedPointer + load () const; + + // Unload the pointer. For transient objects this function is equivalent + // to clear(). + // + void + unload () const; + + template + QLazyWeakPointer (database_type&, const ID&); + + template + QLazyWeakPointer (database_type&, const QSharedPointer&); + + template + QLazyWeakPointer (database_type&, const QWeakPointer&); + + // The objectId() function can only be called when the object is persistent, + // or: toStrongRef().isNull() XOR loaded() (can use != for XOR). + // + template + typename odb::object_traits::id_type + objectId () const; + + database_type& + database () const; + + // Helpers. + // +public: + template + bool + equal (const QLazyWeakPointer&) const; + + template + bool + equal (const QLazySharedPointer&) const; + +private: + template friend class QLazySharedPointer; + template friend class QLazyWeakPointer; + + mutable QWeakPointer p_; + mutable odb::lazy_ptr_impl i_; +}; + +// +// QLazyWeakPointer related non-members. +// + +template +QLazySharedPointer +qSharedPointerCast (const QLazyWeakPointer&); + +template +QLazySharedPointer +qSharedPointerConstCast (const QLazyWeakPointer&); + +template +QLazySharedPointer +qSharedPointerDynamicCast (const QLazyWeakPointer&); + +template +QLazyWeakPointer +qWeakPointerCast (const QLazyWeakPointer&); + +template +bool +operator== (const QLazyWeakPointer&, const QLazyWeakPointer&); + +template +bool +operator== (const QLazyWeakPointer&, const QLazySharedPointer&); + +template +bool +operator== (const QLazySharedPointer&, const QLazyWeakPointer&); + +template +bool +operator!= (const QLazyWeakPointer&, const QLazyWeakPointer&); + +template +bool +operator!= (const QLazyWeakPointer&, const QLazySharedPointer&); + +template +bool +operator!= (const QLazySharedPointer&, const QLazyWeakPointer&); + +#include +#include + +#include + +#include + +#endif // ODB_QT_SMART_PTR_LAZY_PTR_HXX diff --git a/odb/qt/smart-ptr/lazy-ptr.ixx b/odb/qt/smart-ptr/lazy-ptr.ixx new file mode 100644 index 0000000..331f20a --- /dev/null +++ b/odb/qt/smart-ptr/lazy-ptr.ixx @@ -0,0 +1,603 @@ +// file : odb/qt/smart-ptr/lazy-ptr.ixx +// author : Constantin Michael +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +// +// QLazySharedPointer definition. +// + +template +inline QLazySharedPointer:: +QLazySharedPointer () {} + +template +inline QLazySharedPointer:: +QLazySharedPointer (T* p): p_ (p) {} + +template +template +inline QLazySharedPointer:: +QLazySharedPointer (T* p, Deleter d): p_ (p, d) {} + +template +inline QLazySharedPointer:: +QLazySharedPointer (const QLazySharedPointer& r): p_ (r.p_), i_ (r.i_) {} + +template +template +inline QLazySharedPointer:: +QLazySharedPointer (const QLazySharedPointer& r): p_ (r.p_), i_ (r.i_) {} + +template +template +inline QLazySharedPointer:: +QLazySharedPointer (const QLazyWeakPointer& r): p_ (r.p_), i_ (r.i_) {} + +template +inline QLazySharedPointer:: +~QLazySharedPointer () {} + +template +inline QLazySharedPointer& QLazySharedPointer:: +operator= (const QLazySharedPointer& r) +{ + p_ = r.p_; + i_ = r.i_; + return *this; +} + +template +template +inline QLazySharedPointer& QLazySharedPointer:: +operator= (const QLazySharedPointer& r) +{ + p_ = r.p_; + i_ = r.i_; + return *this; +} + +template +template +inline QLazySharedPointer& QLazySharedPointer:: +operator= (const QLazyWeakPointer& r) +{ + p_ = r.p_; + i_ = r.i_; + return *this; +} + +template +inline bool QLazySharedPointer:: +operator! () const +{ + return isNull (); +} + +template +inline T& QLazySharedPointer:: +operator* () const +{ + return *p_; +} + +template +inline T* QLazySharedPointer:: +operator-> () const +{ + return p_.operator-> (); +} + +template +inline void QLazySharedPointer:: +swap (QLazySharedPointer& x) +{ + p_.swap (x.p_); + i_.swap (x.i_); +} + +template +inline bool QLazySharedPointer:: +isNull () const +{ + return !(p_ || i_); +} + +template +inline T* QLazySharedPointer:: +data () const +{ + return p_.data (); +} + +template +inline QLazyWeakPointer QLazySharedPointer:: +toWeakRef () const +{ + return QLazyWeakPointer (*this); +} + +template +inline void QLazySharedPointer:: +clear () +{ + p_.clear (); + i_.reset (); +} + +template +template +inline QLazySharedPointer QLazySharedPointer:: +staticCast () const +{ + QLazySharedPointer c (p_.staticCast ()); + c.i_ = i_; + return c; +} + +template +template +inline QLazySharedPointer QLazySharedPointer:: +dynamicCast () const +{ + QLazySharedPointer c (p_.dynamicCast ()); + + if (c) + c.i_ = i_; + + return c; +} + +template +template +inline QLazySharedPointer QLazySharedPointer:: +constCast () const +{ + QLazySharedPointer c (p_.constCast ()); + c.i_ = i_; + return c; +} + +template +template +inline QLazySharedPointer:: +QLazySharedPointer (const QSharedPointer& r): p_ (r) {} + +template +template +inline QLazySharedPointer:: +QLazySharedPointer (const QWeakPointer& r): p_ (r) {} + +template +template +inline QLazySharedPointer& QLazySharedPointer:: +operator= (const QSharedPointer& r) +{ + p_ = r; + i_.reset (); + return *this; +} + +template +template +inline QLazySharedPointer& QLazySharedPointer:: +operator= (const QWeakPointer& r) +{ + p_ = r; + i_.reset (); + return *this; +} + +template +inline bool QLazySharedPointer:: +loaded () const +{ + bool i (i_); + return !p_ != i; // !p_ XOR i_ +} + +template +inline QSharedPointer QLazySharedPointer:: +load () const +{ + if (!loaded ()) + p_ = i_.template load (true); // Reset id. + + return p_; +} + +template +inline void QLazySharedPointer:: +unload () const +{ + typedef typename odb::object_traits::object_type object_type; + + if (p_) + { + if (i_.database () != 0) + i_.reset_id (odb::object_traits::id (*p_)); + + p_.clear (); + } +} + +template +template +inline QLazySharedPointer:: +QLazySharedPointer (database_type& db, const ID& id): i_ (db, id) {} + +template +inline QLazySharedPointer:: +QLazySharedPointer (database_type& db, T* p) + : p_ (p) +{ + if (p_) + i_.reset (db); +} + +template +template +inline QLazySharedPointer:: +QLazySharedPointer (database_type& db, T* p, Deleter d) + : p_ (p, d) +{ + if (p_) + i_.reset (db); +} + +template +template +inline QLazySharedPointer:: +QLazySharedPointer (database_type& db, const QSharedPointer& r) + : p_ (r) +{ + if (p_) + i_.reset (db); +} + +template +template +inline QLazySharedPointer:: +QLazySharedPointer (database_type& db, const QWeakPointer& r) + : p_ (r) +{ + if (p_) + i_.reset (db); +} + +template +inline typename QLazySharedPointer::database_type& QLazySharedPointer:: +database () const +{ + return *i_.database (); +} + +template +template +inline typename odb::object_traits::id_type QLazySharedPointer:: +objectId () const +{ + typedef typename odb::object_traits::object_type object_type; + + return p_ ? + odb::object_traits::id (*p_) : i_.template object_id (); +} + +// +// QLazySharedPointer related non-member function definitions. +// + +template +inline QLazySharedPointer +qSharedPointerCast (const QLazySharedPointer& r) +{ + return r.template staticCast (); +} + +template +inline QLazySharedPointer +qSharedPointerConstCast (const QLazySharedPointer& r) +{ + return r.template constCast (); +} + +template +inline QLazySharedPointer +qSharedPointerDynamicCast (const QLazySharedPointer& r) +{ + return r.template dynamicCast (); +} + +template +inline bool +operator== (const QLazySharedPointer& a, const QLazySharedPointer& b) +{ + return a.equal (b); +} + +template +inline bool +operator!= (const QLazySharedPointer& a, const QLazySharedPointer& b) +{ + return !a.equal (b); +} + +// +// QLazyWeakPointer definition. +// + +template +inline QLazyWeakPointer:: +QLazyWeakPointer () {} + + +template +inline QLazyWeakPointer:: +QLazyWeakPointer (const QLazyWeakPointer& r): p_ (r.p_), i_ (r.i_) {} + +template +template +inline QLazyWeakPointer:: +QLazyWeakPointer (const QLazyWeakPointer& r): p_ (r.p_), i_ (r.i_) {} + +template +template +inline QLazyWeakPointer:: +QLazyWeakPointer (const QLazySharedPointer& r): p_ (r.p_), i_ (r.i_) {} + +template +inline QLazyWeakPointer:: +~QLazyWeakPointer () {} + +template +inline QLazyWeakPointer& QLazyWeakPointer:: +operator= (const QLazyWeakPointer& r) +{ + p_ = r.p_; + i_ = r.i_; + return *this; +} + +template +template +inline QLazyWeakPointer& QLazyWeakPointer:: +operator= (const QLazyWeakPointer& r) +{ + p_ = r.p_; + i_ = r.i_; + return *this; +} + +template +template +inline QLazyWeakPointer& QLazyWeakPointer:: +operator= (const QLazySharedPointer& r) +{ + p_ = r.p_; + i_ = r.i_; + return *this; +} + +template +inline bool QLazyWeakPointer:: +operator! () const +{ + return isNull (); +} + +#ifdef QWEAKPOINTER_ENABLE_ARROW +template +inline T* QLazyWeakPointer:: +operator-> () const +{ + return p_.operator-> (); +} +#endif + +template +inline void QLazyWeakPointer:: +clear () +{ + p_.clear (); + i_.reset (); +} + +template +inline T* QLazyWeakPointer:: +data () const +{ + return p_.data (); +} + +template +inline bool QLazyWeakPointer:: +isNull () const +{ + return !(p_ || i_); +} + +template +template +inline QLazyWeakPointer:: +QLazyWeakPointer (const QWeakPointer& r): p_ (r) {} + +template +template +inline QLazyWeakPointer:: +QLazyWeakPointer (const QSharedPointer& r): p_ (r) {} + +template +template +inline QLazyWeakPointer& QLazyWeakPointer:: +operator= (const QWeakPointer& r) +{ + p_ = r; + i_.reset (); + return *this; +} + +template +template +inline QLazyWeakPointer& QLazyWeakPointer:: +operator= (const QSharedPointer& r) +{ + p_ = r; + i_.reset (); + return *this; +} + +template +inline bool QLazyWeakPointer:: +loaded () const +{ + bool i (i_); + return p_.toStrongRef ().isNull () != i; // expired () XOR i_ +} + +template +inline QSharedPointer QLazyWeakPointer:: +load () const +{ + QSharedPointer r (p_.toStrongRef ()); + + if (r || !i_) + return r; + + r = i_.template load (false); // Keep id. + p_ = r; + return r; +} + +template +inline void QLazyWeakPointer:: +unload () const +{ + // With weak pointer we always keep i_ up to date. + // + p_.clear (); +} + +template +template +inline QLazyWeakPointer:: +QLazyWeakPointer (database_type& db, const ID& id): i_ (db, id) {} + +template +template +inline QLazyWeakPointer:: +QLazyWeakPointer (database_type& db, const QSharedPointer& r) + : p_ (r) +{ + typedef typename odb::object_traits::object_type object_type; + + if (r) + i_.reset (db, odb::object_traits::id (*r)); +} + +template +template +inline QLazyWeakPointer:: +QLazyWeakPointer (database_type& db, const QWeakPointer& r) + : p_ (r) +{ + typedef typename odb::object_traits::object_type object_type; + + QSharedPointer sp (p_.toStrongRef ()); + + if (sp) + i_.reset (db, odb::object_traits::id (*sp)); +} + +template +template +inline typename odb::object_traits::id_type QLazyWeakPointer:: +objectId () const +{ + typedef typename odb::object_traits::object_type object_type; + + QSharedPointer sp (p_.toStrongRef ()); + + return sp ? + odb::object_traits::id (*sp) :i_.template object_id (); +} + +template +inline typename QLazyWeakPointer::database_type& QLazyWeakPointer:: +database () const +{ + return *i_.database (); +} + +// +// QLazyWeakPointer related non-member functions. +// + +template +inline QLazySharedPointer +qSharedPointerCast (const QLazyWeakPointer& r) +{ + return QLazySharedPointer (r).template staticCast (); +} + +template +inline QLazySharedPointer +qSharedPointerConstCast (const QLazyWeakPointer& r) +{ + return QLazySharedPointer (r).template constCast (); +} + +template +inline QLazySharedPointer +qSharedPointerDynamicCast (const QLazyWeakPointer& r) +{ + return QLazySharedPointer (r).template dynamicCast (); +} + +template +inline QLazyWeakPointer +qWeakPointerCast (const QLazyWeakPointer& r) +{ + return QLazySharedPointer (r).template staticCast ().toWeakRef (); +} + +template +inline bool +operator== (const QLazyWeakPointer& t, const QLazyWeakPointer& x) +{ + return t.equal (x); +} + +template +inline bool +operator== (const QLazyWeakPointer& t, const QLazySharedPointer& x) +{ + return t.equal (x); +} + +template +inline bool +operator== (const QLazySharedPointer& t, const QLazyWeakPointer& x) +{ + return x.equal (t); +} + +template +inline bool +operator!= (const QLazyWeakPointer& t, const QLazyWeakPointer& x) +{ + return !t.equal (x); +} + +template +inline bool +operator!= (const QLazyWeakPointer& t, const QLazySharedPointer& x) +{ + return !t.equal (x); +} + +template +inline bool +operator!= (const QLazySharedPointer& t, const QLazyWeakPointer& x) +{ + return !x.equal (t); +} diff --git a/odb/qt/smart-ptr/lazy-ptr.txx b/odb/qt/smart-ptr/lazy-ptr.txx new file mode 100644 index 0000000..2323536 --- /dev/null +++ b/odb/qt/smart-ptr/lazy-ptr.txx @@ -0,0 +1,99 @@ +// file : odb/qt/smart-ptr/lazy-ptr.txx +// author : Constantin Michael +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +template +template +bool QLazySharedPointer:: +equal (const QLazySharedPointer& 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 odb::object_traits::object_type object_type1; + typedef typename odb::object_traits::object_type object_type2; + + return i_.database () == r.i_.database () && + objectId () == r.objectId (); +} + +// +// QLazyWeakPointer +// + +template +template +bool QLazyWeakPointer:: +equal (const QLazyWeakPointer& r) const +{ + if (isNull () && r.isNull ()) + return true; + + QLazySharedPointer sp1 (toStrongRef ()); + QLazySharedPointer sp2 (r.toStrongRef ()); + + // If either one has expired, they are not equal. + // + if (!sp1 || !sp2) + return false; + + return sp1.equal (sp2); +} + +template +template +bool QLazyWeakPointer:: +equal (const QLazySharedPointer& r) const +{ + if (isNull () && r.isNull ()) + return true; + + QLazySharedPointer sp (toStrongRef ()); + + // If the weak pointer has expired, they are not equal. + // + if (!sp) + return false; + + return r.equal (sp); +} + +template +QLazySharedPointer QLazyWeakPointer:: +toStrongRef () const +{ + QSharedPointer sp (p_.toStrongRef ()); + + if (sp) + { + if (database_type* db = i_.database ()) + return QLazySharedPointer (*db, sp); + else + return QLazySharedPointer (sp); + } + else + { + if (i_) + return QLazySharedPointer (*i_.database (), + i_.template object_id ()); + else + return QLazySharedPointer (); + } +} diff --git a/odb/qt/smart-ptr/pointer-traits.hxx b/odb/qt/smart-ptr/pointer-traits.hxx new file mode 100644 index 0000000..488e1db --- /dev/null +++ b/odb/qt/smart-ptr/pointer-traits.hxx @@ -0,0 +1,87 @@ +// file : odb/qt/smart-ptr/pointer-traits.hxx +// author : Constantin Michael +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_QT_SMART_PTR_POINTER_TRAITS_HXX +#define ODB_QT_SMART_PTR_POINTER_TRAITS_HXX + +#include + +#include +#include + +#include + +namespace odb +{ + // Specialization for QSharedPointer. + // + template + class pointer_traits > + { + public: + static const pointer_kind kind = pk_shared; + static const bool lazy = false; + + typedef T element_type; + typedef QSharedPointer pointer_type; + typedef QSharedPointer const_pointer_type; + typedef smart_ptr_guard guard; + + static element_type* + get_ptr (const pointer_type& p) + { + return p.data (); + } + + 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 QWeakPointer. + // + template + class pointer_traits > + { + public: + static const pointer_kind kind = pk_weak; + static const bool lazy = false; + + typedef T element_type; + typedef QWeakPointer pointer_type; + typedef QSharedPointer strong_pointer_type; + + static strong_pointer_type + lock (const pointer_type& p) + { + return p.toStrongRef (); + } + }; +} + +#include + +#endif // ODB_QT_SMART_PTR_POINTER_TRAITS_HXX diff --git a/odb/qt/version.hxx b/odb/qt/version.hxx index 9f97768..a186f40 100644 --- a/odb/qt/version.hxx +++ b/odb/qt/version.hxx @@ -29,15 +29,15 @@ // Check that we have compatible ODB version. // -#if ODB_VERSION != 10251 +#if ODB_VERSION != 10300 # error incompatible odb interface version detected #endif // libodb-qt version: odb interface version plus the bugfix // version. // -#define LIBODB_QT_VERSION 1029951 -#define LIBODB_QT_VERSION_STR "1.3.0.b1" +#define LIBODB_QT_VERSION 1030000 +#define LIBODB_QT_VERSION_STR "1.3.0" #include diff --git a/odb/qt/version.options b/odb/qt/version.options index 13285bc..cbec4c6 100644 --- a/odb/qt/version.options +++ b/odb/qt/version.options @@ -8,6 +8,6 @@ # --hxx-prologue '#include ' ---hxx-prologue '#if LIBODB_QT_VERSION != 1029951 // 1.3.0.b1' +--hxx-prologue '#if LIBODB_QT_VERSION != 1030000 // 1.3.0' --hxx-prologue '# error ODB and C++ compilers see different libodb-qt versions' --hxx-prologue '#endif' diff --git a/version b/version index c92d778..f0bb29e 100644 --- a/version +++ b/version @@ -1 +1 @@ -1.3.0.b1 +1.3.0 -- cgit v1.1