-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[wombat/utils] Created a Cached<T> API for lazily loading values
This allows us to create optimisations for values that do not need to be called regularly.
- Loading branch information
1 parent
af62b5d
commit c87bc00
Showing
2 changed files
with
88 additions
and
0 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,31 @@ | ||
#include "utils/Cache.h" | ||
|
||
#include "utils/Util.h" | ||
|
||
template <typename T> | ||
wom::utils::Cached<T>::Cached(std::function<T()> supplier) | ||
: m_supplier{supplier}, k_timestamp{wom::utils::now()}, k_dt{20_ms} {}; | ||
|
||
template <typename T> | ||
wom::utils::Cached<T>::Cached(std::function<T()> supplier, units::second_t dt) | ||
: m_supplier{supplier}, k_timestamp{wom::utils::now()}, k_dt{dt} {}; | ||
|
||
template <typename T> | ||
wom::utils::Cached<T>& wom::utils::Cached<T>::Update() { | ||
this->k_timestamp = wom::utils::now(); | ||
this->m_value = m_supplier(); | ||
return this; | ||
} | ||
|
||
template <typename T> | ||
T& wom::utils::Cached<T>::GetValue() { | ||
if (IsStale()) { | ||
Update(); | ||
} | ||
return m_value; | ||
} | ||
|
||
template <typename T> | ||
bool wom::utils::Cached<T>::IsStale() { | ||
return wom::utils::now() - this->k_timestamp > k_dt; | ||
} |
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,57 @@ | ||
#pragma once | ||
|
||
#include <functional> | ||
|
||
#include <units/time.h> | ||
|
||
namespace wom { | ||
namespace utils { | ||
|
||
/* A class for a cached value that can be updated lazily */ | ||
template <typename T> | ||
class Cached { | ||
public: | ||
/** | ||
* Creates a new Cached<T> with the given supplier. Sets the validity period to 20 milliseconds by default. | ||
* | ||
* @param supplier A function that sources the value. | ||
*/ | ||
Cached(std::function<T()> supplier); | ||
|
||
/** | ||
* Creates a new Cached<T> with the given supplier. | ||
* | ||
* @param supplier A function that sources the value. | ||
* @param dt The validity length of the stored value. | ||
*/ | ||
Cached(std::function<T()> supplier, units::second_t dt); | ||
|
||
Cached(Cached&&) = default; | ||
Cached& operator=(Cached&&) = default; | ||
|
||
/** | ||
* Updates the Cached class with the current timestamp and value. | ||
* | ||
* @return The updated class. | ||
*/ | ||
Cached<T>& Update(); | ||
|
||
/** | ||
* Gets the cached value. | ||
*/ | ||
T& GetValue(); | ||
|
||
/** | ||
* Checks whether or not the stored value is stale. | ||
*/ | ||
inline bool IsStale(); | ||
|
||
private: | ||
T& m_value; | ||
std::function<T()> m_supplier; | ||
const units::second_t k_timestamp; | ||
const units::second_t k_dt; | ||
}; | ||
|
||
} // namespace utils | ||
} // namespace wom |