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 structures 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 
48 
59  class MetaValue {
60 
61  public:
64  set(0L);
65  }
66 
68  MetaValue(const char *s) {
69  set(s);
70  }
71 
73  MetaValue(long l) {
74  set(l);
75  }
76 
78  MetaValue(double d) {
79  set(d);
80  }
81 
82 
84  MetaValue &operator=(const char *s) {
85  set(s);
86  return *this;
87  }
88 
90  MetaValue &operator=(long l) {
91  set(l);
92  return *this;
93  }
94 
96  MetaValue &operator=(double d) {
97  set(d);
98  return *this;
99  }
100 
102  long as_long() const {
103  return l_;
104  }
105 
107  double as_double() const {
108  return d_;
109  }
110 
112  const char *as_str() const {
113  return s_.c_str();
114  }
115 
116 
117  protected:
118  long l_;
119  double d_;
120  std::string s_;
121 
122  void set(const char *s) {
123  s_ = std::string(s);
124  sscanf(s_.c_str(), "%ld", &l_);
125  sscanf(s_.c_str(), "%lf", &d_);
126  }
127 
128  void set(long l) {
129  l_ = l;
130  d_ = static_cast<double>(l_);
131  std::stringstream strstream;
132  strstream << l_;
133  strstream >> s_;
134  }
135 
136  void set(double d) {
137  d_ = d;
138  l_ = static_cast<long>(d_);
139  std::stringstream strstream;
140  strstream << d_;
141  strstream >> s_;
142  }
143  };
144 
145  class MetaContainer;
146 
147  EXPORTISMRMRD void deserialize(const char *xml, MetaContainer &h);
148 
149  EXPORTISMRMRD void serialize(const MetaContainer &h, std::ostream &o);
150 
153  protected:
154  typedef std::map<std::string, std::vector<MetaValue> > map_t;
155 
156  public:
157  MetaContainer() {
158 
159  }
160 
168  template<class T>
169  void set(const char *name, T value) {
170  MetaValue v(value);
171  map_[std::string(name)] = std::vector<MetaValue>(1, value);
172  }
173 
174 
175  template<class T>
176  void append(const char *name, T value) {
177  map_t::iterator it = map_.find(std::string(name));
178  if (it == map_.end()) {
179  set(name, value);
180  } else {
181  MetaValue v(value);
182  it->second.push_back(v);
183  }
184  }
185 
186  void remove(const char *name) {
187  map_t::iterator it = map_.find(std::string(name));
188  if (it != map_.end()) {
189  map_.erase(it);
190  }
191  }
192 
194  size_t length(const char *name) const {
195  map_t::const_iterator it = map_.find(std::string(name));
196  if (it != map_.end()) {
197  return it->second.size();
198  }
199  return 0;
200  }
201 
203  long as_long(const char *name, size_t index = 0) const {
204  return value(name, index).as_long();
205  }
206 
208  double as_double(const char *name, size_t index = 0) const {
209  return value(name, index).as_double();
210  }
211 
213  const char *as_str(const char *name, size_t index = 0) const {
214  return value(name, index).as_str();
215  }
216 
217  const MetaValue &value(const char *name, size_t index = 0) const {
218  map_t::const_iterator it = map_.find(std::string(name));
219  if (it == map_.end()) {
220  throw std::runtime_error("Attempting to access unknown parameter");
221  }
222  if (index >= it->second.size()) {
223  throw std::runtime_error("Attempting to access indexed value out of bounds");
224  }
225  return it->second[index];
226  }
227 
228  bool empty() const {
229  return map_.empty();
230  }
231 
232  map_t::iterator begin() {
233  return map_.begin();
234  }
235 
236  map_t::iterator end() {
237  return map_.end();
238  }
239 
240  map_t::const_iterator begin() const {
241  return map_.begin();
242  }
243 
244  map_t::const_iterator end() const {
245  return map_.end();
246  }
247 
248 
249  protected:
250  map_t map_;
251  };
252 
255  //Template function instantiations
256  /*
257  template void MetaContainer::set<const char*>(const char* name, const char* value);
258  template void MetaContainer::set<long>(const char* name, long value);
259  template void MetaContainer::set<double>(const char* name, double);
260  template void MetaContainer::append<const char*>(const char* name, const char* value);
261  template void MetaContainer::append<long>(const char* name, long value);
262  template void MetaContainer::append<double>(const char* name, double);
263  */
264 }
265 
266 
267 #endif //ISMRMRDMETA_H
Meta Container.
Definition: meta.h:152
size_t length(const char *name) const
Return number of values of a particular parameter.
Definition: meta.h:194
Definition: meta.h:59
MetaValue & operator=(const char *s)
Assignment operator for string.
Definition: meta.h:84
MetaValue()
Definition: meta.h:63
MetaValue(const char *s)
Null terminated string constructor.
Definition: meta.h:68
double as_double() const
Get the floating point representation of the value.
Definition: meta.h:107
MetaValue & operator=(double d)
Assignment operator for double.
Definition: meta.h:96
MetaValue & operator=(long l)
Assignment operator for long.
Definition: meta.h:90
MetaValue(long l)
Long constructor.
Definition: meta.h:73
MetaValue(double d)
Long constructor.
Definition: meta.h:78
const char * as_str() const
get the C string representation of the value
Definition: meta.h:112
long as_long() const
Get the ingeter representation of the value.
Definition: meta.h:102