mirror of
https://github.com/jkriege2/JKQtPlotter.git
synced 2024-11-15 18:15:52 +08:00
NEW JKQTPDataCache class template to generate a (possibly thread-safe) key-value cache
This commit is contained in:
parent
6607432af0
commit
26a86df032
@ -27,6 +27,7 @@ set(SOURCES
|
|||||||
${CMAKE_CURRENT_LIST_DIR}/jkqtpcodestructuring.cpp
|
${CMAKE_CURRENT_LIST_DIR}/jkqtpcodestructuring.cpp
|
||||||
${CMAKE_CURRENT_LIST_DIR}/jkqtpbasicimagetools.cpp
|
${CMAKE_CURRENT_LIST_DIR}/jkqtpbasicimagetools.cpp
|
||||||
${CMAKE_CURRENT_LIST_DIR}/jkqtpconcurrencytools.cpp
|
${CMAKE_CURRENT_LIST_DIR}/jkqtpconcurrencytools.cpp
|
||||||
|
${CMAKE_CURRENT_LIST_DIR}/jkqtpcachingtools.cpp
|
||||||
${CMAKE_CURRENT_LIST_DIR}/jkqtpgeometrytools.cpp
|
${CMAKE_CURRENT_LIST_DIR}/jkqtpgeometrytools.cpp
|
||||||
${CMAKE_CURRENT_LIST_DIR}/jkqtpdrawingtools.cpp
|
${CMAKE_CURRENT_LIST_DIR}/jkqtpdrawingtools.cpp
|
||||||
${CMAKE_CURRENT_LIST_DIR}/jkqtpenhancedpainter.cpp
|
${CMAKE_CURRENT_LIST_DIR}/jkqtpenhancedpainter.cpp
|
||||||
@ -62,6 +63,8 @@ set(HEADERS
|
|||||||
$<INSTALL_INTERFACE:jkqtpgeometrytools.h>
|
$<INSTALL_INTERFACE:jkqtpgeometrytools.h>
|
||||||
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/jkqtpconcurrencytools.h>
|
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/jkqtpconcurrencytools.h>
|
||||||
$<INSTALL_INTERFACE:jkqtpconcurrencytools.h>
|
$<INSTALL_INTERFACE:jkqtpconcurrencytools.h>
|
||||||
|
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/jkqtpcachingtools.h>
|
||||||
|
$<INSTALL_INTERFACE:jkqtpcachingtools.h>
|
||||||
)
|
)
|
||||||
|
|
||||||
include(CMakePackageConfigHelpers)
|
include(CMakePackageConfigHelpers)
|
||||||
@ -80,6 +83,10 @@ function(JKQtCommon_setDefaultLibOptions TARGETNAME)
|
|||||||
target_compile_options(${TARGETNAME} PUBLIC /EHsc)
|
target_compile_options(${TARGETNAME} PUBLIC /EHsc)
|
||||||
target_compile_definitions(${TARGETNAME} PUBLIC NOMINMAX)
|
target_compile_definitions(${TARGETNAME} PUBLIC NOMINMAX)
|
||||||
endif()
|
endif()
|
||||||
|
if(JKQtPlotter_BUILD_WITH_TIMING_INFO_OUTPUT)
|
||||||
|
target_compile_definitions(${TARGETNAME} PRIVATE JKQTBP_AUTOTIMER)
|
||||||
|
endif()
|
||||||
|
|
||||||
target_include_directories(${TARGETNAME} PUBLIC
|
target_include_directories(${TARGETNAME} PUBLIC
|
||||||
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/../>
|
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/../>
|
||||||
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
|
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
|
||||||
|
22
lib/jkqtcommon/jkqtpcachingtools.cpp
Normal file
22
lib/jkqtcommon/jkqtpcachingtools.cpp
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
/*
|
||||||
|
Copyright (c) 2008-2024 Jan W. Krieger (<jan@jkrieger.de>)
|
||||||
|
|
||||||
|
last modification: $LastChangedDate$ (revision $Rev$)
|
||||||
|
|
||||||
|
This software is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU Lesser General Public License (LGPL) as published by
|
||||||
|
the Free Software Foundation, either version 2.1 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU Lesser General Public License (LGPL) for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public License (LGPL)
|
||||||
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#include "jkqtpcachingtools.h"
|
95
lib/jkqtcommon/jkqtpcachingtools.h
Normal file
95
lib/jkqtcommon/jkqtpcachingtools.h
Normal file
@ -0,0 +1,95 @@
|
|||||||
|
/*
|
||||||
|
Copyright (c) 2008-2024 Jan W. Krieger (<jan@jkrieger.de>)
|
||||||
|
|
||||||
|
last modification: $LastChangedDate$ (revision $Rev$)
|
||||||
|
|
||||||
|
This software is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU Lesser General Public License (LGPL) as published by
|
||||||
|
the Free Software Foundation, either version 2.1 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU Lesser General Public License (LGPL) for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public License (LGPL)
|
||||||
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef JKQTPCACHINGTOOLS_H
|
||||||
|
#define JKQTPCACHINGTOOLS_H
|
||||||
|
|
||||||
|
#include "jkqtcommon/jkqtcommon_imexport.h"
|
||||||
|
#include <QReadWriteLock>
|
||||||
|
#include <QReadLocker>
|
||||||
|
#include <QWriteLocker>
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
|
/** \brief this class can be used to implement a general cache for values
|
||||||
|
* \ingroup jkqtptools_concurrency
|
||||||
|
*
|
||||||
|
* It is typically used to generate a static (ThreadSafe=true) of thread_local (ThreadSafe=false) cache inside a function.
|
||||||
|
*
|
||||||
|
* The class is parametrized by a key (TKeay) and value (TData) data type and receives (in the constructor) a functor that
|
||||||
|
* calculates the data for a key. An additional template parameter ThreadSafe indicates (true|false) whether the class
|
||||||
|
* is implemented in a thread-safe way (for static instances) or not (then it should be used as thread_local instances in a
|
||||||
|
* multi-threaded environment or in a single-thread environment).
|
||||||
|
*
|
||||||
|
* The class member function get(key) returns a value for a given key, which is either taken from the internal cache,
|
||||||
|
* or generated using the functor provided to the constructor. In the latter case the generated value is stored in the internal cache.
|
||||||
|
*
|
||||||
|
* Internally the cache maps TKey to TData, but the signature of the get()-function and the generator functor actually uses TKeyInSignature,
|
||||||
|
* which may differ from TKey. The only limitation is that TKeyInSignature can be converted/assigned to a TKey
|
||||||
|
*/
|
||||||
|
template <class TData, class TKey, bool ThreadSafe=true, class TKeyInSignature=TKey>
|
||||||
|
struct JKQTPDataCache {
|
||||||
|
template <typename FF>
|
||||||
|
inline JKQTPDataCache(FF generateData):
|
||||||
|
m_generateData(std::forward<FF>(generateData))
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class... Args>
|
||||||
|
inline TData get_inline(Args... args) {
|
||||||
|
return get(TKeyInSignature(args...));
|
||||||
|
}
|
||||||
|
|
||||||
|
template <bool TSS=ThreadSafe>
|
||||||
|
inline TData get(const typename std::enable_if<TSS, TKeyInSignature>::type& key) {
|
||||||
|
const TKey cacheKey=key;
|
||||||
|
|
||||||
|
QReadLocker lockR(&m_mutex);
|
||||||
|
if (m_cache.contains(cacheKey)) return m_cache[cacheKey];
|
||||||
|
lockR.unlock();
|
||||||
|
|
||||||
|
QWriteLocker lockW(&m_mutex);
|
||||||
|
if (m_cache.contains(cacheKey)) return m_cache[cacheKey];
|
||||||
|
|
||||||
|
const auto newData=m_generateData(key);
|
||||||
|
m_cache[cacheKey]=newData;
|
||||||
|
return newData;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <bool TSS=ThreadSafe>
|
||||||
|
inline TData get(const typename std::enable_if<!TSS, TKeyInSignature>::type& key) {
|
||||||
|
const TKey cacheKey=key;
|
||||||
|
|
||||||
|
if (m_cache.contains(cacheKey)) return m_cache[cacheKey];
|
||||||
|
const auto newData=m_generateData(key);
|
||||||
|
m_cache[cacheKey]=newData;
|
||||||
|
return newData;
|
||||||
|
}
|
||||||
|
private:
|
||||||
|
QHash<TKey, TData> m_cache;
|
||||||
|
mutable typename std::enable_if<ThreadSafe, QReadWriteLock>::type m_mutex;
|
||||||
|
const std::function<TData(TKeyInSignature)> m_generateData;
|
||||||
|
Q_DISABLE_COPY(JKQTPDataCache)
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // JKQTPCACHINGTOOLS_H
|
Loading…
Reference in New Issue
Block a user