My Project
productcache.hh
Go to the documentation of this file.
1/* -*- mia-c++ -*-
2 *
3 * This file is part of MIA - a toolbox for medical image analysis
4 * Copyright (c) Leipzig, Madrid 1999-2017 Gert Wollny
5 *
6 * MIA is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with MIA; if not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21#ifndef mia_core_productcache_hh
22#define mia_core_productcache_hh
23
24#include <map>
25#include <string>
26#include <mia/core/parallel.hh>
27
42{
43public:
48 CProductCache(const std::string& name);
49
58 void enable_write(bool enable);
59
61 void clear();
62protected:
64 bool is_enabled() const;
65private:
66 virtual void do_clear() = 0;
67 bool m_enabled;
68 mutable CMutex m_enable_mutex;
69};
70
71
78template <typename ProductPtr>
80{
81public:
82
83
87 TProductCache(const std::string& descriptor);
88
89
95 ProductPtr get(const std::string& name) const;
96
97
103 void add(const std::string& name, ProductPtr product);
104private:
105
106 virtual void do_clear();
107
108 typedef std::map<std::string, ProductPtr> CMap;
109 CMap m_cache;
110 mutable CRecursiveMutex m_cache_mutex;
111};
112
113
120{
121public:
124
126 void clear_all();
127
133 void register_cache(const std::string& name, CProductCache *cache);
134private:
136
137 CProductCacheHandler(const CProductCacheHandler& other) = delete;
138 CProductCacheHandler& operator = (const CProductCacheHandler& other) = delete;
139
140 std::map<std::string, CProductCache *> m_caches;
141 static CMutex m_creation_mutex;
142};
143
144
146
147template <typename ProductPtr>
148TProductCache<ProductPtr>::TProductCache(const std::string& descriptor):
149 CProductCache(descriptor)
150{
151}
152
153template <typename ProductPtr>
154ProductPtr TProductCache<ProductPtr>::get(const std::string& name) const
155{
156 CRecursiveScopedLock lock(m_cache_mutex);
157 auto i = m_cache.find(name);
158
159 if (i != m_cache.end())
160 return i->second;
161
162 return ProductPtr();
163}
164
165template <typename ProductPtr>
166void TProductCache<ProductPtr>::add(const std::string& name, ProductPtr product)
167{
168 if (is_enabled()) {
169 CRecursiveScopedLock lock(m_cache_mutex);
170
171 //re-check whether another thead already added the product
172 if (!get(name))
173 m_cache[name] = product;
174 }
175}
176
177template <typename ProductPtr>
179{
180 CRecursiveScopedLock lock(m_cache_mutex);
181 m_cache.clear();
182}
183
185
186#endif
The singleton that handles all product caches.
void register_cache(const std::string &name, CProductCache *cache)
static CProductCacheHandler & instance()
void clear_all()
clears all registered product caches
base class for the product cache
void enable_write(bool enable)
CProductCache(const std::string &name)
void clear()
clear the cache
bool is_enabled() const
The type specific product cache.
TProductCache(const std::string &descriptor)
ProductPtr get(const std::string &name) const
void add(const std::string &name, ProductPtr product)
#define NS_MIA_BEGIN
conveniance define to start the mia namespace
Definition defines.hh:33
#define EXPORT_CORE
Macro to manage Visual C++ style dllimport/dllexport.
Definition defines.hh:101
#define NS_MIA_END
conveniance define to end the mia namespace
Definition defines.hh:36
std::recursive_mutex CRecursiveMutex
std::mutex CMutex