From c83dd679b7a0d8245a99c6fb57717834d7425b71 Mon Sep 17 00:00:00 2001 From: Andrej Babic Date: Fri, 12 Jan 2018 18:44:14 +0100 Subject: [PATCH] Simplify file format definition --- src/h5_file_format.cpp | 43 +++++++++++++++++++++++++++++++----------- src/h5_file_format.hpp | 25 ++++++++---------------- 2 files changed, 40 insertions(+), 28 deletions(-) diff --git a/src/h5_file_format.cpp b/src/h5_file_format.cpp index edb992a..8210b31 100644 --- a/src/h5_file_format.cpp +++ b/src/h5_file_format.cpp @@ -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& 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*>(&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*>(&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(value))); + return; + } catch (const boost::bad_any_cast& exception) {} + + // Atempt to convert to string. + try { + h5_utils::write_attribute(target, name, boost::any_cast(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(value)); } } \ No newline at end of file diff --git a/src/h5_file_format.hpp b/src/h5_file_format.hpp index 2b728c0..c407b52 100644 --- a/src/h5_file_format.hpp +++ b/src/h5_file_format.hpp @@ -3,9 +3,11 @@ #include #include - +#include #include +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 struct h5_attribute_val: public h5_base, public h5_data_base {}; - -template<> struct h5_attribute_val: 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: 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& 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); }