RSB  0.7.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Repository.h
Go to the documentation of this file.
1 /* ============================================================
2  *
3  * This file is a part of the RSB project.
4  *
5  * Copyright (C) 2011 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 <string>
30 #include <stdexcept>
31 #include <set>
32 #include <iomanip>
33 
34 #include <boost/format.hpp>
35 
36 #include <rsc/runtime/Printable.h>
37 #include <rsc/runtime/TypeStringTools.h>
38 #include <rsc/runtime/NoSuchObject.h>
39 #include <rsc/logging/Logger.h>
40 
41 #include "Converter.h"
44 #include "rsb/rsbexports.h"
45 
46 namespace rsb {
47 namespace converter {
48 
58 template<class WireType>
59 class Repository: public rsc::runtime::Printable {
60 public:
62 
64  typedef std::pair<std::string, std::string> ConverterSignature;
65 
66  typedef std::map<std::string, std::string> ConverterSelectionMap;
67 
69  logger(rsc::logging::Logger::getLogger("rsb.converter.Repository")) {
70  }
71 
73  const ConverterSelectionMap& selection =
74  ConverterSelectionMap()) const {
76  WireType>();
77  for (typename ConverterMap::const_iterator it =
78  this->converters.begin(); it != this->converters.end(); ++it) {
79  std::string wireSchema = it->first.first;
80  std::string dataType = it->first.second;
81  // The data-type is not mentioned in the explicit
82  // selection. Try to add the converter. This may throw in
83  // case of ambiguity.
84  if (selection.find(dataType) == selection.end()) {
85  try {
86  result->addConverter(dataType, it->second);
87  } catch (const std::invalid_argument& e) {
88  std::set<std::string> wireSchemas;
89  for (typename ConverterMap::const_iterator it_ =
90  this->converters.begin();
91  it_ != this->converters.end(); ++it_) {
92  if (dataType == it_->first.second)
93  wireSchemas.insert(it_->first.first);
94  }
95  throw std::runtime_error(
96  boost::str(
97  boost::format(
98  "Ambiguous converter set for wire-type `%1%' and data-type `%2%': candidate wire-schemas are %3%; hint: add a configuration option `transport.<name>.converter.cpp.<one of %3%> = %2%' to resolve the ambiguity.")
99  % rsc::runtime::typeName<WireType>()
100  % dataType % wireSchemas));
101  }
102  }
103  // There is an entry for data-type in the explicit
104  // selection. Add the converter if the wire-schema matches.
105  else if (wireSchema == selection.find(dataType)->second) {
106  RSCDEBUG(this->logger,
107  "Found configured converter signature " << it->first);
108  result->addConverter(dataType, it->second);
109  }
110  }
111  return typename ConverterSelectionStrategy<WireType>::Ptr(result);
112  }
113 
115  const ConverterSelectionMap& selection =
116  ConverterSelectionMap()) const {
118  WireType>();
119  for (typename ConverterMap::const_iterator it =
120  this->converters.begin(); it != this->converters.end(); ++it) {
121  std::string wireSchema = it->first.first;
122  std::string dataType = it->first.second;
123  // The wire-schema is not mentioned in the explicit
124  // selection. Try to add the converter. This may throw in
125  // case of ambiguity.
126  if (selection.find(wireSchema) == selection.end()) {
127  try {
128  result->addConverter(wireSchema, it->second);
129  } catch (const std::invalid_argument& e) {
130  std::set<std::string> dataTypes;
131  for (typename ConverterMap::const_iterator it_ =
132  this->converters.begin();
133  it_ != this->converters.end(); ++it_) {
134  if (wireSchema == it_->first.first)
135  dataTypes.insert(it_->first.second);
136  }
137  throw std::runtime_error(
138  boost::str(
139  boost::format(
140  "Ambiguous converter set for wire-type `%1%' and wire-schema `%2%': candidate data-types are %3%; hint: add a configuration option `transport.<name>.converter.cpp.%2% = <one of %3%>' to resolve the ambiguity.")
141  % rsc::runtime::typeName<WireType>()
142  % wireSchema % dataTypes));
143  }
144  }
145  // There is an entry for wire-schema in the explicit
146  // selection. Add the converter if the data-type matches.
147  else if (dataType == selection.find(wireSchema)->second) {
148  RSCDEBUG(this->logger,
149  "Found configured converter signature " << it->first);
150  result->addConverter(wireSchema, it->second);
151  }
152  }
153  return typename ConverterSelectionStrategy<WireType>::Ptr(result);
154  }
155 
163  void registerConverter(ConverterPtr converter) {
164  std::string wireSchema = converter->getWireSchema();
165  std::string dataType = converter->getDataType();
166  if (this->converters.find(std::make_pair(wireSchema, dataType))
167  != this->converters.end()) {
168  // TODO use RSB execption; but do we have one for invalid argument?
169  throw std::invalid_argument(
170  boost::str(
171  boost::format(
172  "There already is a converter for wire-schema `%1%' and data-type `%2%'")
173  % wireSchema % dataType));
174  }
175  this->converters[std::make_pair(wireSchema, dataType)] = converter;
176  }
177 
178  ConverterPtr getConverter(const std::string& wireSchema,
179  const std::string& dataType) const {
180  typename ConverterMap::const_iterator it = this->converters.find(
181  std::make_pair(wireSchema, dataType));
182  if (it == this->converters.end()) {
183  throw rsc::runtime::NoSuchObject(
184  boost::str(
185  boost::format(
186  "Could not find a converter for wire-schema `%1%' and data-type `%2%'")
187  % wireSchema % dataType));
188  }
189  return it->second;
190  }
191 
192  ConverterPtr getConverter(const ConverterSignature& signature) const {
193  return getConverter(signature.first, signature.second);
194  }
195 
196  void clear() {
197  this->converters.clear();
198  }
199 
200  typedef boost::shared_ptr<Repository<WireType> > Ptr;
201 
202 private:
203  typedef std::map<ConverterSignature, ConverterPtr> ConverterMap;
204 
205  rsc::logging::LoggerPtr logger;
207 
208  std::string getClassName() const {
209  return "Repository<" + rsc::runtime::typeName<WireType>() + ">";
210  }
211 
212  void printContents(std::ostream& stream) const {
213  stream << std::endl;
214  for (typename ConverterMap::const_iterator it =
215  this->converters.begin(); it != this->converters.end(); ++it) {
216  stream << "\t" << std::setw(16) << std::left << it->first.first
217  << " <-> " << std::setw(16) << std::left << it->first.second
218  << ": " << *it->second << std::endl;
219  }
220  }
221 };
222 
223 
246 
253 class RSB_EXPORT RepositoryCreater {
254 public:
255  virtual ~RepositoryCreater();
256 
262  virtual void* create() = 0;
263 };
264 
275 RSB_EXPORT void* converterRepositoryByName(const std::string &wireTypeName,
276  RepositoryCreater &creater);
277 
285 template<class WireType>
287 public:
289  }
290  void* create() {
291  return new Repository<WireType> ;
292  }
293 };
294 
296 
304 template<class WireType>
307  return typename Repository<WireType>::Ptr(
309  rsc::runtime::typeName<WireType>(), creater),
310  rsc::misc::NullDeleter());
311 }
312 
318 DEPRECATED(RSB_EXPORT Repository<std::string>::Ptr stringConverterRepository());
319 
320 }
321 }