| 1 |
#ifndef STUBSHADOW_H |
| 2 |
#define STUBSHADOW_H |
| 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 |
#include <unordered_map> |
| 31 |
#include <assert.h> |
| 32 |
|
| 33 |
namespace stub_ext { |
| 34 |
|
| 35 |
#define LAMDA_FUNCTION_TYPE decltype(&Lamda::operator()) |
| 36 |
|
| 37 |
class Wrapper |
| 38 |
{ |
| 39 |
public: |
| 40 |
Wrapper(){} |
| 41 |
virtual ~Wrapper(){} |
| 42 |
}; |
| 43 |
|
| 44 |
template<class Lamda> |
| 45 |
class LamdaWrapper : public Wrapper |
| 46 |
{ |
| 47 |
public: |
| 48 |
LamdaWrapper(Lamda func): Wrapper(),_func(func){} |
| 49 |
~LamdaWrapper(){} |
| 50 |
Lamda _func; |
| 51 |
}; |
| 52 |
|
| 53 |
template <typename Func> |
| 54 |
struct VFLocator |
| 55 |
{ |
| 56 |
|
| 57 |
}; |
| 58 |
|
| 59 |
template <class Obj, typename Ret, typename... Args> |
| 60 |
struct VFLocator<Ret (Obj::*)(Args...)> |
| 61 |
{ |
| 62 |
typedef Ret (*Func)(Obj*, Args...); |
| 63 |
}; |
| 64 |
|
| 65 |
template <class Obj, typename Ret, typename... Args> |
| 66 |
struct VFLocator<Ret (Obj::*)(Args...) const> |
| 67 |
{ |
| 68 |
typedef Ret (*Func)(Obj*, Args...); |
| 69 |
}; |
| 70 |
|
| 71 |
template <typename Func> |
| 72 |
struct LamdaCaller |
| 73 |
{ |
| 74 |
|
| 75 |
}; |
| 76 |
|
| 77 |
template <class Obj, typename Ret, typename... Args> |
| 78 |
struct LamdaCaller<Ret (Obj::*)(Args...) const> |
| 79 |
{ |
| 80 |
template<class Lamda, typename ...OrgArgs> |
| 81 |
static Ret call(LamdaWrapper<Lamda> *wrapper, OrgArgs... args) |
| 82 |
{ |
| 83 |
return wrapper->_func(args...); |
| 84 |
} |
| 85 |
}; |
| 86 |
|
| 87 |
template <class Obj, typename Ret> |
| 88 |
struct LamdaCaller<Ret (Obj::*)() const> |
| 89 |
{ |
| 90 |
template<class Lamda, typename ...OrgArgs> |
| 91 |
static Ret call(LamdaWrapper<Lamda> *wrapper, OrgArgs... args) |
| 92 |
{ |
| 93 |
return wrapper->_func(); |
| 94 |
} |
| 95 |
}; |
| 96 |
|
| 97 |
|
| 98 |
static std::unordered_map<long, Wrapper* > stub_wrappers; |
| 99 |
|
| 100 |
template<typename Func, class Lamda> |
| 101 |
struct FuncShadow |
| 102 |
{ |
| 103 |
|
| 104 |
}; |
| 105 |
|
| 106 |
template<typename Ret, typename... Args, class Lamda> |
| 107 |
struct FuncShadow<Ret (*)(Args...), Lamda> |
| 108 |
{ |
| 109 |
typedef Ret (*Shadow)(Args...); |
| 110 |
typedef Ret RetType; |
| 111 |
|
| 112 |
static Ret call(Args ...args) |
| 113 |
{ |
| 114 |
Shadow shadow = &call; |
| 115 |
long id = (long)shadow; |
| 116 |
auto iter = stub_wrappers.find(id); |
| 117 |
assert(stub_wrappers.find(id) != stub_wrappers.end()); |
| 118 |
LamdaWrapper<Lamda> *wrapper = dynamic_cast<LamdaWrapper<Lamda> *>(iter->second); |
| 119 |
return LamdaCaller<LAMDA_FUNCTION_TYPE>::call(wrapper, args...); |
| 120 |
} |
| 121 |
}; |
| 122 |
|
| 123 |
template<typename Ret, class Obj,typename... Args, class Lamda> |
| 124 |
struct FuncShadow<Ret (Obj::*)(Args...), Lamda> |
| 125 |
{ |
| 126 |
typedef Ret (*Shadow)(Obj *,Args...); |
| 127 |
typedef Ret RetType; |
| 128 |
static Ret call(Obj *obj, Args ...args) |
| 129 |
{ |
| 130 |
Shadow shadow = &call; |
| 131 |
long id = (long)shadow; |
| 132 |
auto iter = stub_wrappers.find(id); |
| 133 |
assert(stub_wrappers.find(id) != stub_wrappers.end()); |
| 134 |
LamdaWrapper<Lamda> *wrapper = dynamic_cast<LamdaWrapper<Lamda> *>(iter->second); |
| 135 |
return LamdaCaller<LAMDA_FUNCTION_TYPE>::call(wrapper, obj, args...); |
| 136 |
} |
| 137 |
}; |
| 138 |
|
| 139 |
|
| 140 |
template<typename Ret, class Obj,typename... Args, class Lamda> |
| 141 |
struct FuncShadow<Ret (Obj::*)(Args...) const, Lamda> |
| 142 |
{ |
| 143 |
typedef Ret (*Shadow)(Obj *,Args...); |
| 144 |
typedef Ret RetType; |
| 145 |
static Ret call(Obj *obj, Args ...args) |
| 146 |
{ |
| 147 |
Shadow shadow = &call; |
| 148 |
long id = (long)shadow; |
| 149 |
auto iter = stub_wrappers.find(id); |
| 150 |
assert(stub_wrappers.find(id) != stub_wrappers.end()); |
| 151 |
LamdaWrapper<Lamda> *wrapper = dynamic_cast<LamdaWrapper<Lamda> *>(iter->second); |
| 152 |
return LamdaCaller<LAMDA_FUNCTION_TYPE>::call(wrapper, obj, args...); |
| 153 |
} |
| 154 |
}; |
| 155 |
|
| 156 |
template<typename Func, class Lamda> |
| 157 |
typename FuncShadow<Func, Lamda>::Shadow depictShadow(Wrapper **wrapper, Func func, Lamda lamda) |
| 158 |
{ |
| 159 |
*wrapper = new LamdaWrapper<Lamda>(lamda); |
| 160 |
typename FuncShadow<Func,Lamda>::Shadow shadow = &FuncShadow<Func,Lamda>::call; |
| 161 |
long id = (long)shadow; |
| 162 |
assert(stub_wrappers.find(id) == stub_wrappers.end()); |
| 163 |
stub_wrappers.insert(std::make_pair(id,*wrapper)); |
| 164 |
return shadow; |
| 165 |
} |
| 166 |
|
| 167 |
static void freeWrapper(Wrapper *wrapper) |
| 168 |
{ |
| 169 |
if (!wrapper) |
| 170 |
return; |
| 171 |
|
| 172 |
for (auto iter = stub_wrappers.begin(); iter != stub_wrappers.end();) { |
| 173 |
if (iter->second == wrapper) |
| 174 |
iter = stub_wrappers.erase(iter); |
| 175 |
else |
| 176 |
++iter; |
| 177 |
} |
| 178 |
|
| 179 |
delete wrapper; |
| 180 |
} |
| 181 |
|
| 182 |
} |
| 183 |
|
| 184 |
#endif |