NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
cancel-handle.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2019 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_DETAIL_CANCEL_HANDLE_HPP
23 #define NDN_DETAIL_CANCEL_HANDLE_HPP
24 
26 
27 namespace ndn {
28 namespace detail {
29 
33 {
34 public:
35  CancelHandle() noexcept;
36 
37  explicit
38  CancelHandle(std::function<void()> cancel) noexcept
39  : m_cancel(std::move(cancel))
40  {
41  }
42 
45  void
46  cancel() const;
47 
48 private:
49  mutable std::function<void()> m_cancel;
50 };
51 
52 inline
53 CancelHandle::CancelHandle() noexcept = default;
54 
57 template<typename HandleT>
59 {
60  static_assert(std::is_convertible<HandleT*, CancelHandle*>::value,
61  "HandleT must publicly derive from CancelHandle");
62 
63 public:
64  ScopedCancelHandle() noexcept;
65 
68  ScopedCancelHandle(HandleT hdl) noexcept
69  : m_hdl(std::move(hdl))
70  {
71  }
72 
76 
80  : m_hdl(other.release())
81  {
82  }
83 
87  operator=(const ScopedCancelHandle&) = delete;
88 
93  {
94  m_hdl.cancel();
95  m_hdl = other.release();
96  return *this;
97  }
98 
102  {
103  m_hdl.cancel();
104  }
105 
108  void
110  {
111  release().cancel();
112  }
113 
117  HandleT
118  release() noexcept
119  {
120  return std::exchange(m_hdl, HandleT{});
121  }
122 
123  explicit
124  operator bool() const noexcept
125  {
126  return !!m_hdl;
127  }
128 
129 private:
130  HandleT m_hdl;
131 };
132 
133 template<typename T>
134 ScopedCancelHandle<T>::ScopedCancelHandle() noexcept = default;
135 
136 } // namespace detail
137 } // namespace ndn
138 
139 #endif // NDN_DETAIL_CANCEL_HANDLE_HPP
common.hpp
Common includes and macros used throughout the library.
ndn::detail::ScopedCancelHandle::cancel
void cancel()
Cancel the operation.
Definition: cancel-handle.hpp:109
nonstd::optional_lite::std11::move
T & move(T &t)
Definition: optional.hpp:421
ndn::detail::ScopedCancelHandle::operator=
ScopedCancelHandle & operator=(const ScopedCancelHandle &)=delete
Copy assignment is disallowed.
ndn::detail::ScopedCancelHandle::operator=
ScopedCancelHandle & operator=(ScopedCancelHandle &&other)
Move assignment operator.
Definition: cancel-handle.hpp:92
ndn::detail::ScopedCancelHandle::ScopedCancelHandle
ScopedCancelHandle() noexcept
ndn::detail::CancelHandle
Handle to cancel an operation.
Definition: cancel-handle.hpp:33
ndn::detail::CancelHandle::cancel
void cancel() const
Cancel the operation.
Definition: cancel-handle.cpp:28
ndn::detail::ScopedCancelHandle::ScopedCancelHandle
ScopedCancelHandle(ScopedCancelHandle &&other) noexcept
Move constructor.
Definition: cancel-handle.hpp:79
ndn::detail::ScopedCancelHandle::~ScopedCancelHandle
~ScopedCancelHandle()
Cancel the operation.
Definition: cancel-handle.hpp:101
ndn::detail::ScopedCancelHandle::ScopedCancelHandle
ScopedCancelHandle(const ScopedCancelHandle &)=delete
Copy construction is disallowed.
ndn::detail::CancelHandle::CancelHandle
CancelHandle() noexcept
ndn::detail::ScopedCancelHandle::release
HandleT release() noexcept
Release the operation so that it won't be cancelled when this ScopedCancelHandle is destructed.
Definition: cancel-handle.hpp:118
ndn
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-strategy-choice-helper.hpp:34
ndn::detail::ScopedCancelHandle
Cancels an operation automatically upon destruction.
Definition: cancel-handle.hpp:59