Skip to content

Commit e3eeee4

Browse files
committed
Make signal_disp templated
1 parent 9cba6ea commit e3eeee4

File tree

4 files changed

+22
-9
lines changed

4 files changed

+22
-9
lines changed

include/dynamic-graph/signal-caster.h

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,26 @@ class DYNAMIC_GRAPH_DLLAPI SignalCastRegisterer {
9393
}
9494
};
9595

96-
/// Global signal cast template (helper) functions
97-
///
98-
/// Using these avoid using the typeid () operator and keeps the
99-
/// implementation details hidden.
100-
template <typename T> void signal_disp(const T &value, std::ostream &os) {
101-
SignalCaster::getInstance()->disp(value, os);
96+
/// Template class used to serialize a signal value.
97+
template <typename T> struct signal_disp {
98+
inline static void run (const T &value, std::ostream &os) { os << value << '\n'; }
99+
};
100+
101+
/// Template specialization of signal_disp for Eigen objects
102+
template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
103+
struct signal_disp<Eigen::Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > > {
104+
inline static void run(const Eigen::Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > &value, std::ostream &os) {
105+
static const Eigen::IOFormat row_format (Eigen::StreamPrecision,
106+
Eigen::DontAlignCols, " ", " ", "", "", "", "");
107+
os << value.format(row_format);
102108
}
109+
};
110+
111+
/// Template specialization of signal_disp for std::string.
112+
/// Do not print '\n' at the end.
113+
template <> struct signal_disp<std::string> {
114+
inline static void run (const std::string &value, std::ostream &os) { os << value; }
115+
};
103116

104117
template <typename T> T signal_cast(std::istringstream &iss) {
105118
return boost::any_cast<T>(SignalCaster::getInstance()->cast(typeid(T), iss));

include/dynamic-graph/signal.t.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ void Signal<T, Time>::set(std::istringstream &stringValue) {
3535

3636
template <class T, class Time>
3737
void Signal<T, Time>::get(std::ostream &os) const {
38-
signal_disp<T>(this->accessCopy(), os);
38+
signal_disp<T>::run(this->accessCopy(), os);
3939
}
4040

4141
template <class T, class Time>

tests/signal-cast-registerer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ BOOST_AUTO_TEST_CASE(custom_vector_registerer) {
152152
output_test_stream output;
153153
myVectorSignal.get(output);
154154

155-
boost::format fmt("[ %d %d %d %d %d ];\n");
155+
boost::format fmt("%d %d %d %d %d");
156156
fmt % (i == 0) % (i == 1) % (i == 2) % (i == 3) % (i == 4);
157157

158158
BOOST_CHECK(output.is_equal(fmt.str()));

tests/signal-ptr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ BOOST_AUTO_TEST_CASE(plug_signal_string) {
286286
inSigVec.recompute(1);
287287
output_test_stream output;
288288
inSigVec.get(output);
289-
BOOST_CHECK(output.is_equal("1\n2\n3\n4\n5\n"));
289+
BOOST_CHECK(output.is_equal("1 2 3 4 5"));
290290

291291
Signal<std::string, int> s("signal");
292292
std::ostringstream os2;

0 commit comments

Comments
 (0)