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 <string>
27 #include <atomic>
28 #include <map>
33 
34 namespace armarx
35 {
36 
37  template <class ObjectTypePtr, class Id = std::string>
39  {
40  public:
42  {
43  auto lock = getMapLock();
45  }
47  {
48  auto lock = getMapLock();
50  }
51 
53  {
54  auto lock = getMapLock();
56  if (getInstanceCounter() == 0)
57  {
58  ARMARX_IMPORTANT << "Clearing global cache for type " << armarx::GetTypeString<ObjectTypePtr>(true);
59  getCache().clear();
60  }
61  }
62 
63  bool hasObject(const Id& id) const
64  {
65  auto lock = getMapLock();
66  return getCache().count(id) != 0;
67  }
68  ObjectTypePtr getCacheObject(const Id& id)
69  {
70  auto lock = getMapLock();
71  auto& map = getCache();
72  auto it = map.find(id);
73  if (it != map.end())
74  {
75  return it->second;
76  }
77  else
78  {
79  throw LocalException("key not found!");
80  }
81  }
82 
83  void insertObject(const Id& id, const ObjectTypePtr& obj)
84  {
85  auto lock = getMapLock();
86  auto& map = getCache();
87  auto it = map.find(id);
88  if (it != map.end())
89  {
90  throw LocalException("key exists already");
91  }
92  map.insert(std::make_pair(id, obj));
93  }
94  protected:
95  std::atomic<int>& getInstanceCounter()
96  {
97  static std::atomic<int> InstanceCounter = {0};
98  return InstanceCounter;
99  }
100 
102  {
103  static Mutex mutex;
104  return ScopedLockPtr(new ScopedLock(mutex));
105  }
106 
107  std::map<Id, ObjectTypePtr>& getCache() const
108  {
109  static std::map<Id, ObjectTypePtr> map;
110  return map;
111  }
112 
113  };
114 
115 
116 }
117 
118 
armarx::GlobalCache::GlobalCache
GlobalCache()
Definition: GlobalCache.h:41
armarx::GlobalCache::getCache
std::map< Id, ObjectTypePtr > & getCache() const
Definition: GlobalCache.h:107
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:183
armarx::GlobalCache::hasObject
bool hasObject(const Id &id) const
Definition: GlobalCache.h:63
armarx::ScopedLock
Mutex::scoped_lock ScopedLock
Definition: Synchronization.h:132
StringHelpers.h
armarx::GlobalCache::getInstanceCounter
std::atomic< int > & getInstanceCounter()
Definition: GlobalCache.h:95
Synchronization.h
armarx::GlobalCache::~GlobalCache
~GlobalCache()
Definition: GlobalCache.h:52
armarx::GlobalCache::getMapLock
ScopedLockPtr getMapLock() const
Definition: GlobalCache.h:101
armarx::GlobalCache::GlobalCache
GlobalCache(const GlobalCache &source)
Definition: GlobalCache.h:46
boost::source
Vertex source(const detail::edge_base< Directed, Vertex > &e, const PCG &)
Definition: point_cloud_graph.h:681
armarx::ScopedLockPtr
std::shared_ptr< ScopedLock > ScopedLockPtr
Definition: Synchronization.h:133
armarx::GlobalCache
Definition: GlobalCache.h:38
armarx::Mutex
boost::mutex Mutex
Definition: Synchronization.h:131
Logging.h
armarx::GlobalCache::getCacheObject
ObjectTypePtr getCacheObject(const Id &id)
Definition: GlobalCache.h:68
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::GlobalCache::insertObject
void insertObject(const Id &id, const ObjectTypePtr &obj)
Definition: GlobalCache.h:83
Exception.h