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-2013 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/defines.hh>
27 #include <tbb/recursive_mutex.h>
28 #include <tbb/spin_mutex.h>
29 
31 typedef tbb::recursive_mutex::scoped_lock CRecursiveScopedLock;
32 
46 public:
51  CProductCache(const std::string& name);
52 
61  void enable_write(bool enable);
62 
64  void clear();
65 protected:
67  bool is_enabled() const;
68 private:
69  virtual void do_clear() = 0;
70  bool m_enabled;
71  mutable tbb::spin_mutex m_enable_mutex;
72 };
73 
74 
81 template <typename ProductPtr>
83 public:
84 
85 
89  TProductCache(const std::string& descriptor);
90 
91 
97  ProductPtr get(const std::string& name) const;
98 
99 
105  void add(const std::string& name, ProductPtr product);
106 private:
107 
108  virtual void do_clear();
109 
110  typedef std::map<std::string, ProductPtr> CMap;
111  CMap m_cache;
112  mutable tbb::recursive_mutex m_cache_mutex;
113 };
114 
115 
122 public:
124  static CProductCacheHandler& instance();
125 
127  void clear_all();
128 
134  void register_cache(const std::string& name, CProductCache* cache);
135 private:
137 
138  CProductCacheHandler(const CProductCacheHandler& other) = delete;
139  CProductCacheHandler& operator = (const CProductCacheHandler& other) = delete;
140 
141  std::map<std::string, CProductCache*> m_caches;
142  static CMutex m_creation_mutex;
143 };
144 
145 
147 
148 template <typename ProductPtr>
149 TProductCache<ProductPtr>::TProductCache(const std::string& descriptor):
150  CProductCache(descriptor)
151 {
152 }
153 
154 template <typename ProductPtr>
155 ProductPtr TProductCache<ProductPtr>::get(const std::string& name) const
156 {
157  CRecursiveScopedLock lock(m_cache_mutex);
158  auto i = m_cache.find(name);
159  if (i != m_cache.end())
160  return i->second;
161  return ProductPtr();
162 }
163 
164 template <typename ProductPtr>
165 void TProductCache<ProductPtr>::add(const std::string& name, ProductPtr product)
166 {
167  if (is_enabled()) {
168  CRecursiveScopedLock lock(m_cache_mutex);
169  //re-check whether another thead already added the product
170  if (!get(name))
171  m_cache[name] = product;
172  }
173 }
174 
175 template <typename ProductPtr>
177 {
178  CRecursiveScopedLock lock(m_cache_mutex);
179  m_cache.clear();
180 }
181 
183 
184 #endif