Skip to content

Commit d19d25d

Browse files
committed
feat(signal_set): add constructors from executor
Mirror the executor-construction pattern used by tcp_socket and tcp_acceptor: add a bare signal_set(Ex const&) constructor plus an executor variant of the variadic signals constructor, each delegating to the executor's context. Add tests covering both new constructors.
1 parent 73cf3ac commit d19d25d

2 files changed

Lines changed: 49 additions & 0 deletions

File tree

include/boost/corosio/signal_set.hpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include <concepts>
2020
#include <system_error>
21+
#include <type_traits>
2122

2223
/*
2324
Signal Set Public API
@@ -224,6 +225,36 @@ class BOOST_COROSIO_DECL signal_set : public io_signal_set
224225
(check(add(signals)), ...);
225226
}
226227

228+
/** Construct an empty signal set from an executor.
229+
230+
The signal set is associated with the executor's context.
231+
232+
@param ex The executor whose context will own this signal set.
233+
*/
234+
template<class Ex>
235+
requires(!std::same_as<std::remove_cvref_t<Ex>, signal_set>) &&
236+
capy::Executor<Ex>
237+
explicit signal_set(Ex const& ex) : signal_set(ex.context())
238+
{
239+
}
240+
241+
/** Construct a signal set with initial signals from an executor.
242+
243+
The signal set is associated with the executor's context.
244+
245+
@param ex The executor whose context will own this signal set.
246+
@param signal First signal number to add.
247+
@param signals Additional signal numbers to add.
248+
249+
@throws std::system_error Thrown on failure.
250+
*/
251+
template<class Ex, std::convertible_to<int>... Signals>
252+
requires capy::Executor<Ex>
253+
signal_set(Ex const& ex, int signal, Signals... signals)
254+
: signal_set(ex.context(), signal, signals...)
255+
{
256+
}
257+
227258
/** Move constructor.
228259
229260
Transfers ownership of the signal set resources.

test/unit/signal_set.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,22 @@ struct signal_set_test
6767
BOOST_TEST_PASS();
6868
}
6969

70+
void testConstructFromExecutor()
71+
{
72+
io_context ioc(Backend);
73+
signal_set s(ioc.get_executor());
74+
75+
BOOST_TEST_PASS();
76+
}
77+
78+
void testConstructFromExecutorWithSignals()
79+
{
80+
io_context ioc(Backend);
81+
signal_set s(ioc.get_executor(), SIGINT, SIGTERM);
82+
83+
BOOST_TEST_PASS();
84+
}
85+
7086
void testMoveConstruct()
7187
{
7288
io_context ioc(Backend);
@@ -795,6 +811,8 @@ struct signal_set_test
795811
testConstructWithOneSignal();
796812
testConstructWithTwoSignals();
797813
testConstructWithThreeSignals();
814+
testConstructFromExecutor();
815+
testConstructFromExecutorWithSignals();
798816
testMoveConstruct();
799817
testMoveAssign();
800818
testMoveAssignCrossContext();

0 commit comments

Comments
 (0)