Computer Assisted Medical Intervention Tool Kit  version 5.1
 
Loading...
Searching...
No Matches
Properties.h
Go to the documentation of this file.
1/*****************************************************************************
2 * $CAMITK_LICENCE_BEGIN$
3 *
4 * CamiTK - Computer Assisted Medical Intervention ToolKit
5 * (c) 2001-2023 Univ. Grenoble Alpes, CNRS, Grenoble INP, TIMC, 38000 Grenoble, France
6 *
7 * Visit http://camitk.imag.fr for more information
8 *
9 * This file is part of CamiTK.
10 *
11 * CamiTK is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
14 *
15 * CamiTK is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * version 3 along with CamiTK. If not, see <http://www.gnu.org/licenses/>.
22 *
23 * $CAMITK_LICENCE_END$
24 ****************************************************************************/
25
26#ifndef PROPERTIES_H
27#define PROPERTIES_H
28
29#include "PhysicalModelIO.h"
30#include <string>
31#include <sstream>
32#include <map>
33#include <cstdlib>
34
35// XSD include
36#include <xsd/cxx/tree/containers-wildcard.hxx>
37
38class PhysicalModel;
60
61public:
63 Properties(const std::string n = "");
64
66 Properties(PhysicalModel*, const std::string n = "");
67
69 virtual ~Properties() = default;
70
72 std::string getName() const;
73
75 void setName(std::string);
76
79
82
86 void xmlToFields(xsd::cxx::tree::attribute_set<char> attrs);
87
89 unsigned int numberOfFields() const;
90
92 bool isAField(std::string attName) const;
93
97 std::string getField(unsigned int) const;
98
100 double getDouble(std::string attName);
101
103 int getInt(std::string attName) const;
104
106 bool getBool(std::string attName) const;
107
109 std::string getString(std::string attName) const;
110
112 void get(std::string attName, std::string& attVal) const;
113
115 void set(std::string attName, double val);
116
118 void set(std::string attName, int val);
119
121 void set(std::string attName, bool val);
122
124 void set(std::string attName, std::string val);
126
127protected :
129 std::map<std::string, std::string> fields;
130
131private:
133 std::string name;
134
136 PhysicalModel* myPM;
137
138};
139
140inline bool Properties::isAField(std::string attName) const {
141 auto it = fields.find(attName);
142 return (it != fields.end());
143}
144
145inline double Properties::getDouble(std::string attName) {
146 auto it = fields.find(attName);
147
148 if (it != fields.end()) {
149 return atof(it->second.c_str());
150 }
151 else {
152 return 0.0;
153 }
154}
155
156inline int Properties::getInt(std::string attName) const {
157 auto it = fields.find(attName);
158
159 if (it != fields.end()) {
160 return std::atoi(it->second.c_str());
161 }
162 else {
163 return 0;
164 }
165}
166
167inline bool Properties::getBool(std::string attName) const {
168 auto it = fields.find(attName);
169
170 if (it == fields.end() || it->second == "false" || it->second == "0") {
171 return false;
172 }
173 else {
174 return true;
175 }
176}
177
178inline std::string Properties::getString(std::string attName) const {
179 auto it = fields.find(attName);
180
181 if (it != fields.end()) {
182 return it->second;
183 }
184 else {
185 return "";
186 }
187}
188
189inline void Properties::get(std::string attName, std::string& attVal) const {
190 auto it = fields.find(attName);
191
192 if (it != fields.end()) {
193 attVal = it->second;
194 }
195 else {
196 attVal = "";
197 }
198}
199
200inline void Properties::set(std::string attName, double val) {
201 std::ostringstream oss;
202 oss << val;
203 auto it = fields.find(attName);
204
205 if (it != fields.end()) {
206 it->second = oss.str();
207 }
208 else {
209 fields.insert(std::pair<std::string, std::string>(attName, oss.str()));
210 }
211}
212
213inline void Properties::set(std::string attName, int val) {
214 std::ostringstream oss;
215 oss << val;
216 auto it = fields.find(attName);
217
218 if (it != fields.end()) {
219 it->second = oss.str() ;
220 }
221 else {
222 fields.insert(std::pair<std::string, std::string>(attName, oss.str()));
223 }
224}
225
226inline void Properties::set(std::string attName, bool val) {
227 std::ostringstream oss;
228 oss << val;
229 auto it = fields.find(attName);
230
231 if (it != fields.end()) {
232 it->second = oss.str() ;
233 }
234 else {
235 fields.insert(std::pair<std::string, std::string>(attName, oss.str()));
236 }
237}
238
239inline void Properties::set(std::string attName, std::string val) {
240 auto it = fields.find(attName);
241
242 if (it != fields.end()) {
243 it->second = val ;
244 }
245 else {
246 fields.insert(std::pair<std::string, std::string>(attName, val));
247 }
248}
249
250inline std::string Properties::getName() const {
251 return name;
252}
253
254inline void Properties::setName(std::string n) {
255 name = std::string(n);
256}
257
259 myPM = pm;
260}
261
263 return myPM;
264}
265
266#endif //PROPERTIES_H
This is the main class of this project.
Definition PhysicalModel.h:86
Describes the properties common to all structures and components.
Definition Properties.h:59
double getDouble(std::string attName)
field accessor: get the field attName as a double value, if field does not exist, 0....
Definition Properties.h:145
std::string getName() const
get the name (be careful, this method DOES NOT return a copy, so you got the direct ptr to the name!...
Definition Properties.h:250
bool isAField(std::string attName) const
check if the field exist in the XML document, return false if it does not
Definition Properties.h:140
PhysicalModel * getPhysicalModel() const
get the physical model
Definition Properties.h:262
std::string getString(std::string attName) const
field accessor: get the field attName as a string value, if field does not exist, empty string is ret...
Definition Properties.h:178
void set(std::string attName, double val)
field modificator: set field attName using a double value
Definition Properties.h:200
void setName(std::string)
set the name (use the string = operator)
Definition Properties.h:254
void xmlToFields(xsd::cxx::tree::attribute_set< char > attrs)
convert the xml node parameters to data fields
Definition Properties.cpp:41
unsigned int numberOfFields() const
get the number of extra fields found in the PML
Definition Properties.cpp:63
virtual ~Properties()=default
The default destructor.
int getInt(std::string attName) const
field accessor: get the field attName as an int value, if field does not exist, 0 is return
Definition Properties.h:156
bool getBool(std::string attName) const
field accessor: get the field attName as a bool value, if field does not exist, false is return
Definition Properties.h:167
std::map< std::string, std::string > fields
map containing all the different fields (name, value stored as string )
Definition Properties.h:129
std::string getField(unsigned int) const
get the name of field of given index
Definition Properties.cpp:68
void setPhysicalModel(PhysicalModel *)
set the physical model
Definition Properties.h:258
void get(std::string attName, std::string &attVal) const
field accessor: get the field attName as a string value in attVal, if field does not exist,...
Definition Properties.h:189