blob: f352be08bb28dced96c278a8bfc6e247b65b35aa [file] [log] [blame]
Junxiao Shi8d71fdb2014-12-07 21:55:19 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi5722cfe2017-07-05 18:52:01 +00002/*
Davide Pesavento3a3e1882018-07-17 14:49:15 -04003 * Copyright (c) 2013-2018 Regents of the University of California.
Junxiao Shi8d71fdb2014-12-07 21:55:19 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
22#ifndef NDN_UTIL_SIGNAL_SCOPED_CONNECTION_HPP
23#define NDN_UTIL_SIGNAL_SCOPED_CONNECTION_HPP
24
Junxiao Shi5722cfe2017-07-05 18:52:01 +000025#include "connection.hpp"
Junxiao Shi8d71fdb2014-12-07 21:55:19 -070026
27namespace ndn {
28namespace util {
29namespace signal {
30
Davide Pesavento3a3e1882018-07-17 14:49:15 -040031/** \brief Disconnects a Connection automatically upon destruction.
Junxiao Shi8d71fdb2014-12-07 21:55:19 -070032 */
Davide Pesavento3a3e1882018-07-17 14:49:15 -040033class ScopedConnection
Junxiao Shi8d71fdb2014-12-07 21:55:19 -070034{
35public:
Davide Pesavento3a3e1882018-07-17 14:49:15 -040036 constexpr
37 ScopedConnection() noexcept = default;
Junxiao Shi8d71fdb2014-12-07 21:55:19 -070038
Davide Pesavento3a3e1882018-07-17 14:49:15 -040039 ScopedConnection(const ScopedConnection&) = delete;
40
41 ScopedConnection&
42 operator=(const ScopedConnection&) = delete;
43
44 /** \brief Move constructor.
45 */
46 ScopedConnection(ScopedConnection&&) noexcept;
47
48 /** \brief Move assignment operator.
49 */
50 ScopedConnection&
51 operator=(ScopedConnection&&) noexcept;
52
53 /** \brief Implicit constructor from Connection.
Junxiao Shi8d71fdb2014-12-07 21:55:19 -070054 * \param connection the Connection to be disconnected upon destruction
55 */
Davide Pesavento3a3e1882018-07-17 14:49:15 -040056 ScopedConnection(Connection connection) noexcept;
Junxiao Shi8d71fdb2014-12-07 21:55:19 -070057
Davide Pesavento3a3e1882018-07-17 14:49:15 -040058 /** \brief Assign a connection.
Junxiao Shi8d71fdb2014-12-07 21:55:19 -070059 *
60 * If a different connection has been assigned to this instance previously,
61 * that connection will be disconnected immediately.
62 */
63 ScopedConnection&
Davide Pesavento3a3e1882018-07-17 14:49:15 -040064 operator=(Connection connection);
Junxiao Shi8d71fdb2014-12-07 21:55:19 -070065
Davide Pesavento3a3e1882018-07-17 14:49:15 -040066 /** \brief Destructor, automatically disconnects the connection.
Junxiao Shi8d71fdb2014-12-07 21:55:19 -070067 */
Davide Pesavento3a3e1882018-07-17 14:49:15 -040068 ~ScopedConnection();
Junxiao Shi8d71fdb2014-12-07 21:55:19 -070069
Davide Pesavento3a3e1882018-07-17 14:49:15 -040070 /** \brief Manually disconnect the connection.
Junxiao Shi8d71fdb2014-12-07 21:55:19 -070071 */
72 void
73 disconnect();
74
Davide Pesavento3a3e1882018-07-17 14:49:15 -040075 /** \brief Check if the connection is connected to the signal.
Chengyu Fanf46482c2015-02-03 16:55:53 -070076 * \return false when a default-constructed connection is used, the connection is released,
77 * or the connection is disconnected
78 */
79 bool
Davide Pesavento3a3e1882018-07-17 14:49:15 -040080 isConnected() const noexcept;
Chengyu Fanf46482c2015-02-03 16:55:53 -070081
Davide Pesavento3a3e1882018-07-17 14:49:15 -040082 /** \brief Release the connection so that it won't be disconnected
83 * when this ScopedConnection is destructed.
Junxiao Shi8d71fdb2014-12-07 21:55:19 -070084 */
85 void
Davide Pesavento3a3e1882018-07-17 14:49:15 -040086 release() noexcept;
Junxiao Shi8d71fdb2014-12-07 21:55:19 -070087
88private:
89 Connection m_connection;
90};
91
Davide Pesavento3a3e1882018-07-17 14:49:15 -040092inline
93ScopedConnection::ScopedConnection(ScopedConnection&&) noexcept = default;
94
95inline ScopedConnection&
96ScopedConnection::operator=(ScopedConnection&&) noexcept = default;
97
Junxiao Shi8d71fdb2014-12-07 21:55:19 -070098} // namespace signal
99} // namespace util
100} // namespace ndn
101
102#endif // NDN_UTIL_SIGNAL_SCOPED_CONNECTION_HPP