rasdaman complete source
dbref.hh
Go to the documentation of this file.
1 /*
2 * This file is part of rasdaman community.
3 *
4 * Rasdaman community is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * Rasdaman community is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with rasdaman community. If not, see <http://www.gnu.org/licenses/>.
16 *
17 * Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Peter Baumann /
18 rasdaman GmbH.
19 *
20 * For more information please see <http://www.rasdaman.org>
21 * or contact Peter Baumann via <baumann@rasdaman.com>.
22 */
23 /*************************************************************
24  *
25  *
26  * PURPOSE:
27  * DBRef is a smart pointer for managing objects derived from
28  * the DbObject class.
29  *
30  *
31  * COMMENTS:
32  *
33  ************************************************************/
34 
35 
36 #ifndef _DBREF_HH_
37 #define _DBREF_HH_
38 
39 class DBHierIndex;
40 class DBRCIndexDS;
41 class DBTCIndex;
42 class BLOBTile;
43 class InlineTile;
44 class DBTile;
45 class OId;
46 class DBObject;
47 class r_Error;
48 class IndexDS;
49 class HierIndexDS;
50 class DBMDDObj;
51 
52 template <class T> class DBRef;
53 
54 #include "oidif.hh"
55 
56 //@ManMemo: Module: {\bf reladminif}.
57 /*@Doc:
58 DBRef is a smart pointer class operating on classes derived from DbObject. A smart
59 pointer to an object with a known id is created using DBRef<T>(id). The object
60 managed by a given smart pointer can be changed (rebinding) by using the assignment
61 operator.
62 All access methods may throw database related r_Errors.
63 */
64 
65 #define DBOBJID_NONE OId()
66 
69 template <class T>
70 class DBRef
71 {
72 public:
73 
74  DBRef(void);
75  /*@Doc:
76  Default constructor. Object must be assigned a value before the first dereferencing.
77  */
78 
79  DBRef(const OId &id);
80  /*@Doc:
81  Id-constructor, binds smart pointer to object with the given id (must only be unique
82  within class T, not within all classes derived from DbObject).
83  */
84 
86  /*@Doc:
87  Id-constructor, binds smart pointer to object with the given id (must only be unique
88  within class T, not within all classes derived from DbObject).
89  */
90 
91  DBRef(const DBRef<T> &src);
92  /*@Doc:
93  Copy-constructor, binds smart pointer to the same object src is bound to.
94  */
95 
96  DBRef(T *ptr);
97  /*@Doc:
98  Object-constructor, binds smart pointer explicitly to the object ptr.
99  */
100 
101  ~DBRef(void);
102  /*@Doc:
103  Destructor: decrements reference count for the object that was managed by this
104  smart pointer.
105  */
106 
107  bool operator<(const DBRef<T>& other) const;
108  /*@Doc:
109  Returns true if me.operator==(other) returns -1
110  */
111 
112  int operator==(const DBRef<T> &src) const;
113  /*@Doc:
114  Comparison operator:
115  Returns:
116  -1 if this is not initialised and src is initialised
117  -1 if persistent and objId < src.objId
118  -1 if transient and src is persistent
119  -1 if transient and object < src.object
120  0 if persistent and src persistent and myOId == src.myOId
121  0 if transient and src transient and object == src.object
122  0 if this is not initialised and src is not initialised
123  +1 if persistent and objId > src.objId
124  +1 if persistent and src is transient
125  +1 if transient and object > src.object
126  -1 if this is initialised and src is not initialised
127  */
128 
129  DBRef<T> &operator=(const DBRef<T> &src);
130  /*@Doc:
131  Assignment operator: removes old binding and rebinds to the same object managed by src.
132  */
133 
134  DBRef<T> &operator=(T *ptr);
135  /*@Doc:
136  Assignment operator: removes old binding and rebinds to object ptr.
137  */
138 
139  T *operator->(void) throw (r_Error);
140  /*@Doc:
141  Dereferencing operator -> for accessing the managed object's members.
142  */
143 
144  const T *operator->(void) const throw (r_Error);
145  /*@Doc:
146  Dereferencing operator -> for accessing the managed object's members.
147  */
148 
149  T &operator*(void) throw (r_Error);
150  /*@Doc:
151  Dereferencing operator * for accessing the managed object.
152  */
153 
154  const T &operator*(void) const throw (r_Error);
155  /*@Doc:
156  Dereferencing operator * for accessing the managed object.
157  */
158 
159 #ifndef __GNUG__
160  T &operator[](int idx) const throw (r_Error);
161  /*@Doc:
162  Dereferencing operator [] for accessing array objects.
163  */
164 #endif
165 
166  T *ptr(void) throw (r_Error);
167  /*@Doc:
168  Returns pointer to managed object.
169  */
170 
171  const T *ptr(void) const throw (r_Error);
172  /*@Doc:
173  Returns pointer to managed object.
174  */
175 
176  OId getOId(void) const;
177  /*@Doc:
178  Returns id of managed object
179  */
180 
181  void delete_object(void);
182  /*@Doc:
183  deletes the object from database if it is valid else throws an exception.
184  */
185 
186  bool is_null(void) const;
187  /*@Doc:
188  Returns false if valid binding exists, true otherwise.
189  this method may instantiate an object from the database
190  */
191 
192  bool is_valid(void) const;
193  /*@Doc:
194  Returns true if valid binding exists, false otherwise
195  */
196 
197  bool isInitialised() const;
198  /*@Doc:
199  Returns true if OId is valid or the pointer is valid, false otherwise
200  */
201 
202  void release();
203  /*@Doc:
204  releases this DBRef pointer and refcount to its object
205  */
206 
207  operator DBRef<DBObject>() const;
208  /*@Doc:
209  cast operator. works allways.
210  */
211 
212  operator DBRef<BLOBTile>() const throw (r_Error);
213  /*@Doc:
214  cast operator. checks it the objects type is of OId::BLOBOID.
215  */
216 
217  operator DBRef<DBTile>() const throw (r_Error);
218  /*@Doc:
219  cast operator. checks it the objects type is of OId::BLOBOID or OId::INLINETILEOID.
220  */
221 
222  operator DBRef<InlineTile>() const throw (r_Error);
223  /*@Doc:
224  cast operator. checks it the objects type is of OId::INLINETILEOID.
225  */
226 
227  operator DBRef<DBHierIndex>() const throw (r_Error);
228  /*@Doc:
229  cast operator. checks it the objects type is of OId::MDDHIERIXOID.
230  */
231 
232  operator DBRef<DBTCIndex>() const throw (r_Error);
233  /*@Doc:
234  cast operator. checks it the objects type is of OId::INLINEIXOID.
235  */
236 
237  operator DBRef<DBRCIndexDS>() const throw (r_Error);
238  /*@Doc:
239  cast operator. checks it the objects type is of OId::MDDRCIXOID.
240  */
241 
242  operator IndexDS*() const throw (r_Error);
243  /*@Doc:
244  cast operator. checks it the objects type is of any valid index.
245  */
246 
247  operator HierIndexDS*() const throw (r_Error);
248  /*@Doc:
249  cast operator. checks it the objects type is of any valid hierarchical index.
250  */
251 
252  operator T*() throw (r_Error);
253  /*@Doc:
254  */
255 
256  operator const T*() const throw (r_Error);
257  /*@Doc:
258  */
259 
260  static void setPointerCaching(bool useIt);
261  /*@Doc:
262  Make the dbref store and use pointers.
263  If set to false the DBRef will always ask the objectbroker.
264  May be set at any time to false.
265  Only between transactions may it be set to true.
266  */
267 
268  static bool getPointerCaching();
269  /*@Doc:
270  returns pointerCaching
271  */
272 
273 private:
274 
275 
276  mutable T *object;
277  /*@Doc:
278  Pointer to the managed object or 0 if no binding exists.
279  */
280 
281  OId objId;
282  /*@Doc:
283  id of managed object.
284  */
285 
286  bool pointerValid;
287  /*@Doc:
288  whenever a smartpointer is initiaised by a pointer, this attribute is set to true.
289  this is neccessary for disabled pointer caching.
290  */
291 
292  static bool pointerCaching;
293 };
294 
295 template <class T> bool operator< (const DBRef<T> &me, const DBRef<T> &him);
296 
297 #ifdef EARLY_TEMPLATE
298 #ifdef __EXECUTABLE__
299 #include "dbref.cc"
300 #endif
301 #endif
302 
303 #endif
Definition: hierindex.hh:45
static void setPointerCaching(bool useIt)
~DBRef(void)
OId getOId(void) const
T * operator->(void)
T & operator[](int idx) const
Definition: dbrcindexds.hh:79
void delete_object(void)
T * ptr(void)
Definition: dbtcindex.hh:39
Definition: dbtile.hh:54
Definition: dbmddobj.hh:65
Definition: oidif.hh:67
bool isInitialised() const
T & operator*(void)
bool is_valid(void) const
Definition: inlinetile.hh:63
double OIdPrimitive
Definition: oidif.hh:102
DBRef(void)
int operator==(const DBRef< T > &src) const
DBRef< T > & operator=(const DBRef< T > &src)
Definition: blobtile.hh:71
bool is_null(void) const
Definition: dbobject.hh:29
void release()
static bool getPointerCaching()
Definition: dbobject.hh:54