Loading...
Searching...
No Matches
meta.h
Go to the documentation of this file.
#ifndef ISMRMRDMETA_H
#define ISMRMRDMETA_H
#include "ismrmrd/export.h"
#include <string>
#include <sstream>
#include <vector>
#include <map>
#include <stdexcept>
#include <stdio.h>
namespace ISMRMRD {
/*
The serialized version of the structures would look like this
<?xml version="1.0"?>
<ismrmrdMeta>
<!-- String value type -->
<meta>
<name>parameter1</name>
<value>value_string</value>
</meta>
<!-- Integer value type -->
<meta>
<name>parameter1</name>
<value>677797</value>
</meta>
<!-- Arrays can have mixed value types -->
<meta>
<name>parameter1</name>
<value>1.456</value>
<value>66797</value>
<value>hsjdhaks</value>
</meta>
</ismrmrdMeta>
*/
class MetaValue {
public:
MetaValue() {
set(0L);
}
MetaValue(const char *s) {
set(s);
}
MetaValue(long l) {
set(l);
}
MetaValue(double d) {
set(d);
}
MetaValue &operator=(const char *s) {
set(s);
return *this;
}
MetaValue &operator=(long l) {
set(l);
return *this;
}
MetaValue &operator=(double d) {
set(d);
return *this;
}
long as_long() const {
return l_;
}
double as_double() const {
return d_;
}
const char *as_str() const {
return s_.c_str();
}
protected:
long l_;
double d_;
std::string s_;
void set(const char *s) {
s_ = std::string(s);
sscanf(s_.c_str(), "%ld", &l_);
sscanf(s_.c_str(), "%lf", &d_);
}
void set(long l) {
l_ = l;
d_ = static_cast<double>(l_);
std::stringstream strstream;
strstream.imbue(std::locale::classic());
strstream << l_;
strstream >> s_;
}
void set(double d) {
d_ = d;
l_ = static_cast<long>(d_);
std::stringstream strstream;
strstream.imbue(std::locale::classic());
strstream << d_;
strstream >> s_;
}
};
class MetaContainer;
EXPORTISMRMRD void deserialize(const char *xml, MetaContainer &h);
EXPORTISMRMRD void serialize(const MetaContainer &h, std::ostream &o);
class MetaContainer {
protected:
typedef std::map<std::string, std::vector<MetaValue> > map_t;
public:
MetaContainer() {
}
template<class T>
void set(const char *name, T value) {
MetaValue v(value);
map_[std::string(name)] = std::vector<MetaValue>(1, value);
}
template<class T>
void append(const char *name, T value) {
map_t::iterator it = map_.find(std::string(name));
if (it == map_.end()) {
set(name, value);
} else {
MetaValue v(value);
it->second.push_back(v);
}
}
void remove(const char *name) {
map_t::iterator it = map_.find(std::string(name));
if (it != map_.end()) {
map_.erase(it);
}
}
size_t length(const char *name) const {
map_t::const_iterator it = map_.find(std::string(name));
if (it != map_.end()) {
return it->second.size();
}
return 0;
}
long as_long(const char *name, size_t index = 0) const {
return value(name, index).as_long();
}
double as_double(const char *name, size_t index = 0) const {
return value(name, index).as_double();
}
const char *as_str(const char *name, size_t index = 0) const {
return value(name, index).as_str();
}
const MetaValue &value(const char *name, size_t index = 0) const {
map_t::const_iterator it = map_.find(std::string(name));
if (it == map_.end()) {
throw std::runtime_error("Attempting to access unknown parameter");
}
if (index >= it->second.size()) {
throw std::runtime_error("Attempting to access indexed value out of bounds");
}
return it->second[index];
}
bool empty() const {
return map_.empty();
}
map_t::iterator begin() {
return map_.begin();
}
map_t::iterator end() {
return map_.end();
}
map_t::const_iterator begin() const {
return map_.begin();
}
map_t::const_iterator end() const {
return map_.end();
}
protected:
map_t map_;
};
//Template function instantiations
/*
template void MetaContainer::set<const char*>(const char* name, const char* value);
template void MetaContainer::set<long>(const char* name, long value);
template void MetaContainer::set<double>(const char* name, double);
template void MetaContainer::append<const char*>(const char* name, const char* value);
template void MetaContainer::append<long>(const char* name, long value);
template void MetaContainer::append<double>(const char* name, double);
*/
}
#endif //ISMRMRDMETA_H
ISMRMRD::MetaContainer
Meta Container.
Definition meta.h:154
ISMRMRD::MetaContainer::length
size_t length(const char *name) const
Return number of values of a particular parameter.
Definition meta.h:196
ISMRMRD::MetaValue
Definition meta.h:59
ISMRMRD::MetaValue::MetaValue
MetaValue()
Definition meta.h:63
ISMRMRD::MetaValue::MetaValue
MetaValue(const char *s)
Null terminated string constructor.
Definition meta.h:68
ISMRMRD::MetaValue::as_double
double as_double() const
Get the floating point representation of the value.
Definition meta.h:107
ISMRMRD::MetaValue::MetaValue
MetaValue(long l)
Long constructor.
Definition meta.h:73
ISMRMRD::MetaValue::MetaValue
MetaValue(double d)
Long constructor.
Definition meta.h:78
ISMRMRD::MetaValue::as_str
const char * as_str() const
get the C string representation of the value
Definition meta.h:112
ISMRMRD::MetaValue::operator=
MetaValue & operator=(const char *s)
Assignment operator for string.
Definition meta.h:84
ISMRMRD::MetaValue::operator=
MetaValue & operator=(long l)
Assignment operator for long.
Definition meta.h:90
ISMRMRD::MetaValue::operator=
MetaValue & operator=(double d)
Assignment operator for double.
Definition meta.h:96
ISMRMRD::MetaValue::as_long
long as_long() const
Get the ingeter representation of the value.
Definition meta.h:102