Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2014 Regents of the University of California. |
| 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 | |
| 25 | #include "../common.hpp" |
| 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: |
| 37 | Connection(); |
| 38 | |
| 39 | /** \brief disconnects from the signal |
| 40 | * \note If the connection is already disconnected, or if the Signal has been destructed, |
| 41 | * this operation has no effect. |
| 42 | * \warning During signal emission, attempting to disconnect a connection other than |
| 43 | * the executing handler's own connection results in undefined behavior. |
| 44 | */ |
| 45 | void |
| 46 | disconnect(); |
| 47 | |
Chengyu Fan | f46482c | 2015-02-03 16:55:53 -0700 | [diff] [blame] | 48 | /** \brief check if connected to the signal |
| 49 | * \return false if disconnected from the signal |
| 50 | */ |
| 51 | bool |
| 52 | isConnected() const; |
| 53 | |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 54 | /** \brief compare for equality |
| 55 | * |
| 56 | * Two connections are equal if they both refer to the same connection that isn't disconnected, |
| 57 | * or they are both disconnected. |
| 58 | */ |
| 59 | bool |
| 60 | operator==(const Connection& other) const; |
| 61 | |
| 62 | bool |
| 63 | operator!=(const Connection& other) const; |
| 64 | |
| 65 | private: |
| 66 | /** \param disconnect weak_ptr to a function that disconnects the handler |
| 67 | */ |
| 68 | explicit |
| 69 | Connection(weak_ptr<function<void()>> disconnect); |
| 70 | |
| 71 | template<typename Owner, typename ...TArgs> |
| 72 | friend class Signal; |
| 73 | |
| 74 | private: |
| 75 | /** \note The only shared_ptr to the disconnect function is stored in Signal<..>::Slot, |
| 76 | * and will be destructed if the handler is disconnected (through another Connection |
| 77 | * instance) or the Signal is destructed. |
| 78 | * Connection needs a weak_ptr instead of a shared_ptr to the disconnect function, |
| 79 | * because the disconnect function is bound with an iterator to the Slot, |
| 80 | * which is invalidated when the Slot is erased. |
| 81 | */ |
| 82 | weak_ptr<function<void()>> m_disconnect; |
| 83 | }; |
| 84 | |
| 85 | } // namespace signal |
| 86 | } // namespace util |
| 87 | } // namespace ndn |
| 88 | |
| 89 | #endif // NDN_UTIL_SIGNAL_CONNECTION_HPP |