Skip to content

Commit 62de012

Browse files
author
Leon Matthes
committed
Fix CI staticcheck warnings
- 2 instances of use-after-move - Missing #pragma once - Also move ScopedConnection into connection_handle.h
1 parent 0d2e8f5 commit 62de012

File tree

3 files changed

+111
-97
lines changed

3 files changed

+111
-97
lines changed

src/kdbindings/connection_handle.h

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
Contact KDAB at <info@kdab.com> for commercial licensing options.
1010
*/
1111

12+
#pragma once
13+
1214
#include <kdbindings/genindex_array.h>
1315
#include <kdbindings/utils.h>
1416
#include <memory>
@@ -206,4 +208,99 @@ class ConnectionHandle
206208
}
207209
};
208210

211+
/**
212+
* @brief A ScopedConnection is a RAII-style way to make sure a Connection is disconnected.
213+
*
214+
* When the ScopedConnections scope ends, the connection this ScopedConnection guards will be disconnected.
215+
*
216+
* Example:
217+
* - @ref 08-managing-connections/main.cpp
218+
*/
219+
class ScopedConnection
220+
{
221+
public:
222+
/**
223+
* @brief A ScopedConnection can be default constructed
224+
*
225+
* A default constructed ScopedConnection has no connection to guard.
226+
* Therefore it does nothing when it is destructed, unless a ConnectionHandle is assigned to it.
227+
*/
228+
ScopedConnection() = default;
229+
230+
/** A ScopedConnection can be move constructed */
231+
ScopedConnection(ScopedConnection &&) = default;
232+
233+
/** A ScopedConnection cannot be copied */
234+
ScopedConnection(const ScopedConnection &) = delete;
235+
/** A ScopedConnection cannot be copied */
236+
ScopedConnection &operator=(const ScopedConnection &) = delete;
237+
238+
/** A ScopedConnection can be move assigned */
239+
ScopedConnection &operator=(ScopedConnection &&other)
240+
{
241+
m_connection.disconnect();
242+
m_connection = std::move(other.m_connection);
243+
return *this;
244+
}
245+
246+
/**
247+
* A ScopedConnection can be constructed from a ConnectionHandle
248+
*/
249+
ScopedConnection(ConnectionHandle &&h)
250+
: m_connection(std::move(h))
251+
{
252+
}
253+
254+
/**
255+
* A ScopedConnection can be assigned from a ConnectionHandle
256+
*/
257+
ScopedConnection &operator=(ConnectionHandle &&h)
258+
{
259+
return *this = ScopedConnection(std::move(h));
260+
}
261+
262+
/**
263+
* @return the handle to the connection this instance is managing
264+
*/
265+
ConnectionHandle &handle()
266+
{
267+
return m_connection;
268+
}
269+
270+
/**
271+
* @overload
272+
*/
273+
const ConnectionHandle &handle() const
274+
{
275+
return m_connection;
276+
}
277+
278+
/**
279+
* Convenience access to the underlying ConnectionHandle using the `->` operator.
280+
*/
281+
ConnectionHandle *operator->()
282+
{
283+
return &m_connection;
284+
}
285+
286+
/**
287+
* @overload
288+
*/
289+
const ConnectionHandle *operator->() const
290+
{
291+
return &m_connection;
292+
}
293+
294+
/**
295+
* When a ConnectionHandle is destructed it disconnects the connection it guards.
296+
*/
297+
~ScopedConnection()
298+
{
299+
m_connection.disconnect();
300+
}
301+
302+
private:
303+
ConnectionHandle m_connection;
304+
};
305+
209306
} // namespace KDBindings

src/kdbindings/signal.h

Lines changed: 0 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -460,101 +460,6 @@ class Signal
460460
mutable std::shared_ptr<Impl> m_impl;
461461
};
462462

463-
/**
464-
* @brief A ScopedConnection is a RAII-style way to make sure a Connection is disconnected.
465-
*
466-
* When the ScopedConnections scope ends, the connection this ScopedConnection guards will be disconnected.
467-
*
468-
* Example:
469-
* - @ref 08-managing-connections/main.cpp
470-
*/
471-
class ScopedConnection
472-
{
473-
public:
474-
/**
475-
* @brief A ScopedConnection can be default constructed
476-
*
477-
* A default constructed ScopedConnection has no connection to guard.
478-
* Therefore it does nothing when it is destructed, unless a ConnectionHandle is assigned to it.
479-
*/
480-
ScopedConnection() = default;
481-
482-
/** A ScopedConnection can be move constructed */
483-
ScopedConnection(ScopedConnection &&) = default;
484-
485-
/** A ScopedConnection cannot be copied */
486-
ScopedConnection(const ScopedConnection &) = delete;
487-
/** A ScopedConnection cannot be copied */
488-
ScopedConnection &operator=(const ScopedConnection &) = delete;
489-
490-
/** A ScopedConnection can be move assigned */
491-
ScopedConnection &operator=(ScopedConnection &&other)
492-
{
493-
m_connection.disconnect();
494-
m_connection = std::move(other.m_connection);
495-
return *this;
496-
}
497-
498-
/**
499-
* A ScopedConnection can be constructed from a ConnectionHandle
500-
*/
501-
ScopedConnection(ConnectionHandle &&h)
502-
: m_connection(std::move(h))
503-
{
504-
}
505-
506-
/**
507-
* A ScopedConnection can be assigned from a ConnectionHandle
508-
*/
509-
ScopedConnection &operator=(ConnectionHandle &&h)
510-
{
511-
return *this = ScopedConnection(std::move(h));
512-
}
513-
514-
/**
515-
* @return the handle to the connection this instance is managing
516-
*/
517-
ConnectionHandle &handle()
518-
{
519-
return m_connection;
520-
}
521-
522-
/**
523-
* @overload
524-
*/
525-
const ConnectionHandle &handle() const
526-
{
527-
return m_connection;
528-
}
529-
530-
/**
531-
* Convenience access to the underlying ConnectionHandle using the `->` operator.
532-
*/
533-
ConnectionHandle *operator->()
534-
{
535-
return &m_connection;
536-
}
537-
538-
/**
539-
* @overload
540-
*/
541-
const ConnectionHandle *operator->() const
542-
{
543-
return &m_connection;
544-
}
545-
546-
/**
547-
* When a ConnectionHandle is destructed it disconnects the connection it guards.
548-
*/
549-
~ScopedConnection()
550-
{
551-
m_connection.disconnect();
552-
}
553-
554-
private:
555-
ConnectionHandle m_connection;
556-
};
557-
558463
/**
559464
* @brief A ConnectionBlocker is a convenient RAII-style mechanism for temporarily blocking a connection.
560465
*

tests/signal/tst_signal.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,14 @@ TEST_CASE("ScopedConnection")
707707

708708
// This should drop the old connection of guard1
709709
guard1 = std::move(guard2);
710-
CHECK(!guard2->isActive());
710+
// Ideally we'd like to assert here that:
711+
// CHECK(!guard2->isActive());
712+
// However, this is not possible, as a moved-from ScopedConnection is
713+
// undefined (as is any C++ object for that matter).
714+
// But because we assert that `guard1` is still active after the scope
715+
// ends and that numCalled is only 1 after the emit, we can be sure that
716+
// `guard2` was moved from and didn't disconnect.
717+
711718
CHECK(guard1->isActive());
712719

713720
signal.emit();
@@ -728,7 +735,12 @@ TEST_CASE("ScopedConnection")
728735
ScopedConnection guard = signal.connect([&called]() { called = true; });
729736
REQUIRE(guard->isActive());
730737
into = std::move(guard);
731-
REQUIRE_FALSE(guard->isActive());
738+
// Ideally we'd like to assert here that:
739+
// REQUIRE_FALSE(guard->isActive());
740+
// However, this is not possible, as a moved-from ScopedConnection is
741+
// undefined (as is any C++ object for that matter).
742+
// But because we assert that `into` is still active after the scope
743+
// ends, we can be sure that `guard` was moved from and didn't disconnect.
732744
}
733745
REQUIRE(into->isActive());
734746

0 commit comments

Comments
 (0)