mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-20 00:51:42 +00:00
wpiutil: Add a signal-slot implementation. (#1163)
Imported from https://github.com/palacaze/sigslot Classes were renamed from lowercase_me to UppercaseMe style, primarily to avoid conflicting with the C standard library "signal" function. They were also moved to the "wpi::sig" namespace.
This commit is contained in:
135
wpiutil/src/test/native/cpp/sigslot/function-traits.cpp
Normal file
135
wpiutil/src/test/native/cpp/sigslot/function-traits.cpp
Normal file
@@ -0,0 +1,135 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2018 FIRST. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
/*
|
||||
|
||||
Sigslot, a signal-slot library
|
||||
|
||||
https://github.com/palacaze/sigslot
|
||||
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2017 Pierre-Antoine Lacaze
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "wpi/Signal.h" // NOLINT(build/include_order)
|
||||
|
||||
#include "gtest/gtest.h" // NOLINT(build/include_order)
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
using namespace wpi::sig::trait;
|
||||
|
||||
namespace {
|
||||
|
||||
void f1(int, char, float) {}
|
||||
void f2(int, char, float) noexcept {}
|
||||
|
||||
struct oo {
|
||||
void operator()(int) {}
|
||||
void operator()(int, char, float) {}
|
||||
};
|
||||
|
||||
struct s {
|
||||
static void s1(int, char, float) {}
|
||||
static void s2(int, char, float) noexcept {}
|
||||
|
||||
void f1(int, char, float) {}
|
||||
void f2(int, char, float) const {}
|
||||
void f3(int, char, float) volatile {}
|
||||
void f4(int, char, float) const volatile {}
|
||||
void f5(int, char, float) noexcept {}
|
||||
void f6(int, char, float) const noexcept {}
|
||||
void f7(int, char, float) volatile noexcept {}
|
||||
void f8(int, char, float) const volatile noexcept {}
|
||||
};
|
||||
|
||||
struct o1 {
|
||||
void operator()(int, char, float) {}
|
||||
};
|
||||
struct o2 {
|
||||
void operator()(int, char, float) const {}
|
||||
};
|
||||
struct o3 {
|
||||
void operator()(int, char, float) volatile {}
|
||||
};
|
||||
struct o4 {
|
||||
void operator()(int, char, float) const volatile {}
|
||||
};
|
||||
struct o5 {
|
||||
void operator()(int, char, float) noexcept {}
|
||||
};
|
||||
struct o6 {
|
||||
void operator()(int, char, float) const noexcept {}
|
||||
};
|
||||
struct o7 {
|
||||
void operator()(int, char, float) volatile noexcept {}
|
||||
};
|
||||
struct o8 {
|
||||
void operator()(int, char, float) const volatile noexcept {}
|
||||
};
|
||||
|
||||
using t = typelist<int, char, float>;
|
||||
|
||||
static_assert(is_callable_v<t, decltype(f1)>, "");
|
||||
static_assert(is_callable_v<t, decltype(f2)>, "");
|
||||
static_assert(is_callable_v<t, decltype(&s::s1)>, "");
|
||||
static_assert(is_callable_v<t, decltype(&s::s2)>, "");
|
||||
static_assert(is_callable_v<t, oo>, "");
|
||||
static_assert(is_callable_v<t, decltype(&s::f1), s*>, "");
|
||||
static_assert(is_callable_v<t, decltype(&s::f2), s*>, "");
|
||||
static_assert(is_callable_v<t, decltype(&s::f3), s*>, "");
|
||||
static_assert(is_callable_v<t, decltype(&s::f4), s*>, "");
|
||||
static_assert(is_callable_v<t, decltype(&s::f5), s*>, "");
|
||||
static_assert(is_callable_v<t, decltype(&s::f6), s*>, "");
|
||||
static_assert(is_callable_v<t, decltype(&s::f7), s*>, "");
|
||||
static_assert(is_callable_v<t, decltype(&s::f8), s*>, "");
|
||||
static_assert(is_callable_v<t, o1>, "");
|
||||
static_assert(is_callable_v<t, o2>, "");
|
||||
static_assert(is_callable_v<t, o3>, "");
|
||||
static_assert(is_callable_v<t, o4>, "");
|
||||
static_assert(is_callable_v<t, o5>, "");
|
||||
static_assert(is_callable_v<t, o6>, "");
|
||||
static_assert(is_callable_v<t, o7>, "");
|
||||
static_assert(is_callable_v<t, o8>, "");
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace wpi {
|
||||
|
||||
TEST(Signal, FunctionTraits) {
|
||||
auto l1 = [](int, char, float) {};
|
||||
auto l2 = [&](int, char, float) mutable {};
|
||||
auto l3 = [&](auto...) mutable {};
|
||||
|
||||
static_assert(is_callable_v<t, decltype(l1)>, "");
|
||||
static_assert(is_callable_v<t, decltype(l2)>, "");
|
||||
static_assert(is_callable_v<t, decltype(l3)>, "");
|
||||
|
||||
f1(0, '0', 0.0);
|
||||
f2(0, '0', 0.0);
|
||||
}
|
||||
|
||||
} // namespace wpi
|
||||
97
wpiutil/src/test/native/cpp/sigslot/recursive.cpp
Normal file
97
wpiutil/src/test/native/cpp/sigslot/recursive.cpp
Normal file
@@ -0,0 +1,97 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2018 FIRST. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
/*
|
||||
|
||||
Sigslot, a signal-slot library
|
||||
|
||||
https://github.com/palacaze/sigslot
|
||||
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2017 Pierre-Antoine Lacaze
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "wpi/Signal.h" // NOLINT(build/include_order)
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
namespace {
|
||||
|
||||
template <typename T>
|
||||
struct object {
|
||||
object(T i) : v{i} {} // NOLINT(runtime/explicit)
|
||||
|
||||
void inc_val(const T& i) {
|
||||
if (i != v) {
|
||||
v++;
|
||||
sig(v);
|
||||
}
|
||||
}
|
||||
|
||||
void dec_val(const T& i) {
|
||||
if (i != v) {
|
||||
v--;
|
||||
sig(v);
|
||||
}
|
||||
}
|
||||
|
||||
T v;
|
||||
wpi::sig::Signal_r<T> sig;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace wpi {
|
||||
|
||||
TEST(Signal, Recursive) {
|
||||
object<int> i1(-1);
|
||||
object<int> i2(10);
|
||||
|
||||
i1.sig.connect(&object<int>::dec_val, &i2);
|
||||
i2.sig.connect(&object<int>::inc_val, &i1);
|
||||
|
||||
i1.inc_val(0);
|
||||
|
||||
ASSERT_EQ(i1.v, i2.v);
|
||||
}
|
||||
|
||||
TEST(Signal, SelfRecursive) {
|
||||
int i = 0;
|
||||
|
||||
wpi::sig::Signal_r<int> s;
|
||||
s.connect([&](int v) {
|
||||
if (i < 10) {
|
||||
i++;
|
||||
s(v + 1);
|
||||
}
|
||||
});
|
||||
|
||||
s(0);
|
||||
|
||||
ASSERT_EQ(i, 10);
|
||||
}
|
||||
|
||||
} // namespace wpi
|
||||
140
wpiutil/src/test/native/cpp/sigslot/signal-extended.cpp
Normal file
140
wpiutil/src/test/native/cpp/sigslot/signal-extended.cpp
Normal file
@@ -0,0 +1,140 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2018 FIRST. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
/*
|
||||
|
||||
Sigslot, a signal-slot library
|
||||
|
||||
https://github.com/palacaze/sigslot
|
||||
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2017 Pierre-Antoine Lacaze
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "wpi/Signal.h" // NOLINT(build/include_order)
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
using namespace wpi::sig;
|
||||
|
||||
namespace {
|
||||
|
||||
int sum = 0;
|
||||
|
||||
void f(Connection& c, int i) {
|
||||
sum += i;
|
||||
c.disconnect();
|
||||
}
|
||||
|
||||
struct s {
|
||||
static void sf(Connection& c, int i) {
|
||||
sum += i;
|
||||
c.disconnect();
|
||||
}
|
||||
void f(Connection& c, int i) {
|
||||
sum += i;
|
||||
c.disconnect();
|
||||
}
|
||||
};
|
||||
|
||||
struct o {
|
||||
void operator()(Connection& c, int i) {
|
||||
sum += i;
|
||||
c.disconnect();
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace wpi {
|
||||
|
||||
TEST(SignalExtended, FreeConnection) {
|
||||
sum = 0;
|
||||
Signal<int> sig;
|
||||
sig.connect_extended(f);
|
||||
|
||||
sig(1);
|
||||
ASSERT_EQ(sum, 1);
|
||||
sig(1);
|
||||
ASSERT_EQ(sum, 1);
|
||||
}
|
||||
|
||||
TEST(SignalExtended, StaticConnection) {
|
||||
sum = 0;
|
||||
Signal<int> sig;
|
||||
sig.connect_extended(&s::sf);
|
||||
|
||||
sig(1);
|
||||
ASSERT_EQ(sum, 1);
|
||||
sig(1);
|
||||
ASSERT_EQ(sum, 1);
|
||||
}
|
||||
|
||||
TEST(SignalExtended, PmfConnection) {
|
||||
sum = 0;
|
||||
Signal<int> sig;
|
||||
s p;
|
||||
sig.connect_extended(&s::f, &p);
|
||||
|
||||
sig(1);
|
||||
ASSERT_EQ(sum, 1);
|
||||
sig(1);
|
||||
ASSERT_EQ(sum, 1);
|
||||
}
|
||||
|
||||
TEST(SignalExtended, FunctionObjectConnection) {
|
||||
sum = 0;
|
||||
Signal<int> sig;
|
||||
sig.connect_extended(o{});
|
||||
|
||||
sig(1);
|
||||
ASSERT_EQ(sum, 1);
|
||||
sig(1);
|
||||
ASSERT_EQ(sum, 1);
|
||||
}
|
||||
|
||||
TEST(SignalExtended, LambdaConnection) {
|
||||
sum = 0;
|
||||
Signal<int> sig;
|
||||
|
||||
sig.connect_extended([&](Connection& c, int i) {
|
||||
sum += i;
|
||||
c.disconnect();
|
||||
});
|
||||
sig(1);
|
||||
ASSERT_EQ(sum, 1);
|
||||
|
||||
sig.connect_extended([&](Connection& c, int i) mutable {
|
||||
sum += 2 * i;
|
||||
c.disconnect();
|
||||
});
|
||||
sig(1);
|
||||
ASSERT_EQ(sum, 3);
|
||||
sig(1);
|
||||
ASSERT_EQ(sum, 3);
|
||||
}
|
||||
|
||||
} // namespace wpi
|
||||
93
wpiutil/src/test/native/cpp/sigslot/signal-threaded.cpp
Normal file
93
wpiutil/src/test/native/cpp/sigslot/signal-threaded.cpp
Normal file
@@ -0,0 +1,93 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2018 FIRST. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
/*
|
||||
|
||||
Sigslot, a signal-slot library
|
||||
|
||||
https://github.com/palacaze/sigslot
|
||||
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2017 Pierre-Antoine Lacaze
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "wpi/Signal.h" // NOLINT(build/include_order)
|
||||
|
||||
#include "gtest/gtest.h" // NOLINT(build/include_order)
|
||||
|
||||
#include <array>
|
||||
#include <atomic>
|
||||
#include <thread>
|
||||
|
||||
using namespace wpi::sig;
|
||||
|
||||
namespace {
|
||||
|
||||
std::atomic<int> sum{0};
|
||||
|
||||
void f(int i) { sum += i; }
|
||||
|
||||
void emit_many(Signal<int>& sig) {
|
||||
for (int i = 0; i < 10000; ++i) sig(1);
|
||||
}
|
||||
|
||||
void connect_emit(Signal<int>& sig) {
|
||||
for (int i = 0; i < 100; ++i) {
|
||||
auto s = sig.connect_scoped(f);
|
||||
for (int j = 0; j < 100; ++j) sig(1);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace wpi {
|
||||
|
||||
TEST(Signal, ThreadedMix) {
|
||||
sum = 0;
|
||||
|
||||
Signal<int> sig;
|
||||
|
||||
std::array<std::thread, 10> threads;
|
||||
for (auto& t : threads) t = std::thread(connect_emit, std::ref(sig));
|
||||
|
||||
for (auto& t : threads) t.join();
|
||||
}
|
||||
|
||||
TEST(Signal, ThreadedEmission) {
|
||||
sum = 0;
|
||||
|
||||
Signal<int> sig;
|
||||
sig.connect(f);
|
||||
|
||||
std::array<std::thread, 10> threads;
|
||||
for (auto& t : threads) t = std::thread(emit_many, std::ref(sig));
|
||||
|
||||
for (auto& t : threads) t.join();
|
||||
|
||||
ASSERT_EQ(sum, 100000);
|
||||
}
|
||||
|
||||
} // namespace wpi
|
||||
181
wpiutil/src/test/native/cpp/sigslot/signal-tracking.cpp
Normal file
181
wpiutil/src/test/native/cpp/sigslot/signal-tracking.cpp
Normal file
@@ -0,0 +1,181 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2018 FIRST. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
/*
|
||||
|
||||
Sigslot, a signal-slot library
|
||||
|
||||
https://github.com/palacaze/sigslot
|
||||
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2017 Pierre-Antoine Lacaze
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "wpi/Signal.h" // NOLINT(build/include_order)
|
||||
|
||||
#include "gtest/gtest.h" // NOLINT(build/include_order)
|
||||
|
||||
#include <cmath>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
using namespace wpi::sig;
|
||||
|
||||
namespace {
|
||||
|
||||
int sum = 0;
|
||||
|
||||
void f1(int i) { sum += i; }
|
||||
struct o1 {
|
||||
void operator()(int i) { sum += 2 * i; }
|
||||
};
|
||||
|
||||
struct s {
|
||||
void f1(int i) { sum += i; }
|
||||
void f2(int i) const { sum += 2 * i; }
|
||||
};
|
||||
|
||||
struct oo {
|
||||
void operator()(int i) { sum += i; }
|
||||
void operator()(double i) { sum += std::round(4 * i); }
|
||||
};
|
||||
|
||||
struct dummy {};
|
||||
|
||||
static_assert(trait::is_callable_v<trait::typelist<int>, decltype(&s::f1),
|
||||
std::shared_ptr<s>>,
|
||||
"");
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace wpi {
|
||||
|
||||
TEST(Signal, TrackShared) {
|
||||
sum = 0;
|
||||
Signal<int> sig;
|
||||
|
||||
auto s1 = std::make_shared<s>();
|
||||
sig.connect(&s::f1, s1);
|
||||
|
||||
auto s2 = std::make_shared<s>();
|
||||
std::weak_ptr<s> w2 = s2;
|
||||
sig.connect(&s::f2, w2);
|
||||
|
||||
sig(1);
|
||||
ASSERT_EQ(sum, 3);
|
||||
|
||||
s1.reset();
|
||||
sig(1);
|
||||
ASSERT_EQ(sum, 5);
|
||||
|
||||
s2.reset();
|
||||
sig(1);
|
||||
ASSERT_EQ(sum, 5);
|
||||
}
|
||||
|
||||
TEST(Signal, TrackOther) {
|
||||
sum = 0;
|
||||
Signal<int> sig;
|
||||
|
||||
auto d1 = std::make_shared<dummy>();
|
||||
sig.connect(f1, d1);
|
||||
|
||||
auto d2 = std::make_shared<dummy>();
|
||||
std::weak_ptr<dummy> w2 = d2;
|
||||
sig.connect(o1(), w2);
|
||||
|
||||
sig(1);
|
||||
ASSERT_EQ(sum, 3);
|
||||
|
||||
d1.reset();
|
||||
sig(1);
|
||||
ASSERT_EQ(sum, 5);
|
||||
|
||||
d2.reset();
|
||||
sig(1);
|
||||
ASSERT_EQ(sum, 5);
|
||||
}
|
||||
|
||||
TEST(Signal, TrackOverloadedFunctionObject) {
|
||||
sum = 0;
|
||||
Signal<int> sig;
|
||||
Signal<double> sig1;
|
||||
|
||||
auto d1 = std::make_shared<dummy>();
|
||||
sig.connect(oo{}, d1);
|
||||
sig(1);
|
||||
ASSERT_EQ(sum, 1);
|
||||
|
||||
d1.reset();
|
||||
sig(1);
|
||||
ASSERT_EQ(sum, 1);
|
||||
|
||||
auto d2 = std::make_shared<dummy>();
|
||||
std::weak_ptr<dummy> w2 = d2;
|
||||
sig1.connect(oo{}, w2);
|
||||
sig1(1);
|
||||
ASSERT_EQ(sum, 5);
|
||||
|
||||
d2.reset();
|
||||
sig1(1);
|
||||
ASSERT_EQ(sum, 5);
|
||||
}
|
||||
|
||||
TEST(Signal, TrackGenericLambda) {
|
||||
std::stringstream s;
|
||||
|
||||
auto f = [&](auto a, auto... args) {
|
||||
using result_t = int[];
|
||||
s << a;
|
||||
result_t r{
|
||||
1,
|
||||
((void)(s << args), 1)...,
|
||||
};
|
||||
(void)r;
|
||||
};
|
||||
|
||||
Signal<int> sig1;
|
||||
Signal<std::string> sig2;
|
||||
Signal<double> sig3;
|
||||
|
||||
auto d1 = std::make_shared<dummy>();
|
||||
sig1.connect(f, d1);
|
||||
sig2.connect(f, d1);
|
||||
sig3.connect(f, d1);
|
||||
|
||||
sig1(1);
|
||||
sig2("foo");
|
||||
sig3(4.1);
|
||||
ASSERT_EQ(s.str(), "1foo4.1");
|
||||
|
||||
d1.reset();
|
||||
sig1(2);
|
||||
sig2("bar");
|
||||
sig3(3.0);
|
||||
ASSERT_EQ(s.str(), "1foo4.1");
|
||||
}
|
||||
|
||||
} // namespace wpi
|
||||
540
wpiutil/src/test/native/cpp/sigslot/signal.cpp
Normal file
540
wpiutil/src/test/native/cpp/sigslot/signal.cpp
Normal file
@@ -0,0 +1,540 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2018 FIRST. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
/*
|
||||
|
||||
Sigslot, a signal-slot library
|
||||
|
||||
https://github.com/palacaze/sigslot
|
||||
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2017 Pierre-Antoine Lacaze
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "wpi/Signal.h" // NOLINT(build/include_order)
|
||||
|
||||
#include "gtest/gtest.h" // NOLINT(build/include_order)
|
||||
|
||||
#include <cmath>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
using namespace wpi::sig;
|
||||
|
||||
namespace {
|
||||
|
||||
int sum = 0;
|
||||
|
||||
void f1(int i) { sum += i; }
|
||||
void f2(int i) noexcept { sum += 2 * i; }
|
||||
|
||||
struct s {
|
||||
static void s1(int i) { sum += i; }
|
||||
static void s2(int i) noexcept { sum += 2 * i; }
|
||||
|
||||
void f1(int i) { sum += i; }
|
||||
void f2(int i) const { sum += i; }
|
||||
void f3(int i) volatile { sum += i; }
|
||||
void f4(int i) const volatile { sum += i; }
|
||||
void f5(int i) noexcept { sum += i; }
|
||||
void f6(int i) const noexcept { sum += i; }
|
||||
void f7(int i) volatile noexcept { sum += i; }
|
||||
void f8(int i) const volatile noexcept { sum += i; }
|
||||
};
|
||||
|
||||
struct oo {
|
||||
void operator()(int i) { sum += i; }
|
||||
void operator()(double i) { sum += std::round(4 * i); }
|
||||
};
|
||||
|
||||
struct o1 {
|
||||
void operator()(int i) { sum += i; }
|
||||
};
|
||||
struct o2 {
|
||||
void operator()(int i) const { sum += i; }
|
||||
};
|
||||
struct o3 {
|
||||
void operator()(int i) volatile { sum += i; }
|
||||
};
|
||||
struct o4 {
|
||||
void operator()(int i) const volatile { sum += i; }
|
||||
};
|
||||
struct o5 {
|
||||
void operator()(int i) noexcept { sum += i; }
|
||||
};
|
||||
struct o6 {
|
||||
void operator()(int i) const noexcept { sum += i; }
|
||||
};
|
||||
struct o7 {
|
||||
void operator()(int i) volatile noexcept { sum += i; }
|
||||
};
|
||||
struct o8 {
|
||||
void operator()(int i) const volatile noexcept { sum += i; }
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace wpi {
|
||||
|
||||
TEST(Signal, FreeConnection) {
|
||||
sum = 0;
|
||||
Signal<int> sig;
|
||||
|
||||
auto c1 = sig.connect(f1);
|
||||
sig(1);
|
||||
ASSERT_EQ(sum, 1);
|
||||
|
||||
sig.connect(f2);
|
||||
sig(1);
|
||||
ASSERT_EQ(sum, 4);
|
||||
}
|
||||
|
||||
TEST(Signal, StaticConnection) {
|
||||
sum = 0;
|
||||
Signal<int> sig;
|
||||
|
||||
sig.connect(&s::s1);
|
||||
sig(1);
|
||||
ASSERT_EQ(sum, 1);
|
||||
|
||||
sig.connect(&s::s2);
|
||||
sig(1);
|
||||
ASSERT_EQ(sum, 4);
|
||||
}
|
||||
|
||||
TEST(Signal, PmfConnection) {
|
||||
sum = 0;
|
||||
Signal<int> sig;
|
||||
s p;
|
||||
|
||||
sig.connect(&s::f1, &p);
|
||||
sig.connect(&s::f2, &p);
|
||||
sig.connect(&s::f3, &p);
|
||||
sig.connect(&s::f4, &p);
|
||||
sig.connect(&s::f5, &p);
|
||||
sig.connect(&s::f6, &p);
|
||||
sig.connect(&s::f7, &p);
|
||||
sig.connect(&s::f8, &p);
|
||||
|
||||
sig(1);
|
||||
ASSERT_EQ(sum, 8);
|
||||
}
|
||||
|
||||
TEST(Signal, ConstPmfConnection) {
|
||||
sum = 0;
|
||||
Signal<int> sig;
|
||||
const s p;
|
||||
|
||||
sig.connect(&s::f2, &p);
|
||||
sig.connect(&s::f4, &p);
|
||||
sig.connect(&s::f6, &p);
|
||||
sig.connect(&s::f8, &p);
|
||||
|
||||
sig(1);
|
||||
ASSERT_EQ(sum, 4);
|
||||
}
|
||||
|
||||
TEST(Signal, FunctionObjectConnection) {
|
||||
sum = 0;
|
||||
Signal<int> sig;
|
||||
|
||||
sig.connect(o1{});
|
||||
sig.connect(o2{});
|
||||
sig.connect(o3{});
|
||||
sig.connect(o4{});
|
||||
sig.connect(o5{});
|
||||
sig.connect(o6{});
|
||||
sig.connect(o7{});
|
||||
sig.connect(o8{});
|
||||
|
||||
sig(1);
|
||||
ASSERT_EQ(sum, 8);
|
||||
}
|
||||
|
||||
TEST(Signal, OverloadedFunctionObjectConnection) {
|
||||
sum = 0;
|
||||
Signal<int> sig;
|
||||
Signal<double> sig1;
|
||||
|
||||
sig.connect(oo{});
|
||||
sig(1);
|
||||
ASSERT_EQ(sum, 1);
|
||||
|
||||
sig1.connect(oo{});
|
||||
sig1(1);
|
||||
ASSERT_EQ(sum, 5);
|
||||
}
|
||||
|
||||
TEST(Signal, LambdaConnection) {
|
||||
sum = 0;
|
||||
Signal<int> sig;
|
||||
|
||||
sig.connect([&](int i) { sum += i; });
|
||||
sig(1);
|
||||
ASSERT_EQ(sum, 1);
|
||||
|
||||
sig.connect([&](int i) mutable { sum += 2 * i; });
|
||||
sig(1);
|
||||
ASSERT_EQ(sum, 4);
|
||||
}
|
||||
|
||||
TEST(Signal, GenericLambdaConnection) {
|
||||
std::stringstream s;
|
||||
|
||||
auto f = [&](auto a, auto... args) {
|
||||
using result_t = int[];
|
||||
s << a;
|
||||
result_t r{
|
||||
1,
|
||||
((void)(s << args), 1)...,
|
||||
};
|
||||
(void)r;
|
||||
};
|
||||
|
||||
Signal<int> sig1;
|
||||
Signal<std::string> sig2;
|
||||
Signal<double> sig3;
|
||||
|
||||
sig1.connect(f);
|
||||
sig2.connect(f);
|
||||
sig3.connect(f);
|
||||
sig1(1);
|
||||
sig2("foo");
|
||||
sig3(4.1);
|
||||
|
||||
ASSERT_EQ(s.str(), "1foo4.1");
|
||||
}
|
||||
|
||||
TEST(Signal, LvalueEmission) {
|
||||
sum = 0;
|
||||
Signal<int> sig;
|
||||
|
||||
auto c1 = sig.connect(f1);
|
||||
int v = 1;
|
||||
sig(v);
|
||||
ASSERT_EQ(sum, 1);
|
||||
|
||||
sig.connect(f2);
|
||||
sig(v);
|
||||
ASSERT_EQ(sum, 4);
|
||||
}
|
||||
|
||||
TEST(Signal, Mutation) {
|
||||
int res = 0;
|
||||
Signal<int&> sig;
|
||||
|
||||
sig.connect([](int& r) { r += 1; });
|
||||
sig(res);
|
||||
ASSERT_EQ(res, 1);
|
||||
|
||||
sig.connect([](int& r) mutable { r += 2; });
|
||||
sig(res);
|
||||
ASSERT_EQ(res, 4);
|
||||
}
|
||||
|
||||
TEST(Signal, CompatibleArgs) {
|
||||
long ll = 0; // NOLINT(runtime/int)
|
||||
std::string ss;
|
||||
short ii = 0; // NOLINT(runtime/int)
|
||||
|
||||
auto f = [&](long l, const std::string& s, short i) { // NOLINT(runtime/int)
|
||||
ll = l;
|
||||
ss = s;
|
||||
ii = i;
|
||||
};
|
||||
|
||||
Signal<int, std::string, bool> sig;
|
||||
sig.connect(f);
|
||||
sig('0', "foo", true);
|
||||
|
||||
ASSERT_EQ(ll, 48);
|
||||
ASSERT_EQ(ss, "foo");
|
||||
ASSERT_EQ(ii, 1);
|
||||
}
|
||||
|
||||
TEST(Signal, Disconnection) {
|
||||
// test removing only connected
|
||||
{
|
||||
sum = 0;
|
||||
Signal<int> sig;
|
||||
|
||||
auto sc = sig.connect(f1);
|
||||
sig(1);
|
||||
ASSERT_EQ(sum, 1);
|
||||
|
||||
sc.disconnect();
|
||||
sig(1);
|
||||
ASSERT_EQ(sum, 1);
|
||||
}
|
||||
|
||||
// test removing first connected
|
||||
{
|
||||
sum = 0;
|
||||
Signal<int> sig;
|
||||
|
||||
auto sc = sig.connect(f1);
|
||||
sig(1);
|
||||
ASSERT_EQ(sum, 1);
|
||||
|
||||
sig.connect(f2);
|
||||
sig(1);
|
||||
ASSERT_EQ(sum, 4);
|
||||
|
||||
sc.disconnect();
|
||||
sig(1);
|
||||
ASSERT_EQ(sum, 6);
|
||||
}
|
||||
|
||||
// test removing last connected
|
||||
{
|
||||
sum = 0;
|
||||
Signal<int> sig;
|
||||
|
||||
sig.connect(f1);
|
||||
sig(1);
|
||||
ASSERT_EQ(sum, 1);
|
||||
|
||||
auto sc = sig.connect(f2);
|
||||
sig(1);
|
||||
ASSERT_EQ(sum, 4);
|
||||
|
||||
sc.disconnect();
|
||||
sig(1);
|
||||
ASSERT_EQ(sum, 5);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(Signal, ScopedConnection) {
|
||||
sum = 0;
|
||||
Signal<int> sig;
|
||||
|
||||
{
|
||||
auto sc1 = sig.connect_scoped(f1);
|
||||
sig(1);
|
||||
ASSERT_EQ(sum, 1);
|
||||
|
||||
auto sc2 = sig.connect_scoped(f2);
|
||||
sig(1);
|
||||
ASSERT_EQ(sum, 4);
|
||||
}
|
||||
|
||||
sig(1);
|
||||
ASSERT_EQ(sum, 4);
|
||||
|
||||
sum = 0;
|
||||
|
||||
{
|
||||
ScopedConnection sc1 = sig.connect(f1);
|
||||
sig(1);
|
||||
ASSERT_EQ(sum, 1);
|
||||
|
||||
ScopedConnection sc2 = sig.connect(f2);
|
||||
sig(1);
|
||||
ASSERT_EQ(sum, 4);
|
||||
}
|
||||
|
||||
sig(1);
|
||||
ASSERT_EQ(sum, 4);
|
||||
}
|
||||
|
||||
TEST(Signal, ConnectionBlocking) {
|
||||
sum = 0;
|
||||
Signal<int> sig;
|
||||
|
||||
auto c1 = sig.connect(f1);
|
||||
sig.connect(f2);
|
||||
sig(1);
|
||||
ASSERT_EQ(sum, 3);
|
||||
|
||||
c1.block();
|
||||
sig(1);
|
||||
ASSERT_EQ(sum, 5);
|
||||
|
||||
c1.unblock();
|
||||
sig(1);
|
||||
ASSERT_EQ(sum, 8);
|
||||
}
|
||||
|
||||
TEST(Signal, ConnectionBlocker) {
|
||||
sum = 0;
|
||||
Signal<int> sig;
|
||||
|
||||
auto c1 = sig.connect(f1);
|
||||
sig.connect(f2);
|
||||
sig(1);
|
||||
ASSERT_EQ(sum, 3);
|
||||
|
||||
{
|
||||
auto cb = c1.blocker();
|
||||
sig(1);
|
||||
ASSERT_EQ(sum, 5);
|
||||
}
|
||||
|
||||
sig(1);
|
||||
ASSERT_EQ(sum, 8);
|
||||
}
|
||||
|
||||
TEST(Signal, SignalBlocking) {
|
||||
sum = 0;
|
||||
Signal<int> sig;
|
||||
|
||||
sig.connect(f1);
|
||||
sig.connect(f2);
|
||||
sig(1);
|
||||
ASSERT_EQ(sum, 3);
|
||||
|
||||
sig.block();
|
||||
sig(1);
|
||||
ASSERT_EQ(sum, 3);
|
||||
|
||||
sig.unblock();
|
||||
sig(1);
|
||||
ASSERT_EQ(sum, 6);
|
||||
}
|
||||
|
||||
TEST(Signal, AllDisconnection) {
|
||||
sum = 0;
|
||||
Signal<int> sig;
|
||||
|
||||
sig.connect(f1);
|
||||
sig.connect(f2);
|
||||
sig(1);
|
||||
ASSERT_EQ(sum, 3);
|
||||
|
||||
sig.disconnect_all();
|
||||
sig(1);
|
||||
ASSERT_EQ(sum, 3);
|
||||
}
|
||||
|
||||
TEST(Signal, ConnectionCopyingMoving) {
|
||||
sum = 0;
|
||||
Signal<int> sig;
|
||||
|
||||
auto sc1 = sig.connect(f1);
|
||||
auto sc2 = sig.connect(f2);
|
||||
|
||||
auto sc3 = sc1;
|
||||
auto sc4{sc2};
|
||||
|
||||
auto sc5 = std::move(sc3);
|
||||
auto sc6{std::move(sc4)};
|
||||
|
||||
sig(1);
|
||||
ASSERT_EQ(sum, 3);
|
||||
|
||||
sc5.block();
|
||||
sig(1);
|
||||
ASSERT_EQ(sum, 5);
|
||||
|
||||
sc1.unblock();
|
||||
sig(1);
|
||||
ASSERT_EQ(sum, 8);
|
||||
|
||||
sc6.disconnect();
|
||||
sig(1);
|
||||
ASSERT_EQ(sum, 9);
|
||||
}
|
||||
|
||||
TEST(Signal, ScopedConnectionMoving) {
|
||||
sum = 0;
|
||||
Signal<int> sig;
|
||||
|
||||
{
|
||||
auto sc1 = sig.connect_scoped(f1);
|
||||
sig(1);
|
||||
ASSERT_EQ(sum, 1);
|
||||
|
||||
auto sc2 = sig.connect_scoped(f2);
|
||||
sig(1);
|
||||
ASSERT_EQ(sum, 4);
|
||||
|
||||
auto sc3 = std::move(sc1);
|
||||
sig(1);
|
||||
ASSERT_EQ(sum, 7);
|
||||
|
||||
auto sc4{std::move(sc2)};
|
||||
sig(1);
|
||||
ASSERT_EQ(sum, 10);
|
||||
}
|
||||
|
||||
sig(1);
|
||||
ASSERT_EQ(sum, 10);
|
||||
}
|
||||
|
||||
TEST(Signal, SignalMoving) {
|
||||
sum = 0;
|
||||
Signal<int> sig;
|
||||
|
||||
sig.connect(f1);
|
||||
sig.connect(f2);
|
||||
|
||||
sig(1);
|
||||
ASSERT_EQ(sum, 3);
|
||||
|
||||
auto sig2 = std::move(sig);
|
||||
sig2(1);
|
||||
ASSERT_EQ(sum, 6);
|
||||
|
||||
auto sig3 = std::move(sig2);
|
||||
sig3(1);
|
||||
ASSERT_EQ(sum, 9);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
struct object {
|
||||
object();
|
||||
object(T i) : v{i} {} // NOLINT(runtime/explicit)
|
||||
|
||||
const T& val() const { return v; }
|
||||
T& val() { return v; }
|
||||
void set_val(const T& i) {
|
||||
if (i != v) {
|
||||
v = i;
|
||||
s(i);
|
||||
}
|
||||
}
|
||||
|
||||
Signal<T>& sig() { return s; }
|
||||
|
||||
private:
|
||||
T v;
|
||||
Signal<T> s;
|
||||
};
|
||||
|
||||
TEST(Signal, Loop) {
|
||||
object<int> i1(0);
|
||||
object<int> i2(3);
|
||||
|
||||
i1.sig().connect(&object<int>::set_val, &i2);
|
||||
i2.sig().connect(&object<int>::set_val, &i1);
|
||||
|
||||
i1.set_val(1);
|
||||
|
||||
ASSERT_EQ(i1.val(), 1);
|
||||
ASSERT_EQ(i2.val(), 1);
|
||||
}
|
||||
|
||||
} // namespace wpi
|
||||
Reference in New Issue
Block a user