Commit 5ed3eebe authored by Kenneth Leiter's avatar Kenneth Leiter

ENH: Remove getHDF5Type from XdmfArray and move into XdmfHDF5Writer so all...

ENH: Remove getHDF5Type from XdmfArray and move into XdmfHDF5Writer so all hdf5 interaction is isolated
in XdmfHDF5Writer.  Friend the XdmfHDF5Writer from XdmfArray so that it can access the variant to perform
typesafe operations on the data stored.  Variable macro generation didn't work so reverted the modified code.
We may just have to write ugly static New() methods when required.
parent f8cd6652
...@@ -76,71 +76,6 @@ public: ...@@ -76,71 +76,6 @@ public:
} }
}; };
class XdmfArray::GetHDF5Type : public boost::static_visitor <hid_t> {
public:
GetHDF5Type()
{
}
hid_t getHDF5Type(const char * const) const
{
return H5T_NATIVE_CHAR;
}
hid_t getHDF5Type(const short * const) const
{
return H5T_NATIVE_SHORT;
}
hid_t getHDF5Type(const int * const) const
{
return H5T_NATIVE_INT;
}
hid_t getHDF5Type(const long * const) const
{
return H5T_NATIVE_LONG;
}
hid_t getHDF5Type(const float * const) const
{
return H5T_NATIVE_FLOAT;
}
hid_t getHDF5Type(const double * const) const
{
return H5T_NATIVE_DOUBLE;
}
hid_t getHDF5Type(const unsigned char * const) const
{
return H5T_NATIVE_UCHAR;
}
hid_t getHDF5Type(const unsigned short * const) const
{
return H5T_NATIVE_USHORT;
}
hid_t getHDF5Type(const unsigned int * const) const
{
return H5T_NATIVE_UINT;
}
template<typename T>
hid_t operator()(const boost::shared_ptr<std::vector<T> > & array) const
{
return this->getHDF5Type(&(array.get()->operator[](0)));
}
template<typename T>
hid_t operator()(const boost::shared_array<const T> & array) const
{
return this->getHDF5Type(array.get());
}
};
class XdmfArray::GetPrecision : public boost::static_visitor <unsigned int> { class XdmfArray::GetPrecision : public boost::static_visitor <unsigned int> {
public: public:
...@@ -455,19 +390,6 @@ unsigned int XdmfArray::getCapacity() const ...@@ -455,19 +390,6 @@ unsigned int XdmfArray::getCapacity() const
return 0; return 0;
} }
hid_t XdmfArray::getHDF5Type() const
{
if(mHaveArray)
{
return boost::apply_visitor(GetHDF5Type(), mArray);
}
else if(mHaveArrayPointer)
{
return boost::apply_visitor(GetHDF5Type(), mArrayPointer);
}
return -1;
}
std::map<std::string, std::string> XdmfArray::getItemProperties() const std::map<std::string, std::string> XdmfArray::getItemProperties() const
{ {
std::map<std::string, std::string> arrayProperties; std::map<std::string, std::string> arrayProperties;
......
...@@ -5,7 +5,6 @@ ...@@ -5,7 +5,6 @@
#include "XdmfItem.hpp" #include "XdmfItem.hpp"
#include <boost/shared_array.hpp> #include <boost/shared_array.hpp>
#include <boost/variant.hpp> #include <boost/variant.hpp>
#include <hdf5.h>
#include <vector> #include <vector>
/** /**
...@@ -48,6 +47,7 @@ public: ...@@ -48,6 +47,7 @@ public:
XdmfNewMacro(XdmfArray); XdmfNewMacro(XdmfArray);
LOKI_DEFINE_VISITABLE(XdmfArray, XdmfItem) LOKI_DEFINE_VISITABLE(XdmfArray, XdmfItem)
friend class XdmfHDF5Writer;
/** /**
* Copy values from an XdmfArray into this array. * Copy values from an XdmfArray into this array.
...@@ -85,13 +85,6 @@ public: ...@@ -85,13 +85,6 @@ public:
*/ */
unsigned int getCapacity() const; unsigned int getCapacity() const;
/**
* Get the hdf5 data type of this array.
*
* @return the hdf5 data type for the array.
*/
virtual hid_t getHDF5Type() const;
std::map<std::string, std::string> getItemProperties() const; std::map<std::string, std::string> getItemProperties() const;
std::string getItemTag() const; std::string getItemTag() const;
......
...@@ -14,7 +14,7 @@ class XdmfHDF5Writer::XdmfHDF5WriterImpl { ...@@ -14,7 +14,7 @@ class XdmfHDF5Writer::XdmfHDF5WriterImpl {
public: public:
XdmfHDF5WriterImpl() : XdmfHDF5WriterImpl(std::string & hdf5FilePath) :
mHDF5Handle(H5Fcreate("output.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT)), mHDF5Handle(H5Fcreate("output.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT)),
mHeavyFileName("output.h5") mHeavyFileName("output.h5")
{ {
...@@ -28,8 +28,73 @@ public: ...@@ -28,8 +28,73 @@ public:
std::string mHeavyFileName; std::string mHeavyFileName;
}; };
XdmfHDF5Writer::XdmfHDF5Writer() : class XdmfHDF5Writer::GetHDF5Type : public boost::static_visitor <hid_t> {
mImpl(new XdmfHDF5WriterImpl()) public:
GetHDF5Type()
{
}
hid_t getHDF5Type(const char * const) const
{
return H5T_NATIVE_CHAR;
}
hid_t getHDF5Type(const short * const) const
{
return H5T_NATIVE_SHORT;
}
hid_t getHDF5Type(const int * const) const
{
return H5T_NATIVE_INT;
}
hid_t getHDF5Type(const long * const) const
{
return H5T_NATIVE_LONG;
}
hid_t getHDF5Type(const float * const) const
{
return H5T_NATIVE_FLOAT;
}
hid_t getHDF5Type(const double * const) const
{
return H5T_NATIVE_DOUBLE;
}
hid_t getHDF5Type(const unsigned char * const) const
{
return H5T_NATIVE_UCHAR;
}
hid_t getHDF5Type(const unsigned short * const) const
{
return H5T_NATIVE_USHORT;
}
hid_t getHDF5Type(const unsigned int * const) const
{
return H5T_NATIVE_UINT;
}
template<typename T>
hid_t operator()(const boost::shared_ptr<std::vector<T> > & array) const
{
return this->getHDF5Type(&(array.get()->operator[](0)));
}
template<typename T>
hid_t operator()(const boost::shared_array<const T> & array) const
{
return this->getHDF5Type(array.get());
}
};
XdmfHDF5Writer::XdmfHDF5Writer(std::string & hdf5FilePath) :
mImpl(new XdmfHDF5WriterImpl(hdf5FilePath))
{ {
std::cout << "Created XdmfHDF5Writer " << this << std::endl; std::cout << "Created XdmfHDF5Writer " << this << std::endl;
} }
...@@ -101,27 +166,42 @@ void XdmfHDF5Writer::pushDataHierarchy(const XdmfItem & item) ...@@ -101,27 +166,42 @@ void XdmfHDF5Writer::pushDataHierarchy(const XdmfItem & item)
std::string XdmfHDF5Writer::visit(XdmfArray & array, boost::shared_ptr<Loki::BaseVisitor> visitor) std::string XdmfHDF5Writer::visit(XdmfArray & array, boost::shared_ptr<Loki::BaseVisitor> visitor)
{ {
herr_t status; hid_t datatype = -1;
hsize_t size = array.getSize(); if(array.mHaveArray)
hid_t dataspace = H5Screate_simple(1, &size, NULL);
hid_t handle = mImpl->mHDF5Handle;
std::string groupName = getHDF5GroupHandle();
if(groupName.compare("") != 0)
{ {
handle = H5Gopen(mImpl->mHDF5Handle, groupName.c_str(), H5P_DEFAULT); datatype = boost::apply_visitor(GetHDF5Type(), array.mArray);
} }
hid_t dataset = H5Dcreate(handle, mImpl->mDataHierarchy.back().c_str(), array.getHDF5Type(), dataspace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); else if(array.mHaveArrayPointer)
status = H5Dwrite(dataset, array.getHDF5Type(), H5S_ALL, H5S_ALL, H5P_DEFAULT, array.getValuesPointer());
if(groupName.compare("") != 0)
{ {
H5Gclose(handle); datatype = boost::apply_visitor(GetHDF5Type(), array.mArrayPointer);
} }
status = H5Dclose(dataset);
status = H5Sclose(dataspace);
std::stringstream dataSetName; if(datatype != -1)
dataSetName << mImpl->mHeavyFileName << ":" << groupName << "/" << mImpl->mDataHierarchy.back(); {
return dataSetName.str(); herr_t status;
hsize_t size = array.getSize();
hid_t dataspace = H5Screate_simple(1, &size, NULL);
hid_t handle = mImpl->mHDF5Handle;
std::string groupName = getHDF5GroupHandle();
if(groupName.compare("") != 0)
{
handle = H5Gopen(mImpl->mHDF5Handle, groupName.c_str(), H5P_DEFAULT);
}
hid_t dataset = H5Dcreate(handle, mImpl->mDataHierarchy.back().c_str(), datatype, dataspace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
status = H5Dwrite(dataset, datatype, H5S_ALL, H5S_ALL, H5P_DEFAULT, array.getValuesPointer());
if(groupName.compare("") != 0)
{
H5Gclose(handle);
}
status = H5Dclose(dataset);
status = H5Sclose(dataspace);
std::stringstream dataSetName;
dataSetName << mImpl->mHeavyFileName << ":" << groupName << "/" << mImpl->mDataHierarchy.back();
return dataSetName.str();
}
return "";
} }
void XdmfHDF5Writer::visit(XdmfItem & item, boost::shared_ptr<Loki::BaseVisitor> visitor) void XdmfHDF5Writer::visit(XdmfItem & item, boost::shared_ptr<Loki::BaseVisitor> visitor)
......
...@@ -19,7 +19,12 @@ class XdmfHDF5Writer : public XdmfVisitor, ...@@ -19,7 +19,12 @@ class XdmfHDF5Writer : public XdmfVisitor,
public: public:
XdmfNewMacro(XdmfHDF5Writer); template <typename T> friend void boost::checked_delete(T * x);
static boost::shared_ptr<XdmfHDF5Writer> New(std::string hdf5FilePath)
{
boost::shared_ptr<XdmfHDF5Writer> p(new XdmfHDF5Writer(hdf5FilePath));
return p;
}
/** /**
* Pop a string from the data hierarchy. This should be called whenever an XdmfItem is finished * Pop a string from the data hierarchy. This should be called whenever an XdmfItem is finished
...@@ -58,7 +63,12 @@ public: ...@@ -58,7 +63,12 @@ public:
protected: protected:
XdmfHDF5Writer(); /**
* Construct XdmfHDF5Writer
*
* @param hdf5FilePath the location of the hdf5 file to output to on disk.
*/
XdmfHDF5Writer(std::string & hdf5FilePath);
virtual ~XdmfHDF5Writer(); virtual ~XdmfHDF5Writer();
private: private:
...@@ -68,6 +78,9 @@ private: ...@@ -68,6 +78,9 @@ private:
*/ */
class XdmfHDF5WriterImpl; class XdmfHDF5WriterImpl;
// Variant Visitor Operation
class GetHDF5Type;
XdmfHDF5Writer(const XdmfHDF5Writer & hdf5Writer); // Not implemented. XdmfHDF5Writer(const XdmfHDF5Writer & hdf5Writer); // Not implemented.
void operator=(const XdmfHDF5Writer & hdf5Writer); // Not implemented. void operator=(const XdmfHDF5Writer & hdf5Writer); // Not implemented.
......
...@@ -4,9 +4,9 @@ ...@@ -4,9 +4,9 @@
// Includes // Includes
#include <boost/shared_ptr.hpp> #include <boost/shared_ptr.hpp>
#define XdmfNewMacro(type, ...) \ #define XdmfNewMacro(type) \
template <typename T> friend void boost::checked_delete(T * x); \ template <typename T> friend void boost::checked_delete(T * x); \
static boost::shared_ptr<type> New(__VA_ARGS__) \ static boost::shared_ptr<type> New() \
{ \ { \
boost::shared_ptr<type> p(new type()); \ boost::shared_ptr<type> p(new type()); \
return p; \ return p; \
......
...@@ -16,7 +16,7 @@ class XdmfWriter::XdmfWriterImpl { ...@@ -16,7 +16,7 @@ class XdmfWriter::XdmfWriterImpl {
public: public:
XdmfWriterImpl() : XdmfWriterImpl() :
mHDF5Writer(XdmfHDF5Writer::New()), mHDF5Writer(XdmfHDF5Writer::New("output.h5")),
mLightDataLimit(100), mLightDataLimit(100),
mXMLDocument(xmlNewDoc((xmlChar*)"1.0")), mXMLDocument(xmlNewDoc((xmlChar*)"1.0")),
mXMLCurrentNode(xmlNewNode(NULL, (xmlChar*)"Xdmf")) mXMLCurrentNode(xmlNewNode(NULL, (xmlChar*)"Xdmf"))
......
...@@ -17,14 +17,12 @@ int main(int argc, char* argv[]) ...@@ -17,14 +17,12 @@ int main(int argc, char* argv[])
assert(array->getType() == ""); assert(array->getType() == "");
assert(array->getPrecision() == 0); assert(array->getPrecision() == 0);
assert(array->getValuesString() == ""); assert(array->getValuesString() == "");
assert(array->getHDF5Type() == -1);
assert(array->getValuesPointer() == NULL); assert(array->getValuesPointer() == NULL);
array->copyValues(0, &values[0], 4, 1, 1); array->copyValues(0, &values[0], 4, 1, 1);
assert(array->getSize() == 4); assert(array->getSize() == 4);
assert(array->getType().compare("Int") == 0); assert(array->getType().compare("Int") == 0);
assert(array->getPrecision() == 4); assert(array->getPrecision() == 4);
assert(array->getValuesString().compare("1 2 3 4 ") == 0); assert(array->getValuesString().compare("1 2 3 4 ") == 0);
assert(array->getHDF5Type() == H5T_NATIVE_INT);
const int * const arrayPointer = (const int * const)array->getValuesPointer(); const int * const arrayPointer = (const int * const)array->getValuesPointer();
assert(arrayPointer[0] == 1); assert(arrayPointer[0] == 1);
assert(arrayPointer[1] == 2); assert(arrayPointer[1] == 2);
...@@ -103,7 +101,6 @@ int main(int argc, char* argv[]) ...@@ -103,7 +101,6 @@ int main(int argc, char* argv[])
assert(array5->getType().compare("Int") == 0); assert(array5->getType().compare("Int") == 0);
assert(array5->getPrecision() == 4); assert(array5->getPrecision() == 4);
assert(array5->getValuesString().compare("1 2 ") == 0); assert(array5->getValuesString().compare("1 2 ") == 0);
assert(array5->getHDF5Type() == H5T_NATIVE_INT);
const int * const array5Pointer = (const int * const)array5->getValuesPointer(); const int * const array5Pointer = (const int * const)array5->getValuesPointer();
assert(array5Pointer[0] == 1); assert(array5Pointer[0] == 1);
assert(array5Pointer[1] == 2); assert(array5Pointer[1] == 2);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment