Commit 5b153544 authored by Kenneth Leiter's avatar Kenneth Leiter
Browse files

ENH: Add XdmfArrayType to store data type / precision information which should make interaction

between XdmfArray and XdmfHDF5Controller a bit more robust.
parent 9c5aaf4b
......@@ -34,6 +34,7 @@ endif(XDMF_WRAP_PYTHON)
set(XdmfSources
XdmfArray
XdmfArrayType
XdmfAttribute
XdmfAttributeCenter
XdmfAttributeType
......@@ -61,19 +62,19 @@ set(XdmfSources
add_library(Xdmf ${XdmfSources})
target_link_libraries(Xdmf ${HDF5_LIBRARIES} ${LIBXML2_LIBRARIES})
option(BUILD_DOCUMENTATION OFF)
if(BUILD_DOCUMENTATION)
option(XDMF_BUILD_DOCUMENTATION OFF)
if(XDMF_BUILD_DOCUMENTATION)
add_subdirectory(doc)
endif(BUILD_DOCUMENTATION)
endif(XDMF_BUILD_DOCUMENTATION)
option(BUILD_TESTING OFF)
if(BUILD_TESTING)
option(XDMF_BUILD_TESTING OFF)
if(XDMF_BUILD_TESTING)
enable_testing()
include(CTest)
add_subdirectory(tests)
endif(BUILD_TESTING)
endif(XDMF_BUILD_TESTING)
option(BUILD_UTILS OFF)
if(BUILD_UTILS)
option(XDMF_BUILD_UTILS OFF)
if(XDMF_BUILD_UTILS)
add_subdirectory(utils)
endif(BUILD_UTILS)
endif(XDMF_BUILD_UTILS)
......@@ -8,10 +8,11 @@
#include <boost/tokenizer.hpp>
#include <sstream>
#include "XdmfArray.hpp"
#include "XdmfArrayType.hpp"
#include "XdmfHDF5Controller.hpp"
#include "XdmfVisitor.hpp"
class XdmfArray::Clear : public boost::static_visitor <void> {
class XdmfArray::Clear : public boost::static_visitor<void> {
public:
Clear()
......@@ -25,7 +26,7 @@ public:
}
};
class XdmfArray::CopyArrayValues : public boost::static_visitor <void> {
class XdmfArray::CopyArrayValues : public boost::static_visitor<void> {
public:
CopyArrayValues(const unsigned int startIndex, const unsigned int valuesStartIndex, const unsigned int numValues, const unsigned int arrayStride, const unsigned int valuesStride) :
......@@ -64,7 +65,7 @@ private:
const unsigned int mValuesStride;
};
class XdmfArray::Erase : public boost::static_visitor <void> {
class XdmfArray::Erase : public boost::static_visitor<void> {
public:
Erase(const unsigned int index) :
......@@ -83,7 +84,7 @@ private:
const unsigned int mIndex;
};
class XdmfArray::GetCapacity : public boost::static_visitor <unsigned int> {
class XdmfArray::GetCapacity : public boost::static_visitor<unsigned int> {
public:
GetCapacity()
......@@ -97,137 +98,72 @@ public:
}
};
class XdmfArray::GetPrecision : public boost::static_visitor <unsigned int> {
public:
GetPrecision()
{
}
unsigned int getPrecision(const char * const) const
{
return 1;
}
unsigned int getPrecision(const short * const) const
{
return 2;
}
unsigned int getPrecision(const int * const) const
{
return 4;
}
unsigned int getPrecision(const long * const) const
{
return 8;
}
unsigned int getPrecision(const float * const) const
{
return 4;
}
unsigned int getPrecision(const double * const) const
{
return 8;
}
unsigned int getPrecision(const unsigned char * const) const
{
return 1;
}
unsigned int getPrecision(const unsigned short * const) const
{
return 2;
}
unsigned int getPrecision(const unsigned int * const) const
{
return 4;
}
template<typename T>
unsigned int operator()(const boost::shared_ptr<std::vector<T> > & array) const
{
return this->getPrecision(&(array.get()->operator[](0)));
}
template<typename T>
unsigned int operator()(const boost::shared_array<const T> & array) const
{
return this->getPrecision(array.get());
}
};
class XdmfArray::GetType : public boost::static_visitor <std::string> {
class XdmfArray::GetType : public boost::static_visitor<boost::shared_ptr<const XdmfArrayType> > {
public:
GetType()
{
}
std::string getType(const char * const) const
boost::shared_ptr<const XdmfArrayType> getType(const char * const) const
{
return "Char";
return XdmfArrayType::Int8();
}
std::string getType(const short * const) const
boost::shared_ptr<const XdmfArrayType> getType(const short * const) const
{
return "Short";
return XdmfArrayType::Int16();
}
std::string getType(const int * const) const
boost::shared_ptr<const XdmfArrayType> getType(const int * const) const
{
return "Int";
return XdmfArrayType::Int32();
}
std::string getType(const long * const) const
boost::shared_ptr<const XdmfArrayType> getType(const long * const) const
{
return "Int";
return XdmfArrayType::Int64();
}
std::string getType(const float * const) const
boost::shared_ptr<const XdmfArrayType> getType(const float * const) const
{
return "Float";
return XdmfArrayType::Float32();
}
std::string getType(const double * const) const
boost::shared_ptr<const XdmfArrayType> getType(const double * const) const
{
return "Float";
return XdmfArrayType::Float64();
}
std::string getType(const unsigned char * const) const
boost::shared_ptr<const XdmfArrayType> getType(const unsigned char * const) const
{
return "UChar";
return XdmfArrayType::UInt8();
}
std::string getType(const unsigned short * const) const
boost::shared_ptr<const XdmfArrayType> getType(const unsigned short * const) const
{
return "UShort";
return XdmfArrayType::UInt16();
}
std::string getType(const unsigned int * const) const
boost::shared_ptr<const XdmfArrayType> getType(const unsigned int * const) const
{
return "UInt";
return XdmfArrayType::UInt32();
}
template<typename T>
std::string operator()(const boost::shared_ptr<std::vector<T> > & array) const
boost::shared_ptr<const XdmfArrayType> operator()(const boost::shared_ptr<std::vector<T> > & array) const
{
return this->getType(&(array.get()->operator[](0)));
}
template<typename T>
std::string operator()(const boost::shared_array<const T> & array) const
boost::shared_ptr<const XdmfArrayType> operator()(const boost::shared_array<const T> & array) const
{
return this->getType(array.get());
}
};
class XdmfArray::GetSize : public boost::static_visitor <unsigned int> {
class XdmfArray::GetSize : public boost::static_visitor<unsigned int> {
public:
GetSize()
......@@ -241,7 +177,7 @@ public:
}
};
class XdmfArray::GetValuesPointer : public boost::static_visitor <const void * const> {
class XdmfArray::GetValuesPointer : public boost::static_visitor<const void * const> {
public:
GetValuesPointer()
......@@ -261,7 +197,7 @@ public:
}
};
class XdmfArray::GetValuesString : public boost::static_visitor <std::string> {
class XdmfArray::GetValuesString : public boost::static_visitor<std::string> {
public:
GetValuesString() :
......@@ -312,7 +248,7 @@ private:
const unsigned int mArrayPointerNumValues;
};
class XdmfArray::InternalizeArrayPointer : public boost::static_visitor <void> {
class XdmfArray::InternalizeArrayPointer : public boost::static_visitor<void> {
public:
InternalizeArrayPointer(XdmfArray * const array) :
......@@ -334,7 +270,7 @@ private:
XdmfArray * const mArray;
};
class XdmfArray::NewArray : public boost::static_visitor <void> {
class XdmfArray::NewArray : public boost::static_visitor<void> {
public:
NewArray()
......@@ -349,7 +285,7 @@ public:
}
};
class XdmfArray::Reserve : public boost::static_visitor <void> {
class XdmfArray::Reserve : public boost::static_visitor<void> {
public:
Reserve(const unsigned int size):
......@@ -448,13 +384,11 @@ std::map<std::string, std::string> XdmfArray::getItemProperties() const
{
arrayProperties["Format"] = "XML";
}
arrayProperties["DataType"] = this->getType();
std::stringstream precision;
precision << this->getPrecision();
arrayProperties["Precision"] = precision.str();
boost::shared_ptr<const XdmfArrayType> type = this->getType();
std::stringstream size;
size << this->getSize();
arrayProperties["Dimensions"] = size.str();
type->getProperties(arrayProperties);
return arrayProperties;
}
......@@ -463,23 +397,6 @@ std::string XdmfArray::getItemTag() const
return ItemTag;
}
unsigned int XdmfArray::getPrecision() const
{
if(mHaveArray)
{
return boost::apply_visitor(GetPrecision(), mArray);
}
else if(mHaveArrayPointer)
{
return boost::apply_visitor(GetPrecision(), mArrayPointer);
}
else if(mHDF5Controller)
{
return mHDF5Controller->getPrecision();
}
return 0;
}
unsigned int XdmfArray::getSize() const
{
if(mHaveArray)
......@@ -497,7 +414,7 @@ unsigned int XdmfArray::getSize() const
return 0;
}
std::string XdmfArray::getType() const
boost::shared_ptr<const XdmfArrayType> XdmfArray::getType() const
{
if(mHaveArray)
{
......@@ -511,7 +428,7 @@ std::string XdmfArray::getType() const
{
return mHDF5Controller->getType();
}
return "None";
return XdmfArrayType::Uninitialized();
}
const void * const XdmfArray::getValuesPointer() const
......@@ -540,47 +457,47 @@ std::string XdmfArray::getValuesString() const
return "";
}
void XdmfArray::initialize(const std::string & type, const unsigned int precision)
void XdmfArray::initialize(const boost::shared_ptr<const XdmfArrayType> arrayType)
{
if(type.compare("Char") == 0 && precision == 1)
if(arrayType == XdmfArrayType::Int8())
{
this->initialize<char>();
}
else if(type.compare("Short") == 0 && precision == 2)
else if(arrayType == XdmfArrayType::Int16())
{
this->initialize<short>();
}
else if(type.compare("Int") == 0 && precision == 4)
else if(arrayType == XdmfArrayType::Int32())
{
this->initialize<int>();
}
else if(type.compare("Long") == 0 && precision == 8)
else if(arrayType == XdmfArrayType::Int64())
{
this->initialize<long>();
}
else if(type.compare("Float") == 0 && precision == 4)
else if(arrayType == XdmfArrayType::Float32())
{
this->initialize<float>();
}
else if(type.compare("Float") == 0 && precision == 8)
else if(arrayType == XdmfArrayType::Float64())
{
this->initialize<double>();
}
else if(type.compare("UChar") == 0 && precision == 1)
else if(arrayType == XdmfArrayType::UInt8())
{
this->initialize<unsigned char>();
}
else if(type.compare("UShort") == 0 && precision == 2)
else if(arrayType == XdmfArrayType::UInt16())
{
this->initialize<unsigned short>();
}
else if(type.compare("UInt") == 0 && precision == 4)
else if(arrayType == XdmfArrayType::Int32())
{
this->initialize<unsigned int>();
}
else if(type.compare("None") == 0)
else if(arrayType == XdmfArrayType::Uninitialized())
{
// No-op uninitialized!
this->release();
}
else
{
......@@ -604,10 +521,9 @@ void XdmfArray::internalizeArrayPointer()
void XdmfArray::populateItem(const std::map<std::string, std::string> & itemProperties, std::vector<boost::shared_ptr<XdmfItem> > & childItems)
{
std::string contentVal;
int precisionVal;
int sizeVal;
std::string typeVal;
unsigned int sizeVal;
const boost::shared_ptr<const XdmfArrayType> arrayType = XdmfArrayType::New(itemProperties);
std::map<std::string, std::string>::const_iterator content = itemProperties.find("Content");
if(content != itemProperties.end())
{
......@@ -617,15 +533,6 @@ void XdmfArray::populateItem(const std::map<std::string, std::string> & itemProp
{
assert(false);
}
std::map<std::string, std::string>::const_iterator precision = itemProperties.find("Precision");
if(precision != itemProperties.end())
{
precisionVal = atoi(precision->second.c_str());
}
else
{
assert(false);
}
std::map<std::string, std::string>::const_iterator size = itemProperties.find("Dimensions");
if(size != itemProperties.end())
{
......@@ -635,25 +542,16 @@ void XdmfArray::populateItem(const std::map<std::string, std::string> & itemProp
{
assert(false);
}
std::map<std::string, std::string>::const_iterator type = itemProperties.find("DataType");
if(type != itemProperties.end())
{
typeVal = type->second;
}
else
{
assert(false);
}
std::map<std::string, std::string>::const_iterator format = itemProperties.find("Format");
if(format != itemProperties.end())
{
if(format->second.compare("HDF") == 0)
{
mHDF5Controller = XdmfHDF5Controller::New(contentVal, precisionVal, sizeVal, typeVal);
mHDF5Controller = XdmfHDF5Controller::New(contentVal, sizeVal, arrayType);
}
else if(format->second.compare("XML") == 0)
{
this->initialize(typeVal, precisionVal);
this->initialize(arrayType);
this->reserve(sizeVal);
boost::char_separator<char> sep(" \t\n");
boost::tokenizer<boost::char_separator<char> > tokens(contentVal, sep);
......
......@@ -2,6 +2,7 @@
#define XDMFARRAY_HPP_
// Forward Declarations
class XdmfArrayType;
class XdmfHDF5Controller;
// Includes
......@@ -112,13 +113,6 @@ public:
*/
boost::shared_ptr<const XdmfHDF5Controller> getHDF5Controller() const;
/**
* Get the precision, in bytes, of the data type of this array.
*
* @return the precision, in bytes, of the data type of this array.
*/
unsigned int getPrecision() const;
/**
* Get the number of values stored in this array.
*
......@@ -132,7 +126,7 @@ public:
* @return a string containing the Xdmf data type for the array, this is one of
* Char, Short, Int, Float, UChar, UShort, UInt.
*/
std::string getType() const;
boost::shared_ptr<const XdmfArrayType> getType() const;
/**
* Get a copy of a single value stored in this array.
......@@ -192,6 +186,11 @@ public:
template <typename T>
boost::shared_ptr<std::vector<T> > initialize();
/**
* Initializes the array to contain an empty container of a particular XdmfArrayType.
*/
void initialize(const boost::shared_ptr<const XdmfArrayType> arrayType);
/**
* Returns whether the array is initialized (contains values in memory).
*/
......@@ -319,7 +318,6 @@ private:
class Erase;
class GetCapacity;
class GetHDF5Type;
class GetPrecision;
class GetSize;
class GetType;
......@@ -341,8 +339,6 @@ private:
struct NullDeleter;
void initialize(const std::string & type, const unsigned int precision);
/**
* 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
......
......@@ -2,7 +2,7 @@
#include "XdmfArray.hpp"
template <typename T>
class XdmfArray::CopyValues : public boost::static_visitor <void> {
class XdmfArray::CopyValues : public boost::static_visitor<void> {
public:
CopyValues(const unsigned int startIndex, const T * const valuesPointer, const unsigned int numValues, const unsigned int arrayStride, const unsigned int valuesStride) :
......@@ -42,7 +42,7 @@ private:
};
template <typename T>
class XdmfArray::GetValuesCopy : public boost::static_visitor <void> {
class XdmfArray::GetValuesCopy : public boost::static_visitor<void> {
public:
GetValuesCopy(const unsigned int startIndex, T * valuesPointer, const unsigned int numValues, const unsigned int arrayStride, const unsigned int valuesStride) :
......@@ -82,7 +82,7 @@ private:
};
template <typename T>
class XdmfArray::PushBack : public boost::static_visitor <void> {
class XdmfArray::PushBack : public boost::static_visitor<void> {
public:
PushBack(const T & val) :
......@@ -102,7 +102,7 @@ private:
};
template <typename T>
class XdmfArray::Resize : public boost::static_visitor <void> {
class XdmfArray::Resize : public boost::static_visitor<void> {
public:
Resize(const unsigned int numValues, const T & val) :
......
/*
* 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()
{
}