ui: fixed crash that had a chance to occur when showing source files of cdb/cbp/sonargraph project

This commit is contained in:
mlangkabel
2018-08-27 16:15:18 +02:00
parent 9bce2de57b
commit e8a4830229
4 changed files with 96 additions and 37 deletions
+1
View File
@@ -595,6 +595,7 @@ add_files(
utility/ScopedFunctor.cpp
utility/ScopedFunctor.h
utility/ScopedSwitcher.h
utility/SingleValueCache.h
utility/TimeStamp.cpp
utility/TimeStamp.h
utility/tracing.cpp
+48
View File
@@ -0,0 +1,48 @@
#ifndef SINGLE_VALUE_CACHE_H
#define SINGLE_VALUE_CACHE_H
#include <functional>
template <typename ValType>
class SingleValueCache
{
public:
SingleValueCache(std::function<ValType()> calculator);
ValType getValue();
void clear();
private:
std::function<ValType()> m_calculator;
ValType m_value;
bool m_hasValue;
};
template <typename ValType>
SingleValueCache<ValType>::SingleValueCache(std::function<ValType()> calculator)
: m_calculator(calculator)
, m_hasValue(false)
{
}
template <typename ValType>
ValType SingleValueCache<ValType>::getValue()
{
if (!m_hasValue)
{
m_value = m_calculator();
m_hasValue = true;
}
return m_value;
}
template <typename ValType>
void SingleValueCache<ValType>::clear()
{
if (m_hasValue)
{
m_value = ValType();
m_hasValue = false;
}
}
#endif // SINGLE_VALUE_CACHE_H