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-2024 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_DETAIL_CANCEL_HANDLE_HPP
23 #define NDN_CXX_DETAIL_CANCEL_HANDLE_HPP
24 
25 #include <functional>
26 #include <type_traits>
27 #include <utility>
28 
32 namespace ndn::detail {
33 
38 {
39 public:
40  CancelHandle() noexcept;
41 
42  explicit
43  CancelHandle(std::function<void()> cancel) noexcept
44  : m_cancel(std::move(cancel))
45  {
46  }
47 
50  void
51  cancel() const;
52 
53 private:
54  mutable std::function<void()> m_cancel;
55 };
56 
57 inline
58 CancelHandle::CancelHandle() noexcept = default;
59 
63 template<typename HandleT>
65 {
66  static_assert(std::is_convertible_v<HandleT*, CancelHandle*>,
67  "HandleT must publicly derive from CancelHandle");
68 
69 public:
70  ScopedCancelHandle() noexcept;
71 
74  ScopedCancelHandle(HandleT hdl) noexcept
75  : m_hdl(std::move(hdl))
76  {
77  }
78 
82 
86  : m_hdl(other.release())
87  {
88  }
89 
93  operator=(const ScopedCancelHandle&) = delete;
94 
99  {
100  m_hdl.cancel();
101  m_hdl = other.release();
102  return *this;
103  }
104 
108  {
109  m_hdl.cancel();
110  }
111 
114  void
116  {
117  release().cancel();
118  }
119 
123  HandleT
124  release() noexcept
125  {
126  return std::exchange(m_hdl, HandleT{});
127  }
128 
129  explicit
130  operator bool() const noexcept
131  {
132  return !!m_hdl;
133  }
134 
135 private:
136  HandleT m_hdl;
137 };
138 
139 template<typename T>
140 ScopedCancelHandle<T>::ScopedCancelHandle() noexcept = default;
141 
142 } // namespace ndn::detail
143 
144 #endif // NDN_CXX_DETAIL_CANCEL_HANDLE_HPP
Handle to cancel an operation.
void cancel() const
Cancel the operation.
Cancels an operation automatically upon destruction.
~ScopedCancelHandle()
Cancel the operation.
ScopedCancelHandle(const ScopedCancelHandle &)=delete
Copy construction is disallowed.
ScopedCancelHandle(ScopedCancelHandle &&other) noexcept
Move constructor.
HandleT release() noexcept
Release the operation so that it won't be cancelled when this ScopedCancelHandle is destructed.
ScopedCancelHandle & operator=(ScopedCancelHandle &&other)
Move assignment operator.
ScopedCancelHandle & operator=(const ScopedCancelHandle &)=delete
Copy assignment is disallowed.
void cancel()
Cancel the operation.
Contains implementation details that are not part of the ndn-cxx public API.