XdmfArray.hpp 12.7 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 5 6
// Forward Declarations
class XdmfHDF5Controller;

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

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
/**
 * @brief Provides a single interface for storing a wide variety of data types.
 *
 * XdmfArray stores data values that are read in or will be written to heavy data on disk. The
 * 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:
 *
 *  copyValues
 * 	getCopyValues
 *
 * 	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:
 *
 * 	setValues
 * 	getValues
 *
 * 	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:
 * 	Char
 * 	Short
 * 	Int
 * 	Long
 * 	Float
 * 	Double
 * 	Unsigned Char
 * 	Unsigned Short
 * 	Unsigned Int
 */
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
47
class XdmfArray : public XdmfItem {
48

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

	XdmfNewMacro(XdmfArray);
52
	LOKI_DEFINE_VISITABLE(XdmfArray, XdmfItem)
53
	friend class XdmfHDF5Writer;
54
	static std::string ItemTag;
55

56
	/**
57
	 * Copy values from an XdmfArray into this array.
58
	 *
59 60
	 * @param startIndex the index in this array to begin insertion.
	 * @param values a shared pointer to an XdmfArray to copy into this array.
61
	 * @param valuesStartIndex the index in the XdmfArray to begin copying.
62 63 64
	 * @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.
65
	 */
66
	void copyValues(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);
67 68 69 70

	/**
	 * Copy values from an array into this array.
	 *
71 72
	 * @param startIndex the index in this XdmfArray to begin insertion.
	 * @param valuesPointer a pointer to the values to copy into this XdmfArray.
73
	 * @param numValues the number of values to copy into this array.
74
	 * @param arrayStride number of values to stride in this XdmfArray between each copy.
75 76
	 * @param valuesStride number of values to stride in the pointer between each copy.
	 */
77
	template<typename T>
78
	void copyValues(const unsigned int startIndex, const T * const valuesPointer, const unsigned int numValues = 1, const unsigned int arrayStride = 1, const unsigned int valuesStride = 1);
79 80

	/**
81
	 * Remove all values from this array
82
	 */
83
	void clear();
84

85 86 87 88 89
	/**
	 * Remove a value from this array.
	 */
	void erase(const unsigned int index);

90 91
	/**
	 * Get the capacity of this array (the number of values this array can store without reallocation).
92 93
	 *
	 * @return the capacity of this array.
94 95 96
	 */
	unsigned int getCapacity() const;

97 98
	std::map<std::string, std::string> getItemProperties() const;

99 100
	std::string getItemTag() const;

101 102 103 104 105 106 107 108 109 110 111 112
	/**
	 * 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.
	 */
113
	boost::shared_ptr<const XdmfHDF5Controller> getHDF5Controller() const;
114

Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
115 116 117
	/**
	 * Get the precision, in bytes, of the data type of this array.
	 *
118
	 * @return the precision, in bytes, of the data type of this array.
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
119
	 */
120
	unsigned int getPrecision() const;
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
121 122 123 124

	/**
	 * Get the number of values stored in this array.
	 *
125
	 * @return the number of values stored in this array.
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
126
	 */
127
	unsigned int getSize() const;
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
128

129
	/**
130 131 132 133 134
	 * 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.
	 */
135
	std::string getType() const;
136

137 138 139 140 141 142 143 144
	/**
	 * Get a copy of a single value stored in this array.
	 *
	 * @return the requested value.
	 */
	template <typename T>
	T getValueCopy(const unsigned int index) const;

Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
145
	/**
146
	 * Get a smart pointer to the values stored in this array.
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
147
	 *
148
	 * @return a smart pointer to the internal vector of values stored in this array.
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
149
	 */
150
	template <typename T>
151
	boost::shared_ptr<std::vector<T> > getValues();
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
152

153 154 155 156 157 158 159 160
	/**
	 * Get a smart pointer to the values stored in this array (const version).
	 *
	 * @return a smart pointer to the internal vector of values stored in this array.
	 */
	//template <typename T>
	//const boost::shared_ptr<const std::vector<T> > getValues() const;

161 162 163 164 165 166 167 168 169 170
	/**
	 * 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>
171
	void getValuesCopy(const unsigned int startIndex, T * const valuesPointer, const unsigned int numValues = 1, const unsigned int arrayStride = 1, const unsigned int valuesStride = 1) const;
172

Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
173
	/**
174
	 * Get a pointer to the values stored in this array (const version).
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
175 176 177
	 *
	 * @return a void pointer to the first value stored in this array.
	 */
178
	const void * const getValuesPointer() const;
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
179

180 181 182 183 184
	/**
	 * Get the values stored in this array as a string.
	 *
	 * @return a string containing the contents of the array.
	 */
185
	std::string getValuesString() const;
186

187 188
	/**
	 * Initializes the array to contain an empty container of a particular type.
189 190
	 *
	 * @return a smart pointer to the internal vector of values initialized in this array.
191 192
	 */
	template <typename T>
193
	boost::shared_ptr<std::vector<T> > initialize();
194

195 196 197 198 199
	/**
	 * Returns whether the array is initialized (contains values in memory).
	 */
	bool isInitialized();

200 201 202 203
	/**
	 * Copy a value to the back of this array
	 */
	template <typename T>
204
	void pushBack(const T & value);
205

206 207 208 209 210
	/**
	 * Read data from disk into memory.
	 */
	void read();

211
	/**
212
	 * Release all data currently held in memory.
213
	 */
214
	void release();
215

216 217 218 219 220 221 222
	/**
	 * 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);

223
	/**
224
	 * Resizes the XdmfArray to contain numValues.  If numValues is larger than the current size, append values to the end equal
225 226 227
	 * 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.
228
	 * @param value the number to initialize newly created values to, if needed.
229 230
	 */
	template<typename T>
231
	void resize(const unsigned int numValues, const T & value = 0);
232

233 234 235 236 237
	/**
	 * Attach an hdf5 controller to this XdmfArray.
	 *
	 * @param hdf5Controller the hdf5 controller to attach to this XdmfArray.
	 */
238
	void setHDF5Controller(const boost::shared_ptr<XdmfHDF5Controller> hdf5Controller);
239

240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
	/**
	 * 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>
255
	void setValues(const T * const arrayPointer, const unsigned int numValues, const bool transferOwnership = 0);
256

257 258 259 260 261
	/**
	 * 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.
262
	 * @param transferOwnership whether to transfer responsibility for deletion of the array to XdmfArray.
263 264
	 */
	template<typename T>
265
	void setValues(std::vector<T> & array, const bool transferOwnership = 0);
266

Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
267
	/**
268 269
	 * 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
270
	 *
271
	 * @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
272
	 */
273
	template<typename T>
274
	void setValues(const boost::shared_ptr<std::vector<T> > array);
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
275

276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
	/**
	 * 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);

	/**
	 * 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>
291
	bool swap(const boost::shared_ptr<std::vector<T> > array);
292 293 294 295 296 297 298

	/**
	 * 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.
	 */
299
	void swap(const boost::shared_ptr<XdmfArray> array);
300

Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
301 302 303 304
protected:

	XdmfArray();
	virtual ~XdmfArray();
305
	virtual void populateItem(const std::map<std::string, std::string> & itemProperties, std::vector<boost::shared_ptr<XdmfItem> > & childItems);
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
306 307 308

private:

309 310 311
	XdmfArray(const XdmfArray & array);  // Not implemented.
	void operator=(const XdmfArray & array);  // Not implemented.

312 313 314 315
	// Variant Visitor Operations
	class Clear;
	class CopyArrayValues;

316
	template <typename T>
317 318
	class CopyValues;

319
	class Erase;
320
	class GetCapacity;
321 322 323 324
	class GetHDF5Type;
	class GetPrecision;
	class GetSize;
	class GetType;
325

326
	template <typename T>
327 328
	class GetValuesCopy;

329 330
	class GetValuesPointer;
	class GetValuesString;
331
	class InternalizeArrayPointer;
332
	class NewArray;
333 334 335 336

	template <typename T>
	class PushBack;

337
	class Reserve;
338

339
	template <typename T>
340 341
	class Resize;

342 343
	struct NullDeleter;

344 345
	void initialize(const std::string & type, const unsigned int precision);

346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
	/**
	 * 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();

364 365 366 367 368 369 370 371 372 373 374
	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;

375 376 377 378 379 380 381 382 383 384
	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;
385

386
	ArrayVariant mArray;
387
	ArrayPointerVariant mArrayPointer;
388
	unsigned int mArrayPointerNumValues;
389 390
	bool mHaveArray;
	bool mHaveArrayPointer;
391
	boost::shared_ptr<XdmfHDF5Controller> mHDF5Controller;
392
	unsigned int mTmpReserveSize;
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
393 394
};

395 396
#include "XdmfArray.tpp"

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