ISMRMRD
ISMRM Raw Data Format
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Modules
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  /*
22  The serialized version of the structues would look like this
23 
24  <?xml version="1.0"?>
25  <ismrmrdMeta>
26  <!-- String value type -->
27  <meta>
28  <name>parameter1</name>
29  <value>value_string</value>
30  </meta>
31 
32  <!-- Integer value type -->
33  <meta>
34  <name>parameter1</name>
35  <value>677797</value>
36  </meta>
37 
38  <!-- Arrays can have mixed value types -->
39  <meta>
40  <name>parameter1</name>
41  <value>1.456</value>
42  <value>66797</value>
43  <value>hsjdhaks</value>
44  </meta>
45  </ismrmrdMeta>
46  */
47 
48 
59  class MetaValue
60  {
61 
62  public:
65  {
66  set(0L);
67  }
68 
70  MetaValue(const char* s)
71  {
72  set(s);
73  }
74 
76  MetaValue(long l)
77  {
78  set(l);
79  }
80 
82  MetaValue(double d)
83  {
84  set(d);
85  }
86 
87 
89  MetaValue& operator=(const char * s)
90  {
91  set(s);
92  return *this;
93  }
94 
96  MetaValue& operator=(long l)
97  {
98  set(l);
99  return *this;
100  }
101 
103  MetaValue& operator=(double d)
104  {
105  set(d);
106  return *this;
107  }
108 
110  long as_long() const
111  {
112  return l_;
113  }
114 
116  double as_double() const
117  {
118  return d_;
119  }
120 
122  const char* as_str() const
123  {
124  return s_.c_str();
125  }
126 
127 
128  protected:
129  long l_;
130  double d_;
131  std::string s_;
132 
133  void set(const char* s)
134  {
135  s_ = std::string(s);
136  sscanf(s_.c_str(),"%ld",&l_);
137  sscanf(s_.c_str(),"%lf",&d_);
138  }
139 
140  void set(long l)
141  {
142  l_ = l;
143  d_ = static_cast<double>(l_);
144  std::stringstream strstream;
145  strstream << l_;
146  strstream >> s_;
147  }
148 
149  void set(double d)
150  {
151  d_ = d;
152  l_ = static_cast<long>(d_);
153  std::stringstream strstream;
154  strstream << d_;
155  strstream >> s_;
156  }
157  };
158 
159  class MetaContainer;
160 
161  EXPORTISMRMRD void deserialize(const char* xml, MetaContainer& h);
162  EXPORTISMRMRD void serialize(MetaContainer& h, std::ostream& o);
163 
166  {
167  typedef std::map< std::string, std::vector<MetaValue> > map_t;
168 
169  friend void serialize(MetaContainer& h, std::ostream& o);
170 
171  public:
172  MetaContainer()
173  {
174 
175  }
176 
184  template <class T> void set(const char* name, T value)
185  {
186  MetaValue v(value);
187  map_[std::string(name)] = std::vector<MetaValue>(1,value);
188  }
189 
190 
191  template <class T> void append(const char* name, T value)
192  {
193  map_t::iterator it = map_.find(std::string(name));
194  if (it == map_.end()) {
195  set(name, value);
196  } else {
197  MetaValue v(value);
198  it->second.push_back(v);
199  }
200  }
201 
203  size_t length(const char* name) const
204  {
205  map_t::const_iterator it = map_.find(std::string(name));
206  if (it != map_.end()) {
207  return it->second.size();
208  }
209  return 0;
210  }
211 
213  long as_long(const char* name, size_t index = 0) const
214  {
215  return value(name,index).as_long();
216  }
217 
219  double as_double(const char* name, size_t index = 0) const
220  {
221  return value(name,index).as_double();
222  }
223 
225  const char* as_str(const char* name, size_t index = 0) const
226  {
227  return value(name,index).as_str();
228  }
229 
230  const MetaValue& value(const char* name, size_t index = 0) const
231  {
232  map_t::const_iterator it = map_.find(std::string(name));
233  if (it == map_.end()) {
234  throw std::runtime_error("Attempting to access unkown parameter");
235  }
236  if (index >= it->second.size()) {
237  throw std::runtime_error("Attempting to access indexed value out of bounds");
238  }
239  return it->second[index];
240  }
241 
242  bool empty()
243  {
244  return map_.empty();
245  }
246 
247  protected:
248  map_t map_;
249  };
250 
251  //Template function instantiations
252  /*
253  template void MetaContainer::set<const char*>(const char* name, const char* value);
254  template void MetaContainer::set<long>(const char* name, long value);
255  template void MetaContainer::set<double>(const char* name, double);
256  template void MetaContainer::append<const char*>(const char* name, const char* value);
257  template void MetaContainer::append<long>(const char* name, long value);
258  template void MetaContainer::append<double>(const char* name, double);
259  */
260 }
261 
264 #endif //ISMRMRDMETA_H
MetaValue(double d)
Long constructor.
Definition: meta.h:82
MetaValue & operator=(const char *s)
Assignment operator for string.
Definition: meta.h:89
Definition: meta.h:59
const char * as_str() const
get the C string representation of the value
Definition: meta.h:122
MetaValue & operator=(long l)
Assignment operator for long.
Definition: meta.h:96
MetaValue(const char *s)
Null terminated string constructor.
Definition: meta.h:70
MetaValue & operator=(double d)
Assignment operator for double.
Definition: meta.h:103
MetaValue()
Definition: meta.h:64
MetaValue(long l)
Long constructor.
Definition: meta.h:76
Definition: dataset.h:15
size_t length(const char *name) const
Return number of values of a particular parameter.
Definition: meta.h:203
long as_long() const
Get the ingeter representation of the value.
Definition: meta.h:110
Meta Container.
Definition: meta.h:165
double as_double() const
Get the floating point representation of the value.
Definition: meta.h:116