forked from fabian-jung/tsmp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.cpp
82 lines (62 loc) · 1.71 KB
/
proxy.cpp
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
#include <concepts>
#include <functional>
#include <memory>
#include <stdexcept>
#include <tsmp/proxy.hpp>
#include <iostream>
#include <type_traits>
#include <utility>
struct foo_t
{
int i;
auto add(int a, int b) { return a + b; }
};
struct interface_t
{
virtual ~interface_t() = default;
virtual void print() const = 0;
};
struct foo_impl : interface_t
{
void print() const override { std::cout << "I am a foo_impl." << std::endl; };
};
struct bar_impl : interface_t
{
virtual void print() const override { std::cout << "I am a bar_impl." << std::endl; };
};
struct late_binding_example
{
late_binding_example() = default;
virtual ~late_binding_example() = default;
virtual void foo()
{
std::cout << "late_binding_example::foo() called\n";
bar();
}
virtual void bar() { std::cout << "late_binding_example::bar() called\n"; }
};
struct printer
{
decltype(auto) operator()(auto& base, std::string_view name, auto&&... args) const
{
std::cout << "printer called " << name << " with args ";
((std::cout << args << ", "), ...);
std::cout << "\n";
return std::invoke(base, std::forward<decltype(args)>(args)...);
}
};
int main(int, char*[])
{
tsmp::unique_proxy pfoo{foo_t{}, printer{}};
pfoo.add(5, 42);
tsmp::polymorphic_value<interface_t> pvalue1{foo_impl{}};
pvalue1.print();
auto cpy_foo = pvalue1;
cpy_foo.print();
tsmp::polymorphic_value<interface_t> pvalue2{bar_impl{}};
pvalue2.print();
tsmp::polymorphic_value<interface_t> pvalue3{tsmp::virtual_proxy<foo_impl, printer>{}};
pvalue3.print();
tsmp::virtual_proxy<late_binding_example, printer> vproxy;
vproxy.foo();
}