ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-4/src/utils/ParameterManager.hpp
Revision: 2516
Committed: Fri Dec 16 18:55:55 2005 UTC (18 years, 6 months ago) by tim
File size: 7706 byte(s)
Log Message:
make minor change to build oopse under solaris

File Contents

# Content
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.2 2005-12-16 18:55:55 tim 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
61
62
63 #include "utils/CaseConversion.hpp"
64
65
66
67 template<typename T>
68 struct ParameterTraits;
69
70 //string
71 template<>
72 struct ParameterTraits<std::string>{
73 typedef std::string RepType; // Representation type of the value
74
75 template<typename T> static bool convert(T v, RepType& r){return false;} // !NB everything is ok
76 template<typename T> static RepType convert(T v) {RepType tmp; convert(v,tmp);return tmp;}
77 static bool convert(RepType v, RepType& r) { r = v; return true;}
78 static std::string getParamType() { return "string";}
79 };
80 //bool
81 template<>
82 struct ParameterTraits<bool>{
83 typedef bool RepType;
84 template<typename T> static bool convert(T, RepType&){return false;}
85 template<typename T> static RepType convert(T v) {RepType tmp; convert(v,tmp);return tmp;}
86 static bool convert(std::string v, RepType& r) {
87 oopse::toLower(v);
88 bool result = false;
89 if (v == "true") {
90 r = true;
91 result = true;
92 } else if (v == "false") {
93 r = false;
94 result = true;
95 }
96
97 return result;
98 }
99 static std::string getParamType() { return "bool";}
100 };
101
102 //int
103 template<>
104 struct ParameterTraits<int>{
105 typedef int RepType;
106 template<typename T> static bool convert(T, RepType&){return false;}
107 template<typename T> static RepType convert(T v) {RepType tmp; convert(v,tmp);return tmp;}
108 static bool convert(RepType v, RepType& r) { r=v; return true;}
109 static std::string getParamType() { return "int";}
110 };
111
112 //double
113 template<>
114 struct ParameterTraits<double>{
115 typedef double RepType;
116 template<typename T> static bool convert(T, RepType&){return false;}
117 template<typename T> static RepType convert(T v) {RepType tmp; convert(v,tmp);return tmp;}
118 static bool convert(RepType v, RepType& r) {r=v; return true;}
119 static bool convert(int v, RepType& r) {r = static_cast<double>(v); return true;}
120 static std::string getParamType() { return "double";}
121 };
122
123
124 class ParameterBase {
125 public:
126 ParameterBase() : keyword_(), optional_(false), defaultValue_(false), empty_(true) {}
127 virtual ~ParameterBase() {}
128 bool isOptional() {return optional_;}
129 void setOptional(bool optional) {optional_ = optional;}
130 bool hasDefaultValue() {return defaultValue_;}
131 virtual bool isValid() { return true;}
132 const std::string& getKeyword() {return keyword_;}
133 void setKeyword(const std::string& keyword) { keyword_ = keyword;}
134 bool empty() {return empty_;}
135 virtual bool setData(std::string) = 0;
136 virtual bool setData(int) = 0;
137 virtual bool setData(double) = 0;
138 virtual std::string getParamType() = 0;
139 protected:
140 std::string keyword_;
141 bool optional_;
142 bool defaultValue_;
143 bool empty_;
144 };
145
146 template<class ParamType>
147 class Parameter : public ParameterBase{
148 public:
149 typedef ParameterTraits<ParamType> ValueType;
150 void setDefaultValue(const ParamType& value) {data_ = value; defaultValue_ = true;}
151 ParamType getData() { return data_;}
152
153 virtual bool setData(std::string sval) {
154 return internalSetData<std::string>(sval);
155 }
156 virtual bool setData(int ival) {
157 return internalSetData<int>(ival);
158 }
159
160 virtual bool setData(double dval) {
161 return internalSetData<double>(dval);
162 }
163
164 virtual std::string getParamType() { return ParameterTraits<ParamType>::getParamType();}
165 private:
166 template<class T> bool internalSetData(T data) {
167 ParamType tmp;
168 bool result = ValueType::convert(data, tmp);
169 if (result) {
170 empty_ = false;
171 data_ = tmp;
172 }
173 return result;
174 }
175
176 private:
177 ParamType data_;
178
179 };
180
181 #define DeclareParameter(NAME, TYPE) \
182 private: \
183 Parameter<TYPE> NAME; \
184 public: \
185 bool have##NAME() { return !NAME.empty();} \
186 TYPE get##NAME() { return NAME.getData();}
187
188
189
190 #define DefineParameter(NAME,KEYWORD) \
191 NAME.setKeyword(KEYWORD); \
192 parameters_.insert(std::map<std::string, ParameterBase*>::value_type(std::string(KEYWORD), static_cast<ParameterBase*>(&NAME)));
193
194 #define DefineOptionalParameter(NAME,KEYWORD) \
195 NAME.setKeyword(KEYWORD); NAME.setOptional(true); \
196 parameters_.insert(std::map<std::string, ParameterBase*>::value_type(std::string(KEYWORD), static_cast<ParameterBase*>(&NAME)));
197
198 #define DefineOptionalParameterWithDefaultValue(NAME,KEYWORD, DEFAULTVALUE) \
199 NAME.setKeyword(KEYWORD); NAME.setOptional(true); NAME.setDefaultValue(DEFAULTVALUE); \
200 parameters_.insert(std::map<std::string, ParameterBase*>::value_type(std::string(KEYWORD), static_cast<ParameterBase*>(&NAME)));
201
202 #define CheckParameter(NAME, CONSTRAINT) \
203 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();} }
204
205
206
207 #endif