Commit e3eeee49 authored by Joseph Mirabel's avatar Joseph Mirabel
Browse files

Make signal_disp templated

parent 9cba6ea9
......@@ -93,13 +93,26 @@ public:
}
};
/// Global signal cast template (helper) functions
///
/// Using these avoid using the typeid () operator and keeps the
/// implementation details hidden.
template <typename T> void signal_disp(const T &value, std::ostream &os) {
SignalCaster::getInstance()->disp(value, os);
/// Template class used to serialize a signal value.
template <typename T> struct signal_disp {
inline static void run (const T &value, std::ostream &os) { os << value << '\n'; }
};
/// Template specialization of signal_disp for Eigen objects
template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
struct signal_disp<Eigen::Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > > {
inline static void run(const Eigen::Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > &value, std::ostream &os) {
static const Eigen::IOFormat row_format (Eigen::StreamPrecision,
Eigen::DontAlignCols, " ", " ", "", "", "", "");
os << value.format(row_format);
}
};
/// Template specialization of signal_disp for std::string.
/// Do not print '\n' at the end.
template <> struct signal_disp<std::string> {
inline static void run (const std::string &value, std::ostream &os) { os << value; }
};
template <typename T> T signal_cast(std::istringstream &iss) {
return boost::any_cast<T>(SignalCaster::getInstance()->cast(typeid(T), iss));
......
......@@ -35,7 +35,7 @@ void Signal<T, Time>::set(std::istringstream &stringValue) {
template <class T, class Time>
void Signal<T, Time>::get(std::ostream &os) const {
signal_disp<T>(this->accessCopy(), os);
signal_disp<T>::run(this->accessCopy(), os);
}
template <class T, class Time>
......
......@@ -152,7 +152,7 @@ BOOST_AUTO_TEST_CASE(custom_vector_registerer) {
output_test_stream output;
myVectorSignal.get(output);
boost::format fmt("[ %d %d %d %d %d ];\n");
boost::format fmt("%d %d %d %d %d");
fmt % (i == 0) % (i == 1) % (i == 2) % (i == 3) % (i == 4);
BOOST_CHECK(output.is_equal(fmt.str()));
......
......@@ -286,7 +286,7 @@ BOOST_AUTO_TEST_CASE(plug_signal_string) {
inSigVec.recompute(1);
output_test_stream output;
inSigVec.get(output);
BOOST_CHECK(output.is_equal("1\n2\n3\n4\n5\n"));
BOOST_CHECK(output.is_equal("1 2 3 4 5"));
Signal<std::string, int> s("signal");
std::ostringstream os2;
......
Markdown is supported
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