connection.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2022 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_CXX_UTIL_SIGNAL_CONNECTION_HPP
23 #define NDN_CXX_UTIL_SIGNAL_CONNECTION_HPP
24 
26 
27 namespace ndn {
28 namespace util {
29 namespace signal {
30 
31 using DisconnectFunction = std::function<void()>;
32 
37 {
38 public:
39  constexpr
40  Connection() noexcept = default;
41 
48  void
49  disconnect();
50 
53  bool
54  isConnected() const noexcept
55  {
56  return !m_disconnect.expired();
57  }
58 
59 private:
62  explicit
63  Connection(weak_ptr<DisconnectFunction> disconnect) noexcept;
64 
65  template<typename Owner, typename ...TArgs>
66  friend class Signal;
67 
68 private:
69  // NOTE: the following "hidden friend" operators are available via
70  // argument-dependent lookup only and must be defined inline.
71 
77  friend bool
78  operator==(const Connection& lhs, const Connection& rhs) noexcept
79  {
80  return (!lhs.isConnected() && !rhs.isConnected()) ||
81  (!lhs.m_disconnect.owner_before(rhs.m_disconnect) &&
82  !rhs.m_disconnect.owner_before(lhs.m_disconnect));
83  }
84 
85  friend bool
86  operator!=(const Connection& lhs, const Connection& rhs) noexcept
87  {
88  return !(lhs == rhs);
89  }
90 
91 private:
99  weak_ptr<DisconnectFunction> m_disconnect;
100 };
101 
102 } // namespace signal
103 } // namespace util
104 } // namespace ndn
105 
106 #endif // NDN_CXX_UTIL_SIGNAL_CONNECTION_HPP
Represents a connection to a signal.
Definition: connection.hpp:37
constexpr Connection() noexcept=default
friend bool operator==(const Connection &lhs, const Connection &rhs) noexcept
Compare for equality.
Definition: connection.hpp:78
bool isConnected() const noexcept
Check if connected to the signal.
Definition: connection.hpp:54
void disconnect()
Disconnects from the signal.
Definition: connection.cpp:36
friend bool operator!=(const Connection &lhs, const Connection &rhs) noexcept
Definition: connection.hpp:86
Provides a lightweight signal / event system.
Definition: signal.hpp:53
Common includes and macros used throughout the library.
std::function< void()> DisconnectFunction
Definition: connection.hpp:31
Definition: data.cpp:25