Brian Silverman | 57be316 | 2018-08-04 23:36:33 -0700 | [diff] [blame^] | 1 | |
| 2 | // (C) Copyright Tobias Schwinger |
| 3 | // |
| 4 | // Use modification and distribution are subject to the boost Software License, |
| 5 | // Version 1.0. (See http://www.boost.org/LICENSE_1_0.txt). |
| 6 | |
| 7 | //------------------------------------------------------------------------------ |
| 8 | // See interface.hpp in this directory for details. |
| 9 | |
| 10 | #include <iostream> |
| 11 | #include <typeinfo> |
| 12 | |
| 13 | #include "interface.hpp" |
| 14 | |
| 15 | |
| 16 | BOOST_EXAMPLE_INTERFACE( interface_x, |
| 17 | (( a_func, (void)(int) , const_qualified )) |
| 18 | (( a_func, (void)(long), const_qualified )) |
| 19 | (( another_func, (int) , non_const )) |
| 20 | ); |
| 21 | |
| 22 | |
| 23 | // two classes that implement interface_x |
| 24 | |
| 25 | struct a_class |
| 26 | { |
| 27 | void a_func(int v) const |
| 28 | { |
| 29 | std::cout << "a_class::void a_func(int v = " << v << ")" << std::endl; |
| 30 | } |
| 31 | |
| 32 | void a_func(long v) const |
| 33 | { |
| 34 | std::cout << "a_class::void a_func(long v = " << v << ")" << std::endl; |
| 35 | } |
| 36 | |
| 37 | int another_func() |
| 38 | { |
| 39 | std::cout << "a_class::another_func() = 3" << std::endl; |
| 40 | return 3; |
| 41 | } |
| 42 | }; |
| 43 | |
| 44 | struct another_class |
| 45 | { |
| 46 | // note: overloaded a_func implemented as a function template |
| 47 | template<typename T> |
| 48 | void a_func(T v) const |
| 49 | { |
| 50 | std::cout << |
| 51 | "another_class::void a_func(T v = " << v << ")" |
| 52 | " [ T = " << typeid(T).name() << " ]" << std::endl; |
| 53 | } |
| 54 | |
| 55 | int another_func() |
| 56 | { |
| 57 | std::cout << "another_class::another_func() = 5" << std::endl; |
| 58 | return 5; |
| 59 | } |
| 60 | }; |
| 61 | |
| 62 | |
| 63 | // both classes above can be assigned to the interface variable and their |
| 64 | // member functions can be called through it |
| 65 | int main() |
| 66 | { |
| 67 | a_class x; |
| 68 | another_class y; |
| 69 | |
| 70 | interface_x i(x); |
| 71 | i.a_func(12); |
| 72 | i.a_func(77L); |
| 73 | i.another_func(); |
| 74 | |
| 75 | i = y; |
| 76 | i.a_func(13); |
| 77 | i.a_func(21L); |
| 78 | i.another_func(); |
| 79 | } |
| 80 | |