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