Simulant  21.09-46
A portable game engine for Windows, OSX, Linux, Dreamcast, and PSP
idle_task_manager.h
1 /* * Copyright (c) 2011-2017 Luke Benstead https://simulant-engine.appspot.com
2  *
3  * This file is part of Simulant.
4  *
5  * Simulant is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * Simulant is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with Simulant. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef IDLE_TASK_MANAGER_H
20 #define IDLE_TASK_MANAGER_H
21 
22 #include <cstdint>
23 #include <functional>
24 #include <map>
25 #include <condition_variable>
26 
27 #include "threads/mutex.h"
28 #include "threads/condition.h"
29 
30 #include "types.h"
31 
32 namespace smlt {
33 
34 class Window;
35 
36 class Seconds {
37 public:
38  Seconds():
39  value_(0) {}
40 
41  explicit Seconds(float t):
42  value_(t) {}
43 
44  Seconds operator+(const Seconds& rhs) const {
45  return Seconds(value_ + rhs.value_);
46  }
47 
48  Seconds& operator+=(const Seconds& rhs) {
49  value_ += rhs.value_;
50  return *this;
51  }
52 
53  float to_float() {
54  return value_;
55  }
56 
57 private:
58  float value_;
59 };
60 
62 public:
63  IdleTaskManager(Window& window);
64 
65  IdleConnectionID add(std::function<bool ()> callback);
66  IdleConnectionID add_once(std::function<void ()> callback);
67  IdleConnectionID add_timeout(const Seconds& seconds, std::function<bool()> callback);
68  IdleConnectionID add_timeout_once(const Seconds& seconds, std::function<void()> callback);
69 
70  void run_sync(std::function<void()> callback);
71 
72  void remove(IdleConnectionID connection);
73 
74  void execute();
75 
76  void wait();
77 
78 private:
79  typedef std::map<IdleConnectionID, std::function<bool ()> > SignalMap;
80  typedef std::map<IdleConnectionID, std::function<void ()> > SignalOnceMap;
81 
82  Window& window_;
83 
84  SignalMap signals_;
85  SignalOnceMap signals_once_;
86 
87  thread::Mutex signals_mutex_;
88  thread::Mutex signals_once_mutex_;
89 
90  thread::Mutex cv_mutex_;
92 };
93 
94 }
95 #endif // IDLE_TASK_MANAGER_H
smlt::IdleTaskManager
Definition: idle_task_manager.h:61
smlt
Definition: animation.cpp:25
smlt::Window
Definition: window.h:90
smlt::thread::Condition
Definition: condition.h:8
smlt::Seconds
Definition: idle_task_manager.h:36
smlt::thread::Mutex
Definition: mutex.h:25