summaryrefslogtreecommitdiff
path: root/tests/test_track_obj.cc
blob: cf18b7bb0887069f9939eca2e6765feca519f400 (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
205
206
207
/* Copyright (C) 2013 - 2016, 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/>.
 */

// The purpose of this test case is threefold.
// - Test sigc::track_obj().
// - Show that a slot with a C++11 lambda expression can be automatically
//   disconnected when an object derived from sigc::trackable is deleted,
//   provided sigc::track_obj() is used.
//   It shows that C++11 lambda expressions can replace the libsigc++ lambda
//   expressions, which have been removed.
//   See https://bugzilla.gnome.org/show_bug.cgi?id=672555
// - Test the code example in the documentation in sigc++/adaptors/track_obj.h.
//
// If test_track_obj writes nothing and the return code is 0, the test has passed.

#include "testutilities.h"
#include <iostream>
#include <sigc++/adaptors/track_obj.h>
#include <sigc++/signal.h>

namespace
{
std::ostringstream result_stream;

struct book : public sigc::trackable
{
  explicit book(const std::string& name) : name_(name) {}
  operator std::string &() { return name_; }
  operator const std::string &() const { return name_; }
  std::string name_;
};

struct bar_group4 : public sigc::trackable
{
};

class Functor1
{
public:
  explicit Functor1(const bar_group4& bar) : bar_(bar) {}

  std::string operator()(int i) { return (i < 0) ? "negative" : ((i > 0) ? "positive" : "zero"); }

protected:
  // Don't make it private. clang++ does not like unused private data.
  const bar_group4& bar_;
};

class Functor2
{
public:
  Functor2(const bar_group4& bar, const book& aBook) : bar_(bar), aBook_(aBook) {}

  std::string operator()(int i, const std::string& str) const
  {
    std::string result = (i < 0) ? "negative, " : ((i > 0) ? "positive, " : "zero, ");
    result += str;
    result += aBook_;
    return result;
  }

protected:
  // Don't make it private. clang++ does not like unused private data.
  const bar_group4& bar_;

private:
  const book& aBook_;
};

// C++11 lamba expressions:

inline std::ostringstream&
operator<<(std::ostringstream& s, const book& b)
{
  s << b.name_;
  return s;
}

void
egon(std::string& str)
{
  result_stream << "egon(string '" << str << "')";
  str = "egon was here";
}

void
foo_group4(bar_group4&)
{
  result_stream << "foo_group4(bar_group4&)";
}

} // 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;

  sigc::slot<std::string(int)> sl1;
  {
    bar_group4 bar4;
    sl1 = sigc::track_obj(Functor1(bar4), bar4);
    result_stream << sl1(-2);
    util->check_result(result_stream, "negative");

  } // auto-disconnect sl1

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

  // Allocate on the heap. valgrind can then find erroneous memory accesses.
  // (There should be none, of course.)
  auto psl2 = new sigc::slot<std::string(int, std::string)>;
  auto pbar4 = new bar_group4;
  auto pbook4 = new book("A Book");
  *psl2 = sigc::track_obj(Functor2(*pbar4, *pbook4), *pbar4, *pbook4);
  result_stream << (*psl2)(0, "Book title: ");
  util->check_result(result_stream, "zero, Book title: A Book");

  delete pbook4; // auto-disconnect *psl2
  pbook4 = nullptr;
  result_stream << (*psl2)(0, "Book title: ");
  util->check_result(result_stream, "");
  delete psl2;
  psl2 = nullptr;
  delete pbar4;
  pbar4 = nullptr;

  // C++11 lambda expressions:

  // auto-disconnect
  // If you want to auto-disconnect a slot with a C++11 lambda expression
  // that contains references to sigc::trackable-derived objects, you must use
  // sigc::track_obj().
  sigc::slot<void(std::ostringstream&)> sl10;
  {
    book guest_book("karl");
    // sl1 = [&guest_book](std::ostringstream& stream){ stream << guest_book << "\n"; }; // no
    // auto-disconnect
    sl10 = sigc::track_obj(
      [&guest_book](std::ostringstream& stream) { stream << guest_book; }, guest_book);
    sl10(result_stream);
    util->check_result(result_stream, "karl");

  } // auto-disconnect sl10

  sl10(result_stream);
  util->check_result(result_stream, "");

  // auto-disconnect
  sigc::slot<void()> sl20;
  {
    book guest_book("karl");
    // sl2 = [&guest_book] () { egon(guest_book); }; // no auto-disconnect
    // sl2 = std::bind(&egon, std::ref(guest_book)); // does not compile (gcc 4.6.3)
    sl20 = sigc::track_obj([&guest_book]() { egon(guest_book); }, guest_book);
    sl20();
    util->check_result(result_stream, "egon(string 'karl')");

    result_stream << static_cast<const std::string&>(guest_book);
    util->check_result(result_stream, "egon was here");

  } // auto-disconnect sl20

  sl20();
  util->check_result(result_stream, "");

  // Code example in the documentation sigc++/adaptors/track_obj.h.
  // --------------------------------------------------------------
  {
    // struct bar : public sigc::trackable {} some_bar;
    sigc::signal<void()> some_signal;
    {
      bar_group4 some_bar;
      // some_signal.connect([&some_bar](){ foo_group4(some_bar); }); // no auto-disconnect
      // some_signal.connect(sigc::bind(&foo_group4, std::ref(some_bar))); // auto-disconnects,
      //   but we prefer C++11 lambda
      some_signal.connect(sigc::track_obj([&some_bar]() { foo_group4(some_bar); }, some_bar));
      some_signal.emit();
      util->check_result(result_stream, "foo_group4(bar_group4&)");

    } // auto-disconnect the lambda expression

    some_signal.emit();
    util->check_result(result_stream, "");
  }

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