-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontext.cpp
More file actions
186 lines (153 loc) · 5.68 KB
/
Copy pathcontext.cpp
File metadata and controls
186 lines (153 loc) · 5.68 KB
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
180
181
182
183
184
185
186
#include "ccs/context.h"
#include <map>
#include <set>
#include <sstream>
#include <stdexcept>
#include "search_state.h"
#include "ccs/types.h"
namespace ccs {
struct MissingProp : public CcsProperty {
virtual bool exists() const { return false; }
virtual Origin origin() const
{ throw std::runtime_error("called origin() on MissingProp"); }
virtual const std::string &strValue() const
{ throw std::runtime_error("called strValue() on MissingProp"); }
virtual int intValue() const
{ throw std::runtime_error("called intValue() on MissingProp"); }
virtual double doubleValue() const
{ throw std::runtime_error("called doubleValue() on MissingProp"); }
virtual bool boolValue() const
{ throw std::runtime_error("called boolValue() on MissingProp"); }
};
namespace { MissingProp Missing; }
CcsContext::CcsContext(std::shared_ptr<const Node> root)
: searchState(new SearchState(root)) {}
CcsContext::CcsContext(const CcsContext &parent, const Key &key)
: searchState(SearchState::newChild(parent.searchState, key)) {}
CcsContext::CcsContext(const CcsContext &parent, const std::string &name)
: searchState(SearchState::newChild(parent.searchState, Key(name, {}))) {}
CcsContext::CcsContext(const CcsContext &parent, const std::string &name,
const std::vector<std::string> &values)
: searchState(SearchState::newChild(parent.searchState, Key(name, values))) {}
void CcsContext::logRuleDag(std::ostream &os) const {
searchState->logRuleDag(os);
}
CcsContext::Builder CcsContext::builder() const { return Builder(*this); }
const CcsProperty &CcsContext::getProperty(const std::string &propertyName)
const {
const CcsProperty *prop = searchState->findProperty(*this, propertyName);
if (prop) return *prop;
return Missing;
}
const std::string &CcsContext::getString(const std::string &propertyName)
const {
const CcsProperty &prop(getProperty(propertyName));
if (!prop.exists()) throw no_such_property(propertyName, *this);
return prop.strValue();
}
const std::string &CcsContext::getString(const std::string &propertyName,
const std::string &defaultVal) const {
const CcsProperty *prop(searchState->findProperty(*this, propertyName));
if (!prop) return defaultVal;
return prop->strValue();
}
bool CcsContext::getInto(std::string &dest, const std::string &propertyName)
const {
const CcsProperty &prop(getProperty(propertyName));
if (!prop.exists()) return false;
dest = prop.strValue();
return true;
}
int CcsContext::getInt(const std::string &propertyName) const {
const CcsProperty &prop(getProperty(propertyName));
if (!prop.exists()) throw no_such_property(propertyName, *this);
return prop.intValue();
}
int CcsContext::getInt(const std::string &propertyName, int defaultVal) const {
const CcsProperty *prop(searchState->findProperty(*this, propertyName));
if (!prop) return defaultVal;
return prop->intValue();
}
bool CcsContext::getInto(int &dest, const std::string &propertyName)
const {
const CcsProperty &prop(getProperty(propertyName));
if (!prop.exists()) return false;
dest = prop.intValue();
return true;
}
double CcsContext::getDouble(const std::string &propertyName) const {
const CcsProperty &prop(getProperty(propertyName));
if (!prop.exists()) throw no_such_property(propertyName, *this);
return prop.doubleValue();
}
double CcsContext::getDouble(const std::string &propertyName,
double defaultVal) const {
const CcsProperty *prop(searchState->findProperty(*this, propertyName));
if (!prop) return defaultVal;
return prop->doubleValue();
}
bool CcsContext::getInto(double &dest, const std::string &propertyName)
const {
const CcsProperty &prop(getProperty(propertyName));
if (!prop.exists()) return false;
dest = prop.doubleValue();
return true;
}
bool CcsContext::getBool(const std::string &propertyName) const {
const CcsProperty &prop(getProperty(propertyName));
if (!prop.exists()) throw no_such_property(propertyName, *this);
return prop.boolValue();
}
bool CcsContext::getBool(const std::string &propertyName, bool defaultVal)
const {
const CcsProperty *prop(searchState->findProperty(*this, propertyName));
if (!prop) return defaultVal;
return prop->boolValue();
}
bool CcsContext::getInto(bool &dest, const std::string &propertyName)
const {
const CcsProperty &prop(getProperty(propertyName));
if (!prop.exists()) return false;
dest = prop.boolValue();
return true;
}
std::ostream &operator<<(std::ostream &str, const CcsContext ctx) {
return str << *ctx.searchState;
}
bool CcsContext::checkEmpty(std::istream &stream) {
if (stream.fail()) return false;
stream.peek();
if (!stream.eof()) return false;
return true;
}
struct CcsContext::Builder::Impl {
CcsContext context;
Key key;
Impl(const CcsContext &context) : context(context) {}
};
CcsContext::Builder::Builder(const CcsContext &context) :
impl(new Impl(context)) {}
CcsContext::Builder::Builder(const Builder &that) :
impl(new Impl(*that.impl)) {}
CcsContext::Builder &CcsContext::Builder::operator=(
const CcsContext::Builder &that) {
impl.reset(new Impl(*that.impl));
return *this;
}
CcsContext::Builder::~Builder() {}
CcsContext CcsContext::Builder::build() const
{ return CcsContext(impl->context, impl->key); }
CcsContext::Builder &CcsContext::Builder::add(const std::string &name,
const std::vector<std::string> &values) {
impl->key.addName(name);
for (auto it = values.cbegin(); it != values.cend(); ++it)
impl->key.addValue(name, *it);
return *this;
}
no_such_property::no_such_property(const std::string &name, CcsContext context)
: context(context) {
std::ostringstream str;
str << "no such property: '" << name << "' (in context: " << context << ")";
msg = str.str();
}
}