Computer Assited Medical Intervention Tool Kit  version 4.0
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-2016 Univ. Grenoble Alpes, CNRS, TIMC-IMAG UMR 5525 (GMCAO)
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 
38 class PhysicalModel;
59 class Properties {
60 
61 public:
63  Properties(const std::string n="");
64 
66  Properties(PhysicalModel *, const std::string n="");
67 
69  virtual ~Properties();
70 
72  std::string getName() const;
73 
75  void setName(std::string);
76 
79 
82 
85  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 
127 protected :
129  std::map<std::string, std::string> fields;
130 
131 private:
133  std::string name;
134 
137 
138 };
139 
140 inline bool Properties::isAField(std::string attName) const {
141  std::map<std::string, std::string>::const_iterator it = fields.find(attName);
142  return (it != fields.end());
143 }
144 
145 inline double Properties::getDouble(std::string attName) {
146  std::map<std::string, std::string>::iterator it = fields.find(attName);
147 
148  if (it != fields.end())
149  return atof( it->second.c_str());
150  else
151  return 0.0;
152 }
153 
154 inline int Properties::getInt(std::string attName) const {
155  std::map<std::string, std::string>::const_iterator it = fields.find(attName);
156 
157  if (it != fields.end())
158  return std::atoi( it->second.c_str());
159  else
160  return 0;
161 }
162 
163 inline bool Properties::getBool(std::string attName) const {
164  std::map<std::string, std::string>::const_iterator it = fields.find(attName);
165 
166  if(it == fields.end() || it->second =="false" || it->second =="0")
167  return false;
168  else
169  return true;
170 }
171 
172 inline std::string Properties::getString(std::string attName) const {
173  std::map<std::string, std::string>::const_iterator it = fields.find(attName);
174 
175  if (it != fields.end())
176  return it->second;
177  else
178  return "";
179 }
180 
181 inline void Properties::get(std::string attName, std::string &attVal) const {
182  std::map<std::string, std::string>::const_iterator it = fields.find(attName);
183 
184  if (it != fields.end())
185  attVal = it->second;
186  else
187  attVal = "";
188 }
189 
190 inline void Properties::set(std::string attName, double val) {
191  std::ostringstream oss;
192  oss << val;
193  std::map<std::string, std::string>::iterator it = fields.find(attName);
194 
195  if (it != fields.end())
196  it->second = oss.str();
197  else
198  fields.insert(std::pair<std::string, std::string>(attName, oss.str()));
199 }
200 
201 inline void Properties::set(std::string attName, int val) {
202  std::ostringstream oss;
203  oss << val;
204  std::map<std::string, std::string>::iterator it = fields.find(attName);
205 
206  if (it != fields.end())
207  it->second = oss.str() ;
208  else
209  fields.insert(std::pair<std::string, std::string>(attName, oss.str()));
210 }
211 
212 inline void Properties::set(std::string attName, bool val) {
213  std::ostringstream oss;
214  oss << val;
215  std::map<std::string, std::string>::iterator it = fields.find(attName);
216 
217  if (it != fields.end())
218  it->second = oss.str() ;
219  else
220  fields.insert(std::pair<std::string, std::string>(attName, oss.str()));
221 }
222 
223 inline void Properties::set(std::string attName, std::string val) {
224  std::map<std::string, std::string>::iterator it = fields.find(attName);
225 
226  if (it != fields.end())
227  it->second = val ;
228  else
229  fields.insert(std::pair<std::string, std::string>(attName, val));
230 }
231 
232 inline std::string Properties::getName() const {
233  return name;
234 }
235 
236 inline void Properties::setName(std::string n) {
237  name = std::string(n);
238 }
239 
241  myPM = pm;
242 }
243 
245  return myPM;
246 }
247 
248 #endif //PROPERTIES_H
void setPhysicalModel(PhysicalModel *)
set the physical model
Definition: Properties.h:240
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:172
Properties(const std::string n="")
A nice simple constructor, with a given name.
Definition: Properties.cpp:34
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:154
void setName(std::string)
set the name (use the string = operator)
Definition: Properties.h:236
void set(std::string attName, double val)
field modificator: set field attName using a double value
Definition: Properties.h:190
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
Describes the properties common to all structures and components.
Definition: Properties.h:59
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:181
std::map< std::string, std::string > fields
map containing all the different fields (name, value stored as string )
Definition: Properties.h:129
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:163
void xmlToFields(xsd::cxx::tree::attribute_set< char > attrs)
convert the xml node parameters to data fields
Definition: Properties.cpp:49
std::string getField(unsigned int) const
get the name of field of given index
Definition: Properties.cpp:77
PhysicalModel * myPM
pointer to the physical model the object is in
Definition: Properties.h:136
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
This is the main class of this project.
Definition: PhysicalModel.h:86
std::string name
name of the physical model object
Definition: Properties.h:133
virtual ~Properties()
The default destructor.
Definition: Properties.cpp:45
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:232
unsigned int numberOfFields() const
get the number of extra fields found in the PML
Definition: Properties.cpp:72
PhysicalModel * getPhysicalModel() const
get the physical model
Definition: Properties.h:244