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-2021 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 
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_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.
Common includes and macros used throughout the library.
Definition: data.cpp:25