XdmfArray.hpp 13.3 KB
Newer Older
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
1 2 3
#ifndef XDMFARRAY_HPP_
#define XDMFARRAY_HPP_

4
// Forward Declarations
5
class XdmfArrayType;
6 7
class XdmfHDF5Controller;

Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
8 9
// Includes
#include "XdmfItem.hpp"
10
#include <boost/shared_array.hpp>
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
11 12
#include <boost/variant.hpp>

13
/**
14
 * @brief Provides a single interface for storing a variety of data types.
15
 *
16
 * XdmfArray stores data values that are read in from or will be written to heavy data on disk. The
17 18 19 20 21
 * data type stored is determined by the type initially inserted into the XdmfArray.  XdmfArray
 * allows for insertion and retrieval of data in two fundamental ways:
 *
 * By Copy:
 *
22 23 24
 *	getValue
 * 	getValues
 *	insert
25 26 27 28 29 30
 *
 * 	XdmfArray stores its own copy of the data.  Modifications to the data stored in the XdmfArray will
 * 	not change values stored in the original array.
 *
 * By Shared Reference:
 *
31 32
 *	getValuesInternal
 * 	setValuesInternal
33 34 35 36 37 38
 *
 * 	XdmfArray shares a reference to the data.  No copy is made. XdmfArray holds a shared pointer to the original
 * 	data.  Modifications to the data stored in the XdmfArray also causes modification to values stored in the original
 * 	array.
 *
 * Xdmf supports the following datatypes:
39 40 41 42 43 44 45 46 47
 * 	Int8
 * 	Int16
 * 	Int32
 * 	Int64
 * 	Float32
 * 	Float64
 * 	UInt8
 * 	UInt16
 * 	UInt32
48
 */
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
49
class XdmfArray : public XdmfItem {
50

Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
51 52
public:

53 54 55 56 57
	/**
	 * Create a new XdmfArray.
	 *
	 * @return constructed XdmfArray.
	 */
58
	static boost::shared_ptr<XdmfArray> New();
59

60 61
	virtual ~XdmfArray();

62
	LOKI_DEFINE_VISITABLE(XdmfArray, XdmfItem)
63
	static const std::string ItemTag;
64

65
	/**
66
	 * Remove all values from this array.
67
	 */
68
	void clear();
69

70 71 72 73 74
	/**
	 * Remove a value from this array.
	 */
	void erase(const unsigned int index);

75 76 77 78 79 80 81 82
	/**
	 * Get the data type of this array.
	 *
	 * @return a string containing the Xdmf data type for the array, this is one of
	 *      Char, Short, Int, Float, UChar, UShort, UInt.
	 */
	boost::shared_ptr<const XdmfArrayType> getArrayType() const;

83 84
	/**
	 * Get the capacity of this array (the number of values this array can store without reallocation).
85 86
	 *
	 * @return the capacity of this array.
87 88 89
	 */
	unsigned int getCapacity() const;

90 91
	std::map<std::string, std::string> getItemProperties() const;

92 93
	std::string getItemTag() const;

94 95 96 97 98 99 100 101 102 103 104 105
	/**
	 * Get the hdf5 controller attached to this XdmfArray.
	 *
	 * @return the hdf5 controller attached to this XdmfArray.
	 */
	boost::shared_ptr<XdmfHDF5Controller> getHDF5Controller();

	/**
	 * Get the hdf5 controller attached to this XdmfArray (const version).
	 *
	 * @return the hdf5 controller attached to this XdmfArray.
	 */
106
	boost::shared_ptr<const XdmfHDF5Controller> getHDF5Controller() const;
107

108 109 110 111 112 113 114
	/**
	 * Get the name of the array.
	 *
	 * @return a string containing the name of the array.
	 */
	std::string getName() const;

115
	/**
116
	 * Get the number of values stored in this array.
117
	 *
118
	 * @return the number of values stored in this array.
119
	 */
120
	unsigned int getSize() const;
121

Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
122
	/**
123
	 * Get a copy of a single value stored in this array.
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
124
	 *
125
	 * @return the requested value.
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
126
	 */
127
	template <typename T>
128
	T getValue(const unsigned int index) const;
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
129

130 131 132 133 134 135 136 137 138 139
	/**
	 * Get a copy of the values stored in this array
	 *
	 * @param startIndex the index in this XdmfArray to begin copying from.
	 * @param valuesPointer a pointer to an array to copy into.
	 * @param numValues the number of values to copy.
	 * @param arrayStride number of values to stride in this XdmfArray between each copy.
	 * @param valuesStride number of values to stride in the pointer between each copy.
	 */
	template <typename T>
140 141 142 143 144 145 146 147 148
	void getValues(const unsigned int startIndex, T * const valuesPointer, const unsigned int numValues = 1, const unsigned int arrayStride = 1, const unsigned int valuesStride = 1) const;

	/**
	 * Get a smart pointer to the internal values stored in this array.
	 *
	 * @return a smart pointer to the internal vector of values stored in this array.
	 */
	template <typename T>
	boost::shared_ptr<std::vector<T> > getValuesInternal();
149

150
	/**
151
	 * Get a pointer to the internal values stored in this array.
152 153 154
	 *
	 * @return a void pointer to the first value stored in this array.
	 */
155
	void * getValuesInternal();
156

Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
157
	/**
158
	 * Get a pointer to the internal values stored in this array (const version).
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
159 160 161
	 *
	 * @return a void pointer to the first value stored in this array.
	 */
162
	const void * getValuesInternal() const;
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
163

164 165 166 167 168
	/**
	 * Get the values stored in this array as a string.
	 *
	 * @return a string containing the contents of the array.
	 */
169
	std::string getValuesString() const;
170

171 172
	/**
	 * Initializes the array to contain an empty container of a particular type.
173
	 *
174
	 * @param size the number of values in the initialized array.
175
	 *
176
	 * @return a smart pointer to the internal vector of values initialized in this array.
177 178
	 */
	template <typename T>
179
	boost::shared_ptr<std::vector<T> > initialize(const unsigned int size = 0);
180

181 182
	/**
	 * Initializes the array to contain an empty container of a particular XdmfArrayType.
183 184 185
	 *
	 * @param arrayType the type of array to initialize.
	 * @param size the number of values in the initialized array.
186
	 */
187
	void initialize(const boost::shared_ptr<const XdmfArrayType> arrayType, const unsigned int size = 0);
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
	/**
	 * Copy values from an XdmfArray into this array.
	 *
	 * @param startIndex the index in this array to begin insertion.
	 * @param values a shared pointer to an XdmfArray to copy into this array.
	 * @param valuesStartIndex the index in the XdmfArray to begin copying.
	 * @param numValues the number of values to copy into this array.
	 * @param arrayStride number of values to stride in this array between each copy.
	 * @param valuesStride number of values to stride in the XdmfArray between each copy.
	 */
	void insert(const unsigned int startIndex, const boost::shared_ptr<const XdmfArray> values, const unsigned int valuesStartIndex= 0, const unsigned int numValues = 1, const unsigned int arrayStride = 1, const unsigned int valuesStride = 1);

	/**
	 * Copy values into this array.
	 *
	 * @param startIndex the index in this XdmfArray to begin insertion.
	 * @param valuesPointer a pointer to the values to copy into this XdmfArray.
	 * @param numValues the number of values to copy into this array.
	 * @param arrayStride number of values to stride in this XdmfArray between each copy.
	 * @param valuesStride number of values to stride in the pointer between each copy.
	 */
	template<typename T>
	void insert(const unsigned int startIndex, const T * const valuesPointer, const unsigned int numValues = 1, const unsigned int arrayStride = 1, const unsigned int valuesStride = 1);

213 214 215
	/**
	 * Returns whether the array is initialized (contains values in memory).
	 */
216
	bool isInitialized() const;
217

218 219 220 221
	/**
	 * Copy a value to the back of this array
	 */
	template <typename T>
222
	void pushBack(const T & value);
223

224 225 226 227 228
	/**
	 * Read data from disk into memory.
	 */
	void read();

229
	/**
230
	 * Release all data currently held in memory.
231
	 */
232
	void release();
233

234 235 236 237 238 239 240
	/**
	 * Set the capacity of the XdmfArray to at least size.
	 *
	 * @param size the capacity to set this XdmfArray to.
	 */
	void reserve(const unsigned int size);

241
	/**
242
	 * Resizes the XdmfArray to contain numValues.  If numValues is larger than the current size, append values to the end equal
243 244 245
	 * to val.  If numValues is less than the current size, values at indices larger than numValues are removed.
	 *
	 * @param numValues the number of values to resize this array to.
246
	 * @param value the number to initialize newly created values to, if needed.
247 248
	 */
	template<typename T>
249
	void resize(const unsigned int numValues, const T & value = 0);
250

251 252 253 254 255
	/**
	 * Attach an hdf5 controller to this XdmfArray.
	 *
	 * @param hdf5Controller the hdf5 controller to attach to this XdmfArray.
	 */
256
	void setHDF5Controller(const boost::shared_ptr<XdmfHDF5Controller> hdf5Controller);
257

258 259 260 261 262 263 264
	/**
	 * Set the name of the array.
	 *
	 * @param name a string containing the name to set.
	 */
	void setName(const std::string & name);

265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
	/**
	 * Sets the values of this array to the values stored in the arrayPointer array.  No copy is made.  Modifications to the array are
	 * not permitted through the XdmfArray API.  Any calls through the XdmfArray API to modify the array (i.e. any non-const function)
	 * will result in the array being copied into internal storage.  The internal copy is then modified.
	 * This prevents situations where a realloc of the pointer could cause other references to become invalid.
	 * The caller of this method can continue to modify the values stored in arrayPointer on its own.
	 * This function is meant for applications that have their own array data structures that merely use Xdmf to output the data,
	 * an operation that should not require a copy.  Other applications that use Xdmf for in memory data storage should avoid
	 * this function.
	 *
	 * @param arrayPointer a pointer to an array to store in this XdmfArray.
	 * @param numValues the number of values in the array.
	 * @param transferOwnership whether to transfer responsibility for deletion of the array to XdmfArray.
	 */
	template<typename T>
280
	void setValuesInternal(const T * const arrayPointer, const unsigned int numValues, const bool transferOwnership = 0);
281

282 283 284 285 286
	/**
	 * Sets the values of this array to the values stored in the vector.  No copy is made.  The caller of this method retains
	 * ownership of the data and must ensure that the array is still valid for the entire time Xdmf needs it.
	 *
	 * @param array a vector to store in this array.
287
	 * @param transferOwnership whether to transfer responsibility for deletion of the array to XdmfArray.
288 289
	 */
	template<typename T>
290
	void setValuesInternal(std::vector<T> & array, const bool transferOwnership = 0);
291

Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
292
	/**
293 294
	 * Sets the values of this array to the values stored in the vector.  No copy is made.  This array shares ownership with
	 * other references to the smart pointer.
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
295
	 *
296
	 * @param array a smart pointer to a vector to store in this array.
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
297
	 */
298
	template<typename T>
299
	void setValuesInternal(const boost::shared_ptr<std::vector<T> > array);
300

301 302 303 304 305 306 307 308 309
	/**
	 * Exchange the contents of the vector with the contents of this XdmfArray.  No copy is made.  The internal arrays are swapped.
	 *
	 * @param array a vector to exchange values with.
	 * @return bool whether the swap was successful.
	 */
	template<typename T>
	bool swap(std::vector<T> & array);

310 311
	std::string getDimensionString() const;

312 313 314 315 316 317
	/**
	 * Exchange the contents of the vector with the contents of this XdmfArray.  No copy is made.  The internal arrays are swapped.
	 *
	 * @param array a smart pointer to a vector to exchange values with.
	 */
	template<typename T>
318
	bool swap(const boost::shared_ptr<std::vector<T> > array);
319 320 321 322 323 324 325

	/**
	 * Exchange the contents of an XdmfArray with the contents of this XdmfArray.  No copy is made.  The internal arrays are swapped.
	 *
	 * @param array a smart pointer to a vector to exchange values with.
	 * @return bool whether the swap was successful.
	 */
326
	void swap(const boost::shared_ptr<XdmfArray> array);
327

Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
328 329 330
protected:

	XdmfArray();
331
	virtual void populateItem(const std::map<std::string, std::string> & itemProperties, std::vector<boost::shared_ptr<XdmfItem> > & childItems, const XdmfCoreReader * const reader);
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
332 333 334

private:

335 336 337
	XdmfArray(const XdmfArray & array);  // Not implemented.
	void operator=(const XdmfArray & array);  // Not implemented.

338 339
	// Variant Visitor Operations
	class Clear;
340
	class Erase;
341
	class GetArrayType;
342
	class GetCapacity;
343
	class GetHDF5Type;
344

345
	template <typename T>
346
	class GetValues;
347

348 349
	class GetValuesPointer;
	class GetValuesString;
350 351 352 353 354

	template <typename T>
	class Insert;

	class InsertArray;
355
	class InternalizeArrayPointer;
356
	class NewArray;
357
	struct NullDeleter;
358 359 360 361

	template <typename T>
	class PushBack;

362
	class Reserve;
363

364
	template <typename T>
365 366
	class Resize;

367
	class Size;
368

369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
	/**
	 * After setValues(const T * const array) is called, XdmfArray stores a pointer that is not allowed to be modified through
	 * the XdmfArray API.  If the user desires to modify the contents of the pointer, they must do so without calling any
	 * non-const functions of XdmfArray.  If they do call non-const functions of XdmfArray, we try to accommodate by copying
	 * the array pointer into internal data structures.
	 */
	void internalizeArrayPointer();

	/**
	 * Release references to internal data.
	 */
	void releaseArray();

	/**
	 * Release references to held array pointer internal data;
	 */
	void releaseArrayPointer();

387 388 389 390 391 392 393 394 395 396 397
	typedef boost::variant<
		boost::shared_ptr<std::vector<char> >,
		boost::shared_ptr<std::vector<short> >,
		boost::shared_ptr<std::vector<int> >,
		boost::shared_ptr<std::vector<long> >,
		boost::shared_ptr<std::vector<float> >,
		boost::shared_ptr<std::vector<double> >,
		boost::shared_ptr<std::vector<unsigned char> >,
		boost::shared_ptr<std::vector<unsigned short> >,
		boost::shared_ptr<std::vector<unsigned int> > > ArrayVariant;

398 399 400 401 402 403 404 405 406 407
	typedef boost::variant<
		boost::shared_array<const char>,
		boost::shared_array<const short>,
		boost::shared_array<const int>,
		boost::shared_array<const long>,
		boost::shared_array<const float>,
		boost::shared_array<const double>,
		boost::shared_array<const unsigned char>,
		boost::shared_array<const unsigned short>,
		boost::shared_array<const unsigned int> > ArrayPointerVariant;
408

409
	ArrayVariant mArray;
410
	ArrayPointerVariant mArrayPointer;
411
	unsigned int mArrayPointerNumValues;
412 413
	bool mHaveArray;
	bool mHaveArrayPointer;
414
	boost::shared_ptr<XdmfHDF5Controller> mHDF5Controller;
415
	std::string mName;
416
	unsigned int mTmpReserveSize;
417
	std::string mDimensionString;
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
418 419
};

420 421
#include "XdmfArray.tpp"

Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
422
#endif /* XDMFARRAY_HPP_ */