AIStatefulTask ‐ Asynchronous, Stateful Task Scheduler library.

Threads-like task objects evolving through user-defined states.

AIEngine.h
Go to the documentation of this file.
1
41#pragma once
42
43#include "threadsafe/aithreadsafe.h"
44#include "threadsafe/ConditionVariable.h"
45#include "AIStatefulTask.h"
46#include "utils/FuzzyBool.h"
47#include "debug.h"
48#include <list>
49#include <chrono>
50#include <boost/intrusive_ptr.hpp>
51
110{
111 private:
112 struct QueueElementComp;
113 class QueueElement {
114 private:
115 boost::intrusive_ptr<AIStatefulTask> mStatefulTask;
116
117 public:
118 QueueElement(AIStatefulTask* stateful_task) : mStatefulTask(stateful_task) { }
119 friend bool operator==(QueueElement const& e1, QueueElement const& e2) { return e1.mStatefulTask == e2.mStatefulTask; }
120 friend bool operator!=(QueueElement const& e1, QueueElement const& e2) { return e1.mStatefulTask != e2.mStatefulTask; }
121 friend struct QueueElementComp;
122
123 AIStatefulTask const& stateful_task() const { return *mStatefulTask; }
124 AIStatefulTask& stateful_task() { return *mStatefulTask; }
125 };
126 struct QueueElementComp {
127 inline bool operator()(QueueElement const& e1, QueueElement const& e2) const;
128 };
129
130 using queued_type = std::list<QueueElement>;
131
132 struct engine_state_st
133 {
134 queued_type list;
135 bool waiting;
136 engine_state_st() : waiting(false) { }
137 };
138
139 using engine_state_type = aithreadsafe::Wrapper<engine_state_st, aithreadsafe::policy::Primitive<aithreadsafe::ConditionVariable>>;
140
141#ifndef DOXYGEN
142 public: // Used by AIStatefulTask.
143 using clock_type = AIStatefulTask::clock_type;
144 using duration_type = AIStatefulTask::duration_type;
145#endif
146
147 private:
148 engine_state_type mEngineState;
149 char const* mName;
150 duration_type mMaxDuration;
151 bool mHasMaxDuration;
152
153 public:
165 AIEngine(char const* name, float max_duration = 0.0f) : mName(name) { setMaxDuration(max_duration); }
166
177 void add(AIStatefulTask* stateful_task);
178
191 utils::FuzzyBool mainloop();
192
194 void wake_up();
195
204 void flush();
205
211 char const* name() const { return mName; }
212
221 void setMaxDuration(float max_duration);
222
229 bool hasMaxDuration() const { return mHasMaxDuration; }
230};
Declaration of base class AIStatefulTask.
Definition: AIEngine.h:110
bool hasMaxDuration() const
Definition: AIEngine.h:229
void add(AIStatefulTask *stateful_task)
Definition: AIEngine.cxx:44
char const * name() const
Definition: AIEngine.h:211
void setMaxDuration(float max_duration)
Definition: AIEngine.cxx:176
void flush()
Definition: AIEngine.cxx:163
utils::FuzzyBool mainloop()
Definition: AIEngine.cxx:67
AIEngine(char const *name, float max_duration=0.0f)
Definition: AIEngine.h:165
void wake_up()
Wake up a sleeping engine.
Definition: AIEngine.cxx:186
Definition: AIStatefulTask.h:96