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 public:
47  CProductCache(const std::string& name);
48 
57  void enable_write(bool enable);
58 
60  void clear();
61 protected:
63  bool is_enabled() const;
64 private:
65  virtual void do_clear() = 0;
66  bool m_enabled;
67  mutable CMutex m_enable_mutex;
68 };
69 
70 
77 template <typename ProductPtr>
79 public:
80 
81 
85  TProductCache(const std::string& descriptor);
86 
87 
93  ProductPtr get(const std::string& name) const;
94 
95 
101  void add(const std::string& name, ProductPtr product);
102 private:
103 
104  virtual void do_clear();
105 
106  typedef std::map<std::string, ProductPtr> CMap;
107  CMap m_cache;
108  mutable CRecursiveMutex m_cache_mutex;
109 };
110 
111 
118 public:
120  static CProductCacheHandler& instance();
121 
123  void clear_all();
124 
130  void register_cache(const std::string& name, CProductCache* cache);
131 private:
133 
134  CProductCacheHandler(const CProductCacheHandler& other) = delete;
135  CProductCacheHandler& operator = (const CProductCacheHandler& other) = delete;
136 
137  std::map<std::string, CProductCache*> m_caches;
138  static CMutex m_creation_mutex;
139 };
140 
141 
143 
144 template <typename ProductPtr>
145 TProductCache<ProductPtr>::TProductCache(const std::string& descriptor):
146  CProductCache(descriptor)
147 {
148 }
149 
150 template <typename ProductPtr>
151 ProductPtr TProductCache<ProductPtr>::get(const std::string& name) const
152 {
153  CRecursiveScopedLock lock(m_cache_mutex);
154  auto i = m_cache.find(name);
155  if (i != m_cache.end())
156  return i->second;
157  return ProductPtr();
158 }
159 
160 template <typename ProductPtr>
161 void TProductCache<ProductPtr>::add(const std::string& name, ProductPtr product)
162 {
163  if (is_enabled()) {
164  CRecursiveScopedLock lock(m_cache_mutex);
165  //re-check whether another thead already added the product
166  if (!get(name))
167  m_cache[name] = product;
168  }
169 }
170 
171 template <typename ProductPtr>
173 {
174  CRecursiveScopedLock lock(m_cache_mutex);
175  m_cache.clear();
176 }
177 
179 
180 #endif
The type specific product cache.
Definition: productcache.hh:78
The singleton that handles all product caches.
void add(const std::string &name, ProductPtr product)
base class for the product cache
Definition: productcache.hh:41
bool is_enabled() const
#define NS_MIA_BEGIN
conveniance define to start the mia namespace
Definition: defines.hh:33
ProductPtr get(const std::string &name) const
std::recursive_mutex CRecursiveMutex
#define EXPORT_CORE
Macro to manage Visual C++ style dllimport/dllexport.
Definition: defines.hh:101
TProductCache(const std::string &descriptor)
std::mutex CMutex
#define NS_MIA_END
conveniance define to end the mia namespace
Definition: defines.hh:36