1
/* This file is part of Charon.
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.
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.
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/>.
16
/** @file Parameter.hxx
17
* Implementation of the abstract class AbstractParameter and
18
* template classes Parameter and ParameterList.
19
* @author <a href="mailto:daniel.kondermann@iwr.uni-heidelberg.de">
20
* Daniel Kondermann</a>
21
* @author <a href="mailto:jmgottfried@web.de">Jens-Malte Gottfried</a>
22
* @author <a href="mailto:bc002@ix.urz.uni-heidelberg.de">Cornelius Ratsch</a>
26
* This file does contain the implementations of the classes
31
* In most cases, you want not to include this file but ParameteredObject.hxx
34
#ifndef _Parameter_HXX_
35
#define _Parameter_HXX_
37
#include "ParameteredObject.h"
38
#include "TypeDetector.h"
42
// ==================== class AbstractParameter =========================
44
inline AbstractParameter::AbstractParameter() :
48
inline void AbstractParameter::init(ParameteredObject* parent,
49
const std::string& name) {
51
throw std::string("Parameter already assigned");
53
throw std::string("Setting invalid parent object!");
55
throw std::string("Invalid name: length 0");
61
inline AbstractParameter::~AbstractParameter() {
64
inline ParameteredObject& AbstractParameter::getParent() {
68
inline const ParameteredObject& AbstractParameter::getParent() const {
72
inline std::string AbstractParameter::getName() const {
76
// ========================= class Parameter ============================
79
Parameter<T>::Parameter(T defaultValue) :
80
_defaultValue(defaultValue),
81
_value(defaultValue) {
85
Parameter<T>::~Parameter() {
89
T& Parameter<T>::operator=(const T& B) {
95
void Parameter<T>::setDefault(const T& defVal) {
96
_defaultValue = defVal;
100
Parameter<T>& Parameter<T>::operator=(const Parameter<T>& B) {
102
_defaultValue = B._defaultValue;
106
template <typename T>
107
Parameter<T>::operator T() const {
111
template <typename T>
112
std::string Parameter<T>::guessType() const {
113
return TypeDetector::instance().type(typeid(T).name());
116
template <typename T>
117
const T& Parameter<T>::operator()() const {
121
template <typename T>
122
T& Parameter<T>::operator()() {
126
template <typename T>
127
void Parameter<T>::save(ParameterFile& pf) const {
128
if (_value != _defaultValue) {
129
pf.set<T>(_parent->getName() + "." + _name, _value);
133
template <typename T>
134
void Parameter<T>::load(const ParameterFile& pf) {
135
if (pf.isSet(_parent->getName() + "." + _name))
136
_value = pf.get<T>(_parent->getName() + "." + _name);
138
_value = _defaultValue;
141
template <typename T>
142
std::string Parameter<T>::getDefaultString() {
143
std::ostringstream strStr;
144
strStr << _defaultValue;
148
template <typename T>
149
void Parameter<T>::intoStream(std::ostream & os) const {
153
// ===================== class ParameterList ============================
154
template <typename T>
155
ParameterList<T>::ParameterList(std::string defaultValue) {
156
_defaultValue = defaultValue;
158
_value = pf.getList<T>("", defaultValue);
161
template <typename T>
162
ParameterList<T>::~ParameterList() {
165
template <typename T>
166
std::vector<T>& ParameterList<T>::operator=(const std::vector<T>& B) {
171
template <typename T>
172
ParameterList<T>& ParameterList<T>::operator=(const ParameterList<T>& B) {
174
_defaultValue = B._defaultValue;
178
template <typename T>
179
ParameterList<T>::operator std::vector<T>() const {
183
template <typename T>
184
std::vector<T>& ParameterList<T>::operator()() {
188
template <typename T>
189
const std::vector<T>& ParameterList<T>::operator()() const {
193
template <typename T>
194
std::string ParameterList<T>::guessType() const {
195
return TypeDetector::instance().type(typeid(T).name()) + " list";
198
template <typename T>
199
void ParameterList<T>::save(ParameterFile& pf) const {
200
std::stringstream stream;
201
this->intoStream(stream);
202
if (stream.str() == _defaultValue) {
203
if (pf.isSet(_parent->getName() + "." + _name))
204
pf.erase(_parent->getName() + "." + _name);
207
pf.set<T>(_parent->getName() + "." + _name, _value);
210
template <typename T>
211
void ParameterList<T>::load(const ParameterFile& pf) {
212
if(pf.isSet(_parent->getName() + "." + _name))
213
_value = pf.getList<T>(_parent->getName() + "." + _name);
216
temp.set("temp", _defaultValue);
217
_value = temp.getList<T>("temp");
221
template <typename T>
222
void ParameterList<T>::setDefault(const std::string& defVal) {
223
_defaultValue = defVal;
226
template <typename T>
227
std::string ParameterList<T>::getDefaultString() {
228
return _defaultValue;
231
template <typename T>
232
void ParameterList<T>::intoStream(std::ostream & os) const {
234
for(unsigned int i = 0; i < _value.size()-1; i++) {
235
os << _value[i] << ";";
237
os << _value[_value.size()-1];
241
#endif // _Parameter_HXX_