interface_example.cpp 1.6 KB

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