12
12
13
13
#include " dynamic-graph/exception-signal.h"
14
14
#include < dynamic-graph/dynamic-graph-api.h>
15
- #include < dynamic-graph/linear-algebra.h>
16
15
#include < dynamic-graph/eigen-io.h>
16
+ #include < dynamic-graph/linear-algebra.h>
17
17
18
18
namespace dynamicgraph {
19
19
20
20
// / Inherit from this class if you want to keep default implementation for some
21
21
// / functions.
22
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 ());
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;
32
34
}
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; }
35
+ // / write a signal value to log file
36
+ inline static void trace (const T &value, std::ostream &os) { os << value; }
37
37
};
38
38
39
39
// / Inherit from this class if tracing is not implemented for a given type.
40
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
- }
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
50
};
51
51
52
52
// / Class used for I/O operations in Signal<T,Time>
53
53
template <typename T> struct signal_io : signal_io_base<T> {};
54
54
55
55
// / Template specialization of signal_disp for Eigen objects
56
- template <typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
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) {
63
- static const Eigen::IOFormat row_format (Eigen::StreamPrecision,
64
- Eigen::DontAlignCols, " " , " " , " " , " " , " " , " " );
65
- os << value.format (row_format);
66
- }
67
-
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
- }
56
+ template <typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows,
57
+ int _MaxCols>
58
+ struct signal_io <
59
+ Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>>
60
+ : signal_io_base<
61
+ Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>> {
62
+ typedef Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>
63
+ matrix_type;
64
+
65
+ inline static void disp (const matrix_type &value, std::ostream &os) {
66
+ static const Eigen::IOFormat row_format (
67
+ Eigen::StreamPrecision, Eigen::DontAlignCols, " " , " " , " " , " " , " " , " " );
68
+ os << value.format (row_format);
69
+ }
70
+
71
+ inline static void trace (const matrix_type &value, std::ostream &os) {
72
+ static const Eigen::IOFormat row_format (Eigen::StreamPrecision,
73
+ Eigen::DontAlignCols, " \t " , " \t " ,
74
+ " " , " " , " " , " " );
75
+ os << value.format (row_format);
76
+ }
73
77
};
74
78
75
79
// / 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);
93
- }
80
+ template <typename _Scalar, int _Options>
81
+ struct signal_io <Eigen::Quaternion<_Scalar, _Options>>
82
+ : signal_io_base<Eigen::Quaternion<_Scalar, _Options>> {
83
+ typedef Eigen::Quaternion<_Scalar, _Options> quat_type;
84
+ typedef Eigen::Matrix<_Scalar, 4 , 1 , _Options> matrix_type;
85
+
86
+ inline static void disp (const quat_type &value, std::ostream &os) {
87
+ signal_io<matrix_type>::disp (value.coeffs (), os);
88
+ }
89
+
90
+ inline static quat_type cast (std::istringstream &is) {
91
+ return quat_type (signal_io<matrix_type>::cast (is));
92
+ }
93
+
94
+ inline static void trace (const quat_type &value, std::ostream &os) {
95
+ signal_io<matrix_type>::trace (value.coeffs (), os);
96
+ }
94
97
};
95
98
96
99
// / Template specialization of signal_io for std::string.
97
100
// / 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 (); }
101
+ template <> struct signal_io <std::string> : signal_io_base<std::string> {
102
+ inline static std::string cast (std::istringstream &iss) { return iss.str (); }
101
103
};
102
104
103
105
// / Template specialization of signal_io for double
@@ -113,24 +115,24 @@ inline static std::string cast (std::istringstream &iss) { return iss.str(); }
113
115
// / (the strings used are the one produces by displaying special
114
116
// / values on a stream).
115
117
template <> struct signal_io <double > : signal_io_base<double > {
116
- inline static double cast (std::istringstream &iss) {
117
- std::string tmp (iss.str ());
118
-
119
- if (tmp == " nan" )
120
- return std::numeric_limits<double >::quiet_NaN ();
121
- else if (tmp == " inf" || tmp == " +inf" )
122
- return std::numeric_limits<double >::infinity ();
123
- else if (tmp == " -inf" )
124
- return -1 . * std::numeric_limits<double >::infinity ();
125
-
126
- try {
127
- return boost::lexical_cast<double >(tmp);
128
- } catch (boost::bad_lexical_cast &) {
129
- boost::format fmt (" failed to serialize %s (to double)" );
130
- fmt % tmp;
131
- throw ExceptionSignal (ExceptionSignal::GENERIC, fmt.str ());
118
+ inline static double cast (std::istringstream &iss) {
119
+ std::string tmp (iss.str ());
120
+
121
+ if (tmp == " nan" )
122
+ return std::numeric_limits<double >::quiet_NaN ();
123
+ else if (tmp == " inf" || tmp == " +inf" )
124
+ return std::numeric_limits<double >::infinity ();
125
+ else if (tmp == " -inf" )
126
+ return -1 . * std::numeric_limits<double >::infinity ();
127
+
128
+ try {
129
+ return boost::lexical_cast<double >(tmp);
130
+ } catch (boost::bad_lexical_cast &) {
131
+ boost::format fmt (" failed to serialize %s (to double)" );
132
+ fmt % tmp;
133
+ throw ExceptionSignal (ExceptionSignal::GENERIC, fmt.str ());
134
+ }
132
135
}
133
- }
134
136
};
135
137
136
138
} // end of namespace dynamicgraph.
0 commit comments