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_CONNECTION_HPP |
| 23 | #define NDN_UTIL_SIGNAL_CONNECTION_HPP |
| 24 | |
Junxiao Shi | 5722cfe | 2017-07-05 18:52:01 +0000 | [diff] [blame] | 25 | #include "../../common.hpp" |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 26 | |
| 27 | namespace ndn { |
| 28 | namespace util { |
| 29 | namespace signal { |
| 30 | |
| 31 | /** \brief represents a connection to a signal |
| 32 | * \note This type is copyable. Any copy can be used to disconnect. |
| 33 | */ |
| 34 | class Connection |
| 35 | { |
| 36 | public: |
Davide Pesavento | 3a3e188 | 2018-07-17 14:49:15 -0400 | [diff] [blame^] | 37 | constexpr |
| 38 | Connection() noexcept = default; |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 39 | |
| 40 | /** \brief disconnects from the signal |
| 41 | * \note If the connection is already disconnected, or if the Signal has been destructed, |
| 42 | * this operation has no effect. |
| 43 | * \warning During signal emission, attempting to disconnect a connection other than |
| 44 | * the executing handler's own connection results in undefined behavior. |
| 45 | */ |
| 46 | void |
| 47 | disconnect(); |
| 48 | |
Chengyu Fan | f46482c | 2015-02-03 16:55:53 -0700 | [diff] [blame] | 49 | /** \brief check if connected to the signal |
| 50 | * \return false if disconnected from the signal |
| 51 | */ |
| 52 | bool |
Davide Pesavento | 3a3e188 | 2018-07-17 14:49:15 -0400 | [diff] [blame^] | 53 | isConnected() const noexcept; |
Chengyu Fan | f46482c | 2015-02-03 16:55:53 -0700 | [diff] [blame] | 54 | |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 55 | /** \brief compare for equality |
| 56 | * |
| 57 | * Two connections are equal if they both refer to the same connection that isn't disconnected, |
| 58 | * or they are both disconnected. |
| 59 | */ |
| 60 | bool |
| 61 | operator==(const Connection& other) const; |
| 62 | |
| 63 | bool |
| 64 | operator!=(const Connection& other) const; |
| 65 | |
| 66 | private: |
| 67 | /** \param disconnect weak_ptr to a function that disconnects the handler |
| 68 | */ |
| 69 | explicit |
Davide Pesavento | 3a3e188 | 2018-07-17 14:49:15 -0400 | [diff] [blame^] | 70 | Connection(weak_ptr<function<void()>> disconnect) noexcept; |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 71 | |
| 72 | template<typename Owner, typename ...TArgs> |
| 73 | friend class Signal; |
| 74 | |
| 75 | private: |
| 76 | /** \note The only shared_ptr to the disconnect function is stored in Signal<..>::Slot, |
| 77 | * and will be destructed if the handler is disconnected (through another Connection |
| 78 | * instance) or the Signal is destructed. |
| 79 | * Connection needs a weak_ptr instead of a shared_ptr to the disconnect function, |
| 80 | * because the disconnect function is bound with an iterator to the Slot, |
| 81 | * which is invalidated when the Slot is erased. |
| 82 | */ |
| 83 | weak_ptr<function<void()>> m_disconnect; |
| 84 | }; |
| 85 | |
| 86 | } // namespace signal |
| 87 | } // namespace util |
| 88 | } // namespace ndn |
| 89 | |
| 90 | #endif // NDN_UTIL_SIGNAL_CONNECTION_HPP |