summaryrefslogtreecommitdiffstats
path: root/relcatalogif/structtype.hh
blob: 7adbfc2a763eb4e1300d616f5570b5813f0c482d (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
// -*-C++-*- (for Emacs)

/*
* 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:
 *   The StructType class is the superclass for all classes
 *   describing the type of a cell
 *
 *
 * COMMENTS:
 *
 ************************************************************/

#ifndef _STRUCTTYPE_HH_
#define _STRUCTTYPE_HH_

#include <iostream>
#include <vector>

#include "compositetype.hh"
#include "catalogmgr/ops.hh"

class BaseType;

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

/*@Doc: 
StructType is the base type used for user defined structures. It
stores the names and BaseTypes of the elements. The size of a
StructType in chars depends on the elements.

StructType now uses alignment for structures with elements of
different sizes. One byte types are aligned to one byte, two byte
types to two, all other types to four. The Size is padded to 4 byte
alignment except for types with only chars (one byte alignment) or
type with only shorts (two byte alignment). Structs as elements of
structs are aligned with the minimum byte alignment needed for the
struct.

*/

class StructType : public CompositeType
	{
	//	friend ostream& operator << (ostream& stream, StructType& b);

	public:
		virtual void printCell(ostream& stream, const char* cell) const;

		virtual char* getTypeStructure() const;

		/// add new element to struct
		unsigned int addElement(const char* elemName, const char* elemType);

		/// add new element to struct using pointer to BaseType
		unsigned int addElement(const char* elemName, const BaseType* elemType);

		/// get offset for an element by name of element.
		unsigned int getOffset(const char* elemName) const;

		/// get offset for an element by number of element (0 based).
		unsigned int getOffset(unsigned int num) const;

		/// get type of an element by name of element.
		const BaseType* getElemType(const char* elemName) const;

		/// get name of an element by number of element (0 based).
		const char* const getElemName(unsigned int num) const;

		/// get type of an element by number of element (0 based).
		const BaseType* getElemType(unsigned int num) const;

		/// get number of elements.
		unsigned int getNumElems() const;

		/// get alignment needed for structure to be embedded in another structure.
		unsigned int getAlignment() const;

		/// checks if a certain StructType is contained in this StructType
		int contains(const StructType* aStruct) const;

		StructType(const OId& structtypeid) throw (r_Error);
		
		/// default constructor, sets type name to "".
		StructType();

		/// constructor getting type name and number of elements.
		StructType(const char* newTypeName, unsigned int numElem );

		/// copy constructor.
		StructType( const StructType& old );

		/// assignment operator.
		StructType& operator=( const StructType& old );

		/// virtual destructor.
		virtual ~StructType();

		virtual int compatibleWith(const Type* aType) const;

		virtual r_Bytes getMemorySize() const;

private:
	// those inherited from BaseType aren't useful at all for StructType
	// made them private to preven calling them
	virtual r_ULong* convertToCULong(const char* cell, r_ULong* value) const;
	virtual char* makeFromCULong(char* cell, const r_ULong* value) const;
	virtual r_Long* convertToCLong(const char* cell, r_Long* value) const;
	virtual char* makeFromCLong(char* cell, const r_Long* value) const;
	virtual double* convertToCDouble(const char* cell, double* value) const;
	virtual char* makeFromCDouble(char* cell, const double* value) const;

		
	protected:

		virtual void insertInDb() throw (r_Error);

		virtual void deleteFromDb() throw (r_Error);
		
		virtual void readFromDb() throw (r_Error);

		// moves back one step all elements all elements behind pos
		void moveBack(int pos);

		// calculates and sets current size of type with alignment
		void calcSize();

		/// Array containing references to base types of elements.
		std::vector< const BaseType* > elements;

		/// Array containing names of elements.
		std::vector< char* > elementNames;

		/// Array containing offsets to elements 
		std::vector< unsigned int > elementOffsets;

		/// The number of elements.
		unsigned int numElems;

		/// Alignment needed for structure if embedded in other structures.
		unsigned int align;
		
		/// add new element to struct using pointer to BaseType
		/// does the actuall adding.  the public method will not let a persitent type
		/// be changed
		unsigned int addElementPriv(const char* elemName, const BaseType* elemType);
	};
                                                                                               
#include "structtype.icc"

#endif