-
Notifications
You must be signed in to change notification settings - Fork 177
Expand file tree
/
Copy pathDynamicProxy.hpp
More file actions
242 lines (204 loc) · 8.61 KB
/
DynamicProxy.hpp
File metadata and controls
242 lines (204 loc) · 8.61 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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
* Copyright (c) 2014 Eran Pe'er.
*
* This program is made available under the terms of the MIT License.
*
* Created on Mar 10, 2014
*/
#pragma once
#undef max
#include <functional>
#include <type_traits>
#include <vector>
#include <array>
#include <new>
#include <limits>
#include "mockutils/VirtualTable.hpp"
#include "mockutils/union_cast.hpp"
#include "mockutils/MethodInvocationHandler.hpp"
#include "mockutils/VTUtils.hpp"
#include "mockutils/FakeObject.hpp"
#include "mockutils/MethodProxy.hpp"
#include "mockutils/MethodProxyCreator.hpp"
namespace fakeit {
class InvocationHandlers : public InvocationHandlerCollection {
std::vector<std::shared_ptr<Destructible>> &_methodMocks;
std::vector<unsigned int> &_offsets;
unsigned int getOffset(unsigned int id) const
{
unsigned int offset = 0;
for (; offset < _offsets.size(); offset++) {
if (_offsets[offset] == id) {
break;
}
}
return offset;
}
public:
InvocationHandlers(
std::vector<std::shared_ptr<Destructible>> &methodMocks,
std::vector<unsigned int> &offsets) :
_methodMocks(methodMocks), _offsets(offsets) {
for (std::vector<unsigned int>::iterator it = _offsets.begin(); it != _offsets.end(); ++it)
{
*it = std::numeric_limits<int>::max();
}
}
Destructible *getInvocatoinHandlerPtrById(unsigned int id) override {
unsigned int offset = getOffset(id);
std::shared_ptr<Destructible> ptr = _methodMocks[offset];
return ptr.get();
}
};
template<typename C, typename ... baseclasses>
struct DynamicProxy {
static_assert(std::is_polymorphic<C>::value, "DynamicProxy requires a polymorphic type");
DynamicProxy(C &inst) :
instance(inst),
originalVtHandle(VirtualTable<C, baseclasses...>::getVTable(instance).createHandle()),
_methodMocks(VTUtils::getVTSize<C>()),
_offsets(VTUtils::getVTSize<C>()),
_invocationHandlers(_methodMocks, _offsets) {
_cloneVt.copyFrom(originalVtHandle.restore());
_cloneVt.setCookie(InvocationHandlerCollection::VT_COOKIE_INDEX, &_invocationHandlers);
getFake().setVirtualTable(_cloneVt);
}
void detach() {
getFake().setVirtualTable(originalVtHandle.restore());
}
~DynamicProxy() {
_cloneVt.dispose();
}
C &get() {
return instance;
}
void Reset() {
_methodMocks = {};
_methodMocks.resize(VTUtils::getVTSize<C>());
_members = {};
_offsets = {};
_offsets.resize(VTUtils::getVTSize<C>());
_cloneVt.copyFrom(originalVtHandle.restore());
}
void Clear()
{
}
template<int id, typename R, typename CONVENTION, typename ... arglist>
void stubMethod(FuncWithConvention<C, R, CONVENTION, arglist... > vMethod, MethodInvocationHandler<R, arglist...> *methodInvocationHandler) {
auto offset = VTUtils::getOffset(vMethod);
MethodProxyCreator<R, CONVENTION, arglist...> creator;
bind(creator.createMethodProxy<id + 1, CONVENTION>(offset), methodInvocationHandler);
}
void stubDtor(MethodInvocationHandler<void> *methodInvocationHandler) {
auto offset = VTUtils::getDestructorOffset<C>();
// For the cases we care about (COM), the destructor uses the default calling convention.
MethodProxyCreator<void, ConventionHelper::DefaultConvention> creator;
bindDtor(creator.createMethodProxy<0,ConventionHelper::DefaultConvention>(offset), methodInvocationHandler);
}
template<typename R, typename CONVENTION, typename ... arglist>
bool isMethodStubbed(FuncWithConvention<C, R, CONVENTION, arglist... > vMethod) {
unsigned int offset = VTUtils::getOffset(vMethod);
return isBinded(offset);
}
template<typename R, typename ... arglist>
bool isMethodStubbed(R(C::*vMethod)(arglist...)) {
unsigned int offset = VTUtils::getOffset(vMethod);
return isBinded(offset);
}
bool isDtorStubbed() {
unsigned int offset = VTUtils::getDestructorOffset<C>();
return isBinded(offset);
}
template<typename R, typename CONVENTION, typename ... arglist>
Destructible *getMethodMock(FuncWithConvention<C, R, CONVENTION, arglist... > vMethod) {
auto offset = VTUtils::getOffset(vMethod);
std::shared_ptr<Destructible> ptr = _methodMocks[offset];
return ptr.get();
}
Destructible *getDtorMock() {
auto offset = VTUtils::getDestructorOffset<C>();
std::shared_ptr<Destructible> ptr = _methodMocks[offset];
return ptr.get();
}
template<typename DATA_TYPE, typename ... arglist>
void stubDataMember(DATA_TYPE C::*member, const arglist &... initargs) {
DATA_TYPE C::*theMember = (DATA_TYPE C::*) member;
C &mock = get();
DATA_TYPE *memberPtr = &(mock.*theMember);
_members.push_back(
std::shared_ptr<DataMemeberWrapper < DATA_TYPE, arglist...> >
{new DataMemeberWrapper < DATA_TYPE, arglist...>(memberPtr,
initargs...)});
}
template<typename DATA_TYPE>
void getMethodMocks(std::vector<DATA_TYPE> &into) const {
for (std::shared_ptr<Destructible> ptr : _methodMocks) {
DATA_TYPE p = dynamic_cast<DATA_TYPE>(ptr.get());
if (p) {
into.push_back(p);
}
}
}
VirtualTable<C, baseclasses...> &getOriginalVT() {
VirtualTable<C, baseclasses...> &vt = originalVtHandle.restore();
return vt;
}
private:
template<typename DATA_TYPE, typename ... arglist>
class DataMemeberWrapper : public Destructible {
private:
DATA_TYPE *dataMember;
public:
DataMemeberWrapper(DATA_TYPE *dataMem, const arglist &... initargs) :
dataMember(dataMem) {
new(dataMember) DATA_TYPE{initargs ...};
}
~DataMemeberWrapper() override
{
dataMember->~DATA_TYPE();
}
};
static_assert(sizeof(C) == sizeof(FakeObject<C, baseclasses...>), "This is a problem");
C &instance;
typename VirtualTable<C, baseclasses...>::Handle originalVtHandle; // avoid delete!! this is the original!
VirtualTable<C, baseclasses...> _cloneVt;
//
std::vector<std::shared_ptr<Destructible>> _methodMocks;
std::vector<std::shared_ptr<Destructible>> _members;
std::vector<unsigned int> _offsets;
InvocationHandlers _invocationHandlers;
FakeObject<C, baseclasses...> &getFake() {
return reinterpret_cast<FakeObject<C, baseclasses...> &>(instance);
}
void bind(const MethodProxy &methodProxy, Destructible *invocationHandler) {
getFake().setMethod(methodProxy.getOffset(), methodProxy.getProxy());
_methodMocks[methodProxy.getOffset()].reset(invocationHandler);
_offsets[methodProxy.getOffset()] = methodProxy.getId();
}
void bindDtor(const MethodProxy &methodProxy, Destructible *invocationHandler) {
getFake().setDtor(methodProxy.getProxy());
_methodMocks[methodProxy.getOffset()].reset(invocationHandler);
_offsets[methodProxy.getOffset()] = methodProxy.getId();
}
template<typename DATA_TYPE>
DATA_TYPE getMethodMock(unsigned int offset) {
std::shared_ptr<Destructible> ptr = _methodMocks[offset];
return dynamic_cast<DATA_TYPE>(ptr.get());
}
template<typename BaseClass>
void checkMultipleInheritance() {
C *ptr = (C *) (unsigned int) 1;
BaseClass *basePtr = ptr;
int delta = (unsigned long) basePtr - (unsigned long) ptr;
if (delta > 0) {
// base class does not start on same position as derived class.
// this is multiple inheritance.
throw std::invalid_argument(std::string("multiple inheritance is not supported"));
}
}
bool isBinded(unsigned int offset) {
std::shared_ptr<Destructible> ptr = _methodMocks[offset];
return ptr.get() != nullptr;
}
};
}