XdmfArray.cpp 5.44 KB
Newer Older
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
1
2
3
4
5
6
7
8
9
10
11
/*
 * XdmfArray.cpp
 *
 *  Created on: Jan 29, 2010
 *      Author: kleiter
 */

#include "XdmfArray.hpp"
#include "XdmfVisitor.hpp"
#include <sstream>

12
class XdmfArrayGetHDF5Type : public boost::static_visitor <hid_t> {
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
13
public:
14
	XdmfArrayGetHDF5Type()
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
15
16
17
	{
	}

18
	hid_t operator()(const boost::shared_ptr<std::vector<char> > array) const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
19
	{
20
		return H5T_NATIVE_CHAR;
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
21
22
	}

23
	hid_t operator()(const boost::shared_ptr<std::vector<short> > array) const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
24
	{
25
		return H5T_NATIVE_SHORT;
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
26
27
	}

28
	hid_t operator()(const boost::shared_ptr<std::vector<int> > array) const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
29
	{
30
		return H5T_NATIVE_INT;
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
31
32
	}

33
	hid_t operator()(const boost::shared_ptr<std::vector<long> > array) const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
34
	{
35
		return H5T_NATIVE_LONG;
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
36
37
	}

38
	hid_t operator()(const boost::shared_ptr<std::vector<float> > array) const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
39
	{
40
		return H5T_NATIVE_FLOAT;
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
41
42
	}

43
	hid_t operator()(const boost::shared_ptr<std::vector<double> > array) const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
44
	{
45
		return H5T_NATIVE_DOUBLE;
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
46
47
	}

48
	hid_t operator()(const boost::shared_ptr<std::vector<unsigned char> > array) const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
49
	{
50
		return H5T_NATIVE_UCHAR;
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
51
52
	}

53
	hid_t operator()(const boost::shared_ptr<std::vector<unsigned short> > array) const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
54
	{
55
		return H5T_NATIVE_USHORT;
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
56
57
	}

58
	hid_t operator()(const boost::shared_ptr<std::vector<unsigned int> > array) const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
59
	{
60
		return H5T_NATIVE_UINT;
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
61
62
63
	}
};

64
class XdmfArrayGetPrecision : public boost::static_visitor <int> {
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
65
public:
66
	XdmfArrayGetPrecision()
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
67
68
69
	{
	}

70
	int operator()(const boost::shared_ptr<std::vector<char> > array) const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
71
	{
72
		return 1;
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
73
74
	}

75
	int operator()(const boost::shared_ptr<std::vector<short> > array) const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
76
	{
77
		return 2;
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
78
79
	}

80
	int operator()(const boost::shared_ptr<std::vector<int> > array) const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
81
	{
82
		return 4;
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
83
84
	}

85
	int operator()(const boost::shared_ptr<std::vector<long> > array) const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
86
	{
87
		return 8;
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
88
89
	}

90
	int operator()(const boost::shared_ptr<std::vector<float> > array) const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
91
	{
92
		return 4;
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
93
94
	}

95
	int operator()(const boost::shared_ptr<std::vector<double> > array) const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
96
	{
97
		return 8;
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
98
99
	}

100
	int operator()(const boost::shared_ptr<std::vector<unsigned char> > array) const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
101
	{
102
		return 1;
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
103
104
	}

105
	int operator()(const boost::shared_ptr<std::vector<unsigned short> > array) const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
106
	{
107
		return 2;
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
108
109
	}

110
	int operator()(const boost::shared_ptr<std::vector<unsigned int> > array) const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
111
	{
112
		return 4;
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
113
114
115
	}
};

116
class XdmfArrayGetType : public boost::static_visitor <std::string> {
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
117
public:
118
	XdmfArrayGetType()
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
119
120
121
	{
	}

122
	std::string operator()(const boost::shared_ptr<std::vector<char> > array) const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
123
	{
124
		return "Char";
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
125
126
	}

127
	std::string operator()(const boost::shared_ptr<std::vector<short> > array) const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
128
	{
129
		return "Short";
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
130
131
	}

132
	std::string operator()(const boost::shared_ptr<std::vector<int> > array) const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
133
	{
134
		return "Int";
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
135
136
	}

137
	std::string operator()(const boost::shared_ptr<std::vector<long> > array) const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
138
	{
139
		return "Int";
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
140
141
	}

142
	std::string operator()(const boost::shared_ptr<std::vector<float> > array) const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
143
	{
144
		return "Float";
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
145
146
	}

147
	std::string operator()(const boost::shared_ptr<std::vector<double> > array) const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
148
	{
149
		return "Float";
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
150
151
	}

152
	std::string operator()(const boost::shared_ptr<std::vector<unsigned char> > array) const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
153
	{
154
		return "UChar";
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
155
156
	}

157
	std::string operator()(const boost::shared_ptr<std::vector<unsigned short> > array) const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
158
	{
159
		return "UShort";
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
160
161
	}

162
	std::string operator()(const boost::shared_ptr<std::vector<unsigned int> > array) const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
163
	{
164
		return "UInt";
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
165
166
167
168
169
170
171
172
173
174
	}
};

class XdmfArrayGetSize : public boost::static_visitor <int> {
public:

	XdmfArrayGetSize()
	{
	}

175
	template<typename T> int operator()(const boost::shared_ptr<std::vector<T> > array) const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
176
	{
177
		return array->size();
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
178
179
180
	}
};

181
class XdmfArrayGetValuesPointer : public boost::static_visitor <const void* const> {
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
182
183
public:

184
185
186
187
188
189
190
191
192
193
194
195
196
197
	XdmfArrayGetValuesPointer()
	{
	}

	template<typename T> const void* const operator()(const boost::shared_ptr<std::vector<T> > array) const
	{
		return &array->operator[](0);
	}
};

class XdmfArrayGetValuesString : public boost::static_visitor <std::string> {
public:

	XdmfArrayGetValuesString()
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
198
199
200
	{
	}

201
	std::string operator()(const boost::shared_ptr<std::vector<char> > array) const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
202
203
	{
		std::stringstream toReturn;
204
		for(int i=0; i<array->size(); ++i)
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
205
		{
206
			toReturn << (int)(array->operator[](i)) << " ";
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
207
208
209
210
		}
		return toReturn.str();
	}

211
	template<typename T> std::string operator()(const boost::shared_ptr<std::vector<T> > array) const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
212
213
	{
		std::stringstream toReturn;
214
		for(int i=0; i<array->size(); ++i)
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
215
		{
216
			toReturn << array->operator[](i) << " ";
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
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
		}
		return toReturn.str();
	}
};

XdmfArray::XdmfArray() :
	mInitialized(false)
{
	std::cout << "Created Array " << this << std::endl;
}

XdmfArray::~XdmfArray()
{
	std::cout << "Deleted Array " << this << std::endl;
}

hid_t XdmfArray::getHDF5Type() const
{
	return boost::apply_visitor( XdmfArrayGetHDF5Type(), mArray);
}

int XdmfArray::getPrecision() const
{
	return boost::apply_visitor( XdmfArrayGetPrecision(), mArray);
}

int XdmfArray::getSize() const
{
	return boost::apply_visitor( XdmfArrayGetSize(), mArray);
}

std::string XdmfArray::getType() const
{
	return boost::apply_visitor( XdmfArrayGetType(), mArray);
}

253
254
255
256
//const boost::shared_ptr<std::vector<void> > getValues() const
//{
	//return boost::apply_visitor( XdmfArrayGetValues(), mArray);
//}
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
257

258
const void* const XdmfArray::getValuesPointer() const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
259
260
261
262
{
	return boost::apply_visitor( XdmfArrayGetValuesPointer(), mArray);
}

263
std::string XdmfArray::getValuesString() const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
264
{
265
	return boost::apply_visitor( XdmfArrayGetValuesString(), mArray);
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
266
267
268
269
270
271
272
273
274
275
276
}

std::string XdmfArray::printSelf() const
{
	return "";
}

void XdmfArray::write(boost::shared_ptr<XdmfVisitor> visitor) const
{
	visitor->visit(this);
}