Simplify file format definition

This commit is contained in:
2018-01-12 18:44:14 +01:00
parent fb51153c66
commit c83dd679b7
2 changed files with 40 additions and 28 deletions
+32 -11
View File
@@ -23,18 +23,39 @@ void h5_utils::write_attribute(H5::H5Object& target, string name, int32_t value)
h5_attribute.write(int_type, &value);
}
void h5_utils::set_attributes(H5::H5Object& target, std::list<h5_attribute>& attributes) {
for (auto& attribute : attributes) {
void h5_utils::write_attribute(H5::H5Object& target, h5_attr& attribute) {
string name = attribute.name;
if (attribute.data_type == NX_CHAR) {
auto attribute_ptr = reinterpret_cast<h5_attribute_val<NX_CHAR>*>(&attribute);
h5_utils::write_attribute(target, name, attribute_ptr->value);
cout<< attribute.name << endl;
} else if (attribute.data_type == NX_INT) {
auto attribute_ptr = reinterpret_cast<h5_attribute_val<NX_INT>*>(&attribute);
h5_utils::write_attribute(target, name, attribute_ptr->value);
}
string name = attribute.name;
boost::any value;
if (attribute.data_location == IMMEDIATE){
value = attribute.value;
} else {
// TODO: Implement value.
}
if (attribute.data_type == NX_CHAR) {
// Attempt to convert to const char * (string "literals" cause that).
try {
h5_utils::write_attribute(target, name, string(boost::any_cast<const char*>(value)));
return;
} catch (const boost::bad_any_cast& exception) {}
// Atempt to convert to string.
try {
h5_utils::write_attribute(target, name, boost::any_cast<string>(value));
return;
} catch (const boost::bad_any_cast& exception) {}
// We cannot really convert this attribute.
stringstream error_message;
error_message << "Cannot convert attribute " << name << "to string or const char*." << endl;
throw runtime_error(error_message.str());
} else if (attribute.data_type == NX_INT) {
h5_utils::write_attribute(target, name, boost::any_cast<int32_t>(value));
}
}
+8 -17
View File
@@ -3,9 +3,11 @@
#include <string>
#include <list>
#include <boost/any.hpp>
#include <H5Cpp.h>
typedef boost::any h5_value;
enum DATA_TYPE {
NX_FLOAT,
NX_CHAR,
@@ -46,25 +48,14 @@ struct h5_dataset : public h5_base, public h5_parent, public h5_data_base{
: h5_base(name), h5_parent(items), h5_data_base(data_type, REFERENCE) {};
};
struct h5_attribute : public h5_base, public h5_data_base {
h5_attribute(std::string name, DATA_TYPE data_type): h5_base(name), h5_data_base(data_type, IMMEDIATE){};
h5_attribute(std::string name, std::string value_alias, DATA_TYPE data_types): h5_base(name), h5_data_base(data_types, REFERENCE){};
};
template<DATA_TYPE T> struct h5_attribute_val: public h5_base, public h5_data_base {};
template<> struct h5_attribute_val<NX_CHAR>: public h5_attribute {
h5_attribute_val(std::string name, std::string value) : h5_attribute(name, NX_CHAR), value(value) {};
std::string value;
};
template<> struct h5_attribute_val<NX_INT>: public h5_attribute {
h5_attribute_val(std::string name, int value) : h5_attribute(name, NX_CHAR), value(value) {};
int value;
struct h5_attr : public h5_base, public h5_data_base {
h5_attr(std::string name, h5_value value, DATA_TYPE data_types, DATA_LOCATION data_location=IMMEDIATE)
: h5_base(name), h5_data_base(data_types, data_location), value(value){};
h5_value value;
};
namespace h5_utils{
void set_attributes(H5::H5Object& target, std::list<h5_attribute>& attributes);
void write_attribute(H5::H5Object& target, h5_attr& attribute);
void write_attribute(H5::H5Object& target, std::string name, std::string value);
void write_attribute(H5::H5Object& target, std::string name, int value);
}