summaryrefslogtreecommitdiff
path: root/tests/test_visit_each.cc
blob: a63fa435acf9fef6392db0e275b7b643f57d2586 (plain)
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/* Copyright (C) 2014 The libsigc++ Development Team
 *
 * This file is part of libsigc++.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 */

#include "testutilities.h"
#include <string>
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <sigc++/signal.h>

// SIGCTEST_CASE 1  Assume that class sigc::visitor has not been implemented.
//                  Don't test with MyClass2, which is expected to fail in this case.
// SIGCTEST_CASE 2  Assume that class sigc::visitor has not been implemented.
//                  Test with MyClass2, although it is expected to fail in this case.
// SIGCTEST_CASE 3  Assume that class sigc::visitor has been implemented.
//                  Test with MyClass2, which is expected to succeed in this case.
// See also https://bugzilla.gnome.org/show_bug.cgi?id=724496
#define SIGCTEST_CASE 3

namespace
{
std::ostringstream result_stream;
}

// namespace assumed to belong to an external package.
namespace ns_ext
{
class NsExtClass
{
};

template <class T_action, class T_functor>
void visit_each(T_action&, const T_functor&)
{
  result_stream << "ns_ext::visit_each() ";
}

} // end namespace ns_ext

namespace
{
class MyClass1 : public sigc::trackable
{
public:
  MyClass1(const std::string& str) : s(str) {}

  void execute(int i)
  {
    result_stream << s << i;
  }
private:
  std::string s;
};

class MyClass2 : public ns_ext::NsExtClass, public sigc::trackable
{
public:
  MyClass2(const std::string& str) : s(str) {}

  void execute(int i)
  {
    result_stream << s << i;
  }
private:
  std::string s;
};

} // end anonymous namespace

namespace ns1
{
// User-defined adaptor, as decribed in adaptor_trait.h.
template <class T_functor>
struct MyAdaptor1 : public sigc::adapts<T_functor>
{
  template <class T_arg1=void, class T_arg2=void>
  struct deduce_result_type
  { typedef sigc::deduce_result_t<T_functor, T_arg1, T_arg2> type; };
  typedef typename sigc::functor_trait<T_functor>::result_type result_type;

  result_type
  operator()() const
  {
    result_stream << "MyAdaptor1()() ";
    return this->functor_();
  }

  template <class T_arg1>
  typename deduce_result_type<T_arg1>::type
  operator()(T_arg1 _A_arg1) const
  {
    result_stream << "MyAdaptor1()(_A_arg1) ";
    return this->functor_(_A_arg1);
  }

  template <class T_arg1, class T_arg2>
  typename deduce_result_type<T_arg1, T_arg2>::type
  operator()(T_arg1 _A_arg1, T_arg2 _A_arg2) const
  {
    result_stream << "MyAdaptor1()(_A_arg1, _A_arg2) ";
    return this->functor_(_A_arg1, _A_arg2);
  }

  // Constructs a MyAdaptor1 object that wraps the passed functor.
  // Initializes adapts<T_functor>::functor_, which is invoked from operator()().
  explicit MyAdaptor1(const T_functor& _A_functor)
    : sigc::adapts<T_functor>(_A_functor) {}
};

template <class T_action, class T_functor>
void visit_each(const T_action& _A_action,
                const MyAdaptor1<T_functor>& _A_target)
{
  visit_each(_A_action, _A_target.functor_);
}

template <typename T_functor>
inline MyAdaptor1<T_functor>
my_adaptor1(const T_functor& _A_func)
{
  return MyAdaptor1<T_functor>(_A_func);
}

} // end namespace ns1

#if SIGCTEST_CASE >= 3
// Specialization of sigc::visitor for MyAdaptor1.
namespace sigc
{
template <class T_functor>
struct visitor<ns1::MyAdaptor1<T_functor> >
{
  template <class T_action>
  static void do_visit_each(const T_action& _A_action,
                            const ns1::MyAdaptor1<T_functor>& _A_target)
  {
    sigc::visit_each(_A_action, _A_target.functor_);
  }
};
} // end namespace sigc
#endif // SIGCTEST_CASE >= 3


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;

  sigc::slot<void, int> sl1;

  {
    MyClass1 my_class1("x=");
    sl1 = sigc::mem_fun(&my_class1, &MyClass1::execute);
    sl1(-2);
    util->check_result(result_stream, "x=-2");

  } // auto-disconnect sl1

  sl1(-2);
  util->check_result(result_stream, "");

#if SIGCTEST_CASE >= 2
  {
    MyClass2 my_class2("y=");
    sl1 = sigc::mem_fun(&my_class2, &MyClass2::execute);
    sl1(2);
    util->check_result(result_stream, "y=2");

  } // auto-disconnect sl1

  sl1(2);
  util->check_result(result_stream, "");
#endif // SIGCTEST_CASE >= 2

  {
    MyClass1 my_class3("a=");
    sl1 = ns1::my_adaptor1(sigc::mem_fun(&my_class3, &MyClass1::execute));
    sl1(42);
    util->check_result(result_stream, "MyAdaptor1()(_A_arg1) a=42");

  } // auto-disconnect sl1

  sl1(42);
  util->check_result(result_stream, "");

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