GlobalCache.h
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2017, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package ArmarX
19  * @author Mirko Waechter( mirko.waechter at kit dot edu)
20  * @date 2017
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 #pragma once
25 
26 #include <atomic>
27 #include <map>
28 #include <string>
29 
34 
35 namespace armarx
36 {
37 
38  template <class ObjectTypePtr, class Id = std::string>
40  {
41  public:
43  {
44  auto lock = getMapLock();
46  }
47 
49  {
50  auto lock = getMapLock();
52  }
53 
55  {
56  auto lock = getMapLock();
58  if (getInstanceCounter() == 0)
59  {
60  ARMARX_IMPORTANT << "Clearing global cache for type "
61  << armarx::GetTypeString<ObjectTypePtr>(true);
62  getCache().clear();
63  }
64  }
65 
66  bool
67  hasObject(const Id& id) const
68  {
69  auto lock = getMapLock();
70  return getCache().count(id) != 0;
71  }
72 
73  ObjectTypePtr
74  getCacheObject(const Id& id)
75  {
76  auto lock = getMapLock();
77  auto& map = getCache();
78  auto it = map.find(id);
79  if (it != map.end())
80  {
81  return it->second;
82  }
83  else
84  {
85  throw LocalException("key not found!");
86  }
87  }
88 
89  void
90  insertObject(const Id& id, const ObjectTypePtr& obj)
91  {
92  auto lock = getMapLock();
93  auto& map = getCache();
94  auto it = map.find(id);
95  if (it != map.end())
96  {
97  throw LocalException("key exists already");
98  }
99  map.insert(std::make_pair(id, obj));
100  }
101 
102  protected:
103  std::atomic<int>&
105  {
106  static std::atomic<int> InstanceCounter = {0};
107  return InstanceCounter;
108  }
109 
111  getMapLock() const
112  {
113  static Mutex mutex;
114  return ScopedLockPtr(new ScopedLock(mutex));
115  }
116 
117  std::map<Id, ObjectTypePtr>&
118  getCache() const
119  {
120  static std::map<Id, ObjectTypePtr> map;
121  return map;
122  }
123  };
124 
125 
126 } // namespace armarx
armarx::GlobalCache::GlobalCache
GlobalCache()
Definition: GlobalCache.h:42
armarx::GlobalCache::getCache
std::map< Id, ObjectTypePtr > & getCache() const
Definition: GlobalCache.h:118
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:190
armarx::GlobalCache::hasObject
bool hasObject(const Id &id) const
Definition: GlobalCache.h:67
armarx::ScopedLock
Mutex::scoped_lock ScopedLock
Definition: Synchronization.h:150
StringHelpers.h
armarx::GlobalCache::getInstanceCounter
std::atomic< int > & getInstanceCounter()
Definition: GlobalCache.h:104
Synchronization.h
armarx::GlobalCache::~GlobalCache
~GlobalCache()
Definition: GlobalCache.h:54
armarx::GlobalCache::getMapLock
ScopedLockPtr getMapLock() const
Definition: GlobalCache.h:111
armarx::GlobalCache::GlobalCache
GlobalCache(const GlobalCache &source)
Definition: GlobalCache.h:48
boost::source
Vertex source(const detail::edge_base< Directed, Vertex > &e, const PCG &)
Definition: point_cloud_graph.h:661
armarx::ScopedLockPtr
std::shared_ptr< ScopedLock > ScopedLockPtr
Definition: Synchronization.h:151
armarx::GlobalCache
Definition: GlobalCache.h:39
armarx::Mutex
boost::mutex Mutex
Definition: Synchronization.h:149
Logging.h
armarx::GlobalCache::getCacheObject
ObjectTypePtr getCacheObject(const Id &id)
Definition: GlobalCache.h:74
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::GlobalCache::insertObject
void insertObject(const Id &id, const ObjectTypePtr &obj)
Definition: GlobalCache.h:90
Exception.h