16
16
#include < dynamic-graph/eigen-io.h>
17
17
18
18
namespace dynamicgraph {
19
- // / Template class used to serialize a signal value.
20
- template <typename T> struct signal_disp {
21
- inline static void run (const T &value, std::ostream &os) { os << value; }
19
+
20
+ // / Inherit from this class if you want to keep default implementation for some
21
+ // / functions.
22
+ template <typename T> struct signal_io_base {
23
+ // / serialize a signal value.
24
+ inline static void disp (const T &value, std::ostream &os) { os << value; }
25
+ // / deserialize a signal value.
26
+ inline static T cast (std::istringstream &is) {
27
+ T inst;
28
+ is >> inst;
29
+ if (is.fail ()) {
30
+ throw ExceptionSignal (ExceptionSignal::GENERIC,
31
+ " failed to serialize " + is.str ());
32
+ }
33
+ return inst;
34
+ }
35
+ // / write a signal value to log file
36
+ inline static void trace (const T &value, std::ostream &os) { os << value; }
22
37
};
23
38
39
+ // / Inherit from this class if tracing is not implemented for a given type.
40
+ template <typename T> struct signal_io_unimplemented {
41
+ inline static void disp (const T &, std::ostream &) {
42
+ throw std::logic_error (" this disp is not implemented." );
43
+ }
44
+ inline static T cast (std::istringstream &) {
45
+ throw std::logic_error (" this cast is not implemented." );
46
+ }
47
+ inline static void trace (const T &, std::ostream &) {
48
+ throw std::logic_error (" this trace is not implemented." );
49
+ }
50
+ };
51
+
52
+ // / Class used for I/O operations in Signal<T,Time>
53
+ template <typename T> struct signal_io : signal_io_base<T> {};
54
+
24
55
// / Template specialization of signal_disp for Eigen objects
25
56
template <typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
26
- struct signal_disp <Eigen::Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > > {
27
- inline static void run (const Eigen::Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > &value, std::ostream &os) {
57
+ struct signal_io <Eigen::Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > >
58
+ : signal_io_base <Eigen::Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > >
59
+ {
60
+ typedef Eigen::Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > matrix_type;
61
+
62
+ inline static void disp (const matrix_type &value, std::ostream &os) {
28
63
static const Eigen::IOFormat row_format (Eigen::StreamPrecision,
29
64
Eigen::DontAlignCols, " " , " " , " " , " " , " " , " " );
30
65
os << value.format (row_format);
31
66
}
32
- };
33
67
34
- // / Template specialization of signal_disp for std::string.
35
- // / Do not print '\n' at the end.
36
- template <> struct signal_disp <std::string> {
37
- inline static void run (const std::string &value, std::ostream &os) { os << value; }
68
+ inline static void trace (const matrix_type &value, std::ostream &os) {
69
+ static const Eigen::IOFormat row_format (Eigen::StreamPrecision,
70
+ Eigen::DontAlignCols, " \t " , " \t " , " " , " " , " " , " " );
71
+ os << value.format (row_format);
72
+ }
38
73
};
39
74
40
- // / Template class used to deserialize a signal value (reverse of signal_disp).
41
- template <typename T> struct signal_cast {
42
- inline static T run (std::istringstream &iss) {
43
- T inst;
44
- iss >> inst;
45
- if (iss.fail ()) {
46
- throw ExceptionSignal (ExceptionSignal::GENERIC,
47
- " failed to serialize " + iss.str ());
48
- }
49
- return inst;
75
+ // / Template specialization of signal_io for Eigen quaternion objects
76
+ template <typename _Scalar, int _Options>
77
+ struct signal_io <Eigen::Quaternion< _Scalar, _Options> >
78
+ : signal_io_base<Eigen::Quaternion< _Scalar, _Options> >
79
+ {
80
+ typedef Eigen::Quaternion< _Scalar, _Options> quat_type;
81
+ typedef Eigen::Matrix< _Scalar, 4 , 1 , _Options> matrix_type;
82
+
83
+ inline static void disp (const quat_type &value, std::ostream &os) {
84
+ signal_io<matrix_type>::disp (value.coeffs (), os);
85
+ }
86
+
87
+ inline static quat_type cast (std::istringstream &is) {
88
+ return quat_type (signal_io<matrix_type>::cast (is));
89
+ }
90
+
91
+ inline static void trace (const quat_type &value, std::ostream &os) {
92
+ signal_io<matrix_type>::trace (value.coeffs (), os);
50
93
}
51
94
};
52
95
53
- // / Template specialization of signal_cast for std::string.
54
- template <> struct signal_cast <std::string> {
55
- inline static std::string run (std::istringstream &iss) { return iss.str (); }
96
+ // / Template specialization of signal_io for std::string.
97
+ // / Do not print '\n' at the end.
98
+ template <> struct signal_io <std::string> : signal_io_base<std::string>
99
+ {
100
+ inline static std::string cast (std::istringstream &iss) { return iss.str (); }
56
101
};
57
102
58
- // / Template specialization of signal_cast for double
103
+ // / Template specialization of signal_io for double
59
104
// / to workaround the limitations of the stream based approach.
60
105
// /
61
106
// / When dealing with double: displaying a double on a stream
@@ -67,8 +112,8 @@ inline static std::string run (std::istringstream &iss) { return iss.str(); }
67
112
// / To workaround this problem, parse special values manually
68
113
// / (the strings used are the one produces by displaying special
69
114
// / values on a stream).
70
- template <> struct signal_cast <double > {
71
- inline static double run (std::istringstream &iss) {
115
+ template <> struct signal_io < double > : signal_io_base <double > {
116
+ inline static double cast (std::istringstream &iss) {
72
117
std::string tmp (iss.str ());
73
118
74
119
if (tmp == " nan" )
@@ -88,21 +133,6 @@ inline static double run (std::istringstream &iss) {
88
133
}
89
134
};
90
135
91
- // / Template class used to display a signal value.
92
- template <typename T> struct signal_trace {
93
- inline static void run (const T &value, std::ostream &os) { os << value; }
94
- };
95
-
96
- // / Template specialization of signal_trace for Eigen objects
97
- template <typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
98
- struct signal_trace <Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> > {
99
- inline static void run (const Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> &value, std::ostream &os) {
100
- static const Eigen::IOFormat row_format (Eigen::StreamPrecision,
101
- Eigen::DontAlignCols, " \t " , " \t " , " " , " " , " " , " " );
102
- os << value.format (row_format);
103
- }
104
- };
105
-
106
136
} // end of namespace dynamicgraph.
107
137
108
138
#endif // ! DYNAMIC_GRAPH_SIGNAL_CASTER_HH
0 commit comments