RSC  0.17.1
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 #include "../misc/langutils.h"
44 
45 namespace rsc {
46 namespace misc {
47 
61 template<class R>
62 class DEPRECATED_CLASS("If possible, use rsc::patterns::Factory instead") Registry: public boost::noncopyable {
63 public:
64 
65  Registry() {
66  }
67 
80  DEPRECATED_MSG(void addRegistree(R* r, const std::string& errorDescription = ""),
81  "If possible, use rsc::patterns::Factory instead") {
82 
83  boost::recursive_mutex::scoped_lock lock(mutex);
84 
85  if (registreesByName.count(r->getRegistryKey())) {
86  throw std::invalid_argument(
87  "There already is a registree with key '"
88  + r->getRegistryKey() + "'. " + errorDescription);
89  }
90 
91  registreesByName[r->getRegistryKey()] = boost::shared_ptr<R>(r);
92 
93  }
94 
102  DEPRECATED_MSG(boost::shared_ptr<R> getRegistree(const std::string& key),
103  "If possible, use rsc::patterns::Factory instead") {
104 
105  boost::recursive_mutex::scoped_lock lock(mutex);
106 
107  typename std::map<std::string, boost::shared_ptr<R> >::const_iterator it =
108  registreesByName.find(key);
109  if (it == registreesByName.end()) {
110  throw std::invalid_argument(
111  "There is no registree with key '" + key + "'.");
112  }
113 
114  return it->second;
115 
116  }
117 
123  DEPRECATED_MSG(std::set<std::string> getKnownRegistryKeys(),
124  "If possible, use rsc::patterns::Factory instead") {
125 
126  boost::recursive_mutex::scoped_lock lock(mutex);
127 
128  std::set<std::string> keys;
129  typename std::map<std::string, boost::shared_ptr<R> >::const_iterator it;
130  for (it = registreesByName.begin(); it != registreesByName.end();
131  ++it) {
132  keys.insert(it->first);
133  }
134  return keys;
135 
136  }
137 
145  DEPRECATED_MSG(bool removeRegistree(const std::string& name),
146  "If possible, use rsc::patterns::Factory instead") {
147  boost::recursive_mutex::scoped_lock lock(mutex);
148  return registreesByName.erase(name) != 0;
149  }
150 
157  DEPRECATED_MSG(boost::recursive_mutex* getMutex(),
158  "If possible, use rsc::patterns::Factory instead") {
159  return &mutex;
160  }
161 
162 private:
163 
164  boost::recursive_mutex mutex;
165  std::map<std::string, boost::shared_ptr<R> > registreesByName;
166 
167 };
168 
182 #define CREATE_GLOBAL_REGISTREE(registry, registree, uniqueName) \
183  class Starter##uniqueName { \
184  public: \
185  Starter##uniqueName() { \
186  (registry)->addRegistree(registree); \
187  } \
188  }; \
189  Starter##uniqueName uniqueName##Starter;
190 
205 #define CREATE_GLOBAL_REGISTREE_MSG(registry, registree, uniqueName, msg) \
206  class Starter##uniqueName { \
207  public: \
208  Starter##uniqueName() { \
209  (registry)->addRegistree(registree, msg); \
210  } \
211  }; \
212  Starter##uniqueName uniqueName##Starter;
213 
214 }
215 }
#define DEPRECATED_CLASS(msg)
This macro allows you to mark a class as being deprecated.
Definition: langutils.h:192
#define DEPRECATED_MSG(func, msg)
This macro allows you to mark a function as being deprecated including a message explaining the depre...
Definition: langutils.h:148