-
Notifications
You must be signed in to change notification settings - Fork 0
/
consumer.h
227 lines (178 loc) · 5.45 KB
/
consumer.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#ifndef INCLUDED_CONSUMER_H
#define INCLUDED_CONSUMER_H
#include <vector>
#include <parameter.h>
#include <step_map.h>
#include <sstream>
#include <assert.h>
#include <iostream>
namespace enlighten {
class ParameterBase;
class lit
{
public:
lit(const std::string &_lit)
: _literal(_lit) {}
std::string GetLiteral() const { return _literal; }
private:
std::string _literal;
};
class Consumer
{
std::string methodName;
std::vector<ParameterBase*> parameters;
void *actualInstance;
std::string className;
bool nextStepShouldThrow;
public:
Consumer() : nextStepShouldThrow(false) {}
struct EndMarker {};
Consumer &operator<<(const char* str)
{
methodName.append(str);
return *this;
}
Consumer &operator<<(int value)
{
parameters.push_back(new Parameter<int>(value,"int"));
return *this;
}
Consumer &operator<<(bool value)
{
parameters.push_back(new Parameter<bool>(value,"bool"));
return *this;
}
Consumer &operator<<(const lit &_literal)
{
parameters.push_back(new Parameter<std::string>(_literal.GetLiteral(),"std::string"));
return *this;
}
Consumer &operator<<(std::string str)
{
parameters.push_back(new Parameter<std::string>(str,"std::string"));
return *this;
}
Consumer &operator<<(EndMarker)
{
Consume();
return *this;
}
void formatTypeName(std::string &name)
{
if (!name.find("class"))
name.erase(0, 5);
if (!name.find("struct"))
name.erase(0, 6);
}
template <typename T>
std::string getTypeName(const T &t)
{
std::string name(typeid(t).name());
formatTypeName(name);
return name;
}
template <typename T>
Consumer &operator<<(T &object)
{
parameters.push_back(new Parameter<T>(object, getTypeName(object)));
return *this;
}
void Consume()
{
if (StepMap().GetInstance().ExistStepDefinition(methodName)) {
StepDefinition *step= StepMap().GetInstance().GetStepDefinition(methodName);
if (step->GetStepImpl()->MatchParameters(parameters)) {
if (this->nextStepShouldThrow)
executeProtected(step);
else
executeUnprotected(step);
return;
}
}
std::stringstream ss;
ss << "\nyou must define the step: \n\n";
ss << methodName << "\n\n";
ss << "you can implement the step definition with this snippet:\n\n";
ss << "STEP";
if (parameters.size()) ss << parameters.size();
ss << "(\"" << methodName << "\","
<< getClassName()
<< ", "
<< wikifyMethodName()
<< printParameters(false, true)
<< ")\nvoid "
<< wikifyMethodName()
<< "("
<< printParameters(true, false)
<< ") \n{ \n pending \n} \n";
//std::cerr << ss.str();
//assert(false);
std::runtime_error e(ss.str());
throw e;
}
void executeUnprotected( StepDefinition * step )
{
step->GetStepImpl()->Execute(actualInstance, parameters);
}
void executeProtected( StepDefinition * step )
{
try {
step->GetStepImpl()->Execute(actualInstance, parameters);
std::stringstream ss;
ss << "\n\n\nENLIGHTEN LOG: \nAn exception should thrown during the step \""
<< step->GetStepName() << "\" execution but it didnt\n";
std::runtime_error e(ss.str());
throw e;
} catch(...)
{
this->nextStepShouldThrow= false;
}
}
void SetThisPointer(void *instance, const std::string &_className)
{
parameters.clear();
methodName= "";
actualInstance= instance;
className= _className;
formatTypeName(className);
}
std::string getClassName()
{
return className;
}
std::string printParameters(bool withName, bool withInitialCommon)
{
std::stringstream paramAsString;
for (size_t i= 0; i < parameters.size(); i++) {
if ((withInitialCommon && i==0) || i > 0) paramAsString << ", ";
paramAsString << parameters[i]->AsString();
if (withName) { paramAsString << " p" << i+1; }
}
return paramAsString.str();
}
std::string wikifyMethodName(std::string methodName, bool lastWasSpace)
{
if (methodName.empty())
return methodName;
else {
char firstChar= methodName[0];
bool isSpace= firstChar == ' ';
if (!isSpace && lastWasSpace)
firstChar= toupper(methodName[0]);
methodName.erase(methodName.begin());
return !isSpace ?
firstChar + wikifyMethodName(methodName, isSpace)
: wikifyMethodName(methodName, isSpace);
}
}
std::string wikifyMethodName()
{
return wikifyMethodName(methodName, false);
}
void NextStepShouldThrow()
{
this->nextStepShouldThrow= true;
}
};
} //namespace enlighten
#endif