34 #include <boost/format.hpp>
36 #include <rsc/runtime/Printable.h>
37 #include <rsc/runtime/TypeStringTools.h>
38 #include <rsc/runtime/NoSuchObject.h>
39 #include <rsc/logging/Logger.h>
44 #include "rsb/rsbexports.h"
58 template<
class WireType>
69 logger(rsc::logging::Logger::getLogger(
"rsb.converter.Repository")) {
75 RSCDEBUG(this->
logger,
"Building unambiguous map for serialization with selection "
78 boost::shared_ptr< UnambiguousConverterMap<WireType> >
80 for (
typename ConverterMap::const_iterator it =
82 std::string wireSchema = it->first.first;
83 std::string dataType = it->first.second;
84 RSCTRACE(this->
logger,
"Considering converter " << it->second);
89 if (selection.find(dataType) == selection.end()) {
90 RSCTRACE(this->
logger,
"No explicit selection for data-type "
93 result->addConverter(dataType, it->second);
94 }
catch (
const std::invalid_argument& e) {
95 std::set<std::string> wireSchemas;
96 for (
typename ConverterMap::const_iterator it_ =
99 if (dataType == it_->first.second)
100 wireSchemas.insert(it_->first.first);
102 throw std::runtime_error(
105 "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 (%4%).")
106 % rsc::runtime::typeName<WireType>()
107 % dataType % wireSchemas % e.what()));
112 else if (wireSchema == selection.find(dataType)->second) {
113 RSCDEBUG(this->
logger,
"Explicit selection "
114 << *selection.find(dataType)
115 <<
" chooses data wire-schema " << wireSchema
116 <<
"; adding the converter");
117 result->addConverter(dataType, it->second);
119 RSCDEBUG(this->
logger,
"Explicit selection "
120 << *selection.find(dataType)
121 <<
" chooses wire-schema "
122 << selection.find(dataType)->second
123 <<
" (not " << wireSchema
124 <<
"); not adding the converter");
133 RSCDEBUG(this->
logger,
"Building unambiguous map for deserialization with selection "
136 boost::shared_ptr< UnambiguousConverterMap<WireType> >
138 for (
typename ConverterMap::const_iterator it =
140 std::string wireSchema = it->first.first;
141 std::string dataType = it->first.second;
142 RSCTRACE(this->
logger,
"Considering converter " << it->second);
147 if (selection.find(wireSchema) == selection.end()) {
148 RSCTRACE(this->
logger,
"No explicit selection for wire-schema "
151 result->addConverter(wireSchema, it->second);
152 }
catch (
const std::invalid_argument& e) {
153 std::set<std::string> dataTypes;
154 for (
typename ConverterMap::const_iterator it_ =
157 if (wireSchema == it_->first.first)
158 dataTypes.insert(it_->first.second);
160 throw std::runtime_error(
163 "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 (%4%).")
164 % rsc::runtime::typeName<WireType>()
165 % wireSchema % dataTypes % e.what()));
170 else if (dataType == selection.find(wireSchema)->second) {
171 RSCDEBUG(this->
logger,
"Explicit selection "
172 << *selection.find(wireSchema)
173 <<
" chooses data-type " << dataType
174 <<
"; adding the converter");
175 result->addConverter(wireSchema, it->second);
177 RSCDEBUG(this->
logger,
"Explicit selection "
178 << *selection.find(wireSchema)
179 <<
" chooses data-type "
180 << selection.find(wireSchema)->second
181 <<
" (not " << dataType
182 <<
"); not adding the converter");
200 RSCINFO(this->
logger,
"Registering converter " << converter);
202 std::string wireSchema = converter->getWireSchema();
203 std::string dataType = converter->getDataType();
204 if (this->
converters.find(std::make_pair(wireSchema, dataType))
208 throw std::invalid_argument(
211 "There already is a converter for wire-schema `%1%' and data-type `%2%'")
212 % wireSchema % dataType));
214 this->
converters[std::make_pair(wireSchema, dataType)] = converter;
218 const std::string& dataType)
const {
219 typename ConverterMap::const_iterator it = this->
converters.find(
220 std::make_pair(wireSchema, dataType));
222 throw rsc::runtime::NoSuchObject(
225 "Could not find a converter for wire-schema `%1%' and data-type `%2%'")
226 % wireSchema % dataType));
239 typedef boost::shared_ptr<Repository<WireType> >
Ptr;
248 return "Repository<" + rsc::runtime::typeName<WireType>() +
">";
253 for (
typename ConverterMap::const_iterator it =
255 stream <<
"\t" << std::setw(16) << std::left << it->first.first
256 <<
" <-> " << std::setw(16) << std::left << it->first.second
258 <<
"\t\t" << *it->second << std::endl;
302 virtual void* create() = 0;
325 template<
class WireType>
344 template<
class WireType>
349 rsc::runtime::typeName<WireType>(), creater),
350 rsc::misc::NullDeleter());
Converter< WireType >::Ptr ConverterPtr
void printContents(std::ostream &stream) const
A RepositoryCreate which can be statically typed using a template argument for the desired wire type...
std::map< std::string, std::string > ConverterSelectionMap
Repository< string >::Ptr stringConverterRepository()
boost::shared_ptr< Repository< WireType > > Ptr
std::pair< std::string, std::string > ConverterSignature
WireSchema and DataType.
ConverterPtr getConverter(const std::string &wireSchema, const std::string &dataType) const
ConverterSelectionStrategy< WireType >::Ptr getConvertersForDeserialization(const ConverterSelectionMap &selection=ConverterSelectionMap()) const
Repository< WireType >::Ptr converterRepository()
Returns a Repository of Converter instances specifically for the given wire type. ...
rsc::logging::LoggerPtr logger
std::string getClassName() const
ConverterPtr getConverter(const ConverterSignature &signature) const
An internal factory object to create typed converter repositories.
Objects this class store mappings of one of the followings forms.
void registerConverter(ConverterPtr converter, bool replace=false)
Registers converter in the collection.
boost::shared_ptr< Converter< WireType > > Ptr
boost::shared_ptr< ConverterSelectionStrategy< WireType > > Ptr
DEPRECATED(RSB_EXPORT Repository< std::string >::Ptr stringConverterRepository())
Maintains a collection of converters for a specific wire format.
void * create()
Factory method to create a new Repository.
void * converterRepositoryByName(const string &wireTypeName, RepositoryCreater &creater)
Returns a repository for a converters of a certain wire type by a look up based on a name for the wir...
virtual ~TypeBasedRepositoryCreater()
std::map< ConverterSignature, ConverterPtr > ConverterMap
ConverterSelectionStrategy< WireType >::Ptr getConvertersForSerialization(const ConverterSelectionMap &selection=ConverterSelectionMap()) const