1
0
mirror of https://github.com/TomHarte/CLK.git synced 2024-06-18 13:29:41 +00:00

Eliminates unused RealTimeActor, provides more feedback from +=, gets specific as to nodiscards.

This commit is contained in:
Thomas Harte 2021-04-03 21:26:43 -04:00
parent faaa4961ed
commit 77b241af4f

View File

@ -56,10 +56,12 @@ template <class T, int multiplier = 1, int divider = 1, class LocalTimeScale = H
}
/// Adds time to the actor.
forceinline void operator += (LocalTimeScale rhs) {
///
/// @returns @c true if adding time caused a flush; @c false otherwise.
forceinline bool operator += (LocalTimeScale rhs) {
if constexpr (std::is_base_of<ClockingHint::Source, T>::value) {
if(clocking_preference_ == ClockingHint::Preference::None) {
return;
return false;
}
}
@ -73,7 +75,7 @@ template <class T, int multiplier = 1, int divider = 1, class LocalTimeScale = H
if constexpr (std::is_base_of<ClockingHint::Source, T>::value) {
if (clocking_preference_ == ClockingHint::Preference::RealTime) {
flush();
return;
return true;
}
}
@ -81,15 +83,18 @@ template <class T, int multiplier = 1, int divider = 1, class LocalTimeScale = H
time_until_event_ -= rhs;
if(time_until_event_ <= LocalTimeScale(0)) {
flush();
return true;
}
}
return false;
}
/// Flushes all accumulated time and returns a pointer to the included object.
///
/// If this object provides sequence points, checks for changes to the next
/// sequence point upon deletion of the pointer.
forceinline auto operator->() {
[[nodiscard]] forceinline auto operator->() {
flush();
return std::unique_ptr<T, SequencePointAwareDeleter>(&object_, SequencePointAwareDeleter(this));
}
@ -97,19 +102,19 @@ template <class T, int multiplier = 1, int divider = 1, class LocalTimeScale = H
/// Acts exactly as per the standard ->, but preserves constness.
///
/// Despite being const, this will flush the object and, if relevant, update the next sequence point.
forceinline auto operator -> () const {
[[nodiscard]] forceinline auto operator -> () const {
auto non_const_this = const_cast<JustInTimeActor<T, multiplier, divider, LocalTimeScale, TargetTimeScale> *>(this);
non_const_this->flush();
return std::unique_ptr<const T, SequencePointAwareDeleter>(&object_, SequencePointAwareDeleter(non_const_this));
}
/// @returns a pointer to the included object, without flushing time.
forceinline T *last_valid() {
[[nodiscard]] forceinline T *last_valid() {
return &object_;
}
/// @returns the amount of time since the object was last flushed, in the target time scale.
forceinline TargetTimeScale time_since_flush() const {
[[nodiscard]] forceinline TargetTimeScale time_since_flush() const {
// TODO: does this handle conversions properly where TargetTimeScale != LocalTimeScale?
if constexpr (divider == 1) {
return time_since_update_;
@ -135,7 +140,7 @@ template <class T, int multiplier = 1, int divider = 1, class LocalTimeScale = H
}
/// Indicates whether a flush has occurred since the last call to did_flush().
forceinline bool did_flush() {
[[nodiscard]] forceinline bool did_flush() {
const bool did_flush = did_flush_;
did_flush_ = false;
return did_flush;
@ -143,12 +148,12 @@ template <class T, int multiplier = 1, int divider = 1, class LocalTimeScale = H
/// @returns the number of cycles until the next sequence-point-based flush, if the embedded object
/// supports sequence points; @c LocalTimeScale() otherwise.
LocalTimeScale cycles_until_implicit_flush() const {
[[nodiscard]] LocalTimeScale cycles_until_implicit_flush() const {
return time_until_event_;
}
/// Indicates whether a sequence-point-caused flush will occur if the specified period is added.
forceinline bool will_flush(LocalTimeScale rhs) const {
[[nodiscard]] forceinline bool will_flush(LocalTimeScale rhs) const {
if constexpr (!has_sequence_points<T>::value) {
return false;
}
@ -178,48 +183,6 @@ template <class T, int multiplier = 1, int divider = 1, class LocalTimeScale = H
}
};
/*!
A RealTimeActor presents the same interface as a JustInTimeActor but doesn't defer work.
Time added will be performed immediately.
Its primary purpose is to allow consumers to remain flexible in their scheduling.
*/
template <class T, int multiplier = 1, int divider = 1, class LocalTimeScale = HalfCycles, class TargetTimeScale = LocalTimeScale> class RealTimeActor {
public:
template<typename... Args> RealTimeActor(Args&&... args) : object_(std::forward<Args>(args)...) {}
forceinline void operator += (const LocalTimeScale &rhs) {
if constexpr (multiplier == 1 && divider == 1) {
object_.run_for(TargetTimeScale(rhs));
return;
}
if constexpr (multiplier == 1) {
accumulated_time_ += rhs;
} else {
accumulated_time_ += rhs * multiplier;
}
if constexpr (divider == 1) {
const auto duration = accumulated_time_.template flush<TargetTimeScale>();
object_.run_for(duration);
} else {
const auto duration = accumulated_time_.template divide<TargetTimeScale>(LocalTimeScale(divider));
if(duration > TargetTimeScale(0))
object_.run_for(duration);
}
}
forceinline T *operator->() { return &object_; }
forceinline const T *operator->() const { return &object_; }
forceinline T *last_valid() { return &object_; }
forceinline void flush() {}
private:
T object_;
LocalTimeScale accumulated_time_;
};
/*!
A AsyncJustInTimeActor acts like a JustInTimeActor but additionally contains an AsyncTaskQueue.
Any time the amount of accumulated time crosses a threshold provided at construction time,