iPlug2 - C++ Audio Plug-in Framework
IPlugTimer.h
Go to the documentation of this file.
1 /*
2  ==============================================================================
3 
4  This file is part of the iPlug 2 library. Copyright (C) the iPlug 2 developers.
5 
6  See LICENSE.txt for more info.
7 
8  ==============================================================================
9 */
10 
11 
12 #pragma once
13 
20 #include <cstring>
21 #include <stdint.h>
22 #include <cstring>
23 #include <cmath>
24 #include <functional>
25 #include "ptrlist.h"
26 #include "mutex.h"
27 
28 #include "IPlugPlatform.h"
29 
30 #if defined OS_MAC || defined OS_IOS
31 #include <CoreFoundation/CoreFoundation.h>
32 #elif defined OS_WEB
33 #include <emscripten/html5.h>
34 #endif
35 
36 BEGIN_IPLUG_NAMESPACE
37 
39 struct Timer
40 {
41  Timer() = default;
42  Timer(const Timer&) = delete;
43  Timer& operator=(const Timer&) = delete;
44 
45  using ITimerFunction = std::function<void(Timer& t)>;
46 
47  static Timer* Create(ITimerFunction func, uint32_t intervalMs);
48  virtual ~Timer() {};
49  virtual void Stop() = 0;
50 };
51 
52 #if defined OS_MAC || defined OS_IOS
53 
54 class Timer_impl : public Timer
55 {
56 public:
57 
58  Timer_impl(ITimerFunction func, uint32_t intervalMs);
59  ~Timer_impl();
60 
61  void Stop() override;
62  static void TimerProc(CFRunLoopTimerRef timer, void *info);
63 
64 private:
65  CFRunLoopTimerRef mOSTimer;
66  ITimerFunction mTimerFunc;
67  uint32_t mIntervalMs;
68 };
69 #elif defined OS_WIN
70 class Timer_impl : public Timer
71 {
72 public:
73  Timer_impl(ITimerFunction func, uint32_t intervalMs);
74  ~Timer_impl();
75  void Stop() override;
76  static void CALLBACK TimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime);
77 
78 private:
79  static WDL_Mutex sMutex;
80  static WDL_PtrList<Timer_impl> sTimers;
81  UINT_PTR ID = 0;
82  ITimerFunction mTimerFunc;
83  uint32_t mIntervalMs;
84 };
85 #elif defined OS_WEB
86 class Timer_impl : public Timer
87 {
88 public:
89  Timer_impl(ITimerFunction func, uint32_t intervalMs);
90  ~Timer_impl();
91  void Stop() override;
92  static void TimerProc(void *userData);
93 
94 private:
95  long ID = 0;
96  ITimerFunction mTimerFunc;
97 };
98 #else
99  #error NOT IMPLEMENTED
100 #endif
101 
102 END_IPLUG_NAMESPACE
Include to get consistently named preprocessor macros for different platforms and logging functionali...
Base class for timer.
Definition: IPlugTimer.h:39