RSB  0.7.0
 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/runtime/NoSuchObject.h>
34 #include <rsc/runtime/TypeStringTools.h>
35 #include <rsc/runtime/Printable.h>
36 #include <rsc/patterns/Factory.h>
37 
38 #include "InPullConnector.h"
39 #include "InPushConnector.h"
40 #include "OutConnector.h"
41 #include "rsb/rsbexports.h"
42 
43 namespace rsb {
44 namespace transport {
45 
53 template <typename Interface>
54 class ConnectorFactory: public rsc::patterns::Factory<std::string, Interface>,
55  public rsc::patterns::Singleton< ConnectorFactory<Interface> >,
56  public rsc::runtime::Printable {
57 public:
64  class ConnectorInfo: public rsc::runtime::Printable {
65  public:
66  typedef std::set<std::string> SchemaList;
67  typedef std::set<std::string> OptionList;
68 
69  ConnectorInfo(const std::string& name,
70  const SchemaList& schemas,
71  bool remote,
72  const OptionList& options) :
73  name(name), schemas(schemas), remote(remote), options(options) {
74  this->options.insert("enabled");
75  }
76 
80  std::string getName() const {
81  return this->name;
82  }
83 
92  return this->schemas;
93  }
94 
103  return this->options;
104  }
105 
112  bool isRemote() const {
113  return this->remote;
114  }
115 
116  bool operator<(const ConnectorInfo& other) const {
117  if (this->name < other.name) {
118  return true;
119  } else if (this->name == other.name) {
120  if (this->schemas < other.schemas) {
121  return true;
122  } else if (this->schemas == other.schemas) {
123  if (this->remote < other.remote) {
124  return true;
125  } else if (this->remote == other.remote) {
126  return this->options < other.options;
127  }
128  }
129  }
130  return false;
131  }
132  private:
133  std::string name;
135  bool remote;
137 
138  std::string getClassName() const {
139  return "ConnectorInfo";
140  }
141 
142  void printContents(std::ostream& stream) const {
143  stream << this->name
144  << ", schemas = " << this->schemas
145  << ", remote = " << this->remote
146  << ", options = " << this->options;
147  }
148  };
149 
150 private:
151  typedef rsc::patterns::Factory<std::string, Interface> Factory;
152  typedef typename Factory::CreateFunction CreateFunction;
153  typedef typename Factory::ImplMapProxy ImplMapProxy;
154  typedef std::map<std::string, ConnectorInfo> InfoMap; // forward
155 public:
156 
166  ConnectorInfo getConnectorInfo(const std::string& name) const {
167  typename InfoMap::const_iterator it = this->infos.find(name);
168  if (it == this->infos.end()) {
169  throw rsc::runtime::NoSuchObject(name);
170  }
171  return it->second;
172  }
173 
174  std::set<ConnectorInfo> getConnectorInfos() const {
175  std::set<ConnectorInfo> result;
176 
177  for (typename InfoMap::const_iterator it = this->infos.begin();
178  it != this->infos.end(); ++it) {
179  result.insert(it->second);
180  }
181  return result;
182  }
183 
196  void registerConnector(const std::string& name,
197  const CreateFunction& constructor,
198  const std::set<std::string>& schemas = std::set<std::string>(),
199  bool remote = true,
200  const std::set<std::string>& options = std::set<std::string>()) {
201  Factory::impls().register_(name, constructor);
202 
203  ConnectorInfo info(name, schemas, remote, options);
204  this->infos.insert(std::make_pair(name, info));
205  }
206 
207  void registerConnector(const std::string& name,
208  const CreateFunction& constructor, const std::string& schema,
209  bool remote = true,
210  const std::set<std::string>& options = std::set<std::string>()) {
211  std::set<std::string> schemas;
212  schemas.insert(schema);
213  registerConnector(name, constructor, schemas, remote, options);
214  }
215 private:
217 
218  std::string getClassName() const {
219  return "ConnectorFactory<" + rsc::runtime::typeName<Interface>() + ">";
220  }
221 
222  void printContents(std::ostream& stream) const {
223  const ImplMapProxy& implementations = Factory::impls();
224  stream << std::endl;
225  for (typename ImplMapProxy::const_iterator it = implementations.begin(); it
226  != implementations.end(); ++it) {
227  stream << "\t" << getConnectorInfo(it->first) << std::endl;
228  }
229  }
230 };
231 
233 
235 
237 
238 RSB_EXPORT InPullFactory& getInPullFactory();
239 RSB_EXPORT InPushFactory& getInPushFactory();
240 RSB_EXPORT OutFactory& getOutFactory();
241 
242 }
243 }