-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathoption.h
169 lines (137 loc) · 4.09 KB
/
option.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
164
165
166
167
168
169
/*
* AQ, a Go playing engine.
* Copyright (C) 2017-2020 Yu Yamaguchi
* except where otherwise indicated.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef OPTION_H_
#define OPTION_H_
#include <string>
#include <unordered_map>
#include "./config.h"
// --------------------
// Option
// --------------------
/**
* @enum OptionType
* Data type to be stored in the Option class.
*/
enum OptionType {
kOptionNone,
kOptionString,
kOptionBool,
kOptionInt,
kOptionDouble,
};
/**
* @class Option
* Option class holds the options specified by the command line argument, which
* are bool, int, double, and string for each option type. The data type is
* determined by the constructor.
*/
class Option {
public:
typedef std::unordered_map<std::string, Option> OptionsMap;
// Constructor
Option() : type_(kOptionNone), min_(0), max_(0) {}
explicit Option(const char* v)
: type_(kOptionString), val_(v), min_(0), max_(0) {}
explicit Option(bool v)
: type_(kOptionBool), val_(v ? "true" : "false"), min_(0), max_(0) {}
Option(int v, int min_v, int max_v)
: type_(kOptionInt), val_(std::to_string(v)), min_(min_v), max_(max_v) {}
explicit Option(double v)
: type_(kOptionDouble), val_(std::to_string(v)), min_(0), max_(0) {}
// Copy
Option& operator=(std::string v) {
ASSERT_LV1(type_ != kOptionNone);
// Out of range or invalid argument.
if (type_ != kOptionString && v.empty() ||
(type_ == kOptionBool && v != "true" && v != "false") ||
(type_ == kOptionInt && (stoll(v) < min_ || stoll(v) > max_)))
return *this;
val_ = v;
return *this;
}
Option& operator=(const char* ptr) { return *this = std::string(ptr); }
Option& operator=(int v) {
ASSERT_LV1(type_ == kOptionInt || type_ == kOptionDouble);
return *this = std::to_string(v);
}
Option& operator=(bool v) {
ASSERT_LV1(type_ == kOptionBool);
return *this = (v ? "true" : "false");
}
Option& operator=(double v) {
ASSERT_LV1(type_ == kOptionDouble);
return *this = std::to_string(v);
}
void operator<<(const Option& o) { *this = o; }
// Accessor for each data type.
int get_int() const {
ASSERT_LV1(type_ == kOptionInt);
return std::stoi(val_);
}
bool get_bool() const {
ASSERT_LV1(type_ == kOptionBool);
return (val_ == "true");
}
double get_double() const {
ASSERT_LV1(type_ == kOptionDouble);
return std::stod(val_);
}
std::string get_string() const {
ASSERT_LV1(type_ != kOptionNone);
return val_;
}
// Implicit type conversion.
// Used for variable initialization and conditional branching by options of
// kOptionBool.
operator int() const {
ASSERT_LV1(type_ == kOptionInt);
return std::stoi(val_);
}
operator bool() const {
ASSERT_LV1(type_ == kOptionBool);
return (val_ == "true");
}
operator double() const {
ASSERT_LV1(type_ == kOptionDouble);
return std::stod(val_);
}
operator std::string() const {
ASSERT_LV1(type_ != kOptionNone);
return val_;
}
private:
OptionType type_;
std::string val_;
int min_;
int max_;
};
/**
* Map to store options with command line arguments.
*/
extern Option::OptionsMap Options;
/**
* Combine directory and file paths.
*/
std::string JoinPath(const std::string s1, const std::string s2,
const std::string s3 = "");
/**
* Parse config.txt and command line arguments and reflect them in Options.
*/
std::string ReadConfiguration(int argc, char** argv);
#endif // OPTION_H_