-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #137 from ImJimmi/misc-utils
Various useful bits and bobs
- Loading branch information
Showing
16 changed files
with
767 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#pragma once | ||
|
||
namespace jive | ||
{ | ||
template <class... Variants> | ||
struct Visitor : Variants... | ||
{ | ||
using Variants::operator()...; | ||
}; | ||
|
||
template <class... Variants> | ||
Visitor(Variants...) -> Visitor<Variants...>; | ||
} // namespace jive |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
#include "jive_Timer.h" | ||
|
||
namespace jive | ||
{ | ||
#if JIVE_UNIT_TESTS | ||
FakeTime::~FakeTime() | ||
{ | ||
clearSingletonInstance(); | ||
} | ||
|
||
void FakeTime::addListener(Listener& listener) | ||
{ | ||
listeners.add(&listener); | ||
} | ||
|
||
void FakeTime::removeListener(Listener& listener) | ||
{ | ||
listeners.remove(&listener); | ||
} | ||
|
||
void FakeTime::incrementTime(juce::RelativeTime increment) | ||
{ | ||
getInstance()->incrementTimeInternal(increment); | ||
} | ||
|
||
juce::Time FakeTime::now() noexcept | ||
{ | ||
return getInstance()->currentTime; | ||
} | ||
|
||
void FakeTime::incrementTimeInternal(juce::RelativeTime increment) | ||
{ | ||
currentTime += increment; | ||
listeners.call(&Listener::timeChanged); | ||
} | ||
|
||
JUCE_IMPLEMENT_SINGLETON(FakeTime) | ||
#endif | ||
|
||
Timer::Timer(Timer::Callback timerCallback, | ||
juce::RelativeTime callbackInterval) | ||
: callback{ timerCallback } | ||
, interval{ callbackInterval } | ||
{ | ||
#if JIVE_UNIT_TESTS | ||
timeLastCallbackInvoked = FakeTime::now(); | ||
FakeTime::getInstance()->addListener(*this); | ||
#else | ||
timeLastCallbackInvoked = juce::Time::getCurrentTime(); | ||
startTimer(static_cast<int>(interval.inMilliseconds())); | ||
#endif | ||
} | ||
|
||
Timer::~Timer() | ||
{ | ||
#if JIVE_UNIT_TESTS | ||
FakeTime::getInstance()->removeListener(*this); | ||
#endif | ||
} | ||
|
||
#if JIVE_UNIT_TESTS | ||
void Timer::timeChanged() | ||
{ | ||
for (auto elapsed = FakeTime::now() - timeLastCallbackInvoked; | ||
elapsed.inMilliseconds() > interval.inMilliseconds(); | ||
elapsed -= interval) | ||
{ | ||
timeLastCallbackInvoked = FakeTime::now() - elapsed + interval; | ||
callback(timeLastCallbackInvoked); | ||
} | ||
} | ||
#else | ||
void Timer::timerCallback() | ||
{ | ||
callback(juce::Time::getCurrentTime()); | ||
} | ||
#endif | ||
} // namespace jive | ||
|
||
#if JIVE_UNIT_TESTS | ||
class TimerUnitTest : public juce::UnitTest | ||
{ | ||
public: | ||
TimerUnitTest() | ||
: juce::UnitTest{ "jive::Timer", "jive" } | ||
{ | ||
} | ||
|
||
void runTest() final | ||
{ | ||
const auto initialTime = jive::FakeTime::now(); | ||
|
||
beginTest("faking the passing of time"); | ||
jive::FakeTime::incrementTime(juce::RelativeTime::seconds(5.3)); | ||
expectEquals(jive::FakeTime::now(), initialTime + juce::RelativeTime::seconds(5.3)); | ||
|
||
beginTest("getting callbacks"); | ||
juce::Array<juce::Time> invokeTimes; | ||
jive::Timer timer{ | ||
[&invokeTimes](juce::Time now) mutable { | ||
invokeTimes.add(now); | ||
}, | ||
juce::RelativeTime::seconds(0.1), | ||
}; | ||
jive::FakeTime::incrementTime(juce::RelativeTime::seconds(0.25)); | ||
expectEquals(invokeTimes.size(), 2); | ||
expectEquals(invokeTimes[0], initialTime + juce::RelativeTime::seconds(5.4)); | ||
expectEquals(invokeTimes[1], initialTime + juce::RelativeTime::seconds(5.5)); | ||
|
||
jive::FakeTime::incrementTime(juce::RelativeTime::seconds(0.3333)); | ||
expectEquals(invokeTimes.size(), 5); | ||
expectEquals(invokeTimes[0], initialTime + juce::RelativeTime::seconds(5.4)); | ||
expectEquals(invokeTimes[1], initialTime + juce::RelativeTime::seconds(5.5)); | ||
expectEquals(invokeTimes[2], initialTime + juce::RelativeTime::seconds(5.6)); | ||
expectEquals(invokeTimes[3], initialTime + juce::RelativeTime::seconds(5.7)); | ||
expectEquals(invokeTimes[4], initialTime + juce::RelativeTime::seconds(5.8)); | ||
} | ||
}; | ||
|
||
static TimerUnitTest timerUnitTest; | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#pragma once | ||
|
||
namespace jive | ||
{ | ||
#if JIVE_UNIT_TESTS | ||
class FakeTime : public juce::DeletedAtShutdown | ||
{ | ||
public: | ||
struct Listener | ||
{ | ||
virtual ~Listener() = default; | ||
|
||
virtual void timeChanged() = 0; | ||
}; | ||
|
||
FakeTime() = default; | ||
~FakeTime(); | ||
|
||
void addListener(Listener& listener); | ||
void removeListener(Listener& listener); | ||
|
||
static void incrementTime(juce::RelativeTime); | ||
static juce::Time now() noexcept; | ||
|
||
JUCE_DECLARE_SINGLETON(FakeTime, false) | ||
|
||
private: | ||
void incrementTimeInternal(juce::RelativeTime); | ||
|
||
juce::Time currentTime; | ||
juce::ListenerList<Listener> listeners; | ||
}; | ||
#endif | ||
|
||
class Timer | ||
#if JIVE_UNIT_TESTS | ||
: private FakeTime::Listener | ||
#else | ||
: private juce::Timer | ||
#endif | ||
{ | ||
public: | ||
using Callback = std::function<void(juce::Time)>; | ||
|
||
Timer(Callback timerCallback, | ||
juce::RelativeTime callbackInterval); | ||
~Timer(); | ||
|
||
private: | ||
#if JIVE_UNIT_TESTS | ||
void timeChanged() final; | ||
#else | ||
void timerCallback() final; | ||
#endif | ||
Callback callback; | ||
juce::RelativeTime interval; | ||
juce::Time timeLastCallbackInvoked; | ||
}; | ||
|
||
[[nodiscard]] static inline juce::Time now() noexcept | ||
{ | ||
#if JIVE_UNIT_TESTS | ||
return FakeTime::now(); | ||
#else | ||
return juce::Time::getCurrentTime(); | ||
#endif | ||
} | ||
} // namespace jive |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.