~charon-developers/charon-core/trunk

« back to all changes in this revision

Viewing changes to src/Parameter.hxx

  • Committer: jmgottfried
  • Date: 2010-03-16 15:18:38 UTC
  • Revision ID: svn-v4:7d56a235-2f8b-4627-957e-5f30cc86da59:charon-core/trunk:680
moved header files to seperate include directory

this makes e.g. Compile and Load test work even if charon-core is not yet installed
and gives a chance to get charon-meta working

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
*/
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>
23
 
 *
24
 
 *  @date 11.12.2008
25
 
 *
26
 
 *  This file does contain the implementations of the classes
27
 
 *  - AbstractParameter
28
 
 *  - Parameter
29
 
 *  - ParameterList
30
 
 *
31
 
 *  In most cases, you want not to include this file but ParameteredObject.hxx
32
 
 */
33
 
 
34
 
#ifndef _Parameter_HXX_
35
 
#define _Parameter_HXX_
36
 
 
37
 
#include "ParameteredObject.h"
38
 
#include "TypeDetector.h"
39
 
#include <set>
40
 
 
41
 
 
42
 
// ====================   class AbstractParameter   =========================
43
 
 
44
 
inline AbstractParameter::AbstractParameter() :
45
 
    _parent(0) {
46
 
}
47
 
 
48
 
inline void AbstractParameter::init(ParameteredObject* parent,
49
 
        const std::string& name) {
50
 
    if(_parent)
51
 
        throw std::string("Parameter already assigned");
52
 
    if(!parent)
53
 
        throw std::string("Setting invalid parent object!");
54
 
    if(!name.length())
55
 
        throw std::string("Invalid name: length 0");
56
 
 
57
 
    _parent = parent;
58
 
    _name   = name;
59
 
}
60
 
 
61
 
inline AbstractParameter::~AbstractParameter() {
62
 
}
63
 
 
64
 
inline ParameteredObject& AbstractParameter::getParent() {
65
 
    return *_parent;
66
 
}
67
 
 
68
 
inline const ParameteredObject& AbstractParameter::getParent() const {
69
 
    return *_parent;
70
 
}
71
 
 
72
 
inline std::string AbstractParameter::getName() const {
73
 
    return _name;
74
 
}
75
 
 
76
 
// =========================   class Parameter   ============================
77
 
 
78
 
template <typename T>
79
 
Parameter<T>::Parameter(T defaultValue) :
80
 
        _defaultValue(defaultValue),
81
 
        _value(defaultValue) {
82
 
}
83
 
 
84
 
template <typename T>
85
 
Parameter<T>::~Parameter() {
86
 
}
87
 
 
88
 
template <typename T>
89
 
T& Parameter<T>::operator=(const T& B) {
90
 
    _value = B;
91
 
    return _value;
92
 
}
93
 
 
94
 
template <typename T>
95
 
void Parameter<T>::setDefault(const T& defVal) {
96
 
    _defaultValue = defVal;
97
 
}
98
 
 
99
 
template <typename T>
100
 
Parameter<T>& Parameter<T>::operator=(const Parameter<T>& B) {
101
 
    _value = B._value;
102
 
    _defaultValue = B._defaultValue;
103
 
    return *this;
104
 
}
105
 
 
106
 
template <typename T>
107
 
Parameter<T>::operator T() const {
108
 
    return _value;
109
 
}
110
 
 
111
 
template <typename T>
112
 
std::string Parameter<T>::guessType() const {
113
 
    return TypeDetector::instance().type(typeid(T).name());
114
 
}
115
 
 
116
 
template <typename T>
117
 
const T& Parameter<T>::operator()() const {
118
 
    return _value;
119
 
}
120
 
 
121
 
template <typename T>
122
 
T& Parameter<T>::operator()() {
123
 
    return _value;
124
 
}
125
 
 
126
 
template <typename T>
127
 
void Parameter<T>::save(ParameterFile& pf) const {
128
 
    if (_value != _defaultValue) {
129
 
        pf.set<T>(_parent->getName() + "." + _name, _value);
130
 
    }
131
 
}
132
 
 
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);
137
 
    else
138
 
        _value = _defaultValue;
139
 
}
140
 
 
141
 
template <typename T>
142
 
std::string Parameter<T>::getDefaultString() {
143
 
    std::ostringstream strStr;
144
 
    strStr << _defaultValue;
145
 
    return strStr.str();
146
 
}
147
 
 
148
 
template <typename T>
149
 
void Parameter<T>::intoStream(std::ostream & os) const {
150
 
        os << _value;
151
 
}
152
 
 
153
 
// =====================   class ParameterList   ============================
154
 
template <typename T>
155
 
ParameterList<T>::ParameterList(std::string defaultValue) {
156
 
    _defaultValue = defaultValue;
157
 
    ParameterFile pf;
158
 
    _value = pf.getList<T>("", defaultValue);
159
 
}
160
 
 
161
 
template <typename T>
162
 
ParameterList<T>::~ParameterList() {
163
 
}
164
 
 
165
 
template <typename T>
166
 
std::vector<T>& ParameterList<T>::operator=(const std::vector<T>& B) {
167
 
    _value = B;
168
 
    return _value;
169
 
}
170
 
 
171
 
template <typename T>
172
 
ParameterList<T>& ParameterList<T>::operator=(const ParameterList<T>& B) {
173
 
    _value = B._value;
174
 
    _defaultValue = B._defaultValue;
175
 
    return *this;
176
 
}
177
 
 
178
 
template <typename T>
179
 
ParameterList<T>::operator std::vector<T>() const {
180
 
    return _value;
181
 
}
182
 
 
183
 
template <typename T>
184
 
std::vector<T>& ParameterList<T>::operator()() {
185
 
    return _value;
186
 
}
187
 
 
188
 
template <typename T>
189
 
const std::vector<T>& ParameterList<T>::operator()() const {
190
 
    return _value;
191
 
}
192
 
 
193
 
template <typename T>
194
 
std::string ParameterList<T>::guessType() const {
195
 
    return TypeDetector::instance().type(typeid(T).name()) + " list";
196
 
}
197
 
 
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);
205
 
    }
206
 
    else
207
 
        pf.set<T>(_parent->getName() + "." + _name, _value);
208
 
}
209
 
 
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);
214
 
    else {
215
 
        ParameterFile temp;
216
 
        temp.set("temp", _defaultValue);
217
 
        _value = temp.getList<T>("temp");
218
 
        }
219
 
}
220
 
 
221
 
template <typename T>
222
 
void ParameterList<T>::setDefault(const std::string& defVal) {
223
 
    _defaultValue = defVal;
224
 
}
225
 
 
226
 
template <typename T>
227
 
std::string ParameterList<T>::getDefaultString() {
228
 
    return _defaultValue;
229
 
}
230
 
 
231
 
template <typename T>
232
 
void ParameterList<T>::intoStream(std::ostream & os) const {
233
 
        if(_value.size()) {
234
 
                for(unsigned int i = 0; i < _value.size()-1; i++) {
235
 
                        os << _value[i] << ";";
236
 
                }
237
 
                os << _value[_value.size()-1];
238
 
        }
239
 
}
240
 
 
241
 
#endif // _Parameter_HXX_