1 #ifndef PARAMETERSINFO_DEF
2 #define PARAMETERSINFO_DEF
3
4 class ParameterInfoBase {
5 public:
6 string nameString; //string that identifies parameter
7 int inputLevel; //where the parameter was defined
8 int inputLevelAllowed; //at which input level parameter definition is allowed
9 virtual void inputValues(istringstream &streamIn) =0;
10 friend std::ostream& operator<< (std::ostream& o, ParameterInfoBase const& b);
~ParameterInfoBase()11 virtual ~ParameterInfoBase() {};
12 protected:
13 virtual void printValues(std::ostream& o) const = 0;
14 };
15
16
17
18 inline std::ostream& operator<< (std::ostream& o, ParameterInfoBase const& b) {
19 b.printValues(o);
20 return o;
21 };
22
23
24 template <class parameterType>
inputOneValue(istringstream & streamIn)25 inline parameterType inputOneValue (istringstream &streamIn) {
26 parameterType oneV;
27 streamIn >> oneV;
28 return oneV;
29 };
30 template <>
31 inline string inputOneValue <string> (istringstream &streamIn) {
32 string oneV="";
33 streamIn >> ws;//skip whitespace
34 if (streamIn.peek()!='"') {//simple parameter with no spaces or "
35 streamIn >> oneV;
36 } else {
37 streamIn.get();//skip "
38 getline(streamIn,oneV,'"');
39 };
40 return oneV;
41 };
42
43
44 template <class parameterType>
printOneValue(parameterType * value,std::ostream & outStr)45 inline void printOneValue (parameterType *value, std::ostream& outStr) {
46 outStr << *value;
47 };
48 template <>
49 inline void printOneValue <string> (string *value, std::ostream& outStr) {
50 if ((*value).find_first_of(" \t")!=std::string::npos) {//there is white space in the argument, put "" around
51 outStr << '\"' << *value <<'\"';
52 } else {
53 outStr << *value;
54 };
55 };
56
57 template <class parameterType>
58 class ParameterInfoScalar : public ParameterInfoBase {
59 public:
60 parameterType *value;
61 vector <parameterType> allowedValues;
62
ParameterInfoScalar(int inputLevelIn,int inputLevelAllowedIn,string nameStringIn,parameterType * valueIn)63 ParameterInfoScalar(int inputLevelIn, int inputLevelAllowedIn, string nameStringIn, parameterType* valueIn) {
64 nameString=nameStringIn;
65 inputLevel=inputLevelIn;
66 inputLevelAllowed=inputLevelAllowedIn;
67 value=valueIn;
68 };
69
inputValues(istringstream & streamIn)70 void inputValues(istringstream &streamIn) {
71 *value=inputOneValue <parameterType> (streamIn);
72 };
73
~ParameterInfoScalar()74 ~ParameterInfoScalar() {};
75 protected:
printValues(std::ostream & outStr)76 virtual void printValues(std::ostream& outStr) const {
77 printOneValue(value, outStr);
78 };
79
80 };
81
82 template <class parameterType>
83 class ParameterInfoVector : public ParameterInfoBase {
84 public:
85 vector <parameterType> *value;
86 vector <parameterType> allowedValues;
87
ParameterInfoVector(int inputLevelIn,int inputLevelAllowedIn,string nameStringIn,vector<parameterType> * valueIn)88 ParameterInfoVector(int inputLevelIn, int inputLevelAllowedIn, string nameStringIn, vector <parameterType> *valueIn) {
89 nameString=nameStringIn;
90 inputLevel=inputLevelIn;
91 inputLevelAllowed=inputLevelAllowedIn;
92 value=valueIn;
93 };
94
inputValues(istringstream & streamIn)95 void inputValues(istringstream &streamIn) {
96 (*value).clear();
97 while (streamIn.good()) {
98 (*value).push_back(inputOneValue <parameterType> (streamIn));
99 streamIn >> ws; //remove white space, may arrive at the end of line
100 };
101 };
102
~ParameterInfoVector()103 ~ParameterInfoVector() {};
104 protected:
printValues(std::ostream & outStr)105 virtual void printValues(std::ostream& outStr) const {
106 for (int ii=0; ii < (int) (*value).size(); ii++) {
107 printOneValue(&(*value).at(ii),outStr);
108 outStr<<" ";
109 };
110 };
111 };
112 #endif
113