blob: 1276182883f81d14cc1a5d9ae14c95a243b4af0f [file] [log] [blame]
Junxiao Shi8d71fdb2014-12-07 21:55:19 -07001/* -*- 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
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:
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
48 /** \brief compare for equality
49 *
50 * Two connections are equal if they both refer to the same connection that isn't disconnected,
51 * or they are both disconnected.
52 */
53 bool
54 operator==(const Connection& other) const;
55
56 bool
57 operator!=(const Connection& other) const;
58
59private:
60 /** \param disconnect weak_ptr to a function that disconnects the handler
61 */
62 explicit
63 Connection(weak_ptr<function<void()>> disconnect);
64
65 template<typename Owner, typename ...TArgs>
66 friend class Signal;
67
68private:
69 /** \note The only shared_ptr to the disconnect function is stored in Signal<..>::Slot,
70 * and will be destructed if the handler is disconnected (through another Connection
71 * instance) or the Signal is destructed.
72 * Connection needs a weak_ptr instead of a shared_ptr to the disconnect function,
73 * because the disconnect function is bound with an iterator to the Slot,
74 * which is invalidated when the Slot is erased.
75 */
76 weak_ptr<function<void()>> m_disconnect;
77};
78
79} // namespace signal
80} // namespace util
81} // namespace ndn
82
83#endif // NDN_UTIL_SIGNAL_CONNECTION_HPP