Skip to content

Commit d3545d1

Browse files
committed
Add "null" method to signals so user can query if a signal
has been moved from. Allow some methods to be called on null signals.
1 parent 4d994bb commit d3545d1

File tree

3 files changed

+51
-6
lines changed

3 files changed

+51
-6
lines changed

doc/reference/signal_header.xml

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,7 @@ file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
145145
<paramtype>signal &amp;&amp;</paramtype>
146146
</parameter>
147147
<description><para>Move constructor.</para></description>
148-
<postconditions><para>The signal <computeroutput>other</computeroutput>
149-
is in a "moved-from" state where it may only be destroyed, swapped, or move assigned.
150-
Any other operation on a "moved-from" signal is invalid.</para></postconditions>
148+
<postconditions><para><computeroutput>other.<methodname>null</methodname>()</computeroutput></para></postconditions>
151149

152150
<throws><para>Will not throw.</para></throws>
153151
</constructor>
@@ -157,9 +155,7 @@ file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
157155
<paramtype>signal &amp;&amp;</paramtype>
158156
</parameter>
159157
<description><para>Move assignment.</para></description>
160-
<postconditions><para>The signal <computeroutput>rhs</computeroutput>
161-
is in a "moved-from" state where it may only be destroyed, swapped, or move assigned.
162-
Any other operation on a "moved-from" signal is invalid.</para></postconditions>
158+
<postconditions><para><computeroutput>rhs.<methodname>null</methodname>()</computeroutput></para></postconditions>
163159

164160
<throws><para>Will not throw.</para></throws>
165161
</copy-assignment>
@@ -431,6 +427,41 @@ file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
431427
<throws><para>Will not throw.</para></throws>
432428
</method>
433429
</method-group>
430+
431+
<method-group name="misc">
432+
<method name="null" cv="const">
433+
<type>bool</type>
434+
<returns><para><computeroutput>false</computeroutput> for a normal signal,
435+
<computeroutput>true</computeroutput> after a signal
436+
has been moved from. Null signals are nonfunctional husks left
437+
behind after a signal is moved. They may not be invoked or have
438+
slots connected to them.</para>
439+
</returns>
440+
441+
<throws><para>Will not throw.</para></throws>
442+
443+
<notes>
444+
<para>A null signal may be destroyed, swapped, or move assigned. Also,
445+
the following methods may be called on a null signal:
446+
</para>
447+
<variablelist>
448+
<varlistentry><term><methodname>disconnect</methodname> and <methodname>disconnect_all_slots</methodname></term>
449+
<listitem>no effect</listitem>
450+
</varlistentry>
451+
<varlistentry><term><methodname>empty</methodname></term>
452+
<listitem>returns <computeroutput>true</computeroutput></listitem>
453+
</varlistentry>
454+
<varlistentry><term><methodname>null</methodname></term>
455+
<listitem>returns <computeroutput>true</computeroutput></listitem>
456+
</varlistentry>
457+
<varlistentry><term><methodname>num_slots</methodname></term>
458+
<listitem>returns <computeroutput>0</computeroutput></listitem>
459+
</varlistentry>
460+
</variablelist>
461+
<para>Any other operation on a null signal is invalid.</para>
462+
</notes>
463+
</method>
464+
</method-group>
434465

435466
<free-function-group name="specialized algorithms">
436467
<function name="swap">

include/boost/signals2/detail/signal_template.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,15 +714,18 @@ namespace boost
714714
}
715715
void disconnect_all_slots()
716716
{
717+
if (_pimpl.get() == 0) return;
717718
(*_pimpl).disconnect_all_slots();
718719
}
719720
void disconnect(const group_type &group)
720721
{
722+
if (_pimpl.get() == 0) return;
721723
(*_pimpl).disconnect(group);
722724
}
723725
template <typename T>
724726
void disconnect(const T &slot)
725727
{
728+
if (_pimpl.get() == 0) return;
726729
(*_pimpl).disconnect(slot);
727730
}
728731
result_type operator ()(BOOST_SIGNALS2_SIGNATURE_FULL_ARGS(BOOST_SIGNALS2_NUM_ARGS))
@@ -735,10 +738,12 @@ namespace boost
735738
}
736739
std::size_t num_slots() const
737740
{
741+
if (_pimpl.get() == 0) return 0;
738742
return (*_pimpl).num_slots();
739743
}
740744
bool empty() const
741745
{
746+
if (_pimpl.get() == 0) return true;
742747
return (*_pimpl).empty();
743748
}
744749
combiner_type combiner() const
@@ -758,6 +763,10 @@ namespace boost
758763
{
759764
return _pimpl.get() == other._pimpl.get();
760765
}
766+
bool null() const
767+
{
768+
return _pimpl.get() == 0;
769+
}
761770
protected:
762771
virtual shared_ptr<void> lock_pimpl() const
763772
{

test/signal_test.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,13 @@ void test_move()
339339
signal_type sig2(dummy_combiner(2));
340340
BOOST_CHECK(sig2() == 2);
341341

342+
BOOST_CHECK(sig2.null() == false);
342343
sig1 = std::move(sig2);
343344
BOOST_CHECK(sig1() == 2);
345+
BOOST_CHECK(sig2.null() == true);
346+
BOOST_CHECK(sig2.empty() == true);
347+
BOOST_CHECK(sig2.num_slots() == 0);
348+
sig2.disconnect_all_slots();
344349

345350
signal_type sig3(std::move(sig1));
346351
BOOST_CHECK(sig3() == 2);

0 commit comments

Comments
 (0)