-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathinterface_example.cpp
More file actions
80 lines (62 loc) · 1.61 KB
/
interface_example.cpp
File metadata and controls
80 lines (62 loc) · 1.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
// (C) Copyright Tobias Schwinger
//
// Use modification and distribution are subject to the boost Software License,
// Version 1.0. (See http://www.boost.org/LICENSE_1_0.txt).
//------------------------------------------------------------------------------
// See interface.hpp in this directory for details.
#include <iostream>
#include <typeinfo>
#include "interface.hpp"
BOOST_EXAMPLE_INTERFACE( interface_x,
(( a_func, (void)(int) , const_qualified ))
(( a_func, (void)(long), const_qualified ))
(( another_func, (int) , non_const ))
);
// two classes that implement interface_x
struct a_class
{
void a_func(int v) const
{
std::cout << "a_class::void a_func(int v = " << v << ")" << std::endl;
}
void a_func(long v) const
{
std::cout << "a_class::void a_func(long v = " << v << ")" << std::endl;
}
int another_func()
{
std::cout << "a_class::another_func() = 3" << std::endl;
return 3;
}
};
struct another_class
{
// note: overloaded a_func implemented as a function template
template<typename T>
void a_func(T v) const
{
std::cout <<
"another_class::void a_func(T v = " << v << ")"
" [ T = " << typeid(T).name() << " ]" << std::endl;
}
int another_func()
{
std::cout << "another_class::another_func() = 5" << std::endl;
return 5;
}
};
// both classes above can be assigned to the interface variable and their
// member functions can be called through it
int main()
{
a_class x;
another_class y;
interface_x i(x);
i.a_func(12);
i.a_func(77L);
i.another_func();
i = y;
i.a_func(13);
i.a_func(21L);
i.another_func();
}