Commit 6fe68723 authored by Joseph Mirabel's avatar Joseph Mirabel
Browse files

Remove DefaultCastRegisterer::disp

parent e3eeee49
......@@ -33,13 +33,9 @@ template <typename T>
class DefaultCastRegisterer : public SignalCastRegisterer {
public:
DefaultCastRegisterer()
: SignalCastRegisterer(typeid(T), disp, cast) {}
: SignalCastRegisterer(typeid(T), cast) {}
static boost::any cast(std::istringstream &iss);
static void disp(const boost::any &object, std::ostream &os) {
os << boost::any_cast<T>(object) << std::endl;
}
};
/// A default version of the caster, to serialize directly from
......@@ -76,16 +72,12 @@ boost::any DefaultCastRegisterer<T>::cast(std::istringstream &iss) {
template <class T> class SignalCast {
public:
static T cast(std::istringstream &) { throw 1; }
static void disp(const T &, std::ostream &) { throw 1; }
public:
// adapter functions for SignalCast
static boost::any cast_(std::istringstream &stringValue) {
return boost::any_cast<T>(cast(stringValue));
}
static void disp_(const boost::any &t, std::ostream &os) {
disp(boost::any_cast<T>(t), os);
}
private:
SignalCast() {}
......
......@@ -39,18 +39,14 @@ public:
/// Typedef of displayer functions that take an encapsulated 'any'
/// object and displays, cast, or trace it on an output stream
/// (serialization).
typedef boost::function2<void, const boost::any &, std::ostream &>
displayer_type;
typedef boost::function1<boost::any, std::istringstream &> caster_type;
/// Get a reference to the unique object of the class.
static SignalCaster *getInstance(void);
/// Displays an object using a registered displayer function.
void disp(const boost::any &object, std::ostream &os);
/// Casts an object using a registered cast function.
boost::any cast(const std::type_info &, std::istringstream &iss);
/// Registers a cast.
void registerCast(const std::type_info &type, displayer_type displayer,
void registerCast(const std::type_info &type,
caster_type caster);
/// Unregister a cast.
void unregisterCast(const std::type_info &type);
......@@ -61,7 +57,7 @@ public:
private:
/// Container for the three cast functions.
typedef boost::tuple<displayer_type, caster_type>
typedef boost::tuple<caster_type>
cast_functions_type;
/// \brief Retrieve cast structure from its name.
......@@ -87,9 +83,8 @@ private:
class DYNAMIC_GRAPH_DLLAPI SignalCastRegisterer {
public:
inline SignalCastRegisterer(const std::type_info &type,
SignalCaster::displayer_type displayer,
SignalCaster::caster_type caster) {
SignalCaster::getInstance()->registerCast(type, displayer, caster);
SignalCaster::getInstance()->registerCast(type, caster);
}
};
......
......@@ -61,13 +61,6 @@ DefaultCastRegisterer<std::string>::cast(std::istringstream &iss) {
return inst;
}
// for std::string, do not add std::endl at the end of the stream.
template <>
inline void DefaultCastRegisterer<std::string>::disp(const boost::any &object,
std::ostream &os) {
os << boost::any_cast<std::string>(object);
}
/// Registers useful casts
namespace {
DefaultCastRegisterer<double> double_reg;
......
......@@ -27,7 +27,6 @@ void SignalCaster::destroy() {
}
void SignalCaster::registerCast(const std::type_info &type,
SignalCaster::displayer_type displayer,
SignalCaster::caster_type caster) {
if (existsCast(type)) {
// If type name has already been registered for same type, do not throw.
......@@ -44,7 +43,7 @@ void SignalCaster::registerCast(const std::type_info &type,
throw ExceptionSignal(ExceptionSignal::GENERIC, os.str());
}
}
functions_[type.name()] = cast_functions_type(displayer, caster);
functions_[type.name()] = cast_functions_type(caster);
type_info_[type.name()] = &type;
}
......@@ -70,10 +69,6 @@ SignalCaster::getCast(const std::string &type_name) {
return it->second;
}
void SignalCaster::disp(const boost::any &object, std::ostream &os) {
getCast(object.type().name()).get<0>()(object, os);
}
std::vector<std::string> SignalCaster::listTypenames() const {
std::vector<std::string> typeList;
for (std::map<std::string, cast_functions_type>::const_iterator iter =
......@@ -85,7 +80,7 @@ std::vector<std::string> SignalCaster::listTypenames() const {
boost::any SignalCaster::cast(const std::type_info &type,
std::istringstream &iss) {
return getCast(type.name()).get<1>()(iss);
return getCast(type.name()).get<0>()(iss);
}
/// Singleton on the library-wide instance of SignalCaster
......
......@@ -223,7 +223,7 @@ BOOST_AUTO_TEST_CASE(signal_caster_basics) {
double ad = 2.0;
output_test_stream output;
try {
asig_caster->disp(ad, output);
signal_disp<double>::run(ad, output);
} catch (ExceptionSignal &aes) {
res = (aes.getCode() == ExceptionSignal::BAD_CAST);
}
......
......@@ -35,21 +35,13 @@ struct EigenCastRegisterer_V : public dynamicgraph::SignalCastRegisterer {
typedef Vector bnuVector;
EigenCastRegisterer_V()
: SignalCastRegisterer(typeid(bnuVector), dispVector, castVector) {}
: SignalCastRegisterer(typeid(bnuVector), castVector) {}
static boost::any castVector(std::istringstream &iss) {
bnuVector res;
iss >> res;
return res;
}
static void dispVector(const boost::any &object, std::ostream &os) {
const bnuVector &v = boost::any_cast<bnuVector>(object);
os << "[ ";
for (int i = 0; i < v.size(); ++i)
os << v(i) << " ";
os << " ];" << std::endl;
}
};
template <typename Derived>
......@@ -57,18 +49,13 @@ struct EigenCastRegisterer_M : public dynamicgraph::SignalCastRegisterer {
typedef Matrix bnuMatrix;
EigenCastRegisterer_M()
: SignalCastRegisterer(typeid(bnuMatrix), dispMatrix, castMatrix) {}
: SignalCastRegisterer(typeid(bnuMatrix), castMatrix) {}
static boost::any castMatrix(std::istringstream &iss) {
bnuMatrix res;
iss >> res;
return res;
}
static void dispMatrix(const boost::any &object, std::ostream &os) {
const bnuMatrix &m = boost::any_cast<bnuMatrix>(object);
os << m << std::endl;
}
};
EigenCastRegisterer_V myVectorCast;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment