ASPiK SDK
cvstguitimer.h
1 // This file is part of VSTGUI. It is subject to the license terms
2 // in the LICENSE file found in the top-level directory of this
3 // distribution and at http://github.com/steinbergmedia/vstgui/LICENSE
4 
5 #ifndef __cvstguitimer__
6 #define __cvstguitimer__
7 
8 #include "vstguibase.h"
9 #include "platform/iplatformtimer.h"
10 #include <functional>
11 
12 namespace VSTGUI {
13 
14 //-----------------------------------------------------------------------------
15 // CVSTGUITimer Declaration
17 //-----------------------------------------------------------------------------
18 class CVSTGUITimer final : public CBaseObject, public IPlatformTimerCallback
19 {
20 public:
21  using CallbackFunc = std::function<void(CVSTGUITimer*)>;
22 
23  CVSTGUITimer (const CallbackFunc& callback, uint32_t fireTime = 100, bool doStart = true);
24  CVSTGUITimer (CallbackFunc&& callback, uint32_t fireTime = 100, bool doStart = true);
25  CVSTGUITimer (CBaseObject* timerObject, uint32_t fireTime = 100, bool doStart = false);
26 
27  bool start ();
28  bool stop ();
29 
30  bool setFireTime (uint32_t newFireTime);
31  uint32_t getFireTime () const { return fireTime; }
32 
33 //-----------------------------------------------------------------------------
34  static IdStringPtr kMsgTimer;
35 //-----------------------------------------------------------------------------
36  CLASS_METHODS_NOCOPY(CVSTGUITimer, CBaseObject)
37 protected:
38  ~CVSTGUITimer () noexcept override;
39 
40  void beforeDelete () override;
41  void fire () override;
42 
43  uint32_t fireTime;
44  CallbackFunc callbackFunc;
45 
46  SharedPointer<IPlatformTimer> platformTimer;
47 };
48 
49 namespace Call
50 {
51  using FunctionCallback = std::function<void ()>;
52 
54  inline void later (FunctionCallback callback, uint32_t delayInMilliseconds = 10)
55  {
56  new CVSTGUITimer ([callback] (CVSTGUITimer* timer) {
57  timer->stop ();
58  callback ();
59  timer->forget ();
60  }, delayInMilliseconds, true);
61  }
62 };
63 
64 } // namespace
65 
66 #endif
bool stop()
stops the timer, returns whether timer was running or not
Definition: cvstguitimer.cpp:76
Definition: iplatformtimer.h:20
Definition: vstguibase.h:299
uint32_t getFireTime() const
in milliseconds
Definition: cvstguitimer.h:31
Base Object with reference counter.
Definition: vstguibase.h:276
Definition: iplatformtimer.h:13
Definition: customcontrols.cpp:8
bool start()
starts the timer
Definition: cvstguitimer.cpp:59
A timer class, which posts timer messages to CBaseObjects or calls a lambda function (c++11 only)...
Definition: cvstguitimer.h:18
void forget() override
decrease refcount and delete object if refcount == 0
Definition: vstguibase.h:260
bool setFireTime(uint32_t newFireTime)
in milliseconds
Definition: cvstguitimer.cpp:92
static IdStringPtr kMsgTimer
message string posted to CBaseObject&#39;s notify method
Definition: cvstguitimer.h:34