XdmfArray.tpp 2.97 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(int startIndex, T * valuesPointer, int numValues = 1, int arrayStride = 1, int valuesStride = 1) :
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 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
	{
		int size = mStartIndex + mNumValues;
		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:

	int mStartIndex;
	T* mValuesPointer;
	int mNumValues;
	int mArrayStride;
	int mValuesStride;
};

44
struct XdmfArray::NullDeleter
45 46 47 48 49 50
{
    void operator()(void const *) const
    {
    }
};

51 52 53 54 55
template<typename T>
void XdmfArray::copyValues(int startIndex, T * valuesPointer, int numValues, int arrayStride, int valuesStride)
{
	if(!mInitialized)
	{
56
		initialize<T>();
57
	}
58
	boost::apply_visitor( CopyValues<T>(startIndex, valuesPointer, numValues, arrayStride, valuesStride), mArray);
59 60 61 62 63
}

template <typename T>
boost::shared_ptr<std::vector<T> > XdmfArray::getValues()
{
64 65 66 67 68 69 70 71 72
	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> >();
	}
73 74 75 76 77
}

template <typename T>
const boost::shared_ptr<const std::vector<T> > XdmfArray::getValues() const
{
78 79 80 81 82 83 84 85 86
	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> >();
	}
87 88
}

89
template <typename T>
90
boost::shared_ptr<std::vector<T> > XdmfArray::initialize()
91 92 93 94 95
{
	// Set type of variant to type of pointer
	boost::shared_ptr<std::vector<T> > newArray(new std::vector<T>());
	mArray = newArray;
	mInitialized = true;
96 97 98 99 100 101
	return newArray;
}

template<typename T>
void XdmfArray::setValues(std::vector<T> & array)
{
102
	boost::shared_ptr<std::vector<T> > newArray(&array, NullDeleter());
103 104
	mArray = newArray;
	mInitialized = true;
105 106
}

107 108 109 110
template<typename T>
void XdmfArray::setValues(boost::shared_ptr<std::vector<T> > array)
{
	mArray = array;
111 112 113 114 115 116 117 118
	mInitialized = true;
}

template<typename T>
bool XdmfArray::swap(std::vector<T> & array)
{
	if(!mInitialized)
	{
119
		initialize<T>();
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
	}
	try
	{
		boost::shared_ptr<std::vector<T> > currArray = boost::get<boost::shared_ptr<std::vector<T> > >(mArray);
		currArray->swap(array);
		if(currArray->size() == 0)
		{
			mInitialized = false;
		}
		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());
141
}