1 |
/* |
2 |
* Copyright (c) 2005 The University of Notre Dame. All Rights Reserved. |
3 |
* |
4 |
* The University of Notre Dame grants you ("Licensee") a |
5 |
* non-exclusive, royalty free, license to use, modify and |
6 |
* redistribute this software in source and binary code form, provided |
7 |
* that the following conditions are met: |
8 |
* |
9 |
* 1. Acknowledgement of the program authors must be made in any |
10 |
* publication of scientific results based in part on use of the |
11 |
* program. An acceptable form of acknowledgement is citation of |
12 |
* the article in which the program was described (Matthew |
13 |
* A. Meineke, Charles F. Vardeman II, Teng Lin, Christopher |
14 |
* J. Fennell and J. Daniel Gezelter, "OOPSE: An Object-Oriented |
15 |
* Parallel Simulation Engine for Molecular Dynamics," |
16 |
* J. Comput. Chem. 26, pp. 252-271 (2005)) |
17 |
* |
18 |
* 2. Redistributions of source code must retain the above copyright |
19 |
* notice, this list of conditions and the following disclaimer. |
20 |
* |
21 |
* 3. Redistributions in binary form must reproduce the above copyright |
22 |
* notice, this list of conditions and the following disclaimer in the |
23 |
* documentation and/or other materials provided with the |
24 |
* distribution. |
25 |
* |
26 |
* This software is provided "AS IS," without a warranty of any |
27 |
* kind. All express or implied conditions, representations and |
28 |
* warranties, including any implied warranty of merchantability, |
29 |
* fitness for a particular purpose or non-infringement, are hereby |
30 |
* excluded. The University of Notre Dame and its licensors shall not |
31 |
* be liable for any damages suffered by licensee as a result of |
32 |
* using, modifying or distributing the software or its |
33 |
* derivatives. In no event will the University of Notre Dame or its |
34 |
* licensors be liable for any lost revenue, profit or data, or for |
35 |
* direct, indirect, special, consequential, incidental or punitive |
36 |
* damages, however caused and regardless of the theory of liability, |
37 |
* arising out of the use of or inability to use software, even if the |
38 |
* University of Notre Dame has been advised of the possibility of |
39 |
* such damages. |
40 |
* |
41 |
* |
42 |
* ParameterManager.hpp |
43 |
* OOPSE-2.0 |
44 |
* |
45 |
* Created by Charles F. Vardeman II on 11/16/05. |
46 |
* @author Charles F. Vardeman II |
47 |
* @version $Id: ParameterManager.hpp,v 1.5 2008-10-22 20:01:49 gezelter Exp $ |
48 |
* |
49 |
*/ |
50 |
|
51 |
#ifndef UTILS_PARAMETERMANAGER_HPP |
52 |
#define UTILS_PARAMETERMANAGER_HPP |
53 |
|
54 |
#include <iostream> |
55 |
|
56 |
#include <stdlib.h> |
57 |
#include <vector> |
58 |
#include <string> |
59 |
#include <map> |
60 |
#include "config.h" |
61 |
|
62 |
|
63 |
#include "utils/simError.h" |
64 |
#include "utils/StringTokenizer.hpp" |
65 |
#include "utils/CaseConversion.hpp" |
66 |
|
67 |
|
68 |
template<typename T> |
69 |
struct ParameterTraits; |
70 |
|
71 |
//string |
72 |
template<> |
73 |
struct ParameterTraits<std::string>{ |
74 |
typedef std::string RepType; // Representation type of the value |
75 |
|
76 |
template<typename T> static bool convert(T v, RepType& r){return false;} // !NB everything is ok |
77 |
template<typename T> static RepType convert(T v) {RepType tmp; convert(v,tmp);return tmp;} |
78 |
static bool convert(RepType v, RepType& r) { r = v; return true;} |
79 |
static std::string getParamType() { return "string";} |
80 |
}; |
81 |
//bool |
82 |
template<> |
83 |
struct ParameterTraits<bool>{ |
84 |
typedef bool RepType; |
85 |
template<typename T> static bool convert(T, RepType&){return false;} |
86 |
template<typename T> static RepType convert(T v) {RepType tmp; convert(v,tmp);return tmp;} |
87 |
static bool convert(std::string v, RepType& r) { |
88 |
oopse::toLower(v); |
89 |
bool result = false; |
90 |
if (v == "true") { |
91 |
r = true; |
92 |
result = true; |
93 |
} else if (v == "false") { |
94 |
r = false; |
95 |
result = true; |
96 |
} |
97 |
|
98 |
return result; |
99 |
} |
100 |
static std::string getParamType() { return "bool";} |
101 |
}; |
102 |
|
103 |
//int |
104 |
template<> |
105 |
struct ParameterTraits<int>{ |
106 |
typedef int RepType; |
107 |
template<typename T> static bool convert(T, RepType&){return false;} |
108 |
template<typename T> static RepType convert(T v) {RepType tmp; convert(v,tmp);return tmp;} |
109 |
static bool convert(RepType v, RepType& r) { r=v; return true;} |
110 |
static std::string getParamType() { return "int";} |
111 |
}; |
112 |
|
113 |
//int |
114 |
template<> |
115 |
struct ParameterTraits<unsigned long int>{ |
116 |
typedef unsigned long int RepType; |
117 |
template<typename T> static bool convert(T, RepType&){return false;} |
118 |
template<typename T> static RepType convert(T v) {RepType tmp; convert(v,tmp);return tmp;} |
119 |
static bool convert(RepType v, RepType& r) { r=v; return true;} |
120 |
static bool convert(int v, RepType& r) {r = static_cast<unsigned long int>(v); return true;} |
121 |
static std::string getParamType() { return "unsigned long int";} |
122 |
}; |
123 |
|
124 |
//RealType |
125 |
template<> |
126 |
struct ParameterTraits<RealType>{ |
127 |
typedef RealType RepType; |
128 |
template<typename T> static bool convert(T, RepType&){return false;} |
129 |
template<typename T> static RepType convert(T v) {RepType tmp; convert(v,tmp);return tmp;} |
130 |
static bool convert(RepType v, RepType& r) {r=v; return true;} |
131 |
static bool convert(int v, RepType& r) {r = static_cast<RealType>(v); return true;} |
132 |
static bool convert(unsigned long int v, RepType& r) {r = static_cast<RealType>(v); return true;} |
133 |
static std::string getParamType() { return "RealType";} |
134 |
}; |
135 |
|
136 |
//Pair of ints |
137 |
template<> |
138 |
struct ParameterTraits<std::pair<int, int> >{ |
139 |
typedef std::pair<int, int> RepType; |
140 |
template<typename T> static bool convert(T, RepType&){return false;} |
141 |
template<typename T> static RepType convert(T v) {RepType tmp; convert(v,tmp);return tmp;} |
142 |
static bool convert(RepType v, RepType& r) {r=v; return true;} |
143 |
static bool convert(std::string v, RepType& r) { |
144 |
oopse::StringTokenizer tokenizer(v," ;,\t\n\r"); |
145 |
if (tokenizer.countTokens() == 2) { |
146 |
int atom1 = tokenizer.nextTokenAsInt(); |
147 |
int atom2 = tokenizer.nextTokenAsInt(); |
148 |
r = std::make_pair(atom1, atom2); |
149 |
return true; |
150 |
} else { |
151 |
sprintf(painCave.errMsg, |
152 |
"ParameterManager Error: " |
153 |
"Not enough tokens to make pair!\n"); |
154 |
painCave.severity = OOPSE_ERROR; |
155 |
painCave.isFatal = 1; |
156 |
simError(); |
157 |
} |
158 |
return false; |
159 |
} |
160 |
static std::string getParamType() { return "std::pair<int, int>";} |
161 |
}; |
162 |
|
163 |
|
164 |
class ParameterBase { |
165 |
public: |
166 |
ParameterBase() : keyword_(), optional_(false), defaultValue_(false), empty_(true) {} |
167 |
virtual ~ParameterBase() {} |
168 |
bool isOptional() {return optional_;} |
169 |
void setOptional(bool optional) {optional_ = optional;} |
170 |
bool hasDefaultValue() {return defaultValue_;} |
171 |
virtual bool isValid() { return true;} |
172 |
const std::string& getKeyword() {return keyword_;} |
173 |
void setKeyword(const std::string& keyword) { keyword_ = keyword;} |
174 |
bool empty() {return empty_;} |
175 |
virtual bool setData(std::string) = 0; |
176 |
virtual bool setData(int) = 0; |
177 |
virtual bool setData(unsigned long int) = 0; |
178 |
virtual bool setData(RealType) = 0; |
179 |
virtual bool setData(std::pair<int, int>) = 0; |
180 |
virtual std::string getParamType() = 0; |
181 |
protected: |
182 |
std::string keyword_; |
183 |
bool optional_; |
184 |
bool defaultValue_; |
185 |
bool empty_; |
186 |
}; |
187 |
|
188 |
template<class ParamType> |
189 |
class Parameter : public ParameterBase{ |
190 |
public: |
191 |
typedef ParameterTraits<ParamType> ValueType; |
192 |
void setDefaultValue(const ParamType& value) {data_ = value; defaultValue_ = true;} |
193 |
ParamType getData() { return data_;} |
194 |
|
195 |
virtual bool setData(std::string sval) { |
196 |
return internalSetData<std::string>(sval); |
197 |
} |
198 |
|
199 |
virtual bool setData(int ival) { |
200 |
return internalSetData<int>(ival); |
201 |
} |
202 |
|
203 |
virtual bool setData(unsigned long int lival) { |
204 |
return internalSetData<unsigned long int>(lival); |
205 |
} |
206 |
|
207 |
virtual bool setData(RealType dval) { |
208 |
return internalSetData<RealType>(dval); |
209 |
} |
210 |
|
211 |
virtual bool setData(std::pair<int, int> pval) { |
212 |
return internalSetData<std::pair<int, int> >(pval); |
213 |
} |
214 |
|
215 |
virtual std::string getParamType() { return ParameterTraits<ParamType>::getParamType();} |
216 |
private: |
217 |
template<class T> bool internalSetData(T data) { |
218 |
ParamType tmp; |
219 |
bool result = ValueType::convert(data, tmp); |
220 |
if (result) { |
221 |
empty_ = false; |
222 |
data_ = tmp; |
223 |
} |
224 |
return result; |
225 |
} |
226 |
|
227 |
private: |
228 |
ParamType data_; |
229 |
|
230 |
}; |
231 |
|
232 |
#define DeclareParameter(NAME, TYPE) \ |
233 |
private: \ |
234 |
Parameter<TYPE> NAME; \ |
235 |
public: \ |
236 |
bool have##NAME() { return !NAME.empty();} \ |
237 |
TYPE get##NAME() { return NAME.getData();} |
238 |
|
239 |
|
240 |
|
241 |
#define DefineParameter(NAME,KEYWORD) \ |
242 |
NAME.setKeyword(KEYWORD); \ |
243 |
parameters_.insert(std::map<std::string, ParameterBase*>::value_type(std::string(KEYWORD), static_cast<ParameterBase*>(&NAME))); |
244 |
|
245 |
#define DefineOptionalParameter(NAME,KEYWORD) \ |
246 |
NAME.setKeyword(KEYWORD); NAME.setOptional(true); \ |
247 |
parameters_.insert(std::map<std::string, ParameterBase*>::value_type(std::string(KEYWORD), static_cast<ParameterBase*>(&NAME))); |
248 |
|
249 |
#define DefineOptionalParameterWithDefaultValue(NAME,KEYWORD, DEFAULTVALUE) \ |
250 |
NAME.setKeyword(KEYWORD); NAME.setOptional(true); NAME.setDefaultValue(DEFAULTVALUE); \ |
251 |
parameters_.insert(std::map<std::string, ParameterBase*>::value_type(std::string(KEYWORD), static_cast<ParameterBase*>(&NAME))); |
252 |
|
253 |
#define CheckParameter(NAME, CONSTRAINT) \ |
254 |
if (!NAME.empty()) { if (!(CONSTRAINT)(NAME.getData())) { sprintf(painCave.errMsg,"Error in checking %s : should be %s\n",NAME.getKeyword().c_str(),(CONSTRAINT).getConstraintDescription().c_str()); painCave.isFatal = 1; painCave.severity = OOPSE_ERROR; simError();} } |
255 |
|
256 |
|
257 |
|
258 |
#endif |