RSC  0.7.17
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Registry.h
Go to the documentation of this file.
1 /* ============================================================
2  *
3  * This file is a part of RSC project
4  *
5  * Copyright (C) 2010 by Johannes Wienke <jwienke at techfak dot uni-bielefeld dot de>
6  *
7  * This file may be licensed under the terms of the
8  * GNU Lesser General Public License Version 3 (the ``LGPL''),
9  * or (at your option) any later version.
10  *
11  * Software distributed under the License is distributed
12  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
13  * express or implied. See the LGPL for the specific language
14  * governing rights and limitations.
15  *
16  * You should have received a copy of the LGPL along with this
17  * program. If not, go to http://www.gnu.org/licenses/lgpl.html
18  * or write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  * The development of this software was supported by:
22  * CoR-Lab, Research Institute for Cognition and Robotics
23  * Bielefeld University
24  *
25  * ============================================================ */
26 
27 #pragma once
28 
29 #include <iostream>
30 #include <map>
31 #include <set>
32 #include <stdexcept>
33 #include <string>
34 
35 #include <boost/noncopyable.hpp>
36 #include <boost/shared_ptr.hpp>
37 #include <boost/static_assert.hpp>
38 #include <boost/thread/recursive_mutex.hpp>
39 
40 #include "rsc/config.h"
41 #include "rsc/rscexports.h"
42 
43 namespace rsc {
44 namespace misc {
45 
57 template<class R>
58 class Registry: public boost::noncopyable {
59 public:
60 
62  }
63 
76  void addRegistree(R* r, const std::string& errorDescription = "") {
77 
78  boost::recursive_mutex::scoped_lock lock(mutex);
79 
80  if (registreesByName.count(r->getRegistryKey())) {
81  throw std::invalid_argument(
82  "There already is a registree with key '"
83  + r->getRegistryKey() + "'. " + errorDescription);
84  }
85 
86  registreesByName[r->getRegistryKey()] = boost::shared_ptr<R>(r);
87 
88  }
89 
97  boost::shared_ptr<R> getRegistree(const std::string& key) {
98 
99  boost::recursive_mutex::scoped_lock lock(mutex);
100 
101  typename std::map<std::string, boost::shared_ptr<R> >::const_iterator it =
102  registreesByName.find(key);
103  if (it == registreesByName.end()) {
104  throw std::invalid_argument(
105  "There is no registree with key '" + key + "'.");
106  }
107 
108  return it->second;
109 
110  }
111 
117  std::set<std::string> getKnownRegistryKeys() {
118 
119  boost::recursive_mutex::scoped_lock lock(mutex);
120 
121  std::set<std::string> keys;
122  typename std::map<std::string, boost::shared_ptr<R> >::const_iterator it;
123  for (it = registreesByName.begin(); it != registreesByName.end();
124  ++it) {
125  keys.insert(it->first);
126  }
127  return keys;
128 
129  }
130 
138  bool removeRegistree(const std::string& name) {
139  boost::recursive_mutex::scoped_lock lock(mutex);
140  return registreesByName.erase(name) != 0;
141  }
142 
149  boost::recursive_mutex* getMutex() {
150  return &mutex;
151  }
152 
153 private:
154 
155  boost::recursive_mutex mutex;
156  std::map<std::string, boost::shared_ptr<R> > registreesByName;
157 
158 };
159 
173 #define CREATE_GLOBAL_REGISTREE(registry, registree, uniqueName) \
174  class Starter##uniqueName { \
175  public: \
176  Starter##uniqueName() { \
177  (registry)->addRegistree(registree); \
178  } \
179  }; \
180  Starter##uniqueName uniqueName##Starter;
181 
196 #define CREATE_GLOBAL_REGISTREE_MSG(registry, registree, uniqueName, msg) \
197  class Starter##uniqueName { \
198  public: \
199  Starter##uniqueName() { \
200  (registry)->addRegistree(registree, msg); \
201  } \
202  }; \
203  Starter##uniqueName uniqueName##Starter;
204 
205 }
206 }
207 
std::map< std::string, boost::shared_ptr< R > > registreesByName
Definition: Registry.h:156
boost::recursive_mutex * getMutex()
Returns a recursive mutex that can be used to make multiple operations on this class atomic...
Definition: Registry.h:149
boost::recursive_mutex mutex
Definition: Registry.h:155
bool removeRegistree(const std::string &name)
Removes a registree from this registry.
Definition: Registry.h:138
A templatized global registry.
Definition: Registry.h:58
boost::shared_ptr< R > getRegistree(const std::string &key)
Returns the registree for the provided key.
Definition: Registry.h:97
void addRegistree(R *r, const std::string &errorDescription="")
Registers a new registree in the Registry.
Definition: Registry.h:76
std::set< std::string > getKnownRegistryKeys()
Returns a set with all known registree keys.
Definition: Registry.h:117