Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 5722cfe | 2017-07-05 18:52:01 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | 3a3e188 | 2018-07-17 14:49:15 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2018 Regents of the University of California. |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 4 | * |
| 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 Shi | 5722cfe | 2017-07-05 18:52:01 +0000 | [diff] [blame] | 25 | #include "connection.hpp" |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 26 | |
| 27 | namespace ndn { |
| 28 | namespace util { |
| 29 | namespace signal { |
| 30 | |
Davide Pesavento | 3a3e188 | 2018-07-17 14:49:15 -0400 | [diff] [blame] | 31 | /** \brief Disconnects a Connection automatically upon destruction. |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 32 | */ |
Davide Pesavento | 3a3e188 | 2018-07-17 14:49:15 -0400 | [diff] [blame] | 33 | class ScopedConnection |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 34 | { |
| 35 | public: |
Davide Pesavento | 3a3e188 | 2018-07-17 14:49:15 -0400 | [diff] [blame] | 36 | constexpr |
| 37 | ScopedConnection() noexcept = default; |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 38 | |
Davide Pesavento | 3a3e188 | 2018-07-17 14:49:15 -0400 | [diff] [blame] | 39 | 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 Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 54 | * \param connection the Connection to be disconnected upon destruction |
| 55 | */ |
Davide Pesavento | 3a3e188 | 2018-07-17 14:49:15 -0400 | [diff] [blame] | 56 | ScopedConnection(Connection connection) noexcept; |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 57 | |
Davide Pesavento | 3a3e188 | 2018-07-17 14:49:15 -0400 | [diff] [blame] | 58 | /** \brief Assign a connection. |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 59 | * |
| 60 | * If a different connection has been assigned to this instance previously, |
| 61 | * that connection will be disconnected immediately. |
| 62 | */ |
| 63 | ScopedConnection& |
Davide Pesavento | 3a3e188 | 2018-07-17 14:49:15 -0400 | [diff] [blame] | 64 | operator=(Connection connection); |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 65 | |
Davide Pesavento | 3a3e188 | 2018-07-17 14:49:15 -0400 | [diff] [blame] | 66 | /** \brief Destructor, automatically disconnects the connection. |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 67 | */ |
Davide Pesavento | 3a3e188 | 2018-07-17 14:49:15 -0400 | [diff] [blame] | 68 | ~ScopedConnection(); |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 69 | |
Davide Pesavento | 3a3e188 | 2018-07-17 14:49:15 -0400 | [diff] [blame] | 70 | /** \brief Manually disconnect the connection. |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 71 | */ |
| 72 | void |
| 73 | disconnect(); |
| 74 | |
Davide Pesavento | 3a3e188 | 2018-07-17 14:49:15 -0400 | [diff] [blame] | 75 | /** \brief Check if the connection is connected to the signal. |
Chengyu Fan | f46482c | 2015-02-03 16:55:53 -0700 | [diff] [blame] | 76 | * \return false when a default-constructed connection is used, the connection is released, |
| 77 | * or the connection is disconnected |
| 78 | */ |
| 79 | bool |
Davide Pesavento | 3a3e188 | 2018-07-17 14:49:15 -0400 | [diff] [blame] | 80 | isConnected() const noexcept; |
Chengyu Fan | f46482c | 2015-02-03 16:55:53 -0700 | [diff] [blame] | 81 | |
Davide Pesavento | 3a3e188 | 2018-07-17 14:49:15 -0400 | [diff] [blame] | 82 | /** \brief Release the connection so that it won't be disconnected |
| 83 | * when this ScopedConnection is destructed. |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 84 | */ |
| 85 | void |
Davide Pesavento | 3a3e188 | 2018-07-17 14:49:15 -0400 | [diff] [blame] | 86 | release() noexcept; |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 87 | |
| 88 | private: |
| 89 | Connection m_connection; |
| 90 | }; |
| 91 | |
Davide Pesavento | 3a3e188 | 2018-07-17 14:49:15 -0400 | [diff] [blame] | 92 | inline |
| 93 | ScopedConnection::ScopedConnection(ScopedConnection&&) noexcept = default; |
| 94 | |
| 95 | inline ScopedConnection& |
| 96 | ScopedConnection::operator=(ScopedConnection&&) noexcept = default; |
| 97 | |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 98 | } // namespace signal |
| 99 | } // namespace util |
| 100 | } // namespace ndn |
| 101 | |
| 102 | #endif // NDN_UTIL_SIGNAL_SCOPED_CONNECTION_HPP |