ISMRMRD: meta.h Source File

ISMRMRD ISMRM Raw Data Format

Loading...
Searching...
No Matches
meta.h
Go to the documentation of this file.
1
6
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
19namespace 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:
63 MetaValue() {
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.imbue(std::locale::classic());
133 strstream << l_;
134 strstream >> s_;
135 }
136
137 void set(double d) {
138 d_ = d;
139 l_ = static_cast<long>(d_);
140 std::stringstream strstream;
141 strstream.imbue(std::locale::classic());
142 strstream << d_;
143 strstream >> s_;
144 }
145 };
146
147 class MetaContainer;
148
149 EXPORTISMRMRD void deserialize(const char *xml, MetaContainer &h);
150
151 EXPORTISMRMRD void serialize(const MetaContainer &h, std::ostream &o);
152
154 class MetaContainer {
155 protected:
156 typedef std::map<std::string, std::vector<MetaValue> > map_t;
157
158 public:
159 MetaContainer() {
160
161 }
162
170 template<class T>
171 void set(const char *name, T value) {
172 MetaValue v(value);
173 map_[std::string(name)] = std::vector<MetaValue>(1, value);
174 }
175
176
177 template<class T>
178 void append(const char *name, T value) {
179 map_t::iterator it = map_.find(std::string(name));
180 if (it == map_.end()) {
181 set(name, value);
182 } else {
183 MetaValue v(value);
184 it->second.push_back(v);
185 }
186 }
187
188 void remove(const char *name) {
189 map_t::iterator it = map_.find(std::string(name));
190 if (it != map_.end()) {
191 map_.erase(it);
192 }
193 }
194
196 size_t length(const char *name) const {
197 map_t::const_iterator it = map_.find(std::string(name));
198 if (it != map_.end()) {
199 return it->second.size();
200 }
201 return 0;
202 }
203
205 long as_long(const char *name, size_t index = 0) const {
206 return value(name, index).as_long();
207 }
208
210 double as_double(const char *name, size_t index = 0) const {
211 return value(name, index).as_double();
212 }
213
215 const char *as_str(const char *name, size_t index = 0) const {
216 return value(name, index).as_str();
217 }
218
219 const MetaValue &value(const char *name, size_t index = 0) const {
220 map_t::const_iterator it = map_.find(std::string(name));
221 if (it == map_.end()) {
222 throw std::runtime_error("Attempting to access unknown parameter");
223 }
224 if (index >= it->second.size()) {
225 throw std::runtime_error("Attempting to access indexed value out of bounds");
226 }
227 return it->second[index];
228 }
229
230 bool empty() const {
231 return map_.empty();
232 }
233
234 map_t::iterator begin() {
235 return map_.begin();
236 }
237
238 map_t::iterator end() {
239 return map_.end();
240 }
241
242 map_t::const_iterator begin() const {
243 return map_.begin();
244 }
245
246 map_t::const_iterator end() const {
247 return map_.end();
248 }
249
250
251 protected:
252 map_t map_;
253 };
254
256
257 //Template function instantiations
258 /*
259 template void MetaContainer::set<const char*>(const char* name, const char* value);
260 template void MetaContainer::set<long>(const char* name, long value);
261 template void MetaContainer::set<double>(const char* name, double);
262 template void MetaContainer::append<const char*>(const char* name, const char* value);
263 template void MetaContainer::append<long>(const char* name, long value);
264 template void MetaContainer::append<double>(const char* name, double);
265 */
266}
267
268
269#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