RSB  0.9.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Factory.h
Go to the documentation of this file.
1 /* ============================================================
2  *
3  * This file is a part of the RSB project
4  *
5  * Copyright (C) 2010 by Sebastian Wrede <swrede 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 <string>
30 #include <vector>
31 #include <map>
32 
33 #include <rsc/logging/Logger.h>
34 #include <rsc/logging/LoggerFactory.h>
35 #include <rsc/misc/langutils.h>
36 #include <rsc/runtime/NoSuchObject.h>
37 #include <rsc/runtime/TypeStringTools.h>
38 #include <rsc/runtime/Printable.h>
39 #include <rsc/patterns/Factory.h>
40 
41 #include <boost/type_traits.hpp>
42 
43 #include "InPullConnector.h"
44 #include "InPushConnector.h"
45 #include "OutConnector.h"
46 #include "rsb/rsbexports.h"
47 
48 namespace rsb {
49 namespace transport {
50 
51 template <typename Interface>
53 
55 
57 
59 
60 RSB_EXPORT InPullFactory& getInPullFactory();
61 RSB_EXPORT InPushFactory& getInPushFactory();
62 RSB_EXPORT OutFactory& getOutFactory();
63 
71 template <typename Interface>
72 class ConnectorFactory: public rsc::patterns::Factory<std::string, Interface>,
73  private rsc::patterns::Singleton< ConnectorFactory<Interface> >,
74  public rsc::runtime::Printable {
75 public:
76 
83  DEPRECATED(static ConnectorFactory<Interface>& getInstance());
84 
91  class ConnectorInfo: public rsc::runtime::Printable {
92  public:
93  typedef std::set<std::string> SchemaList;
94  typedef std::set<std::string> OptionList;
95 
96  ConnectorInfo(const std::string& name,
97  const SchemaList& schemas,
98  bool remote,
99  const OptionList& options) :
100  name(name), schemas(schemas), remote(remote), options(options) {
101  this->options.insert("enabled");
102  }
103 
107  std::string getName() const {
108  return this->name;
109  }
110 
119  return this->schemas;
120  }
121 
130  return this->options;
131  }
132 
139  bool isRemote() const {
140  return this->remote;
141  }
142 
143  bool operator<(const ConnectorInfo& other) const {
144  if (this->name < other.name) {
145  return true;
146  } else if (this->name == other.name) {
147  if (this->schemas < other.schemas) {
148  return true;
149  } else if (this->schemas == other.schemas) {
150  if (this->remote < other.remote) {
151  return true;
152  } else if (this->remote == other.remote) {
153  return this->options < other.options;
154  }
155  }
156  }
157  return false;
158  }
159  private:
160  std::string name;
162  bool remote;
164 
165  void printContents(std::ostream& stream) const {
166  stream << this->name
167  << ", schemas = " << this->schemas
168  << ", remote = " << this->remote
169  << ", options = " << this->options;
170  }
171  };
172 
173 private:
174  rsc::logging::LoggerPtr logger;
175 
177  return rsc::patterns::Singleton< ConnectorFactory<Interface> >::getInstance();
178  }
181  friend OutFactory& getOutFactory();
182 
183  typedef rsc::patterns::Factory<std::string, Interface> Factory;
184  typedef typename Factory::CreateFunction CreateFunction;
185  typedef typename Factory::ImplMapProxy ImplMapProxy;
186  typedef std::map<std::string, ConnectorInfo> InfoMap; // forward
187 public:
189  logger(rsc::logging::Logger::getLogger("rsb.transport.ConnectorFactory<" + rsc::runtime::typeName<Interface>() + ">")) {
190  }
191 
201  ConnectorInfo getConnectorInfo(const std::string& name) const {
202  typename InfoMap::const_iterator it = this->infos.find(name);
203  if (it == this->infos.end()) {
204  throw rsc::runtime::NoSuchObject(name);
205  }
206  return it->second;
207  }
208 
209  std::set<ConnectorInfo> getConnectorInfos() const {
210  std::set<ConnectorInfo> result;
211 
212  for (typename InfoMap::const_iterator it = this->infos.begin();
213  it != this->infos.end(); ++it) {
214  result.insert(it->second);
215  }
216  return result;
217  }
218 
231  void registerConnector(const std::string& name,
232  const CreateFunction& constructor,
233  const std::set<std::string>& schemas = std::set<std::string>(),
234  bool remote = true,
235  const std::set<std::string>& options = std::set<std::string>()) {
236  RSCINFO(this->logger, "Registering connector "
237  << name
238  << " for schemas " << schemas);
239 
240  Factory::impls().register_(name, constructor);
241 
242  ConnectorInfo info(name, schemas, remote, options);
243  this->infos.insert(std::make_pair(name, info));
244  }
245 
246  void registerConnector(const std::string& name,
247  const CreateFunction& constructor, const std::string& schema,
248  bool remote = true,
249  const std::set<std::string>& options = std::set<std::string>()) {
250  std::set<std::string> schemas;
251  schemas.insert(schema);
252  registerConnector(name, constructor, schemas, remote, options);
253  }
254 private:
256 
257  void printContents(std::ostream& stream) const {
258  const ImplMapProxy& implementations = Factory::impls();
259  stream << std::endl;
260  for (typename ImplMapProxy::const_iterator it = implementations.begin(); it
261  != implementations.end(); ++it) {
262  stream << "\t" << getConnectorInfo(it->first) << std::endl;
263  }
264  }
265 };
266 
267 template <typename Interface>
268 ConnectorFactory<Interface>& ConnectorFactory<Interface>::getInstance() {
269 
270  // This weird implementation is a tribute to backwards compatibility. We
271  // previously had a generic template class but now need to map it to
272  // specific getter implementations depending on the type. However, there
273  // is no chance in C++ to provide template specializations based on a return
274  // type of a method. Therefore, we do the distinction at runtime using.
275  // As all paths of the if-else expression are always possible from the
276  // compiler's point of view, we need to convince it for all paths that for
277  // any template parameter the correct type is returned by doing a harsh
278  // cast. This is unfortunately a bit complicated from a syntactical point of
279  // view to also achieve the correct reference behavior.
280 
281  if (boost::is_same<Interface, InPullConnector>::value) {
282  return (*(ConnectorFactory<Interface>*) &getInPullFactory());
283  } else if (boost::is_same<Interface, InPushConnector>::value) {
284  return (*(ConnectorFactory<Interface>*) &getInPushFactory());
285  } else if (boost::is_same<Interface, OutConnector>::value) {
286  return (*(ConnectorFactory<Interface>*) &getOutFactory());
287  } else {
288  assert(false);
289  return (*(ConnectorFactory<Interface>*) 0);
290  }
291 }
292 
293 }
294 }
std::map< std::string, ConnectorInfo > InfoMap
Definition: Factory.h:186
Instances of this class describe capabilities and properties of connector implementations.
Definition: Factory.h:91
void printContents(std::ostream &stream) const
Definition: Factory.h:165
Factory::CreateFunction CreateFunction
Definition: Factory.h:184
friend InPushFactory & getInPushFactory()
Definition: Factory.cpp:36
bool operator<(const ConnectorInfo &other) const
Definition: Factory.h:143
void registerConnector(const std::string &name, const CreateFunction &constructor, const std::set< std::string > &schemas=std::set< std::string >(), bool remote=true, const std::set< std::string > &options=std::set< std::string >())
For the connector implementation named name, register the construct function constructor, supported schemas schemas and recognized configuration options options.
Definition: Factory.h:231
rsc::logging::LoggerPtr logger
Definition: Factory.h:174
ConnectorInfo getConnectorInfo(const std::string &name) const
Return information regarding the connector implementation named name.
Definition: Factory.h:201
std::set< ConnectorInfo > getConnectorInfos() const
Definition: Factory.h:209
ConnectorFactory< InPullConnector > InPullFactory
Definition: Factory.h:52
ConnectorFactory< OutConnector > OutFactory
Definition: Factory.h:58
void registerConnector(const std::string &name, const CreateFunction &constructor, const std::string &schema, bool remote=true, const std::set< std::string > &options=std::set< std::string >())
Definition: Factory.h:246
DEPRECATED(static ConnectorFactory< Interface > &getInstance())
ConnectorInfo(const std::string &name, const SchemaList &schemas, bool remote, const OptionList &options)
Definition: Factory.h:96
void printContents(std::ostream &stream) const
Definition: Factory.h:257
static ConnectorFactory< Interface > & getInstanceBase()
Definition: Factory.h:176
std::string getName() const
Return the name of the implementation.
Definition: Factory.h:107
rsc::patterns::Factory< std::string, Interface > Factory
Definition: Factory.h:183
friend InPullFactory & getInPullFactory()
Definition: Factory.cpp:32
InPullFactory & getInPullFactory()
Definition: Factory.cpp:32
OptionList getOptions() const
Return a list of option names describing configurations options recognized by the implementation...
Definition: Factory.h:129
bool isRemote() const
Return "remoteness" of the implementation.
Definition: Factory.h:139
friend OutFactory & getOutFactory()
Definition: Factory.cpp:40
InPushFactory & getInPushFactory()
Definition: Factory.cpp:36
Factory::ImplMapProxy ImplMapProxy
Definition: Factory.h:185
OutFactory & getOutFactory()
Definition: Factory.cpp:40
ConnectorFactory< InPushConnector > InPushFactory
Definition: Factory.h:56
Objects of this class are specialized factories that construct Connector objects and provide introspe...
Definition: Factory.h:52
SchemaList getSchemas() const
Return the set of schemas supported by the connector implementation.
Definition: Factory.h:118