summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMikaël Capelle <capelle.mikael@gmail.com>2020-05-25 17:56:45 +0200
committerMikaël Capelle <capelle.mikael@gmail.com>2020-05-25 17:56:45 +0200
commit86f516953df9dc80c6726c30f47e15b660a89a4e (patch)
tree6d2f026341cd8c0998e45eaa5595648923a8ce85 /src
parente7afedaebee928ac64bc009f7019117b1cae0b69 (diff)
Update MemoizedLock to allow more flexible callable.
Diffstat (limited to 'src')
-rw-r--r--src/thread_utils.h14
1 files changed, 8 insertions, 6 deletions
diff --git a/src/thread_utils.h b/src/thread_utils.h
index 6c044138..f0067b6a 100644
--- a/src/thread_utils.h
+++ b/src/thread_utils.h
@@ -1,6 +1,7 @@
#ifndef MO2_THREAD_UTILS_H
#define MO2_THREAD_UTILS_H
+#include <functional>
#include <mutex>
#include <thread>
@@ -19,11 +20,12 @@ namespace MOShared {
* @tparam T Type of value ot memoized.
* @tparam Fn Type of the callback.
*/
-template <class T, class Fn>
+template <class T, class Fn = std::function<T()>>
struct MemoizedLocked {
- MemoizedLocked(Fn callback, T value = {}) :
- m_Fn{ callback }, m_Value{ std::move(value) } { }
+ template <class Callable>
+ MemoizedLocked(Callable &&callable, T value = {}) :
+ m_Fn{ std::forward<Callable>(callable) }, m_Value{ std::move(value) } { }
template <class... Args>
T& value(Args&&... args) const {
@@ -31,14 +33,14 @@ struct MemoizedLocked {
std::scoped_lock lock(m_Mutex);
if (m_NeedUpdating) {
m_Value = std::invoke(m_Fn, std::forward<Args>(args)... );
- m_NeedUpdating.store(false);
+ m_NeedUpdating = false;
}
}
return m_Value;
}
- void invalidate() const {
- m_NeedUpdating.store(true);
+ void invalidate() {
+ m_NeedUpdating = true;
}
private: