2019-04-11 14:40:54 +08:00
|
|
|
|
// Copyright (c) 2016-2018 Kiwano - Nomango
|
2019-03-31 01:37:06 +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:
|
|
|
|
|
|
//
|
|
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
|
|
//
|
|
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
|
|
|
|
#pragma once
|
2019-11-13 14:33:15 +08:00
|
|
|
|
#include <kiwano/core/ObjectBase.h>
|
2020-01-17 16:55:47 +08:00
|
|
|
|
#include <kiwano/core/Time.h>
|
2019-03-31 01:37:06 +08:00
|
|
|
|
|
2019-04-11 14:40:54 +08:00
|
|
|
|
namespace kiwano
|
2019-03-31 01:37:06 +08:00
|
|
|
|
{
|
|
|
|
|
|
class TimerManager;
|
|
|
|
|
|
|
2019-04-11 14:40:54 +08:00
|
|
|
|
KGE_DECLARE_SMART_PTR(Timer);
|
2019-03-31 01:37:06 +08:00
|
|
|
|
|
2019-12-25 18:07:57 +08:00
|
|
|
|
/// \~chinese
|
|
|
|
|
|
/// @brief <20><>ʱ<EFBFBD><CAB1>
|
|
|
|
|
|
/// @details <20><>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ÿ<EFBFBD><C3BF>һ<EFBFBD><D2BB>ʱ<EFBFBD><CAB1>ִ<EFBFBD><D6B4>һ<EFBFBD>λص<CEBB><D8B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҿ<EFBFBD><D2BF><EFBFBD>ָ<EFBFBD><D6B8>ִ<EFBFBD><D6B4><EFBFBD>ܴ<EFBFBD><DCB4><EFBFBD>
|
2019-04-11 14:40:54 +08:00
|
|
|
|
class KGE_API Timer
|
2020-01-09 08:45:00 +08:00
|
|
|
|
: public virtual ObjectBase
|
2019-10-30 23:12:18 +08:00
|
|
|
|
, protected IntrusiveListItem<TimerPtr>
|
2019-03-31 01:37:06 +08:00
|
|
|
|
{
|
|
|
|
|
|
friend class TimerManager;
|
2019-10-30 23:12:18 +08:00
|
|
|
|
friend IntrusiveList<TimerPtr>;
|
2019-03-31 01:37:06 +08:00
|
|
|
|
|
|
|
|
|
|
public:
|
2019-12-25 18:07:57 +08:00
|
|
|
|
/// \~chinese
|
|
|
|
|
|
/// @brief <20><>ʱ<EFBFBD><CAB1><EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
/// @details <20>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƕ<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڶ<EFBFBD><DAB6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ǿ<EFBFBD><C7BE><EFBFBD><EFBFBD>ϴθ<CFB4><CEB8>µ<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
using Callback = Function<void(Timer* /* self */, Duration /* dt */)>;
|
|
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
|
|
|
|
|
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD>ն<EFBFBD>ʱ<EFBFBD><CAB1>
|
|
|
|
|
|
Timer();
|
|
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
|
|
|
|
|
/// @brief <20><><EFBFBD>춨ʱ<ECB6A8><CAB1>
|
|
|
|
|
|
/// @param cb <20>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
/// @param interval ʱ<><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
/// @param times ִ<>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> -1 Ϊ<><CEAA><EFBFBD><EFBFBD>ִ<EFBFBD>У<EFBFBD>
|
|
|
|
|
|
Timer(Callback const& cb, Duration interval, int times = -1);
|
|
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
|
|
|
|
|
/// @brief <20><><EFBFBD>춨ʱ<ECB6A8><CAB1>
|
|
|
|
|
|
/// @param name <20><><EFBFBD><EFBFBD>
|
|
|
|
|
|
/// @param cb <20>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
/// @param interval ʱ<><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
/// @param times ִ<>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> -1 Ϊ<><CEAA><EFBFBD><EFBFBD>ִ<EFBFBD>У<EFBFBD>
|
|
|
|
|
|
Timer(String const& name, Callback const& cb, Duration interval, int times = -1);
|
|
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
|
|
|
|
|
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
|
2019-03-31 01:37:06 +08:00
|
|
|
|
void Start();
|
|
|
|
|
|
|
2019-12-25 18:07:57 +08:00
|
|
|
|
/// \~chinese
|
|
|
|
|
|
/// @brief ֹͣ<CDA3><D6B9>ʱ<EFBFBD><CAB1>
|
2019-03-31 01:37:06 +08:00
|
|
|
|
void Stop();
|
|
|
|
|
|
|
2019-12-25 18:07:57 +08:00
|
|
|
|
/// \~chinese
|
|
|
|
|
|
/// @brief <20>Ƴ<EFBFBD><C6B3><EFBFBD>ʱ<EFBFBD><CAB1>
|
|
|
|
|
|
void Remove();
|
|
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
|
|
|
|
|
/// @brief <20><>ʱ<EFBFBD><CAB1><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2019-03-31 01:37:06 +08:00
|
|
|
|
bool IsRunning() const;
|
|
|
|
|
|
|
2019-12-25 18:07:57 +08:00
|
|
|
|
/// \~chinese
|
|
|
|
|
|
/// @brief <20><>ʱ<EFBFBD><CAB1><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>Ƴ<EFBFBD>
|
|
|
|
|
|
bool IsRemoveable() const;
|
|
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
|
|
|
|
|
/// @brief <20><>ȡ<EFBFBD><C8A1>ʱ<EFBFBD><CAB1>ִ<EFBFBD>й<EFBFBD><D0B9>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD>
|
|
|
|
|
|
int GetRunTimes() const;
|
|
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
|
|
|
|
|
/// @brief <20><>ȡ<EFBFBD><C8A1>ʱ<EFBFBD><CAB1>ִ<EFBFBD>лص<D0BB><D8B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܴ<EFBFBD><DCB4><EFBFBD>
|
|
|
|
|
|
int GetTotalRunTimes() const;
|
|
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
|
|
|
|
|
/// @brief <20><><EFBFBD>ö<EFBFBD>ʱ<EFBFBD><CAB1>ִ<EFBFBD>лص<D0BB><D8B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܴ<EFBFBD><DCB4><EFBFBD>
|
|
|
|
|
|
void SetTotalRunTimes(int times);
|
|
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
|
|
|
|
|
/// @brief <20><>ȡ<EFBFBD><C8A1>ʱ<EFBFBD><CAB1>ִ<EFBFBD><D6B4>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
Duration GetInterval() const;
|
|
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
|
|
|
|
|
/// @brief <20><><EFBFBD>ö<EFBFBD>ʱ<EFBFBD><CAB1>ִ<EFBFBD><D6B4>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
void SetInterval(Duration interval);
|
|
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
|
|
|
|
|
/// @brief <20><>ȡ<EFBFBD><C8A1>ʱ<EFBFBD><CAB1><EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
Callback GetCallback() const;
|
|
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
|
|
|
|
|
/// @brief <20><><EFBFBD>ö<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
void SetCallback(const Callback& callback);
|
|
|
|
|
|
|
2019-12-23 18:05:08 +08:00
|
|
|
|
private:
|
2019-12-25 18:07:57 +08:00
|
|
|
|
/// \~chinese
|
|
|
|
|
|
/// @brief <20><><EFBFBD>¶<EFBFBD>ʱ<EFBFBD><CAB1>
|
|
|
|
|
|
void Update(Duration dt);
|
2019-03-31 01:37:06 +08:00
|
|
|
|
|
2019-12-25 18:07:57 +08:00
|
|
|
|
/// \~chinese
|
|
|
|
|
|
/// @brief <20><><EFBFBD>ö<EFBFBD>ʱ<EFBFBD><CAB1>
|
2019-03-31 01:37:06 +08:00
|
|
|
|
void Reset();
|
|
|
|
|
|
|
2019-12-23 18:05:08 +08:00
|
|
|
|
private:
|
2019-03-31 01:37:06 +08:00
|
|
|
|
bool running_;
|
2019-12-25 18:07:57 +08:00
|
|
|
|
bool removeable_;
|
|
|
|
|
|
int run_times_;
|
|
|
|
|
|
int total_times_;
|
|
|
|
|
|
Duration interval_;
|
|
|
|
|
|
Duration elapsed_;
|
2019-03-31 01:37:06 +08:00
|
|
|
|
Callback callback_;
|
|
|
|
|
|
};
|
2019-12-25 18:07:57 +08:00
|
|
|
|
|
|
|
|
|
|
inline void Timer::Start()
|
|
|
|
|
|
{
|
|
|
|
|
|
running_ = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline void Timer::Stop()
|
|
|
|
|
|
{
|
|
|
|
|
|
running_ = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline void Timer::Remove()
|
|
|
|
|
|
{
|
|
|
|
|
|
removeable_ = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline bool Timer::IsRunning() const
|
|
|
|
|
|
{
|
|
|
|
|
|
return running_;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline bool Timer::IsRemoveable() const
|
|
|
|
|
|
{
|
|
|
|
|
|
return removeable_;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline int Timer::GetRunTimes() const
|
|
|
|
|
|
{
|
|
|
|
|
|
return run_times_;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline int Timer::GetTotalRunTimes() const
|
|
|
|
|
|
{
|
|
|
|
|
|
return total_times_;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline void Timer::SetTotalRunTimes(int times)
|
|
|
|
|
|
{
|
|
|
|
|
|
total_times_ = times;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline Duration Timer::GetInterval() const
|
|
|
|
|
|
{
|
|
|
|
|
|
return interval_;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline void Timer::SetInterval(Duration interval)
|
|
|
|
|
|
{
|
|
|
|
|
|
interval_ = interval;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline Timer::Callback Timer::GetCallback() const
|
|
|
|
|
|
{
|
|
|
|
|
|
return callback_;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline void Timer::SetCallback(const Timer::Callback& callback)
|
|
|
|
|
|
{
|
|
|
|
|
|
callback_ = callback;
|
|
|
|
|
|
}
|
2019-03-31 01:37:06 +08:00
|
|
|
|
}
|