charon-core  0.3.1
Parameter.hxx
Go to the documentation of this file.
1 /* This file is part of Charon.
2 
3  Charon is free software: you can redistribute it and/or modify
4  it under the terms of the GNU Lesser General Public License as published by
5  the Free Software Foundation, either version 3 of the License, or
6  (at your option) any later version.
7 
8  Charon is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU Lesser General Public License for more details.
12 
13  You should have received a copy of the GNU Lesser General Public License
14  along with Charon. If not, see <http://www.gnu.org/licenses/>.
15 */
34 #ifndef _Parameter_HXX_
35 #define _Parameter_HXX_
36 
37 #include "ParameteredObject.h"
38 #include "TypeDetector.h"
39 #include <sstream>
40 #include <set>
41 
42 // ========================= class Parameter ============================
43 
44 template <typename T>
45 Parameter<T>::Parameter(T defaultValue) :
46  _defaultValue(defaultValue),
47  _value(defaultValue) {
48 }
49 
50 template <typename T>
52 }
53 
54 template <typename T>
55 T& Parameter<T>::operator=(const T& B) {
56  _value = B;
57  return _value;
58 }
59 
60 template <typename T>
61 void Parameter<T>::setDefault(const T& defVal) {
62  _defaultValue = defVal;
63 }
64 
65 template <typename T>
67  _value = B._value;
68  _defaultValue = B._defaultValue;
69  return *this;
70 }
71 
72 template <typename T>
74  return _value;
75 }
76 
77 template <typename T>
78 std::string Parameter<T>::guessType() const {
79  return TypeDetector::type(typeid(T).name());
80 }
81 
82 template <typename T>
83 const T& Parameter<T>::operator()() const {
84  return _value;
85 }
86 
87 template <typename T>
89  return _value;
90 }
91 
92 template <typename T>
94  std::string paramName = _parent->getName() + "." + _name;
95  if(pf.isSet(paramName)) {
96  if(pf.get<std::string>(paramName).substr(0,1) == "@")
97  throw std::runtime_error(paramName
98  + " : Attempt to overwrite Reference.");
99  else if(_value == _defaultValue)
100  pf.erase(paramName);
101  else
102  pf.set<T>(paramName, _value);
103  }
104  else if (_value != _defaultValue)
105  pf.set<T>(paramName, _value);
106 }
107 
108 template <typename T>
110  std::string paramName = _followLink(pf, _parent->getName() + "." + _name);
111  if (pf.isSet(paramName))
112  _value = pf.get<T>(paramName);
113  else
114  _value = _defaultValue;
115 }
116 
117 template <typename T>
119  std::ostringstream strStr;
120  strStr << _defaultValue;
121  return strStr.str();
122 }
123 
124 template <typename T>
125 void Parameter<T>::intoStream(std::ostream & os) const {
126  os << _value;
127 }
128 
129 // ===================== class ParameterList ============================
130 template <typename T>
131 ParameterList<T>::ParameterList(std::string defaultValue) {
132  _defaultValue = defaultValue;
133  ParameterFile pf;
134  _value = pf.getList<T>("", defaultValue);
135 }
136 
137 template <typename T>
139 }
140 
141 template <typename T>
142 std::vector<T>& ParameterList<T>::operator=(const std::vector<T>& B) {
143  _value = B;
144  return _value;
145 }
146 
147 template <typename T>
149  _value = B._value;
150  _defaultValue = B._defaultValue;
151  return *this;
152 }
153 
154 template <typename T>
155 ParameterList<T>::operator std::vector<T>() const {
156  return _value;
157 }
158 
159 template <typename T>
160 std::vector<T>& ParameterList<T>::operator()() {
161  return _value;
162 }
163 
164 template <typename T>
165 const std::vector<T>& ParameterList<T>::operator()() const {
166  return _value;
167 }
168 
169 template <typename T>
170 std::string ParameterList<T>::guessType() const {
171  return TypeDetector::type(typeid(T).name()) + " list";
172 }
173 
174 template <typename T>
176  std::stringstream stream;
177  std::string paramName = _parent->getName() + "." + _name;
178  this->intoStream(stream);
179 
180  if(pf.isSet(paramName)) {
181  if(pf.get<std::string>(paramName).substr(0,1) == "@")
182  throw std::runtime_error(paramName
183  + " : Attempt to overwrite Reference.");
184  else if(stream.str() == _defaultValue)
185  pf.erase(paramName);
186  else
187  pf.set<T>(paramName, _value);
188  }
189  else if (stream.str() != _defaultValue)
190  pf.set<T>(paramName, _value);
191 }
192 
193 template <typename T>
195  std::string paramName = _followLink(pf, _parent->getName() + "." + _name);
196  if(pf.isSet(paramName))
197  _value = pf.getList<T>(paramName);
198  else {
199  ParameterFile temp;
200  temp.set("temp", _defaultValue);
201  _value = temp.getList<T>("temp");
202  }
203 }
204 
205 template <typename T>
206 void ParameterList<T>::setDefault(const std::string& defVal) {
207  _defaultValue = defVal;
208 }
209 
210 template <typename T>
212  return _defaultValue;
213 }
214 
215 template <typename T>
216 void ParameterList<T>::intoStream(std::ostream & os) const {
217  if(_value.size()) {
218  for(unsigned int i = 0; i < _value.size()-1; i++) {
219  os << _value[i] << ";";
220  }
221  os << _value[_value.size()-1];
222  }
223 }
224 
225 template <typename T>
226 std::size_t ParameterList<T>::size() const {
227  return _value.size();
228 }
229 
230 template <typename T>
231 const T& ParameterList<T>::operator[](std::size_t pos) const {
232  return _value[pos];
233 }
234 
235 template <typename T>
236 T& ParameterList<T>::operator[](std::size_t pos) {
237  return _value[pos];
238 }
239 
240 #endif // _Parameter_HXX_
virtual const T & operator[](std::size_t pos) const
Access to specific member (read-only).
Definition: Parameter.hxx:231
This class serves to store parameters used within the Charon Project.
Definition: ParameterFile.h:68
void erase(std::string parameter)
Delete a parameter from the parameter list.
virtual void load(const ParameterFile &pf)
Load from ParameterFile.
Definition: Parameter.hxx:194
std::vector< T > & operator()()
Call operator to get a reference to the parameter list content.
Definition: Parameter.hxx:160
Declaration of class TypeDetector.
std::vector< T > getList(std::string parameter) const
If multiple values are set, return a list containing these values.
virtual std::string guessType() const
Try to guess type.
Definition: Parameter.hxx:170
virtual std::string getDefaultString()
Get default value.
Definition: Parameter.hxx:118
virtual std::size_t size() const
Access to number of members.
Definition: Parameter.hxx:226
Declaration of the base class ParameteredObject.
ParameterList(std::string defaultValue="")
Create parameter list.
Definition: Parameter.hxx:131
void setDefault(const std::string &value)
Set default value.
Definition: Parameter.hxx:206
T _defaultValue
default value
Definition: Parameter.h:157
Implementation of the AbstractParameter interface for one single parameter.
Definition: Parameter.h:153
void setDefault(const T &value)
Set default value.
Definition: Parameter.hxx:61
virtual T & operator=(const T &B)
Assignment operator for internal value.
Definition: Parameter.hxx:55
Implementation of the AbstractParameter interface for a list of parameters.
Definition: Parameter.h:213
void set(std::string parameter, const T &value=T())
Set a parameter to the given single value.
virtual void intoStream(std::ostream &os) const
Inserts the value of the Parameter at the end of a stream.
Definition: Parameter.hxx:125
T get(std::string parameter) const
Get the value of a specified parameter.
bool isSet(std::string parameter) const
Check if a givem parameter has already been set.
virtual void save(ParameterFile &pf) const
Save to ParameterFile.
Definition: Parameter.hxx:175
virtual std::string getDefaultString()
Get default value.
Definition: Parameter.hxx:211
virtual void load(const ParameterFile &pf)
Load from ParameterFile.
Definition: Parameter.hxx:109
std::vector< T > _value
Internal value (list).
Definition: Parameter.h:217
Parameter(T defaultValue=T())
Create parameter.
Definition: Parameter.hxx:45
virtual std::vector< T > & operator=(const std::vector< T > &B)
Assign parameter content.
Definition: Parameter.hxx:142
virtual void intoStream(std::ostream &os) const
Inserts the value of the ParameterList at the end of a stream.
Definition: Parameter.hxx:216
charon_core_DLL_PUBLIC std::string type(const std::string &typeInfo)
Get type representation.
T _value
current value
Definition: Parameter.h:160
virtual std::string guessType() const
Try to guess type.
Definition: Parameter.hxx:78
std::string _defaultValue
Default value, string representation, separated by ";".
Definition: Parameter.h:220
virtual void save(ParameterFile &pf) const
Save to ParameterFile.
Definition: Parameter.hxx:93
virtual const T & operator()() const
Call operator.
Definition: Parameter.hxx:83