blob: 703a745b6393fa2442c742b2d5067791eae06e8d [file] [log] [blame]
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento3a3e1882018-07-17 14:49:15 -04002/*
3 * Copyright (c) 2013-2018 Regents of the University of California.
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -08004 *
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_SCHEDULER_SCOPED_EVENT_ID_HPP
23#define NDN_UTIL_SCHEDULER_SCOPED_EVENT_ID_HPP
24
25#include "scheduler.hpp"
26
27namespace ndn {
28namespace util {
29namespace scheduler {
30
Davide Pesavento3a3e1882018-07-17 14:49:15 -040031/** \brief Event that is automatically cancelled upon destruction.
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -080032 */
Davide Pesavento3a3e1882018-07-17 14:49:15 -040033class ScopedEventId
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -080034{
35public:
Davide Pesavento3a3e1882018-07-17 14:49:15 -040036 /** \brief Construct ScopedEventId tied to the specified scheduler.
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -080037 * \param scheduler Scheduler to which the event is tied. Behavior is undefined if
Davide Pesavento3a3e1882018-07-17 14:49:15 -040038 * \p scheduler is destructed before an uncanceled ScopedEventId.
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -080039 */
40 explicit
Davide Pesavento3a3e1882018-07-17 14:49:15 -040041 ScopedEventId(Scheduler& scheduler) noexcept;
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -080042
Davide Pesavento3a3e1882018-07-17 14:49:15 -040043 ScopedEventId(const ScopedEventId&) = delete;
44
45 ScopedEventId&
46 operator=(const ScopedEventId&) = delete;
47
48 /** \brief Move constructor.
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -080049 */
Davide Pesavento3a3e1882018-07-17 14:49:15 -040050 ScopedEventId(ScopedEventId&&) noexcept;
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -080051
Davide Pesavento3a3e1882018-07-17 14:49:15 -040052 /** \brief Move assignment operator.
53 */
54 ScopedEventId&
55 operator=(ScopedEventId&&) noexcept;
56
57 /** \brief Assign an event.
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -080058 *
59 * If a different event has been assigned to this instance previously,
60 * that event will be cancelled immediately.
61 *
Davide Pesavento3a3e1882018-07-17 14:49:15 -040062 * \note The caller should ensure that this ScopedEventId is tied to the correct Scheduler.
63 * Behavior is undefined when assigning an event scheduled in another Scheduler instance.
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -080064 */
65 ScopedEventId&
Davide Pesavento3a3e1882018-07-17 14:49:15 -040066 operator=(EventId event);
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -080067
Davide Pesavento3a3e1882018-07-17 14:49:15 -040068 /** \brief Destructor, automatically cancels the event.
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -080069 */
Davide Pesavento3a3e1882018-07-17 14:49:15 -040070 ~ScopedEventId();
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -080071
Davide Pesavento3a3e1882018-07-17 14:49:15 -040072 /** \brief Manually cancel the event.
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -080073 */
74 void
75 cancel();
76
Davide Pesavento3a3e1882018-07-17 14:49:15 -040077 /** \brief Release the event so that it won't be canceled when this ScopedEventId is destructed.
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -080078 */
79 void
Alexander Afanasyevce7b0622016-01-04 10:47:46 -080080 release() noexcept;
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -080081
82private:
83 Scheduler* m_scheduler; // pointer to allow move semantics
84 EventId m_event;
85};
86
Davide Pesavento3a3e1882018-07-17 14:49:15 -040087inline
88ScopedEventId::ScopedEventId(ScopedEventId&&) noexcept = default;
89
90inline ScopedEventId&
91ScopedEventId::operator=(ScopedEventId&&) noexcept = default;
92
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -080093} // namespace scheduler
94} // namespace util
95} // namespace ndn
96
97#endif // NDN_UTIL_SCHEDULER_SCOPED_EVENT_ID_HPP