Magic_Game/src/kiwano/utils/Task.cpp

108 lines
2.5 KiB
C++
Raw Normal View History

2019-04-11 14:40:54 +08:00
// Copyright (c) 2016-2018 Kiwano - Nomango
2020-01-21 10:09:55 +08:00
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
2020-01-21 10:09:55 +08:00
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
2020-01-21 10:09:55 +08:00
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
2020-05-20 23:48:56 +08:00
#include <kiwano/utils/Task.h>
2019-04-11 14:40:54 +08:00
namespace kiwano
{
2020-07-22 21:08:48 +08:00
Task::Task(const Callback& cb, TickerPtr ticker)
: running_(true)
, removeable_(false)
, callback_(cb)
, ticker_(ticker)
2020-02-06 16:54:47 +08:00
{
}
2020-07-22 21:08:48 +08:00
Task::Task(const String& name, const Callback& cb, TickerPtr ticker)
: Task(cb, ticker)
2020-02-06 16:54:47 +08:00
{
2020-07-22 21:08:48 +08:00
SetName(name);
2020-02-06 16:54:47 +08:00
}
2020-07-22 21:08:48 +08:00
Task::Task(const Callback& cb, Duration interval, int times)
: running_(true)
, removeable_(false)
, callback_(cb)
2020-05-20 00:47:46 +08:00
{
2020-07-22 21:08:48 +08:00
ticker_ = MakePtr<Ticker>(interval, times);
2020-05-20 00:47:46 +08:00
}
2020-07-22 21:08:48 +08:00
Task::Task(const String& name, const Callback& cb, Duration interval, int times)
: Task(cb, interval, times)
2020-05-20 00:47:46 +08:00
{
2020-07-22 21:08:48 +08:00
SetName(name);
2020-05-20 00:47:46 +08:00
}
2020-05-19 21:25:00 +08:00
Task::Task()
2020-01-21 10:09:55 +08:00
: running_(true)
, removeable_(false)
, callback_()
{
}
2020-07-20 16:55:56 +08:00
void Task::Start()
{
if (!running_)
{
running_ = true;
if (ticker_)
ticker_->Resume();
}
}
void Task::Stop()
{
if (running_)
{
running_ = false;
if (ticker_)
ticker_->Pause();
}
}
2020-05-19 21:25:00 +08:00
void Task::Update(Duration dt)
2020-01-21 10:09:55 +08:00
{
2020-07-20 16:55:56 +08:00
if (!running_ || removeable_)
return;
2020-05-26 15:20:02 +08:00
if (!ticker_ || ticker_->GetTotalTickCount() == 0)
2020-01-21 10:09:55 +08:00
{
2020-05-20 00:47:46 +08:00
Remove();
return;
}
2020-05-20 00:47:46 +08:00
if (ticker_->Tick(dt))
{
2020-01-21 10:09:55 +08:00
if (callback_)
2020-05-20 00:47:46 +08:00
callback_(this, ticker_->GetDeltaTime());
2020-05-26 15:20:02 +08:00
if (ticker_->GetTickedCount() == ticker_->GetTotalTickCount())
2020-01-21 10:09:55 +08:00
Remove();
}
}
2020-05-19 21:25:00 +08:00
void Task::Reset()
2020-01-21 10:09:55 +08:00
{
2020-05-20 00:47:46 +08:00
if (ticker_)
ticker_->Reset();
2019-08-20 21:15:15 +08:00
}
2020-01-21 10:09:55 +08:00
} // namespace kiwano