summaryrefslogtreecommitdiffstats
path: root/reladminif/dbref.hh
blob: 617c7faf50ff9e4f1390068087c8cf9857a5495b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
* This file is part of rasdaman community.
*
* Rasdaman community is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Rasdaman community is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with rasdaman community.  If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Peter Baumann /
rasdaman GmbH.
*
* For more information please see <http://www.rasdaman.org>
* or contact Peter Baumann via <baumann@rasdaman.com>.
*/
/*************************************************************
 *
 *
 * PURPOSE:
 *   DBRef is a smart pointer for managing objects derived from
 *   the DbObject class.
 *
 *
 * COMMENTS:
 *
 ************************************************************/


#ifndef _DBREF_HH_
#define _DBREF_HH_

class DBHierIndex;
class DBRCIndexDS;
class DBTCIndex; 
class BLOBTile;
class InlineTile;
class DBTile;
class OId;
class DBObject;
class r_Error;
class IndexDS;
class HierIndexDS;
class DBMDDObj;

template <class T> class DBRef;

#include "oidif.hh"
 
//@ManMemo: Module: {\bf reladminif}.
/*@Doc:
DBRef is a smart pointer class operating on classes derived from DbObject. A smart
pointer to an object with a known id is created using DBRef<T>(id). The object
managed by a given smart pointer can be changed (rebinding) by using the assignment
operator.
All access methods may throw database related r_Errors.
*/

#define DBOBJID_NONE	OId()
/**
  * \ingroup Reladminifs
  */
template <class T>
class DBRef
	{
	public:

		DBRef(void);
		/*@Doc: 
		Default constructor. Object must be assigned a value before the first dereferencing.
		*/
		
		DBRef(const OId &id);
		/*@Doc: 
		Id-constructor, binds smart pointer to object with the given id (must only be unique
		within class T, not within all classes derived from DbObject).
		*/
		
		DBRef(OId::OIdPrimitive id);
		/*@Doc: 
		Id-constructor, binds smart pointer to object with the given id (must only be unique
		within class T, not within all classes derived from DbObject).
		*/
		
		DBRef(const DBRef<T> &src);
		/*@Doc: 
		Copy-constructor, binds smart pointer to the same object src is bound to.
		*/
		
		DBRef(T *ptr);
		/*@Doc: 
		Object-constructor, binds smart pointer explicitly to the object ptr.
		*/
		
		~DBRef(void);
		/*@Doc: 
		Destructor: decrements reference count for the object that was managed by this
		smart pointer.
		*/

		bool operator<(const DBRef<T>& other) const;
		/*@Doc: 
		Returns true if me.operator==(other) returns -1
		*/

		int operator==(const DBRef<T> &src) const;
		/*@Doc: 
		Comparison operator:
		Returns:
			-1 if this is not initialised and src is initialised
			-1 if persistent and objId < src.objId
			-1 if transient and src is persistent
			-1 if transient and object < src.object
			0 if persistent and src persistent and myOId == src.myOId
			0 if transient and src transient and object == src.object
			0 if this is not initialised and src is not initialised
			+1 if persistent and objId > src.objId
			+1 if persistent and src is transient
			+1 if transient and object > src.object
			-1 if this is initialised and src is not initialised
		*/
		
		DBRef<T> &operator=(const DBRef<T> &src);
		/*@Doc: 
		Assignment operator: removes old binding and rebinds to the same object managed by src.
		*/
		
		DBRef<T> &operator=(T *ptr);
		/*@Doc: 
		Assignment operator: removes old binding and rebinds to object ptr.
		*/

		T *operator->(void) throw (r_Error);
		/*@Doc: 
		Dereferencing operator -> for accessing the managed object's members.
		*/
		
		const T *operator->(void) const throw (r_Error);
		/*@Doc: 
		Dereferencing operator -> for accessing the managed object's members.
		*/
		
		T &operator*(void) throw (r_Error);
		/*@Doc: 
		Dereferencing operator * for accessing the managed object.
		*/
		
		const T &operator*(void) const throw (r_Error);
		/*@Doc: 
		Dereferencing operator * for accessing the managed object.
		*/
		
#ifndef __GNUG__
		T &operator[](int idx) const throw (r_Error);
		/*@Doc: 
		Dereferencing operator [] for accessing array objects.
		*/
#endif

		T *ptr(void) throw (r_Error);
		/*@Doc: 
		Returns pointer to managed object.
		*/
		
		const T *ptr(void) const throw (r_Error);
		/*@Doc: 
		Returns pointer to managed object.
		*/
		
		OId getOId(void) const;
		/*@Doc: 
		Returns id of managed object
		*/

		void delete_object(void);
		/*@Doc: 
		deletes the object from database if it is valid else throws an exception.
		*/

		bool is_null(void) const;
		/*@Doc: 
		Returns false if valid binding exists, true otherwise.
		this method may instantiate an object from the database
		*/
		
		bool is_valid(void) const;
		/*@Doc: 
		Returns true if valid binding exists, false otherwise
		*/

		bool isInitialised() const;
		/*@Doc: 
		Returns true if OId is valid or the pointer is valid, false otherwise
		*/

		void release();
		/*@Doc: 
		releases this DBRef pointer and refcount to its object
		*/

		operator DBRef<DBObject>() const;
		/*@Doc: 
		cast operator. works allways.
		*/

		operator DBRef<BLOBTile>() const throw (r_Error);
		/*@Doc: 
		cast operator.  checks it the objects type is of OId::BLOBOID.
		*/

		operator DBRef<DBTile>() const throw (r_Error);
		/*@Doc: 
		cast operator.  checks it the objects type is of OId::BLOBOID or OId::INLINETILEOID.
		*/

 		operator DBRef<InlineTile>() const throw (r_Error);
                /*@Doc:
                cast operator.  checks it the objects type is of OId::INLINETILEOID.
                */

		operator DBRef<DBHierIndex>() const throw (r_Error);
		/*@Doc: 
		cast operator.  checks it the objects type is of OId::MDDHIERIXOID.
		*/

                operator DBRef<DBTCIndex>() const throw (r_Error);
                /*@Doc:
                cast operator.  checks it the objects type is of OId::INLINEIXOID.
                */

                operator DBRef<DBRCIndexDS>() const throw (r_Error);
                /*@Doc:
                cast operator.  checks it the objects type is of OId::MDDRCIXOID.
                */

		operator IndexDS*() const throw (r_Error);
                /*@Doc:
                cast operator.  checks it the objects type is of any valid index.
                */

		operator HierIndexDS*() const throw (r_Error);
                /*@Doc:
                cast operator.  checks it the objects type is of any valid hierarchical index.
                */

		operator T*() throw (r_Error);
		/*@Doc: 
		*/

		operator const T*() const throw (r_Error);
		/*@Doc: 
		*/

		static void setPointerCaching(bool useIt);
		/*@Doc: 
		Make the dbref store and use pointers.
		If set to false the DBRef will always ask the objectbroker.
		May be set at any time to false.
		Only between transactions may it be set to true.
		*/

		static bool getPointerCaching();
		/*@Doc: 
		returns pointerCaching 
		*/

	private:


		mutable T *object;
		/*@Doc: 
		Pointer to the managed object or 0 if no binding exists.
		*/
		
		OId objId;
		/*@Doc: 
		id of managed object.
		*/

		bool pointerValid;
		/*@Doc:
		whenever a smartpointer is initiaised by a pointer, this attribute is set to true.
		this is neccessary for disabled pointer caching.
		*/

		static bool pointerCaching;
	};

template <class T> bool operator< (const DBRef<T> &me, const DBRef<T> &him);

#ifdef EARLY_TEMPLATE
#ifdef __EXECUTABLE__
#include "dbref.cc"
#endif 
#endif

#endif