swift
connectionguard.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2015 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
4 #include "misc/connectionguard.h"
5 
6 #include <utility> // for as_const
7 
8 #include <QObject>
9 
10 namespace swift::misc
11 {
12  CConnectionGuard::CConnectionGuard(const QMetaObject::Connection &connection) { m_connections.append(connection); }
13 
14  bool CConnectionGuard::append(const QMetaObject::Connection &connection)
15  {
16  if (!connection) { return false; }
17  m_connections.append(connection);
18  return true;
19  }
20 
21  bool CConnectionGuard::append(const QList<QMetaObject::Connection> &connections)
22  {
23  int c = 0;
24  for (const QMetaObject::Connection &connection : connections)
25  {
26  if (!connection) { continue; }
27  if (this->append(connection)) { c++; }
28  }
29  return c > 0;
30  }
31 
33 
35  {
36  if (m_connections.isEmpty()) { return 0; }
37  int c = 0;
38  for (const QMetaObject::Connection &con : std::as_const(m_connections))
39  {
40  if (QObject::disconnect(con)) { c++; }
41  }
42  m_connections.clear();
43  return c;
44  }
45 } // namespace swift::misc
CConnectionGuard()=default
Constructor.
bool append(const QMetaObject::Connection &connection)
Add connection.
int disconnectAll()
Disconnect all.
Free functions in swift::misc.