Common ODB Runtime Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
tr1/lazy-ptr.hxx
Go to the documentation of this file.
1 // file : odb/tr1/lazy-ptr.hxx
2 // copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC
3 // license : GNU GPL v2; see accompanying LICENSE file
4 
5 #ifndef ODB_TR1_LAZY_PTR_HXX
6 #define ODB_TR1_LAZY_PTR_HXX
7 
8 #include <odb/pre.hxx>
9 
10 //
11 // This header assumes that the necessary TR1 header has already
12 // been included.
13 //
14 
15 #include <memory> // std::auto_ptr
16 
17 #include <odb/forward.hxx> // odb::database
18 #include <odb/traits.hxx>
19 #include <odb/lazy-ptr-impl.hxx>
20 #include <odb/details/config.hxx> // ODB_CXX11
21 
22 namespace odb
23 {
24  namespace tr1
25  {
26  template <class T>
28 
29  //
30  //
31  template <class T>
33  {
34  // The standard shared_ptr interface.
35  //
36  public:
37  typedef T element_type;
38 
39  lazy_shared_ptr ();
40  template <class Y> explicit lazy_shared_ptr (Y*);
41  template <class Y, class D> lazy_shared_ptr (Y*, D);
42 
44  template <class Y> lazy_shared_ptr (const lazy_shared_ptr<Y>&);
45  template <class Y> explicit lazy_shared_ptr (const lazy_weak_ptr<Y>&);
46  template <class Y> explicit lazy_shared_ptr (std::auto_ptr<Y>&);
47 
49 
51  template <class Y> lazy_shared_ptr& operator= (const lazy_shared_ptr<Y>&);
52  template <class Y> lazy_shared_ptr& operator= (std::auto_ptr<Y>&);
53 
54  void swap (lazy_shared_ptr&);
55  void reset ();
56  template <class Y> void reset (Y*);
57  template <class Y, class D> void reset (Y*, D);
58 
59  T& operator* () const;
60  T* operator-> () const;
61  T* get () const;
62 
63  bool unique () const;
64  long use_count () const;
65 
66  typedef std::tr1::shared_ptr<T> lazy_shared_ptr::*unspecified_bool_type;
67  operator unspecified_bool_type () const
68  {
69  return (p_ || i_) ? &lazy_shared_ptr::p_ : 0;
70  }
71 
72  // Initialization/assignment from shared_ptr and weak_ptr.
73  //
74  public:
75  template <class Y> lazy_shared_ptr (const std::tr1::shared_ptr<Y>&);
76  template <class Y> explicit lazy_shared_ptr (const std::tr1::weak_ptr<Y>&);
77 
78  template <class Y> lazy_shared_ptr& operator= (const std::tr1::shared_ptr<Y>&);
79 
80  // Lazy loading interface.
81  //
82  public:
84 
85  // NULL loaded()
86  //
87  // true true NULL pointer to transient object
88  // false true valid pointer to persistent object
89  // true false unloaded pointer to persistent object
90  // false false valid pointer to transient object
91  //
92  bool loaded () const;
93 
94  std::tr1::shared_ptr<T> load () const;
95 
96  // Unload the pointer. For transient objects this function is
97  // equivalent to reset().
98  //
99  void unload () const;
100 
101  // Get the underlying eager pointer. If this is an unloaded pointer
102  // to a persistent object, then the returned pointer will be NULL.
103  //
104  std::tr1::shared_ptr<T> get_eager () const;
105 
106  template <class DB, class ID> lazy_shared_ptr (DB&, const ID&);
107  template <class DB, class Y> lazy_shared_ptr (DB&, Y*);
108  template <class DB, class Y, class D> lazy_shared_ptr (DB&, Y*, D);
109  template <class DB, class Y> lazy_shared_ptr (DB&, std::auto_ptr<Y>&);
110  template <class DB, class Y> lazy_shared_ptr (DB&, const std::tr1::shared_ptr<Y>&);
111  template <class DB, class Y> lazy_shared_ptr (DB&, const std::tr1::weak_ptr<Y>&);
112 
113  template <class DB, class ID> void reset (DB&, const ID&);
114  template <class DB, class Y> void reset (DB&, Y*);
115  template <class DB, class Y, class D> void reset (DB&, Y*, D);
116  template <class DB, class Y> void reset (DB&, std::auto_ptr<Y>&);
117  template <class DB, class Y> void reset (DB&, const std::tr1::shared_ptr<Y>&);
118 
119 #ifdef ODB_CXX11_FUNCTION_TEMPLATE_DEFAULT_ARGUMENT
120  template <class O = T>
121 #else
122  template <class O /*= T*/>
123 #endif
124  typename object_traits<O>::id_type object_id () const;
125 
126  database_type& database () const;
127 
128  // Helpers.
129  //
130  public:
131  template <class Y> bool equal (const lazy_shared_ptr<Y>&) const;
132 
133  private:
134  template <class Y> friend class lazy_shared_ptr;
135  template <class Y> friend class lazy_weak_ptr;
136 
137  mutable std::tr1::shared_ptr<T> p_;
138  mutable lazy_ptr_impl<T> i_;
139  };
140 
141  // operator< and operator<< are not provided.
142  //
143  template<class T, class Y>
145 
146  template<class T, class Y>
148 
149  template<class T> void swap (lazy_shared_ptr<T>&, lazy_shared_ptr<T>&);
150 
151  template<class D, class T>
152  D* get_deleter (const lazy_shared_ptr<T>&);
153 
154  //
155  //
156  template <class T>
157  class lazy_weak_ptr
158  {
159  // The standard weak_ptr interface.
160  //
161  public:
162  typedef T element_type;
163 
164  lazy_weak_ptr ();
165  template <class Y> lazy_weak_ptr (const lazy_shared_ptr<Y>&);
166  lazy_weak_ptr (const lazy_weak_ptr&);
167  template <class Y> lazy_weak_ptr (const lazy_weak_ptr<Y>&);
168 
169  ~lazy_weak_ptr ();
170 
172  template <class Y> lazy_weak_ptr& operator= (const lazy_weak_ptr<Y>&);
173  template <class Y> lazy_weak_ptr& operator= (const lazy_shared_ptr<Y>&);
174 
175  void swap (lazy_weak_ptr<T>&);
176  void reset ();
177 
178  long use_count () const;
179  bool expired () const;
180 
181  lazy_shared_ptr<T> lock () const;
182 
183  // Initialization/assignment from shared_ptr and weak_ptr.
184  //
185  public:
186  template <class Y> lazy_weak_ptr (const std::tr1::weak_ptr<Y>&);
187  template <class Y> lazy_weak_ptr (const std::tr1::shared_ptr<Y>&);
188 
189  template <class Y> lazy_weak_ptr& operator= (const std::tr1::weak_ptr<Y>&);
190  template <class Y> lazy_weak_ptr& operator= (const std::tr1::shared_ptr<Y>&);
191 
192  // Lazy loading interface.
193  //
194  public:
196 
197  // expired() loaded()
198  //
199  // true true expired pointer to transient object
200  // false true valid pointer to persistent object
201  // true false expired pointer to persistent object
202  // false false valid pointer to transient object
203  //
204  bool loaded () const;
205 
206  // Performs both lock and load.
207  //
208  std::tr1::shared_ptr<T> load () const;
209 
210  // Unload the pointer. For transient objects this function is
211  // equivalent to reset().
212  //
213  void unload () const;
214 
215  // Get the underlying eager pointer. If this is an unloaded pointer
216  // to a persistent object, then the returned pointer will be NULL.
217  //
218  std::tr1::weak_ptr<T> get_eager () const;
219 
220  template <class DB, class ID> lazy_weak_ptr (DB&, const ID&);
221  template <class DB, class Y> lazy_weak_ptr (DB&, const std::tr1::shared_ptr<Y>&);
222  template <class DB, class Y> lazy_weak_ptr (DB&, const std::tr1::weak_ptr<Y>&);
223 
224  template <class DB, class ID> void reset (DB&, const ID&);
225  template <class DB, class Y> void reset (DB&, const std::tr1::shared_ptr<Y>&);
226  template <class DB, class Y> void reset (DB&, const std::tr1::weak_ptr<Y>&);
227 
228  // The object_id() function can only be called when the object is
229  // persistent, or: expired() XOR loaded() (can use != for XOR).
230  //
231 #ifdef ODB_CXX11_FUNCTION_TEMPLATE_DEFAULT_ARGUMENT
232  template <class O = T>
233 #else
234  template <class O /* = T */>
235 #endif
236  typename object_traits<O>::id_type object_id () const;
237 
238  database_type& database () const;
239 
240  private:
241  template <class Y> friend class lazy_shared_ptr;
242  template <class Y> friend class lazy_weak_ptr;
243 
244  mutable std::tr1::weak_ptr<T> p_;
245  mutable lazy_ptr_impl<T> i_;
246  };
247 
248  // operator< is not provided.
249  //
250  template<class T> void swap (lazy_weak_ptr<T>&, lazy_weak_ptr<T>&);
251  }
252 }
253 
254 #include <odb/tr1/lazy-ptr.ixx>
255 #include <odb/tr1/lazy-ptr.txx>
256 
258 
259 #include <odb/post.hxx>
260 
261 #endif // ODB_TR1_LAZY_PTR_HXX
database_type & database() const
object_traits< O >::id_type object_id() const
Definition: lazy-ptr-impl.hxx:115
std::tr1::shared_ptr< T > lazy_shared_ptr::* unspecified_bool_type
Definition: tr1/lazy-ptr.hxx:66
Definition: tr1/lazy-ptr.hxx:32
T element_type
Definition: tr1/lazy-ptr.hxx:162
std::tr1::shared_ptr< T > get_eager() const
std::tr1::weak_ptr< T > get_eager() const
bool operator==(const lazy_shared_ptr< T > &, const lazy_shared_ptr< Y > &)
lazy_shared_ptr< T > lock() const
odb::database database_type
Definition: tr1/lazy-ptr.hxx:83
void swap(lazy_shared_ptr< T > &, lazy_shared_ptr< T > &)
object_traits< O >::id_type object_id() const
Definition: tr1/lazy-ptr.hxx:27
Definition: forward.hxx:123
T element_type
Definition: tr1/lazy-ptr.hxx:37
std::tr1::shared_ptr< T > load() const
bool operator!=(const lazy_shared_ptr< T > &, const lazy_shared_ptr< Y > &)
std::tr1::shared_ptr< T > load() const
bool equal(const lazy_shared_ptr< Y > &) const
lazy_shared_ptr & operator=(const lazy_shared_ptr &)
void swap(lazy_weak_ptr< T > &)
lazy_weak_ptr & operator=(const lazy_weak_ptr &)
Definition: database.hxx:38
odb::database database_type
Definition: tr1/lazy-ptr.hxx:195
D * get_deleter(const lazy_shared_ptr< T > &)
void swap(lazy_shared_ptr &)
long use_count() const
database_type & database() const