RSB  0.17.0
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  public rsc::runtime::Printable {
74 public:
75 
82  class ConnectorInfo: public rsc::runtime::Printable {
83  public:
84  typedef std::set<std::string> SchemaList;
85  typedef std::set<std::string> OptionList;
86 
87  ConnectorInfo(const std::string& name,
88  const SchemaList& schemas,
89  bool remote,
90  const OptionList& options) :
91  name(name), schemas(schemas), remote(remote), options(options) {
92  this->options.insert("enabled");
93  }
94 
98  std::string getName() const {
99  return this->name;
100  }
101 
109  SchemaList getSchemas() const {
110  return this->schemas;
111  }
112 
120  OptionList getOptions() const {
121  return this->options;
122  }
123 
130  bool isRemote() const {
131  return this->remote;
132  }
133 
134  bool operator<(const ConnectorInfo& other) const {
135  if (this->name < other.name) {
136  return true;
137  } else if (this->name == other.name) {
138  if (this->schemas < other.schemas) {
139  return true;
140  } else if (this->schemas == other.schemas) {
141  if (this->remote < other.remote) {
142  return true;
143  } else if (this->remote == other.remote) {
144  return this->options < other.options;
145  }
146  }
147  }
148  return false;
149  }
150  private:
151  std::string name;
152  SchemaList schemas;
153  bool remote;
154  OptionList options;
155 
156  void printContents(std::ostream& stream) const {
157  stream << this->name
158  << ", schemas = " << this->schemas
159  << ", remote = " << this->remote
160  << ", options = " << this->options;
161  }
162  };
163 
164 private:
165  rsc::logging::LoggerPtr logger;
166 
167  friend InPullFactory& getInPullFactory();
168  friend InPushFactory& getInPushFactory();
169  friend OutFactory& getOutFactory();
170 
171  typedef rsc::patterns::Factory<std::string, Interface> Factory;
172  typedef typename Factory::CreateFunction CreateFunction;
173  typedef typename Factory::ImplMapProxy ImplMapProxy;
174  typedef std::map<std::string, ConnectorInfo> InfoMap; // forward
175 public:
177  logger(rsc::logging::Logger::getLogger("rsb.transport.ConnectorFactory<" + rsc::runtime::typeName<Interface>() + ">")) {
178  }
179 
189  ConnectorInfo getConnectorInfo(const std::string& name) const {
190  typename InfoMap::const_iterator it = this->infos.find(name);
191  if (it == this->infos.end()) {
192  throw rsc::runtime::NoSuchObject(name);
193  }
194  return it->second;
195  }
196 
197  std::set<ConnectorInfo> getConnectorInfos() const {
198  std::set<ConnectorInfo> result;
199 
200  for (typename InfoMap::const_iterator it = this->infos.begin();
201  it != this->infos.end(); ++it) {
202  result.insert(it->second);
203  }
204  return result;
205  }
206 
219  void registerConnector(const std::string& name,
220  const CreateFunction& constructor,
221  const std::set<std::string>& schemas = std::set<std::string>(),
222  bool remote = true,
223  const std::set<std::string>& options = std::set<std::string>()) {
224  RSCINFO(this->logger, "Registering connector "
225  << name
226  << " for schemas " << schemas);
227 
228  Factory::impls().register_(name, constructor);
229 
230  ConnectorInfo info(name, schemas, remote, options);
231  this->infos.insert(std::make_pair(name, info));
232  }
233 
234  void registerConnector(const std::string& name,
235  const CreateFunction& constructor, const std::string& schema,
236  bool remote = true,
237  const std::set<std::string>& options = std::set<std::string>()) {
238  std::set<std::string> schemas;
239  schemas.insert(schema);
240  registerConnector(name, constructor, schemas, remote, options);
241  }
242 private:
243  InfoMap infos;
244 
245  void printContents(std::ostream& stream) const {
246  const ImplMapProxy& implementations = Factory::impls();
247  stream << std::endl;
248  for (typename ImplMapProxy::const_iterator it = implementations.begin(); it
249  != implementations.end(); ++it) {
250  stream << "\t" << getConnectorInfo(it->first) << std::endl;
251  }
252  }
253 };
254 
255 }
256 }
std::map< std::string, ConnectorInfo > InfoMap
Definition: Factory.h:174
Instances of this class describe capabilities and properties of connector implementations.
Definition: Factory.h:82
void printContents(std::ostream &stream) const
Definition: Factory.h:156
Factory::CreateFunction CreateFunction
Definition: Factory.h:172
friend InPushFactory & getInPushFactory()
Definition: Factory.cpp:37
bool operator<(const ConnectorInfo &other) const
Definition: Factory.h:134
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:219
rsc::logging::LoggerPtr logger
Definition: Factory.h:165
ConnectorInfo getConnectorInfo(const std::string &name) const
Return information regarding the connector implementation named name.
Definition: Factory.h:189
std::set< ConnectorInfo > getConnectorInfos() const
Definition: Factory.h:197
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:234
ConnectorInfo(const std::string &name, const SchemaList &schemas, bool remote, const OptionList &options)
Definition: Factory.h:87
void printContents(std::ostream &stream) const
Definition: Factory.h:245
std::string getName() const
Return the name of the implementation.
Definition: Factory.h:98
rsc::patterns::Factory< std::string, Interface > Factory
Definition: Factory.h:171
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:120
bool isRemote() const
Return "remoteness" of the implementation.
Definition: Factory.h:130
friend OutFactory & getOutFactory()
Definition: Factory.cpp:42
InPushFactory & getInPushFactory()
Definition: Factory.cpp:37
Factory::ImplMapProxy ImplMapProxy
Definition: Factory.h:173
OutFactory & getOutFactory()
Definition: Factory.cpp:42
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:109