]> git.tdb.fi Git - ext/sigc++-2.0.git/blob - tests/test_functor_trait.cc
Adjust the name of the library to match upstream
[ext/sigc++-2.0.git] / tests / test_functor_trait.cc
1 /* Copyright 2002, The libsigc++ Development Team
2  *  Assigned to public domain.  Use as you wish without restriction.
3  */
4
5 #include "testutilities.h"
6 #include <sstream>
7 #include <cstdlib>
8 #include <sigc++/adaptors/bind.h>
9 #include <sigc++/adaptors/compose.h>
10 #include <sigc++/functors/mem_fun.h>
11 #include <sigc++/functors/ptr_fun.h>
12
13 namespace
14 {
15 std::ostringstream result_stream;
16
17 class trackable {};
18
19 struct A : public trackable { A() {} };
20
21 template <class T_type, bool I_derived = std::is_base_of<trackable,T_type>::value>
22 struct with_trackable;
23
24 template <class T_type>
25 struct with_trackable<T_type,false>
26 {
27   static void perform(const T_type&)
28   {
29     result_stream << "other ";
30   }
31 };
32
33 template <class T_type>
34 struct with_trackable<T_type,true>
35 {
36   static void perform(const T_type&)
37   {
38     result_stream << "trackable ";
39   }
40
41   static void perform(T_type*)
42   {
43     result_stream << "trackable* ";
44   }
45
46   static void perform(const T_type*)
47   {
48     result_stream << "const trackable* ";
49   }
50 };
51
52 struct print
53 {
54   void operator()(int i) const
55   {
56     result_stream << "int: " << i << " ";
57   }
58
59   template <class T>
60   void operator()(const T& t) const
61   {
62     with_trackable<T>::perform(t);
63   }
64 };
65
66 void foo(int, int, int)
67 {}
68
69 void bar(int)
70 {}
71
72 } // end anonymous namespace
73
74 int main(int argc, char* argv[])
75 {
76   auto util = TestUtilities::get_instance();
77
78   if (!util->check_command_args(argc, argv))
79     return util->get_result_and_delete_instance() ? EXIT_SUCCESS : EXIT_FAILURE;
80
81   int i = 1;
82   int j = 2;
83   int k = 3;
84   A a;
85   result_stream << "hit all targets: ";
86   sigc::visit_each(print(), sigc::compose(sigc::bind(sigc::ptr_fun3(&foo), std::ref(a), i), sigc::ptr_fun1(&bar)));
87   util->check_result(result_stream, "hit all targets: other trackable int: 1 other ");
88
89   result_stream << "hit all ints: ";
90   sigc::visit_each_type<int>(print(), sigc::compose(sigc::bind(sigc::ptr_fun3(&foo), std::ref(a), j),sigc::ptr_fun1(&bar)));
91   util->check_result(result_stream, "hit all ints: int: 2 ");
92
93   result_stream << "hit all trackable: ";
94   sigc::visit_each_type<trackable>(print(), sigc::compose(sigc::bind(sigc::ptr_fun3(&foo), std::ref(a), k),sigc::ptr_fun1(&bar)));
95   util->check_result(result_stream, "hit all trackable: trackable ");
96
97   return util->get_result_and_delete_instance() ? EXIT_SUCCESS : EXIT_FAILURE;
98 }