productcache.hh
Go to the documentation of this file.
/* -*- mia-c++ -*-
*
* This file is part of MIA - a toolbox for medical image analysis
* Copyright (c) Leipzig, Madrid 1999-2017 Gert Wollny
*
* MIA is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MIA; if not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef mia_core_productcache_hh
#define mia_core_productcache_hh
#include <map>
#include <string>
#include <mia/core/parallel.hh>
NS_MIA_BEGIN
class EXPORT_CORE CProductCache
{
public:
CProductCache(const std::string& name);
void enable_write(bool enable);
void clear();
protected:
bool is_enabled() const;
private:
virtual void do_clear() = 0;
bool m_enabled;
mutable CMutex m_enable_mutex;
};
template <typename ProductPtr>
class EXPORT_CORE TProductCache: public CProductCache
{
public:
TProductCache(const std::string& descriptor);
ProductPtr get(const std::string& name) const;
void add(const std::string& name, ProductPtr product);
private:
virtual void do_clear();
typedef std::map<std::string, ProductPtr> CMap;
CMap m_cache;
mutable CRecursiveMutex m_cache_mutex;
};
class EXPORT_CORE CProductCacheHandler
{
public:
static CProductCacheHandler& instance();
void clear_all();
void register_cache(const std::string& name, CProductCache *cache);
private:
CProductCacheHandler();
CProductCacheHandler(const CProductCacheHandler& other) = delete;
CProductCacheHandler& operator = (const CProductCacheHandler& other) = delete;
std::map<std::string, CProductCache *> m_caches;
static CMutex m_creation_mutex;
};
template <typename ProductPtr>
TProductCache<ProductPtr>::TProductCache(const std::string& descriptor):
CProductCache(descriptor)
{
}
template <typename ProductPtr>
ProductPtr TProductCache<ProductPtr>::get(const std::string& name) const
{
CRecursiveScopedLock lock(m_cache_mutex);
auto i = m_cache.find(name);
if (i != m_cache.end())
return i->second;
return ProductPtr();
}
template <typename ProductPtr>
void TProductCache<ProductPtr>::add(const std::string& name, ProductPtr product)
{
if (is_enabled()) {
CRecursiveScopedLock lock(m_cache_mutex);
//re-check whether another thead already added the product
if (!get(name))
m_cache[name] = product;
}
}
template <typename ProductPtr>
void TProductCache<ProductPtr>::do_clear()
{
CRecursiveScopedLock lock(m_cache_mutex);
m_cache.clear();
}
NS_MIA_END
#endif
CProductCacheHandler::register_cache
void register_cache(const std::string &name, CProductCache *cache)
CProductCacheHandler::instance
static CProductCacheHandler & instance()
CProductCacheHandler::clear_all
void clear_all()
clears all registered product caches
CProductCache
base class for the product cache
Definition productcache.hh:42
CProductCache::enable_write
void enable_write(bool enable)
CProductCache::CProductCache
CProductCache(const std::string &name)
CProductCache::clear
void clear()
clear the cache
CProductCache::is_enabled
bool is_enabled() const
TProductCache::TProductCache
TProductCache(const std::string &descriptor)
Definition productcache.hh:148
TProductCache::get
ProductPtr get(const std::string &name) const
Definition productcache.hh:154
TProductCache::add
void add(const std::string &name, ProductPtr product)
Definition productcache.hh:166
NS_MIA_BEGIN
#define NS_MIA_BEGIN
conveniance define to start the mia namespace
Definition defines.hh:33
EXPORT_CORE
#define EXPORT_CORE
Macro to manage Visual C++ style dllimport/dllexport.
Definition defines.hh:101
NS_MIA_END
#define NS_MIA_END
conveniance define to end the mia namespace
Definition defines.hh:36
parallel.hh
CRecursiveMutex
std::recursive_mutex CRecursiveMutex
Definition parallelcxx11.hh:35
CRecursiveScopedLock
TScopedLock< CRecursiveMutex > CRecursiveScopedLock
Definition parallelcxx11.hh:81
CMutex
std::mutex CMutex
Definition parallelcxx11.hh:34