Kiwano Engine v1.3.x
Task.h
1// Copyright (c) 2016-2018 Kiwano - Nomango
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19// THE SOFTWARE.
20
21#pragma once
22#include <kiwano/utils/Ticker.h>
23#include <kiwano/core/IntrusiveList.h>
24
25namespace kiwano
26{
27class TaskScheduler;
28
32class KGE_API Task
33 : public ObjectBase
34 , protected IntrusiveListValue<RefPtr<Task>>
35{
36 friend class TaskScheduler;
38
39public:
44 using Callback = Function<void(Task* /* self */, Duration /* dt */)>;
45
50 Task(const Callback& cb, RefPtr<Ticker> ticker);
51
57 Task(StringView name, const Callback& cb, RefPtr<Ticker> ticker);
58
64 Task(const Callback& cb, Duration interval, int times = -1);
65
72 Task(StringView name, const Callback& cb, Duration interval, int times = -1);
73
76 Task();
77
80 void Start();
81
84 void Stop();
85
88 void Remove();
89
92 bool IsRunning() const;
93
96 bool IsRemoveable() const;
97
100 Callback GetCallback() const;
101
104 void SetCallback(const Callback& callback);
105
108 RefPtr<Ticker> GetTicker() const;
109
112 void SetTicker(RefPtr<Ticker> ticker);
113
114private:
117 void Update(Duration dt);
118
121 void Reset();
122
123private:
124 bool running_;
125 bool removeable_;
126 RefPtr<Ticker> ticker_;
127 Callback callback_;
128};
129
130inline void Task::Remove()
131{
132 removeable_ = true;
133}
134
135inline bool Task::IsRunning() const
136{
137 return running_;
138}
139
140inline bool Task::IsRemoveable() const
141{
142 return removeable_;
143}
144
146{
147 return ticker_;
148}
149
151{
152 ticker_ = ticker;
153 if (ticker_)
154 {
155 if (running_)
156 ticker_->Resume();
157 else
158 ticker_->Pause();
159 }
160}
161
163{
164 return callback_;
165}
166
167inline void Task::SetCallback(const Task::Callback& callback)
168{
169 callback_ = callback;
170}
171} // namespace kiwano
侵入式链表元素
Definition: IntrusiveList.h:434
侵入式链表
Definition: IntrusiveList.h:34
基础对象
Definition: ObjectBase.h:138
引用计数智能指针
Definition: RefBasePtr.hpp:35
任务调度器
Definition: TaskScheduler.h:36
任务
Definition: Task.h:35
RefPtr< Ticker > GetTicker() const
获取任务的报时器
Definition: Task.h:145
void SetCallback(const Callback &callback)
设置任务回调函数
Definition: Task.h:167
void SetTicker(RefPtr< Ticker > ticker)
设置任务的报时器
Definition: Task.h:150
Callback GetCallback() const
获取任务回调函数
Definition: Task.h:162
bool IsRemoveable() const
任务是否可移除
Definition: Task.h:140
bool IsRunning() const
任务是否在运行
Definition: Task.h:135
void Remove()
移除任务
Definition: Task.h:130
时间段
Definition: Duration.h:48