Create format writer

This commit is contained in:
2018-01-12 16:25:01 +01:00
parent f85dcabf23
commit c82eb0e5e1
2 changed files with 76 additions and 12 deletions
+40
View File
@@ -0,0 +1,40 @@
#include <string>
#include <sstream>
#include <stdexcept>
#include <iostream>
#include "h5_file_format.hpp"
using namespace std;
void h5_utils::write_attribute(H5::H5Object& target, string name, string value){
H5::StrType str_type(0, H5T_VARIABLE);
H5::DataSpace att_space(H5S_SCALAR);
H5::Attribute h5_attribute = target.createAttribute(name, str_type, att_space);
h5_attribute.write(str_type, value);
}
void h5_utils::write_attribute(H5::H5Object& target, string name, int32_t value){
H5::IntType int_type(H5::PredType::NATIVE_INT32);
H5::DataSpace att_space(H5S_SCALAR);
H5::Attribute h5_attribute = target.createAttribute(name, int_type, att_space);
h5_attribute.write(int_type, &value);
}
void h5_utils::set_attributes(H5::H5Object& target, std::list<h5_attribute>& attributes) {
for (auto& attribute : attributes) {
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);
} 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);
}
}
}
+36 -12
View File
@@ -4,6 +4,8 @@
#include <string>
#include <list>
#include <H5Cpp.h>
enum DATA_TYPE {
NX_FLOAT,
NX_CHAR,
@@ -13,11 +15,21 @@ enum DATA_TYPE {
NXnote
};
enum DATA_LOCATION {
IMMEDIATE,
REFERENCE
};
struct h5_base {
h5_base(std::string name) : name(name), value_alias(name){};
h5_base(std::string name, std::string value_alias) : name(name), value_alias(value_alias){};
h5_base(std::string name) : name(name){};
std::string name;
std::string value_alias;
};
struct h5_data_base{
h5_data_base(DATA_TYPE data_type, DATA_LOCATION data_location) : data_type(data_type), data_location(data_location) {};
DATA_TYPE data_type;
DATA_LOCATION data_location;
};
struct h5_parent{
@@ -29,20 +41,32 @@ struct h5_group : public h5_base, public h5_parent {
h5_group(std::string name, std::list<h5_base> items) : h5_base(name), h5_parent(items) {};
};
struct h5_dataset : public h5_base, public h5_parent{
h5_dataset(std::string name, DATA_TYPE type, std::list<h5_base> items={}) : h5_base(name), h5_parent(items), type(type) {};
h5_dataset(std::string name, std::string value_alias, DATA_TYPE type, std::list<h5_base> items={}) : h5_base(name, value_alias), h5_parent(items), type(type) {};
DATA_TYPE type;
struct h5_dataset : public h5_base, public h5_parent, public h5_data_base{
h5_dataset(std::string name, std::string value_alias, DATA_TYPE data_type, std::list<h5_base> items={})
: h5_base(name), h5_parent(items), h5_data_base(data_type, REFERENCE) {};
};
struct h5_attribute : public h5_base {
h5_attribute(std::string name): h5_base(name){};
h5_attribute(std::string name, std::string value_alias): h5_base(name, value_alias){};
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){};
};
struct h5_attribute_val : public h5_attribute {
h5_attribute_val(std::string name, std::string value): h5_attribute(name){};
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;
};
namespace h5_utils{
void set_attributes(H5::H5Object& target, std::list<h5_attribute>& attributes);
void write_attribute(H5::H5Object& target, std::string name, std::string value);
void write_attribute(H5::H5Object& target, std::string name, int value);
}
#endif