signal-caster.h
Go to the documentation of this file.
1 // -*- c++-mode -*-
2 // Copyright 2010 François Bleibel Thomas Moulard, Olivier Stasse
3 //
4 
5 #ifndef DYNAMIC_GRAPH_SIGNAL_CASTER_HH
6 #define DYNAMIC_GRAPH_SIGNAL_CASTER_HH
10 
11 #include <boost/format.hpp>
12 #include <boost/lexical_cast.hpp>
13 #include <map>
14 #include <vector>
15 
17 
18 namespace dynamicgraph {
19 
22 template <typename T>
25  inline static void disp(const T &value, std::ostream &os) { os << value; }
27  inline static T cast(std::istringstream &is) {
28  T inst;
29  is >> inst;
30  if (is.fail()) {
32  "failed to serialize " + is.str());
33  }
34  return inst;
35  }
37  inline static void trace(const T &value, std::ostream &os) { os << value; }
38 };
39 
41 template <typename T>
43  inline static void disp(const T &, std::ostream &) {
44  throw std::logic_error("this disp is not implemented.");
45  }
46  inline static T cast(std::istringstream &) {
47  throw std::logic_error("this cast is not implemented.");
48  }
49  inline static void trace(const T &, std::ostream &) {
50  throw std::logic_error("this trace is not implemented.");
51  }
52 };
53 
55 template <typename T>
56 struct signal_io : signal_io_base<T> {};
57 
59 template <typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows,
60  int _MaxCols>
61 struct signal_io<
62  Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>>
64  Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>> {
65  typedef Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>
67 
68  inline static void disp(const matrix_type &value, std::ostream &os) {
69  static const Eigen::IOFormat row_format(
70  Eigen::StreamPrecision, Eigen::DontAlignCols, " ", " ", "", "", "", "");
71  os << value.format(row_format);
72  }
73 
74  inline static void trace(const matrix_type &value, std::ostream &os) {
75  static const Eigen::IOFormat row_format(Eigen::StreamPrecision,
76  Eigen::DontAlignCols, "\t", "\t",
77  "", "", "", "");
78  os << value.format(row_format);
79  }
80 };
81 
83 template <typename _Scalar, int _Options>
84 struct signal_io<Eigen::Quaternion<_Scalar, _Options>>
85  : signal_io_base<Eigen::Quaternion<_Scalar, _Options>> {
86  typedef Eigen::Quaternion<_Scalar, _Options> quat_type;
87  typedef Eigen::Matrix<_Scalar, 4, 1, _Options> matrix_type;
88 
89  inline static void disp(const quat_type &value, std::ostream &os) {
90  signal_io<matrix_type>::disp(value.coeffs(), os);
91  }
92 
93  inline static quat_type cast(std::istringstream &is) {
94  return quat_type(signal_io<matrix_type>::cast(is));
95  }
96 
97  inline static void trace(const quat_type &value, std::ostream &os) {
98  signal_io<matrix_type>::trace(value.coeffs(), os);
99  }
100 };
101 
104 template <>
105 struct signal_io<std::string> : signal_io_base<std::string> {
106  inline static std::string cast(std::istringstream &iss) { return iss.str(); }
107 };
108 
121 template <>
122 struct signal_io<double> : signal_io_base<double> {
123  inline static double cast(std::istringstream &iss) {
124  std::string tmp(iss.str());
125 
126  if (tmp == "nan")
127  return std::numeric_limits<double>::quiet_NaN();
128  else if (tmp == "inf" || tmp == "+inf")
129  return std::numeric_limits<double>::infinity();
130  else if (tmp == "-inf")
131  return -1. * std::numeric_limits<double>::infinity();
132 
133  try {
134  return boost::lexical_cast<double>(tmp);
135  } catch (boost::bad_lexical_cast &) {
136  boost::format fmt("failed to serialize %s (to double)");
137  fmt % tmp;
138  throw ExceptionSignal(ExceptionSignal::GENERIC, fmt.str());
139  }
140  }
141 };
142 
143 } // end of namespace dynamicgraph.
144 
145 #endif
static T cast(std::istringstream &)
Definition: signal-caster.h:46
static void disp(const quat_type &value, std::ostream &os)
Definition: signal-caster.h:89
Definition: eigen-io.h:27
Class used for I/O operations in Signal<T,Time>
Definition: signal-caster.h:56
static T cast(std::istringstream &is)
deserialize a signal value.
Definition: signal-caster.h:27
Eigen::Matrix< _Scalar, 4, 1, _Options > matrix_type
Definition: signal-caster.h:87
static double cast(std::istringstream &iss)
Eigen::Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > matrix_type
Definition: signal-caster.h:66
Exceptions raised when an error related to signals happen.
static void trace(const quat_type &value, std::ostream &os)
Definition: signal-caster.h:97
Inherit from this class if tracing is not implemented for a given type.
Definition: signal-caster.h:42
static void trace(const T &, std::ostream &)
Definition: signal-caster.h:49
Eigen::MatrixXd Matrix
static std::string cast(std::istringstream &iss)
static void disp(const T &value, std::ostream &os)
serialize a signal value.
Definition: signal-caster.h:25
static void disp(const T &, std::ostream &)
Definition: signal-caster.h:43
static void trace(const T &value, std::ostream &os)
write a signal value to log file
Definition: signal-caster.h:37


dynamic-graph
Author(s): Nicolas Mansard, Olivier Stasse
autogenerated on Sun Jun 25 2023 02:06:03