blob: 528dcba92def381bbcfaf3eaff58c6655c5cf220 [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_CONNECTION_HPP
23#define NDN_UTIL_SIGNAL_CONNECTION_HPP
24
Junxiao Shi5722cfe2017-07-05 18:52:01 +000025#include "../../common.hpp"
Junxiao Shi8d71fdb2014-12-07 21:55:19 -070026
27namespace ndn {
28namespace util {
29namespace signal {
30
31/** \brief represents a connection to a signal
32 * \note This type is copyable. Any copy can be used to disconnect.
33 */
34class Connection
35{
36public:
Davide Pesavento3a3e1882018-07-17 14:49:15 -040037 constexpr
38 Connection() noexcept = default;
Junxiao Shi8d71fdb2014-12-07 21:55:19 -070039
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 Fanf46482c2015-02-03 16:55:53 -070049 /** \brief check if connected to the signal
50 * \return false if disconnected from the signal
51 */
52 bool
Davide Pesavento3a3e1882018-07-17 14:49:15 -040053 isConnected() const noexcept;
Chengyu Fanf46482c2015-02-03 16:55:53 -070054
Junxiao Shi8d71fdb2014-12-07 21:55:19 -070055 /** \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
66private:
67 /** \param disconnect weak_ptr to a function that disconnects the handler
68 */
69 explicit
Davide Pesavento3a3e1882018-07-17 14:49:15 -040070 Connection(weak_ptr<function<void()>> disconnect) noexcept;
Junxiao Shi8d71fdb2014-12-07 21:55:19 -070071
72 template<typename Owner, typename ...TArgs>
73 friend class Signal;
74
75private:
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