File: test_functor_trait.cc

package info (click to toggle)
libsigc%2B%2B-2.0 2.12.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,432 kB
  • sloc: cpp: 4,132; xml: 339; python: 196; makefile: 192; sh: 5
file content (98 lines) | stat: -rw-r--r-- 2,327 bytes parent folder | download | duplicates (6)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* Copyright 2002, The libsigc++ Development Team
 *  Assigned to public domain.  Use as you wish without restriction.
 */

#include "testutilities.h"
#include <sstream>
#include <cstdlib>
#include <sigc++/adaptors/bind.h>
#include <sigc++/adaptors/compose.h>
#include <sigc++/functors/mem_fun.h>
#include <sigc++/functors/ptr_fun.h>

namespace
{
std::ostringstream result_stream;

class trackable {};

struct A : public trackable { A() {} };

template <class T_type, bool I_derived = std::is_base_of<trackable,T_type>::value>
struct with_trackable;

template <class T_type>
struct with_trackable<T_type,false>
{
  static void perform(const T_type&)
  {
    result_stream << "other ";
  }
};

template <class T_type>
struct with_trackable<T_type,true>
{
  static void perform(const T_type&)
  {
    result_stream << "trackable ";
  }

  static void perform(T_type*)
  {
    result_stream << "trackable* ";
  }

  static void perform(const T_type*)
  {
    result_stream << "const trackable* ";
  }
};

struct print
{
  void operator()(int i) const
  {
    result_stream << "int: " << i << " ";
  }

  template <class T>
  void operator()(const T& t) const
  {
    with_trackable<T>::perform(t);
  }
};

void foo(int, int, int)
{}

void bar(int)
{}

} // end anonymous namespace

int main(int argc, char* argv[])
{
  auto util = TestUtilities::get_instance();

  if (!util->check_command_args(argc, argv))
    return util->get_result_and_delete_instance() ? EXIT_SUCCESS : EXIT_FAILURE;

  int i = 1;
  int j = 2;
  int k = 3;
  A a;
  result_stream << "hit all targets: ";
  sigc::visit_each(print(), sigc::compose(sigc::bind(sigc::ptr_fun3(&foo), std::ref(a), i), sigc::ptr_fun1(&bar)));
  util->check_result(result_stream, "hit all targets: other trackable int: 1 other ");

  result_stream << "hit all ints: ";
  sigc::visit_each_type<int>(print(), sigc::compose(sigc::bind(sigc::ptr_fun3(&foo), std::ref(a), j),sigc::ptr_fun1(&bar)));
  util->check_result(result_stream, "hit all ints: int: 2 ");

  result_stream << "hit all trackable: ";
  sigc::visit_each_type<trackable>(print(), sigc::compose(sigc::bind(sigc::ptr_fun3(&foo), std::ref(a), k),sigc::ptr_fun1(&bar)));
  util->check_result(result_stream, "hit all trackable: trackable ");

  return util->get_result_and_delete_instance() ? EXIT_SUCCESS : EXIT_FAILURE;
}