Allolib  1.0
C++ Components For Interactive Multimedia
al_ComputationDomain.hpp
1 #ifndef COMPUTATIONDOMAIN_H
2 #define COMPUTATIONDOMAIN_H
3 
4 #include <cassert>
5 #include <functional>
6 #include <iostream>
7 #include <memory>
8 #include <mutex>
9 #include <stack>
10 #include <vector>
11 
12 #include "al/ui/al_Parameter.hpp"
13 
14 namespace al {
15 class SynchronousDomain;
16 
22 public:
23  virtual ~ComputationDomain() {}
31  virtual bool init(ComputationDomain *parent = nullptr);
32 
38  virtual bool cleanup(ComputationDomain *parent = nullptr);
39 
40  template <class DomainType>
53  std::shared_ptr<DomainType> newSubDomain(bool prepend = false);
54 
63  void removeSubDomain(std::shared_ptr<SynchronousDomain> subDomain);
64 
75  double timeDelta() { return mTimeDrift; }
76 
77  void setTimeDelta(double delta) { mTimeDrift = delta; }
78 
83  void
84  registerInitializeCallback(std::function<void(ComputationDomain *)> callback);
85 
90  void
91  registerCleanupCallback(std::function<void(ComputationDomain *)> callback);
92 
104  std::vector<ParameterMeta *> parameters() { return mParameters; }
105 
106 protected:
117  bool initializeSubdomains(bool pre = false);
118 
127  bool tickSubdomains(bool pre = false);
128 
137  bool cleanupSubdomains(bool pre = false);
138 
144 
150 
151  std::mutex mSubdomainLock; // It is the domain's responsibility to lock and
152  // unlock while processing.
153  double mTimeDrift{0.0};
154  std::vector<std::pair<std::shared_ptr<SynchronousDomain>, bool>>
155  mSubDomainList;
156 
157  // Add parameters for domain control here
158  std::vector<ParameterMeta *> mParameters;
159 
160 private:
161  std::vector<std::function<void(ComputationDomain *)>> mInitializeCallbacks;
162  std::vector<std::function<void(ComputationDomain *)>> mCleanupCallbacks;
163 };
164 
166  friend class ComputationDomain;
167 
168 public:
173  virtual bool tick();
174 };
175 
177 public:
184  virtual bool start() = 0;
185 
193  virtual bool stop() = 0;
194 
195 protected:
201 
207  for (auto callback : mStopCallbacks) {
208  callback(this);
209  }
210  }
211 
212 private:
213  std::vector<std::function<void(ComputationDomain *)>> mStartCallbacks;
214  std::vector<std::function<void(ComputationDomain *)>> mStopCallbacks;
215 };
216 
217 template <class DomainType>
218 std::shared_ptr<DomainType> ComputationDomain::newSubDomain(bool prepend) {
219  // std::lock_guard<std::mutex> lk(mSubdomainLock);
220  // Only Synchronous domains are allowed as subdomains
221  auto newDomain = std::make_shared<DomainType>();
222  assert(dynamic_cast<SynchronousDomain *>(newDomain.get()));
223  if (newDomain) {
224  mSubDomainList.push_back({newDomain, prepend});
225  // if (newDomain->init(this)) {
226  // } else {
227  // newDomain = nullptr;
228  // }
229  }
230  return newDomain;
231 }
232 
233 } // namespace al
234 
235 #endif // COMPUTATIONDOMAIN_H
virtual bool start()=0
start the asyncrhonous execution of the domain
void callStartCallbacks()
callStartCallbacks should be called by children of this class after the domain has been set up to sta...
void callStopCallbacks()
callStopCallbacks should be called by children of this class on the stop request, before the domain h...
virtual bool stop()=0
stop the asyncrhonous execution of the domain
ComputationDomain class.
bool cleanupSubdomains(bool pre=false)
cleanup subdomains
std::vector< ParameterMeta * > parameters()
Return a list of parameters that control this domain.
bool initializeSubdomains(bool pre=false)
initializeSubdomains should be called within the domain's initialization function
void callCleanupCallbacks()
callInitializeCallbacks should be called by children of this class before the domain has been cleaned...
std::shared_ptr< DomainType > newSubDomain(bool prepend=false)
Add a synchronous domain to this domain.
void registerCleanupCallback(std::function< void(ComputationDomain *)> callback)
register callbacks to be called in the cleanup() function
virtual bool cleanup(ComputationDomain *parent=nullptr)
cleanup
void callInitializeCallbacks()
callInitializeCallbacks should be called by children of this class after the domain has been initiali...
void registerInitializeCallback(std::function< void(ComputationDomain *)> callback)
register callbacks to be called in the init() function
void removeSubDomain(std::shared_ptr< SynchronousDomain > subDomain)
Remove a subdomain.
bool tickSubdomains(bool pre=false)
execute subdomains
virtual bool init(ComputationDomain *parent=nullptr)
initialize
double timeDelta()
Return time delta with respect to previous processing pass for this domain.
virtual bool tick()
Execute a pass of the domain.
Definition: al_App.hpp:23