-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.cpp
179 lines (165 loc) · 5.61 KB
/
options.cpp
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
170
171
172
173
174
175
176
177
178
179
/* -*- mode: C++; c-basic-offOptions::set: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Guido Tack <[email protected]>
*/
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <minizinc/options.hh>
#include <minizinc/stl_map_set.hh>
namespace MiniZinc {
Options& Options::copyEntries(Options& copy) {
UNORDERED_NAMESPACE::unordered_map<std::string, KeepAlive >::const_iterator it;
for(it = _options.begin(); it!= _options.end(); ++it) {
const std::string& name = it->first;
Expression* e = (it->second)();
copy._options[name] = e;
}
return copy;
}
Expression* Options::getParam(const std::string& name) const {
UNORDERED_NAMESPACE::unordered_map<std::string, KeepAlive >::const_iterator it = _options.find(name);
if(it == _options.end()) {
std::stringstream ss;
ss << "Could not find option: \"" << name << "\"." << std::endl;
throw InternalError(ss.str());
}
return (it->second)();
}
void Options::setIntParam(const std::string& name, KeepAlive ka) {
Expression* e = ka();
if(e && e->type().ispar() && e->type().isint()) {
_options[name] = e;
} else {
std::stringstream ss;
ss << "For option: " << name << " expected Par Int, received " << e->type().toString() << std::endl;
throw InternalError(ss.str());
}
}
void Options::setFloatParam(const std::string& name, KeepAlive ka) {
Expression* e = ka();
if(e && e->type().ispar() && e->type().isfloat()) {
_options[name] = e;
} else {
std::stringstream ss;
ss << "For option: " << name << " expected Par Float, received " << e->type().toString() << std::endl;
throw InternalError(ss.str());
}
}
void Options::setBoolParam(const std::string& name, KeepAlive ka) {
Expression* e = ka();
if(e && e->type().ispar() && e->type().isbool()) {
_options[name] = e;
} else {
std::stringstream ss;
ss << "For option: " << name << " expected Par Bool, received " << e->type().toString() << std::endl;
throw InternalError(ss.str());
}
}
void Options::setStringParam(const std::string& name, KeepAlive ka) {
Expression* e = ka();
if(e && e->type().ispar() && e->type().isstring()) {
_options[name] = e;
} else {
std::stringstream ss;
ss << "For option: " << name << " expected Par string, received " << e->type().toString() << std::endl;
throw InternalError(ss.str());
}
}
void Options::setIntParam(const std::string& name, long long int e) {
GCLock lock;
IntLit* il = new IntLit(Location(), e);
KeepAlive ka(il);
setIntParam(name, ka);
};
void Options::setFloatParam(const std::string& name, double e) {
GCLock lock;
FloatLit* fl = new FloatLit(Location(), e);
KeepAlive ka(fl);
setFloatParam(name, ka);
}
void Options::setBoolParam(const std::string& name, bool e) {
GCLock lock;
BoolLit* bl = new BoolLit(Location(), e);
KeepAlive ka(bl);
setBoolParam(name, ka);
}
void Options::setStringParam(const std::string& name, std::string e) {
GCLock lock;
StringLit* sl = new StringLit(Location(), e);
KeepAlive ka(sl);
setStringParam(name, ka);
}
long long int Options::getIntParam(const std::string& name) const {
if(IntLit* il = getParam(name)->dyn_cast<IntLit>()) {
return il->v().toInt();
} else {
std::stringstream ss;
ss << "Option: \"" << name << "\" is not Par Int" << std::endl;
throw InternalError(ss.str());
}
}
long long int Options::getIntParam(const std::string& name, long long int def) const {
if (hasParam(name)) {
if(IntLit* il = getParam(name)->dyn_cast<IntLit>()) {
return il->v().toInt();
}
}
return def;
}
double Options::getFloatParam(const std::string& name) const {
if(FloatLit* fl = getParam(name)->dyn_cast<FloatLit>()) {
return fl->v();
} else {
std::stringstream ss;
ss << "Option: \"" << name << "\" is not Par Float" << std::endl;
throw InternalError(ss.str());
}
}
double Options::getFloatParam(const std::string& name, double def) const {
if (hasParam(name)) {
if(FloatLit* fl = getParam(name)->dyn_cast<FloatLit>()) {
return fl->v();
}
}
return def;
}
bool Options::getBoolParam(const std::string& name) const {
if(BoolLit* bl = getParam(name)->dyn_cast<BoolLit>()) {
return bl->v();
} else {
std::stringstream ss;
ss << "Option: \"" << name << "\" is not Par Bool" << std::endl;
throw InternalError(ss.str());
}
}
bool Options::getBoolParam(const std::string& name, bool def) const {
if (hasParam(name)) {
if(BoolLit* bl = getParam(name)->dyn_cast<BoolLit>()) {
return bl->v();
}
}
return def;
}
std::string Options::getStringParam(const std::string& name) const {
if(StringLit* sl = getParam(name)->dyn_cast<StringLit>()) {
return sl->v().str();
} else {
std::stringstream ss;
ss << "Option: \"" << name << "\" is not Par String" << std::endl;
throw InternalError(ss.str());
}
}
std::string Options::getStringParam(const std::string& name, std::string def) const {
if (hasParam(name)) {
if(StringLit* sl = getParam(name)->dyn_cast<StringLit>()) {
return sl->v().str();
}
}
return def;
}
bool Options::hasParam(const std::string& name) const {
return _options.find(name) != _options.end();
}
}