dynamic-graph  4.3.1
Dynamic graph library
signal-caster.h
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
7 #include <map>
8 #include <vector>
9 
10 #include <boost/format.hpp>
11 #include <boost/lexical_cast.hpp>
12 
13 #include "dynamic-graph/exception-signal.h"
14 #include <dynamic-graph/dynamic-graph-api.h>
15 #include <dynamic-graph/eigen-io.h>
16 #include <dynamic-graph/linear-algebra.h>
17 
18 namespace dynamicgraph {
19 
22 template <typename T> struct signal_io_base {
24  inline static void disp(const T &value, std::ostream &os) { os << 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  }
36  inline static void trace(const T &value, std::ostream &os) { os << value; }
37 };
38 
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 
53 template <typename T> struct signal_io : signal_io_base<T> {};
54 
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>>
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  }
77 };
78 
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  }
97 };
98 
101 template <> struct signal_io<std::string> : signal_io_base<std::string> {
102  inline static std::string cast(std::istringstream &iss) { return iss.str(); }
103 };
104 
117 template <> struct signal_io<double> : signal_io_base<double> {
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  }
135  }
136 };
137 
138 } // end of namespace dynamicgraph.
139 
140 #endif
Definition: eigen-io.h:29
Class used for I/O operations in Signal<T,Time>
Definition: signal-caster.h:53
Exceptions raised when an error related to signals happen.
static void trace(const T &value, std::ostream &os)
write a signal value to log file
Definition: signal-caster.h:36
Inherit from this class if tracing is not implemented for a given type.
Definition: signal-caster.h:40
static void disp(const T &value, std::ostream &os)
serialize a signal value.
Definition: signal-caster.h:24
static T cast(std::istringstream &is)
deserialize a signal value.
Definition: signal-caster.h:26