ISMRMRD
ISMRM Raw Data Format
meta.h
Go to the documentation of this file.
1 
7 #ifndef ISMRMRDMETA_H
8 #define ISMRMRDMETA_H
9 
10 #include "ismrmrd/export.h"
11 
12 #include <string>
13 #include <sstream>
14 #include <vector>
15 #include <map>
16 #include <stdexcept>
17 #include <stdio.h>
18 
19 namespace ISMRMRD {
20  /*
21  The serialized version of the structues would look like this
22 
23  <?xml version="1.0"?>
24  <ismrmrdMeta>
25  <!-- String value type -->
26  <meta>
27  <name>parameter1</name>
28  <value>value_string</value>
29  </meta>
30 
31  <!-- Integer value type -->
32  <meta>
33  <name>parameter1</name>
34  <value>677797</value>
35  </meta>
36 
37  <!-- Arrays can have mixed value types -->
38  <meta>
39  <name>parameter1</name>
40  <value>1.456</value>
41  <value>66797</value>
42  <value>hsjdhaks</value>
43  </meta>
44  </ismrmrdMeta>
45  */
46 
47 
58  class MetaValue {
59 
60  public:
63  set(0L);
64  }
65 
67  MetaValue(const char *s) {
68  set(s);
69  }
70 
72  MetaValue(long l) {
73  set(l);
74  }
75 
77  MetaValue(double d) {
78  set(d);
79  }
80 
81 
83  MetaValue &operator=(const char *s) {
84  set(s);
85  return *this;
86  }
87 
89  MetaValue &operator=(long l) {
90  set(l);
91  return *this;
92  }
93 
95  MetaValue &operator=(double d) {
96  set(d);
97  return *this;
98  }
99 
101  long as_long() const {
102  return l_;
103  }
104 
106  double as_double() const {
107  return d_;
108  }
109 
111  const char *as_str() const {
112  return s_.c_str();
113  }
114 
115 
116  protected:
117  long l_;
118  double d_;
119  std::string s_;
120 
121  void set(const char *s) {
122  s_ = std::string(s);
123  sscanf(s_.c_str(), "%ld", &l_);
124  sscanf(s_.c_str(), "%lf", &d_);
125  }
126 
127  void set(long l) {
128  l_ = l;
129  d_ = static_cast<double>(l_);
130  std::stringstream strstream;
131  strstream << l_;
132  strstream >> s_;
133  }
134 
135  void set(double d) {
136  d_ = d;
137  l_ = static_cast<long>(d_);
138  std::stringstream strstream;
139  strstream << d_;
140  strstream >> s_;
141  }
142  };
143 
144  class MetaContainer;
145 
146  EXPORTISMRMRD void deserialize(const char *xml, MetaContainer &h);
147 
148  EXPORTISMRMRD void serialize(const MetaContainer &h, std::ostream &o);
149 
152  protected:
153  typedef std::map<std::string, std::vector<MetaValue> > map_t;
154 
155  public:
156  MetaContainer() {
157 
158  }
159 
167  template<class T>
168  void set(const char *name, T value) {
169  MetaValue v(value);
170  map_[std::string(name)] = std::vector<MetaValue>(1, value);
171  }
172 
173 
174  template<class T>
175  void append(const char *name, T value) {
176  map_t::iterator it = map_.find(std::string(name));
177  if (it == map_.end()) {
178  set(name, value);
179  } else {
180  MetaValue v(value);
181  it->second.push_back(v);
182  }
183  }
184 
185  void remove(const char *name) {
186  map_t::iterator it = map_.find(std::string(name));
187  if (it != map_.end()) {
188  map_.erase(it);
189  }
190  }
191 
193  size_t length(const char *name) const {
194  map_t::const_iterator it = map_.find(std::string(name));
195  if (it != map_.end()) {
196  return it->second.size();
197  }
198  return 0;
199  }
200 
202  long as_long(const char *name, size_t index = 0) const {
203  return value(name, index).as_long();
204  }
205 
207  double as_double(const char *name, size_t index = 0) const {
208  return value(name, index).as_double();
209  }
210 
212  const char *as_str(const char *name, size_t index = 0) const {
213  return value(name, index).as_str();
214  }
215 
216  const MetaValue &value(const char *name, size_t index = 0) const {
217  map_t::const_iterator it = map_.find(std::string(name));
218  if (it == map_.end()) {
219  throw std::runtime_error("Attempting to access unknown parameter");
220  }
221  if (index >= it->second.size()) {
222  throw std::runtime_error("Attempting to access indexed value out of bounds");
223  }
224  return it->second[index];
225  }
226 
227  bool empty() const {
228  return map_.empty();
229  }
230 
231  map_t::iterator begin() {
232  return map_.begin();
233  }
234 
235  map_t::iterator end() {
236  return map_.end();
237  }
238 
239  map_t::const_iterator begin() const {
240  return map_.begin();
241  }
242 
243  map_t::const_iterator end() const {
244  return map_.end();
245  }
246 
247 
248  protected:
249  map_t map_;
250  };
251 
252  //Template function instantiations
253  /*
254  template void MetaContainer::set<const char*>(const char* name, const char* value);
255  template void MetaContainer::set<long>(const char* name, long value);
256  template void MetaContainer::set<double>(const char* name, double);
257  template void MetaContainer::append<const char*>(const char* name, const char* value);
258  template void MetaContainer::append<long>(const char* name, long value);
259  template void MetaContainer::append<double>(const char* name, double);
260  */
261 }
262 
265 #endif //ISMRMRDMETA_H
ISMRMRD::MetaContainer::length
size_t length(const char *name) const
Return number of values of a particular parameter.
Definition: meta.h:193
ISMRMRD::MetaValue::operator=
MetaValue & operator=(const char *s)
Assignment operator for string.
Definition: meta.h:83
ISMRMRD::MetaValue::as_long
long as_long() const
Get the ingeter representation of the value.
Definition: meta.h:101
ISMRMRD::MetaValue::operator=
MetaValue & operator=(long l)
Assignment operator for long.
Definition: meta.h:89
ISMRMRD::MetaValue::MetaValue
MetaValue()
Definition: meta.h:62
ISMRMRD::MetaValue::MetaValue
MetaValue(const char *s)
Null terminated string constructor.
Definition: meta.h:67
ISMRMRD::MetaContainer
Meta Container.
Definition: meta.h:151
ISMRMRD::MetaValue::MetaValue
MetaValue(long l)
Long constructor.
Definition: meta.h:72
ISMRMRD::MetaValue::operator=
MetaValue & operator=(double d)
Assignment operator for double.
Definition: meta.h:95
ISMRMRD::MetaValue
Definition: meta.h:58
ISMRMRD::MetaValue::as_str
const char * as_str() const
get the C string representation of the value
Definition: meta.h:111
ISMRMRD::MetaValue::MetaValue
MetaValue(double d)
Long constructor.
Definition: meta.h:77
ISMRMRD::MetaValue::as_double
double as_double() const
Get the floating point representation of the value.
Definition: meta.h:106
ISMRMRD
Definition: dataset.h:17