-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathxdr_call.cc
159 lines (116 loc) · 2.93 KB
/
xdr_call.cc
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
/*
* xdr_call.cc
*
* Created on: Feb 3, 2012
* Author: ptroja
*/
#include <iostream>
#include <sstream>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/units/detail/utility.hpp>
std::string buffer;
class iface {
private:
std::string id;
private:
template<typename... Args>
struct dummy {
};
template<typename C, typename... Args>
std::size_t arity(void (C::* op)(Args...)) const {
return sizeof...(Args);
}
template<typename C, typename... Args>
C * caller_cast(void (C::* op)(Args...)) {
return dynamic_cast<C *>(this);
};
template <typename F, typename... Fargs>
void
unpack(boost::archive::text_iarchive & ia, F & f, const dummy< > &&, Fargs&... fargs)
{
std::cout << "Final: " << sizeof...(Fargs) << "/" << arity(f) << std::endl;
(caller_cast(f) ->* f)(fargs...);
}
template <typename F, typename Targ, typename... Targs, typename... Fargs>
void
unpack(boost::archive::text_iarchive & ia, F & f, const dummy<Targ, Targs...> &&, Fargs&... fargs)
{
std::cout << "Partial: " << sizeof...(Fargs) << "/" << arity(f) << std::endl;
Targ t;
ia >> t;
std::cout
<< "unpack(" << boost::units::detail::demangle(typeid(Targ).name())
<< ") = " << t << std::endl;
unpack(ia, f, dummy<Targs...>(), fargs..., t);
}
public:
template<typename T>
iface(const T * me)
{
id = (boost::units::detail::demangle(typeid(T).name()));
std::cout << id << std::endl;
}
virtual ~iface() {};
template<typename C, typename... Args>
void
call_me(boost::archive::text_iarchive & ia, void (C::* op)(Args...))
{
std::cout << "operator()::arity = " << arity(op) << std::endl;
unpack(ia, op, dummy<Args...>() );
}
};
class stub {
private:
template<typename T>
void operator()(boost::archive::text_oarchive & oa, const T& value) {
oa << value;
}
template<typename T, typename... Args>
void operator()(boost::archive::text_oarchive & oa, const T& value, const Args&... args) {
oa << value;
this->operator()(oa, args...);
}
public:
template<typename... Args>
void operator()(const Args&... args) {
std::ostringstream os;
boost::archive::text_oarchive oa(os);
this->operator()(oa, args...);
std::cout << os.str() << std::endl;
buffer = os.str();
}
};
class foo_iface : public iface {
public:
foo_iface() : iface(this) {
}
virtual void operator()(int a, int b, int c, int d) = 0;
};
class foo_impl : public foo_iface {
public:
void operator()(int a, int b, int c, int d) {
std::cout << a << "+" << b << "+" << c << "+" << d << std::endl;
}
};
class foo_stub : public foo_iface, private stub {
public:
void operator()(int a, int b, int c, int d) {
stub::operator()(a, b, c, d);
}
};
int main()
{
{
foo_stub fs;
fs(1, 2, 3, 4);
}
std::cout << "buffer is: " << buffer << std::endl;
{
std::istringstream is(buffer);
boost::archive::text_iarchive ia(is);
foo_impl fo;
fo.call_me(ia, &foo_iface::operator());
}
return 0;
}