XdmfArrayType.cpp 3.68 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
/*
 * XdmfArrayType.cpp
 *
 *  Created on: Jan 29, 2010
 *      Author: kleiter
 */

#include <sstream>
#include "XdmfArrayType.hpp"

// Supported XdmfArrayTypes
boost::shared_ptr<const XdmfArrayType> XdmfArrayType::Uninitialized()
{
	static boost::shared_ptr<const XdmfArrayType> p(new XdmfArrayType("None", 0));
	return p;
}

boost::shared_ptr<const XdmfArrayType> XdmfArrayType::Int8()
{
	static boost::shared_ptr<const XdmfArrayType> p(new XdmfArrayType("Char", 1));
	return p;
}

boost::shared_ptr<const XdmfArrayType> XdmfArrayType::Int16()
{
	static boost::shared_ptr<const XdmfArrayType> p(new XdmfArrayType("Short", 2));
	return p;
}

boost::shared_ptr<const XdmfArrayType> XdmfArrayType::Int32()
{
	static boost::shared_ptr<const XdmfArrayType> p(new XdmfArrayType("Int", 4));
	return p;
}

boost::shared_ptr<const XdmfArrayType> XdmfArrayType::Int64()
{
	static boost::shared_ptr<const XdmfArrayType> p(new XdmfArrayType("Int", 8));
	return p;
}

boost::shared_ptr<const XdmfArrayType> XdmfArrayType::Float32()
{
	static boost::shared_ptr<const XdmfArrayType> p(new XdmfArrayType("Float", 4));
	return p;
}

boost::shared_ptr<const XdmfArrayType> XdmfArrayType::Float64()
{
	static boost::shared_ptr<const XdmfArrayType> p(new XdmfArrayType("Float", 8));
	return p;
}

boost::shared_ptr<const XdmfArrayType> XdmfArrayType::UInt8()
{
	static boost::shared_ptr<const XdmfArrayType> p(new XdmfArrayType("UChar", 1));
	return p;
}

boost::shared_ptr<const XdmfArrayType> XdmfArrayType::UInt16()
{
	static boost::shared_ptr<const XdmfArrayType> p(new XdmfArrayType("UShort", 2));
	return p;
}

boost::shared_ptr<const XdmfArrayType> XdmfArrayType::UInt32()
{
	static boost::shared_ptr<const XdmfArrayType> p(new XdmfArrayType("UInt", 4));
	return p;
}

XdmfArrayType::XdmfArrayType(const std::string & name, const unsigned int precision) :
	mName(name),
	mPrecision(precision)
{
}

XdmfArrayType::~XdmfArrayType()
{
}

boost::shared_ptr<const XdmfArrayType> XdmfArrayType::New(const std::map<std::string, std::string> & itemProperties)
{
	std::map<std::string, std::string>::const_iterator type = itemProperties.find("DataType");
	std::map<std::string, std::string>::const_iterator precision = itemProperties.find("Precision");
	if(type != itemProperties.end() && precision != itemProperties.end())
	{
		const std::string typeVal = type->second;
		const unsigned int precisionVal = atoi(precision->second.c_str());
		if(typeVal.compare("None") == 0)
		{
			return Uninitialized();
		}
		else if(typeVal.compare("Char") == 0)
		{
			return Int8();
		}
		else if(typeVal.compare("Short") == 0)
		{
			return Int16();
		}
		else if(typeVal.compare("Int") == 0 && precisionVal == 4)
		{
			return Int32();
		}
		else if(typeVal.compare("Int") == 0 && precisionVal == 8)
		{
			return Int64();
		}
		else if(typeVal.compare("Float") == 0 && precisionVal == 4)
		{
			return Float32();
		}
		else if(typeVal.compare("Float") == 0 && precisionVal == 8)
		{
			return Float64();
		}
		else if(typeVal.compare("UChar") == 0)
		{
			return UInt8();
		}
		else if(typeVal.compare("UShort") == 0)
		{
			return UInt16();
		}
		else if(typeVal.compare("UInt") == 0)
		{
			return UInt32();
		}
		else
		{
			assert(false);
		}
	}
	assert(false);
}

bool XdmfArrayType::operator==(const XdmfArrayType & arrayType) const
{
	return mName.compare(arrayType.mName) == 0 && mPrecision == arrayType.mPrecision;
}

bool XdmfArrayType::operator!=(const XdmfArrayType & arrayType) const
{
	return !this->operator==(arrayType);
}

void XdmfArrayType::getProperties(std::map<std::string, std::string> & collectedProperties) const
{
	collectedProperties["DataType"] = mName;
	std::stringstream precision;
	precision << mPrecision;
	collectedProperties["Precision"] = precision.str();
}