XdmfArray.tpp 4.87 KB
Newer Older
1 2 3 4
// Includes
#include "XdmfArray.hpp"

template<typename T>
5
class XdmfArray::CopyValues : public boost::static_visitor <void> {
6 7
public:

8
	CopyValues(const unsigned int startIndex, const T * const valuesPointer, const unsigned int numValues, const unsigned int arrayStride, const unsigned int valuesStride) :
9 10 11 12 13 14 15 16
		mStartIndex(startIndex),
		mValuesPointer(valuesPointer),
		mNumValues(numValues),
		mArrayStride(arrayStride),
		mValuesStride(valuesStride)
	{
	}

17 18
	template<typename U>
	void operator()(boost::shared_ptr<std::vector<U> > & array) const
19
	{
20
		unsigned int size = mStartIndex + mNumValues;
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
		if(mArrayStride > 1)
		{
			size = mStartIndex + mNumValues * mArrayStride - 1;
		}
		if(array->size() < size)
		{
			array->resize(size);
		}
		for(int i=0; i<mNumValues; ++i)
		{
			array->operator[](mStartIndex + i*mArrayStride) = (U)mValuesPointer[i*mValuesStride];
		}
	}

private:

37
	const unsigned int mStartIndex;
38
	const T * const mValuesPointer;
39 40 41
	const unsigned int mNumValues;
	const unsigned int mArrayStride;
	const unsigned int mValuesStride;
42 43
};

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
template<typename T>
class XdmfArray::Resize : public boost::static_visitor <void> {
public:

	Resize(const unsigned int numValues, const T & val) :
		mNumValues(numValues),
		mVal(val)
	{
	}

	template<typename U>
	void operator()(boost::shared_ptr<std::vector<U> > & array) const
	{
		array->resize(mNumValues, (U)mVal);
	}

private:

	const unsigned int mNumValues;
	const T & mVal;
};

66
struct XdmfArray::NullDeleter
67 68 69 70 71 72
{
    void operator()(void const *) const
    {
    }
};

73
template<typename T>
74
void XdmfArray::copyValues(const unsigned int startIndex, const T * const valuesPointer, const unsigned int numValues, const unsigned int arrayStride, const unsigned int valuesStride)
75
{
76 77 78 79 80
	if(mHaveArrayPointer)
	{
		internalizeArrayPointer();
	}
	if(!mHaveArray)
81
	{
82
		initialize<T>();
83
	}
84
	boost::apply_visitor( CopyValues<T>(startIndex, valuesPointer, numValues, arrayStride, valuesStride), mArray);
85 86 87 88 89
}

template <typename T>
boost::shared_ptr<std::vector<T> > XdmfArray::getValues()
{
90
	if(mHaveArrayPointer)
91
	{
92
		internalizeArrayPointer();
93 94 95 96 97 98 99 100 101 102
	}
	try
	{
		boost::shared_ptr<std::vector<T> > currArray = boost::get<boost::shared_ptr<std::vector<T> > >(mArray);
		return currArray;
	}
	catch(const boost::bad_get& exception)
	{
		return boost::shared_ptr<std::vector<T> >();
	}
103 104
}

105 106 107 108 109 110 111 112 113 114 115 116 117 118
//template <typename T>
//const boost::shared_ptr<const std::vector<T> > XdmfArray::getValues() const
//{
//	try
//	{
//		boost::shared_ptr<std::vector<T> > currArray = boost::get<boost::shared_ptr<std::vector<T> > >(mArray);
//		return currArray;
//	}
//	catch(const boost::bad_get& exception)
//	{
//		return boost::shared_ptr<std::vector<T> >();
//	}
//}

119
template <typename T>
120
boost::shared_ptr<std::vector<T> > XdmfArray::initialize()
121
{
122 123 124 125
	if(mHaveArrayPointer)
	{
		releaseArrayPointer();
	}
126 127
	// Set type of variant to type of pointer
	boost::shared_ptr<std::vector<T> > newArray(new std::vector<T>());
128 129 130 131 132
	if(mTmpReserveSize > 0)
	{
		newArray->reserve(mTmpReserveSize);
		mTmpReserveSize = 0;
	}
133
	mArray = newArray;
134
	mHaveArray = true;
135 136 137
	return newArray;
}

138 139 140 141 142 143 144 145 146 147 148 149 150 151
template<typename T>
void XdmfArray::resize(const unsigned int numValues, const T & val)
{
	if(mHaveArrayPointer)
	{
		internalizeArrayPointer();
	}
	if(!mHaveArray)
	{
		initialize<T>();
	}
	return boost::apply_visitor(Resize<T>(numValues, val), mArray);
}

152
template<typename T>
153
void XdmfArray::setValues(const T * const arrayPointer, const unsigned int numValues, const bool transferOwnership)
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
{
	// Remove contents of internal array.
	if(mHaveArray)
	{
		releaseArray();
	}
	if(transferOwnership)
	{
		const boost::shared_array<const T> newArrayPointer(arrayPointer);
		mArrayPointer = newArrayPointer;
	}
	else
	{
		const boost::shared_array<const T> newArrayPointer(arrayPointer, NullDeleter());
		mArrayPointer = newArrayPointer;
	}
	mHaveArrayPointer = true;
	mArrayPointerNumValues = numValues;
}

174
template<typename T>
175
void XdmfArray::setValues(std::vector<T> & array, const bool transferOwnership)
176
{
177 178 179 180
	if(mHaveArrayPointer)
	{
		releaseArrayPointer();
	}
181 182 183 184 185 186 187 188 189 190
	if(transferOwnership)
	{
		boost::shared_ptr<std::vector<T> > newArray(&array);
		mArray = newArray;
	}
	else
	{
		boost::shared_ptr<std::vector<T> > newArray(&array, NullDeleter());
		mArray = newArray;
	}
191
	mHaveArray = true;
192 193
}

194 195 196
template<typename T>
void XdmfArray::setValues(boost::shared_ptr<std::vector<T> > array)
{
197 198 199 200
	if(mHaveArrayPointer)
	{
		releaseArrayPointer();
	}
201
	mArray = array;
202
	mHaveArray = true;
203 204 205 206 207
}

template<typename T>
bool XdmfArray::swap(std::vector<T> & array)
{
208 209 210 211 212
	if(mHaveArrayPointer)
	{
		internalizeArrayPointer();
	}
	if(!mHaveArray)
213
	{
214
		initialize<T>();
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
	}
	try
	{
		boost::shared_ptr<std::vector<T> > currArray = boost::get<boost::shared_ptr<std::vector<T> > >(mArray);
		currArray->swap(array);
		return true;
	}
	catch(const boost::bad_get& exception)
	{
		return false;
	}
}

template<typename T>
bool XdmfArray::swap(boost::shared_ptr<std::vector<T> > array)
{
	return this->swap(*array.get());
232
}