summaryrefslogtreecommitdiffstats
path: root/catalogmgr/typefactory.hh
blob: 39fcc2b85a8ce092a1f6cd9754fd9a7b09f47836 (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
/*
* 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:
 *
 *
 * COMMENTS:
 *
 ************************************************************/

#ifndef _TYPEFACTORY_HH_
#define _TYPEFACTORY_HH_

class TypeFactory;

#include <vector>
#include <map>

#include "reladminif/oidif.hh"
#include "relcatalogif/typeiterator.hh"

class ULongType;
class LongType;
class CharType;
class BoolType;
class UShortType;
class ShortType;
class OctetType;
class DoubleType;
class FloatType;
class ComplexType1;
class ComplexType2;
class StructType;
class BaseType;
class SetType;
class MDDType;
class Type;
class DBMinterval;

//@ManMemo: Module: {\bf relcatalogif}.

/*@Doc:

The user specifies types as strings. This is passed down to the
TypeFactory where according to the string the object representing
the type is read from the database. The base DBMS manages
persistent objects for all base types. Pointers to these
persistent objects are returned if #mapType# is called. In the
current implementation, these pointers get invalid after a
transaction is committed. So the user has to call #mapType# again
after a commit(most programs do this anyway).

The name of the type used for accessing it is the class name without
the Type suffix. e.g. "Bool" for \Ref{BoolType} or "ULong" for
\Ref{ULongType}.

Only one instance of TypeFactory exists at any time. Also only one
instance of the types exits in memory. The pointers returned
should not be freed, the memory is freed, when the TypeFactory is
destroyed. The static member function can also be called directly
with the scope operator. The class follows the singleton design
pattern(Gamma et. al. S. 127ff.) and the builder design pattern
(Gamma et. al.  S. 97ff.).

{\bf Functionality}

Returns a pointer to \Ref{BaseType} to the type which is identified by
a string.

{\bf Interdependencies}

Will be used from \Ref{MDDObject} and subclasses when created.
Because all types are stored persistently, it is necessary to open
a session using AdminIf::instance()(see \Ref{AdminIf}), a
database using DatabaseIf::open()(see \Ref{DatabaseIf) and a
transaction using TransactionIf::begin(see \Ref{TransactionIf)
before creating instances of types with mapType().
*/

/**
  * \ingroup Catalogmgrs
  */
class TypeFactory
	{
	public:
		static TypeFactory* instance();
		/*@Doc:
		used to access instance of TileFactory.
		*/

		static const BaseType* mapType(const char* typeName);
		/*@Doc:
		maps a string to a base type
			e.g. #aTypeFactory->mapType("ULong")# returns a pointer to an
			instance of ULongType. This also works for user defined type
			like #aTypeFactory->mapType("myStruct")# returning a pointer
			to a user defined StructType.
		*/
		
		//static BaseType* mapType(char* typeName);
		/*@Doc: 
		maps a string to a base type
		use the const char* version!!
		*/
		
		static const SetType* mapSetType(const char* typeName);
		/*@Doc:
		maps a string to a set type
		*/

		//static const SetType* mapSetType(char* typeName);
		/*@Doc:
		maps a string to a set type
		use the const char* version!!
		*/
		
		static const MDDType* mapMDDType(const char* typeName);
		/*@Doc:
		maps a string to a mdd type
		*/

		//static const MDDType* mapMDDType(char* typeName);
		/*@Doc:
		maps a string to a mdd type.
		use the const char* version!!
		*/

		static const StructType* addStructType(const StructType* type);
		/*@Doc:
		add a new struct type to the current DBMS.
			After calling this function, a user defined type can be
			retrieved with function #mapType()#. The type only becomes
			persistent after commit.
		*/
		
		static const SetType* addSetType(const SetType* type);
		/*@Doc:
		add a new set type to the current DBMS.
			After calling this function, a user defined set type can be
			retrieved with function #mapSetType()#. The type only becomes
			persistent after commit.
		*/
		
		static const MDDType* addMDDType(const MDDType* type);
		/*@Doc:
		add a new set type to the current DBMS.
			After calling this function, a user defined mdd type can be
			retrieved with function #mapMDDType()#. The type only becomes
			persistent after commit.
		*/
		
		static void deleteStructType(const char* typeName);
		/*@Doc:
		delete a struct type in the current DBMS.
		*/

		static void deleteSetType(const char* typeName);
		/*@Doc:
		delete a set type in the current DBMS.
		*/
		
		static void deleteMDDType(const char* typeName);
		/*@Doc:
		delete a mdd type in the current DBMS.
		*/
		
		static TypeIterator<StructType> createStructIter();
		/*@Doc:
			Note that get_element returns a pointer to a StructType!
		returns an iterator for StructTypes.
		*/

		static TypeIterator<SetType> createSetIter();
		/*@Doc:
			Note that get_element returns a pointer to a SetType!
		returns an iterator for SetTypes.
		*/
		
		static TypeIterator<MDDType> createMDDIter();
		/*@Doc:
			Note that get_element returns a pointer to a MDDType!
		returns an iterator for MDDTypes.
		*/


		static Type* addTempType(Type* type);
		/*@Doc:
			Memory is freed at commit time of transaction in
			TransactionIf::commit(), TransactionIf::validate() and
			TransactionIf::abort().

			{\em Note:} You have to use addTempType() on a composite type
			(e.g. \Ref{SetType}) first before calling addTempType() on a
			component(e.g. \Ref{MDDType}). Otherwise the component may be
			freed first sometimes leading to a crash.
		registers a temporary type with the type factory.
		*/

		static void initialize();
		/*@Doc:	
			Should only be called by \Ref{TransactionIf}).
		initializes after a begin transaction.  theTempTypes are created.
		 */

		static void freeTempTypes();
		/*@Doc:	
			Should only be called by \Ref{TransactionIf}).
		frees temporary types.
		 */

		static const Type* ensurePersistence(Type* type);
		/*@Doc:
			This function has to be called on all types registered as
			temporary types but getting persistent during query execution. The
			type is deleted from the tempTypes list, i.e. not freed on end of
			transaction. If a structurally equivalent type is found in the
			database it will be used and a pointer to it returned. Note that
			this currently works only for StructTypes!
		has to be called on temporary types getting persistent.
		*/

		static const short MaxBuiltInId;

	protected:
		TypeFactory();
		/*@Doc:
		constructor, can not be used from outside.
		*/
		
	private:
		static TypeFactory* myInstance;
		/*@Doc:
		pointer to instance for Singleton pattern.
		*/

		static std::vector<Type*> *theTempTypes;
		/*@Doc:
		a vector containing pointers to temporary allocated types.
		*/
		
	};

#endif