-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 2 instances of use-after-move - Missing #pragma once - Also move ScopedConnection into connection_handle.h
- Loading branch information
1 parent
86d6bd5
commit d1904cf
Showing
3 changed files
with
111 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,8 @@ | |
Contact KDAB at <[email protected]> for commercial licensing options. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <kdbindings/genindex_array.h> | ||
#include <kdbindings/utils.h> | ||
#include <memory> | ||
|
@@ -206,4 +208,99 @@ class ConnectionHandle | |
} | ||
}; | ||
|
||
/** | ||
* @brief A ScopedConnection is a RAII-style way to make sure a Connection is disconnected. | ||
* | ||
* When the ScopedConnections scope ends, the connection this ScopedConnection guards will be disconnected. | ||
* | ||
* Example: | ||
* - @ref 08-managing-connections/main.cpp | ||
*/ | ||
class ScopedConnection | ||
{ | ||
public: | ||
/** | ||
* @brief A ScopedConnection can be default constructed | ||
* | ||
* A default constructed ScopedConnection has no connection to guard. | ||
* Therefore it does nothing when it is destructed, unless a ConnectionHandle is assigned to it. | ||
*/ | ||
ScopedConnection() = default; | ||
|
||
/** A ScopedConnection can be move constructed */ | ||
ScopedConnection(ScopedConnection &&) = default; | ||
|
||
/** A ScopedConnection cannot be copied */ | ||
ScopedConnection(const ScopedConnection &) = delete; | ||
/** A ScopedConnection cannot be copied */ | ||
ScopedConnection &operator=(const ScopedConnection &) = delete; | ||
|
||
/** A ScopedConnection can be move assigned */ | ||
ScopedConnection &operator=(ScopedConnection &&other) | ||
{ | ||
m_connection.disconnect(); | ||
m_connection = std::move(other.m_connection); | ||
return *this; | ||
} | ||
|
||
/** | ||
* A ScopedConnection can be constructed from a ConnectionHandle | ||
*/ | ||
ScopedConnection(ConnectionHandle &&h) | ||
: m_connection(std::move(h)) | ||
{ | ||
} | ||
|
||
/** | ||
* A ScopedConnection can be assigned from a ConnectionHandle | ||
*/ | ||
ScopedConnection &operator=(ConnectionHandle &&h) | ||
{ | ||
return *this = ScopedConnection(std::move(h)); | ||
} | ||
|
||
/** | ||
* @return the handle to the connection this instance is managing | ||
*/ | ||
ConnectionHandle &handle() | ||
{ | ||
return m_connection; | ||
} | ||
|
||
/** | ||
* @overload | ||
*/ | ||
const ConnectionHandle &handle() const | ||
{ | ||
return m_connection; | ||
} | ||
|
||
/** | ||
* Convenience access to the underlying ConnectionHandle using the `->` operator. | ||
*/ | ||
ConnectionHandle *operator->() | ||
{ | ||
return &m_connection; | ||
} | ||
|
||
/** | ||
* @overload | ||
*/ | ||
const ConnectionHandle *operator->() const | ||
{ | ||
return &m_connection; | ||
} | ||
|
||
/** | ||
* When a ConnectionHandle is destructed it disconnects the connection it guards. | ||
*/ | ||
~ScopedConnection() | ||
{ | ||
m_connection.disconnect(); | ||
} | ||
|
||
private: | ||
ConnectionHandle m_connection; | ||
}; | ||
|
||
} // namespace KDBindings |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters