| 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.4 2008-09-11 19:40:59 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 | 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 | //RealType | 
| 113 | template<> | 
| 114 | struct ParameterTraits<RealType>{ | 
| 115 | typedef RealType 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<RealType>(v); return true;} | 
| 120 | static std::string getParamType() { return "RealType";} | 
| 121 | }; | 
| 122 |  | 
| 123 | //Pair of ints | 
| 124 | template<> | 
| 125 | struct ParameterTraits<std::pair<int, int> >{ | 
| 126 | typedef std::pair<int, int>  RepType; | 
| 127 | template<typename T> static bool    convert(T, RepType&){return false;} | 
| 128 | template<typename T> static RepType convert(T v)        {RepType tmp; convert(v,tmp);return tmp;} | 
| 129 | static bool convert(RepType v, RepType& r)            {r=v; return true;} | 
| 130 | static bool convert(std::string v, RepType& r) { | 
| 131 | oopse::StringTokenizer tokenizer(v," ;,\t\n\r"); | 
| 132 | if (tokenizer.countTokens() == 2) { | 
| 133 | int atom1 = tokenizer.nextTokenAsInt(); | 
| 134 | int atom2 = tokenizer.nextTokenAsInt(); | 
| 135 | r = std::make_pair(atom1, atom2); | 
| 136 | return true; | 
| 137 | } else { | 
| 138 | sprintf(painCave.errMsg, | 
| 139 | "ParameterManager Error: " | 
| 140 | "Not enough tokens to make pair!\n"); | 
| 141 | painCave.severity = OOPSE_ERROR; | 
| 142 | painCave.isFatal = 1; | 
| 143 | simError(); | 
| 144 | } | 
| 145 | return false; | 
| 146 | } | 
| 147 | static std::string getParamType() { return "std::pair<int, int>";} | 
| 148 | }; | 
| 149 |  | 
| 150 |  | 
| 151 | class ParameterBase { | 
| 152 | public: | 
| 153 | ParameterBase() : keyword_(), optional_(false), defaultValue_(false), empty_(true) {} | 
| 154 | virtual ~ParameterBase() {} | 
| 155 | bool isOptional() {return optional_;} | 
| 156 | void setOptional(bool optional) {optional_ = optional;} | 
| 157 | bool hasDefaultValue() {return defaultValue_;} | 
| 158 | virtual bool isValid() { return true;} | 
| 159 | const std::string& getKeyword() {return keyword_;} | 
| 160 | void setKeyword(const std::string& keyword) { keyword_ = keyword;} | 
| 161 | bool empty() {return empty_;} | 
| 162 | virtual bool setData(std::string) = 0; | 
| 163 | virtual bool setData(int) = 0; | 
| 164 | virtual bool setData(RealType) = 0; | 
| 165 | virtual bool setData(std::pair<int, int>) = 0; | 
| 166 | virtual std::string getParamType() = 0; | 
| 167 | protected: | 
| 168 | std::string keyword_; | 
| 169 | bool optional_; | 
| 170 | bool defaultValue_; | 
| 171 | bool empty_; | 
| 172 | }; | 
| 173 |  | 
| 174 | template<class ParamType> | 
| 175 | class Parameter : public ParameterBase{ | 
| 176 | public: | 
| 177 | typedef ParameterTraits<ParamType> ValueType; | 
| 178 | void setDefaultValue(const ParamType& value) {data_ = value; defaultValue_ = true;} | 
| 179 | ParamType getData() { return data_;} | 
| 180 |  | 
| 181 | virtual bool setData(std::string sval) { | 
| 182 | return internalSetData<std::string>(sval); | 
| 183 | } | 
| 184 | virtual bool setData(int ival) { | 
| 185 | return internalSetData<int>(ival); | 
| 186 | } | 
| 187 |  | 
| 188 | virtual bool setData(RealType dval) { | 
| 189 | return internalSetData<RealType>(dval); | 
| 190 | } | 
| 191 |  | 
| 192 | virtual bool setData(std::pair<int, int> pval) { | 
| 193 | return internalSetData<std::pair<int, int> >(pval); | 
| 194 | } | 
| 195 |  | 
| 196 | virtual std::string getParamType() { return ParameterTraits<ParamType>::getParamType();} | 
| 197 | private: | 
| 198 | template<class T> bool internalSetData(T data) { | 
| 199 | ParamType tmp; | 
| 200 | bool result = ValueType::convert(data, tmp); | 
| 201 | if (result) { | 
| 202 | empty_ = false; | 
| 203 | data_ = tmp; | 
| 204 | } | 
| 205 | return result; | 
| 206 | } | 
| 207 |  | 
| 208 | private: | 
| 209 | ParamType data_; | 
| 210 |  | 
| 211 | }; | 
| 212 |  | 
| 213 | #define DeclareParameter(NAME, TYPE)         \ | 
| 214 | private:                                                   \ | 
| 215 | Parameter<TYPE> NAME;                                     \ | 
| 216 | public:                                                      \ | 
| 217 | bool have##NAME() { return !NAME.empty();}  \ | 
| 218 | TYPE get##NAME() { return NAME.getData();} | 
| 219 |  | 
| 220 |  | 
| 221 |  | 
| 222 | #define DefineParameter(NAME,KEYWORD)                              \ | 
| 223 | NAME.setKeyword(KEYWORD);                  \ | 
| 224 | parameters_.insert(std::map<std::string, ParameterBase*>::value_type(std::string(KEYWORD), static_cast<ParameterBase*>(&NAME))); | 
| 225 |  | 
| 226 | #define DefineOptionalParameter(NAME,KEYWORD)                              \ | 
| 227 | NAME.setKeyword(KEYWORD); NAME.setOptional(true);                    \ | 
| 228 | parameters_.insert(std::map<std::string, ParameterBase*>::value_type(std::string(KEYWORD), static_cast<ParameterBase*>(&NAME))); | 
| 229 |  | 
| 230 | #define DefineOptionalParameterWithDefaultValue(NAME,KEYWORD, DEFAULTVALUE)                              \ | 
| 231 | NAME.setKeyword(KEYWORD); NAME.setOptional(true); NAME.setDefaultValue(DEFAULTVALUE);                      \ | 
| 232 | parameters_.insert(std::map<std::string, ParameterBase*>::value_type(std::string(KEYWORD), static_cast<ParameterBase*>(&NAME))); | 
| 233 |  | 
| 234 | #define CheckParameter(NAME, CONSTRAINT)                              \ | 
| 235 | 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();} } | 
| 236 |  | 
| 237 |  | 
| 238 |  | 
| 239 | #endif |