// file : odb/tr1/lazy-ptr.hxx // copyright : Copyright (c) 2009-2012 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 #include // ODB_CXX11 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); 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); 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; // NULL 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; 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&, 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&, std::auto_ptr&); template void reset (database_type&, const std::tr1::shared_ptr&); #ifdef ODB_CXX11 template #else template #endif 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 transient 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). // #ifdef ODB_CXX11 template #else template #endif 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