NAP
sequenceplayerclock.h
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4 
5 #pragma once
6 
7 // external includes
8 #include <future>
9 #include <nap/resource.h>
10 #include <nap/signalslot.h>
11 #include <nap/timer.h>
12 #include <rtti/rtti.h>
13 #include <rtti/factory.h>
14 
15 namespace nap
16 {
18 
19  // forward declares
20  class SequenceService;
21 
26  class NAPAPI SequencePlayerClock : public Resource
27  {
28  RTTI_ENABLE(Resource)
29  public:
30  // default constructor and deconstructor
31  SequencePlayerClock() = default;
32  ~SequencePlayerClock() = default;
33 
34  // make this class explicitly non-copyable
36  SequencePlayerClock& operator=(const SequencePlayerClock&) = delete;
37 
43  virtual void start(Slot<double>& updateSlot) = 0;
44 
48  virtual void stop() = 0;
49 
50  protected:
51  // the slot
53  };
54 
60  {
61  friend class SequenceService;
62 
63  RTTI_ENABLE(SequencePlayerClock)
64  public:
70 
71  // make this class explicitly non-copyable
73  SequencePlayerStandardClock& operator=(const SequencePlayerStandardClock&) = delete;
74 
79  void start(Slot<double>& updateSlot) override;
80 
84  void stop() override;
85 
86  private:
91  void update(double deltaTime);
92 
93  // reference to service
94  SequenceService& mService;
95  };
96 
97  // factory method shortcut
99 
105  {
106  RTTI_ENABLE(SequencePlayerClock)
107  public:
111  SequencePlayerIndependentClock() = default;
112 
116  ~SequencePlayerIndependentClock() = default;
117 
118  // make this class explicitly non-copyable
121 
127  bool init(utility::ErrorState& errorState) override;
128 
133  void start(Slot<double>& updateSlot) override;
134 
138  void stop() override;
139 
140  // properties
141  float mFrequency = 1000.0f;
142  private:
143  void onUpdate();
144 
145  // the task
146  std::future<void> mUpdateTask;
147 
148  // bool indicating if thread is running
149  std::atomic_bool mRunning = {false};
150 
151  // previous timestamp, used to calculate deltaTime
152  SteadyTimeStamp mBefore;
153  };
154 }
nap::SequencePlayerClock::mSlot
Slot< double > mSlot
Definition: sequenceplayerclock.h:52
nap::Slot< double >
nap::SequenceService
Definition: sequenceservice.h:42
nap::rtti::ObjectCreator
Definition: factory.h:49
nap::utility::ErrorState
Definition: errorstate.h:19
nap::SequencePlayerIndependentClock
Definition: sequenceplayerclock.h:104
nap
Definition: templateapp.h:17
nap::Resource
Definition: resource.h:19
nap::SequencePlayerStandardClock
Definition: sequenceplayerclock.h:59
nap::SequencePlayerClock
Definition: sequenceplayerclock.h:26
nap::SteadyTimeStamp
std::chrono::time_point< SteadyClock > SteadyTimeStamp
Point in time associated with the SteadyClock.
Definition: datetime.h:30