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
35namespace armarx
36{
37
38 template <class ObjectTypePtr, class Id = std::string>
40 {
41 public:
43 {
44 auto lock = getMapLock();
46 }
47
48 GlobalCache(const GlobalCache& source)
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 "
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
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
std::map< Id, ObjectTypePtr > & getCache() const
bool hasObject(const Id &id) const
Definition GlobalCache.h:67
ScopedLockPtr getMapLock() const
void insertObject(const Id &id, const ObjectTypePtr &obj)
Definition GlobalCache.h:90
GlobalCache(const GlobalCache &source)
Definition GlobalCache.h:48
std::atomic< int > & getInstanceCounter()
ObjectTypePtr getCacheObject(const Id &id)
Definition GlobalCache.h:74
#define ARMARX_IMPORTANT
The logging level for always important information, but expected behaviour (in contrast to ARMARX_WAR...
Definition Logging.h:190
std::shared_ptr< ScopedLock > ScopedLockPtr
boost::mutex Mutex
Mutex::scoped_lock ScopedLock
This file offers overloads of toIce() and fromIce() functions for STL container types.
std::string GetTypeString(const std::type_info &tinf, bool withoutNamespaceSpecifier=false)