-
Notifications
You must be signed in to change notification settings - Fork 1
/
configoption.h
163 lines (133 loc) · 4.45 KB
/
configoption.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// See the file COPYRIGHT.txt for authors and copyright information.
// See the file LICENSE.txt for copying conditions.
#ifndef CFG_OPTION_H
#define CFG_OPTION_H
#include <string>
#include <memory>
#include <vector>
#include "strlib.h"
namespace cfg
{
// This class can store a value of different types based on a string
class Option
{
using OptionVector = std::vector<Option>;
public:
Option(); // Default constructor
Option(const std::string& data); // Initialize with a string value
Option(const Option& data); // Copy constructor
void reset(); // Sets all values to 0 and removes the range
// Setting will compute all possible types
bool operator=(const char* data);
bool operator=(const std::string& data);
Option& operator=(const Option& data);
template <typename Type> bool operator=(Type data);
bool setString(const std::string& data);
// Getting will simply return the precomputed values
const std::string& toString() const;
std::string toStringWithQuotes() const;
int toInt() const;
long toLong() const;
float toFloat() const;
double toDouble() const;
bool toBool() const;
char toChar() const; // Based on int
// This will try to cast the decimal to another type
template <typename Type> Type to() const;
operator const std::string&() const;
// For determining if the option was originally read in as a string with quotes
void setQuotes(bool setting);
bool hasQuotes();
// For setting the valid range
void setRange(double num1);
void setRange(double num1, double num2);
void removeRange();
// Array manipulation
Option& push(const Option& opt = Option()); // push_back
void pop(); // pop_back
Option& operator[](unsigned pos);
Option& back();
unsigned size() const;
void clear();
OptionVector::iterator begin();
OptionVector::iterator end();
OptionVector::const_iterator begin() const;
OptionVector::const_iterator end() const;
// Arrays as strings
std::string buildArrayString(const std::string& indentStr = "") const; // Returns the array in string format
//bool parseArrayString(const std::string& arrayStr); // Sets the array elements from a string
//bool parseArrayLines(const std::vector<std::string>& lines); // Sets the array elements from multiple lines
// Note: This will be moved to private later.
private:
bool isInRange(double num);
enum RangeType
{
NoRange = 0,
MinRange,
MinMaxRange
};
// The "set" function will set all of these, no matter what the type is
std::string str;
long number;
double decimal;
bool logical;
bool quotes;
RangeType range;
double rangeMin;
double rangeMax;
std::unique_ptr<OptionVector> options;
// Wrapping the vector with a pointer to prevent recursive construction and incomplete type issues
// Also, this is only created when push() is called for the first time
// Also, this array is separate from the option itself, and nothing is kept in sync
// This means that the first element can be different from the option.
static OptionVector emptyVector;
// This is used for returning iterators when the array isn't allocated
};
template <typename Type>
bool Option::operator=(Type data)
{
// Only set the value if it is in range
if (isInRange((double)data))
{
number = data;
decimal = data;
logical = (data != 0);
str = strlib::toString<Type>(data);
quotes = false;
return true;
}
return false;
}
template <typename Type>
Type Option::to() const
{
return static_cast<Type>(decimal);
}
// Stream operator overload
std::ostream& operator<<(std::ostream& stream, const Option& option);
// Factory functions
template <typename Type>
Option makeOption(Type data)
{
Option tmp;
tmp = data;
return tmp;
}
template <typename Type>
Option makeOption(Type data, double num1)
{
Option tmp;
tmp = data;
tmp.setRange(num1);
return tmp;
}
template <typename Type>
Option makeOption(Type data, double num1, double num2)
{
Option tmp;
tmp = data;
tmp.setRange(num1, num2);
return tmp;
}
}
#endif