| 
									
										
										
										
											2019-04-11 14:40:54 +08:00
										 |  |  |  | // Copyright (c) 2016-2018 Kiwano - Nomango
 | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |  | //
 | 
					
						
							| 
									
										
										
										
											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:
 | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |  | //
 | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +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
										 |  |  |  | //
 | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +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.
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | #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>
 | 
					
						
							| 
									
										
										
										
											2020-02-15 17:32:32 +08:00
										 |  |  |  | #include <kiwano/core/IntrusiveList.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
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |  | class TimerManager; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | KGE_DECLARE_SMART_PTR(Timer); | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-18 12:53:18 +08:00
										 |  |  |  | /// \~chinese
 | 
					
						
							| 
									
										
										
										
											2020-03-19 14:28:50 +08:00
										 |  |  |  | /// @brief <20><>ʱ<EFBFBD><CAB1><EFBFBD>б<EFBFBD>
 | 
					
						
							| 
									
										
										
										
											2020-02-18 12:53:18 +08:00
										 |  |  |  | typedef IntrusiveList<TimerPtr> TimerList; | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |  | /// \~chinese
 | 
					
						
							| 
									
										
										
										
											2020-03-19 14:28:50 +08:00
										 |  |  |  | /// @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>
 | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |  | class KGE_API Timer | 
					
						
							| 
									
										
										
										
											2020-02-20 18:22:52 +08:00
										 |  |  |  |     : public ObjectBase | 
					
						
							| 
									
										
										
										
											2020-02-15 17:32:32 +08:00
										 |  |  |  |     , protected IntrusiveListValue<TimerPtr> | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |  | { | 
					
						
							|  |  |  |  |     friend class TimerManager; | 
					
						
							|  |  |  |  |     friend IntrusiveList<TimerPtr>; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | public: | 
					
						
							|  |  |  |  |     /// \~chinese
 | 
					
						
							| 
									
										
										
										
											2020-03-19 14:28:50 +08:00
										 |  |  |  |     /// @brief <20><>ʱ<EFBFBD><CAB1><EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
 | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |  |     /// @details
 | 
					
						
							| 
									
										
										
										
											2020-03-19 14:28:50 +08:00
										 |  |  |  |     /// <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>
 | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |  |     using Callback = Function<void(Timer* /* self */, Duration /* dt */)>; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     /// \~chinese
 | 
					
						
							| 
									
										
										
										
											2020-03-19 14:28:50 +08:00
										 |  |  |  |     /// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><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>
 | 
					
						
							| 
									
										
										
										
											2020-02-19 12:09:50 +08:00
										 |  |  |  |     static TimerPtr Create(const Callback& cb, Duration interval, int times = -1); | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     /// \~chinese
 | 
					
						
							| 
									
										
										
										
											2020-03-19 14:28:50 +08:00
										 |  |  |  |     /// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><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>
 | 
					
						
							| 
									
										
										
										
											2020-02-19 12:09:50 +08:00
										 |  |  |  |     static TimerPtr Create(const String& name, const Callback& cb, Duration interval, int times = -1); | 
					
						
							| 
									
										
										
										
											2020-02-06 16:54:47 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     /// \~chinese
 | 
					
						
							| 
									
										
										
										
											2020-03-19 14:28:50 +08:00
										 |  |  |  |     /// @brief <20><><EFBFBD><EFBFBD><EFBFBD>ն<EFBFBD>ʱ<EFBFBD><CAB1>
 | 
					
						
							| 
									
										
										
										
											2020-02-06 16:54:47 +08:00
										 |  |  |  |     Timer(); | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     /// \~chinese
 | 
					
						
							| 
									
										
										
										
											2020-03-19 14:28:50 +08:00
										 |  |  |  |     /// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
 | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |  |     void Start(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     /// \~chinese
 | 
					
						
							| 
									
										
										
										
											2020-03-19 14:28:50 +08:00
										 |  |  |  |     /// @brief ֹͣ<CDA3><D6B9>ʱ<EFBFBD><CAB1>
 | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |  |     void Stop(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     /// \~chinese
 | 
					
						
							| 
									
										
										
										
											2020-03-19 14:28:50 +08:00
										 |  |  |  |     /// @brief <20>Ƴ<EFBFBD><C6B3><EFBFBD>ʱ<EFBFBD><CAB1>
 | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |  |     void Remove(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     /// \~chinese
 | 
					
						
							| 
									
										
										
										
											2020-03-19 14:28:50 +08:00
										 |  |  |  |     /// @brief <20><>ʱ<EFBFBD><CAB1><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
 | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |  |     bool IsRunning() const; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     /// \~chinese
 | 
					
						
							| 
									
										
										
										
											2020-03-19 14:28:50 +08:00
										 |  |  |  |     /// @brief <20><>ʱ<EFBFBD><CAB1><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>Ƴ<EFBFBD>
 | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |  |     bool IsRemoveable() const; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     /// \~chinese
 | 
					
						
							| 
									
										
										
										
											2020-03-19 14:28:50 +08:00
										 |  |  |  |     /// @brief <20><>ȡ<EFBFBD><C8A1>ʱ<EFBFBD><CAB1>ִ<EFBFBD>й<EFBFBD><D0B9>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD>
 | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |  |     int GetRunTimes() const; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     /// \~chinese
 | 
					
						
							| 
									
										
										
										
											2020-03-19 14:28:50 +08:00
										 |  |  |  |     /// @brief <20><>ȡ<EFBFBD><C8A1>ʱ<EFBFBD><CAB1>ִ<EFBFBD>лص<D0BB><D8B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܴ<EFBFBD><DCB4><EFBFBD>
 | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |  |     int GetTotalRunTimes() const; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     /// \~chinese
 | 
					
						
							| 
									
										
										
										
											2020-03-19 14:28:50 +08:00
										 |  |  |  |     /// @brief <20><><EFBFBD>ö<EFBFBD>ʱ<EFBFBD><CAB1>ִ<EFBFBD>лص<D0BB><D8B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܴ<EFBFBD><DCB4><EFBFBD>
 | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |  |     void SetTotalRunTimes(int times); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     /// \~chinese
 | 
					
						
							| 
									
										
										
										
											2020-03-19 14:28:50 +08:00
										 |  |  |  |     /// @brief <20><>ȡ<EFBFBD><C8A1>ʱ<EFBFBD><CAB1>ִ<EFBFBD><D6B4>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
 | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |  |     Duration GetInterval() const; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     /// \~chinese
 | 
					
						
							| 
									
										
										
										
											2020-03-19 14:28:50 +08:00
										 |  |  |  |     /// @brief <20><><EFBFBD>ö<EFBFBD>ʱ<EFBFBD><CAB1>ִ<EFBFBD><D6B4>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
 | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |  |     void SetInterval(Duration interval); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     /// \~chinese
 | 
					
						
							| 
									
										
										
										
											2020-03-19 14:28:50 +08:00
										 |  |  |  |     /// @brief <20><>ȡ<EFBFBD><C8A1>ʱ<EFBFBD><CAB1><EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
 | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |  |     Callback GetCallback() const; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     /// \~chinese
 | 
					
						
							| 
									
										
										
										
											2020-03-19 14:28:50 +08:00
										 |  |  |  |     /// @brief <20><><EFBFBD>ö<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
 | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |  |     void SetCallback(const Callback& callback); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | private: | 
					
						
							|  |  |  |  |     /// \~chinese
 | 
					
						
							| 
									
										
										
										
											2020-03-19 14:28:50 +08:00
										 |  |  |  |     /// @brief <20><><EFBFBD>¶<EFBFBD>ʱ<EFBFBD><CAB1>
 | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |  |     void Update(Duration dt); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     /// \~chinese
 | 
					
						
							| 
									
										
										
										
											2020-03-19 14:28:50 +08:00
										 |  |  |  |     /// @brief <20><><EFBFBD>ö<EFBFBD>ʱ<EFBFBD><CAB1>
 | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |  |     void Reset(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | private: | 
					
						
							|  |  |  |  |     bool     running_; | 
					
						
							|  |  |  |  |     bool     removeable_; | 
					
						
							|  |  |  |  |     int      run_times_; | 
					
						
							|  |  |  |  |     int      total_times_; | 
					
						
							|  |  |  |  |     Duration interval_; | 
					
						
							|  |  |  |  |     Duration elapsed_; | 
					
						
							|  |  |  |  |     Callback callback_; | 
					
						
							|  |  |  |  | }; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 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
										 |  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |  | }  // namespace kiwano
 |