Libbarrett  1.2.4
include/barrett/log/traits.h
00001 
00033 #ifndef BARRETT_LOG_TRAITS_H_
00034 #define BARRETT_LOG_TRAITS_H_
00035 
00036 
00037 #include <ostream>
00038 #include <cstring>
00039 
00040 #include <boost/tuple/tuple.hpp>
00041 #include <boost/array.hpp>
00042 #include <Eigen/Geometry>
00043 
00044 #include <barrett/math/matrix.h>
00045 #include <barrett/log/detail/traits-helper.h>
00046 
00047 
00048 namespace barrett {
00049 namespace log {
00050 
00051 
00052 // default traits delegate to the type in question
00053 template<typename T> struct DefaultTraits {
00054         typedef const T& parameter_type;
00055 
00056         static size_t serializedLength() {
00057                 return T::serializedLength();
00058         }
00059 
00060         static void serialize(parameter_type source, char* dest) {
00061                 source.serialize(dest);
00062         }
00063 
00064         static T unserialize(char* source) {
00065                 return T::unserialize(source);
00066         }
00067 
00068         static void asCSV(parameter_type source, std::ostream& os) {
00069                 os << source;
00070         }
00071 };
00072 
00073 template<typename T> struct Traits : public DefaultTraits<T> {};
00074 
00075 
00076 template<typename T> struct PODTraits {
00077         typedef const T& parameter_type;
00078 
00079         static size_t serializedLength() {
00080                 return sizeof(T);
00081         }
00082 
00083         static void serialize(parameter_type source, char* dest) {
00084                 std::memcpy(dest, &source, serializedLength());
00085         }
00086 
00087         static T unserialize(char* source) {
00088                 return *reinterpret_cast<T*>(source);
00089         }
00090 
00091         static void asCSV(parameter_type source, std::ostream& os) {
00092                 os << source;
00093         }
00094 };
00095 
00096 template<> struct Traits<bool>                                  : public PODTraits<bool> {};
00097 template<> struct Traits<float>                                 : public PODTraits<float> {};
00098 template<> struct Traits<double>                                : public PODTraits<double> {};
00099 template<> struct Traits<signed char>                   : public PODTraits<signed char> {};
00100 template<> struct Traits<short>                                 : public PODTraits<short> {};
00101 template<> struct Traits<int>                                   : public PODTraits<int> {};
00102 template<> struct Traits<long>                                  : public PODTraits<long> {};
00103 template<> struct Traits<long long>                             : public PODTraits<long long> {};
00104 template<> struct Traits<unsigned char>                 : public PODTraits<unsigned char> {};
00105 template<> struct Traits<unsigned short>                : public PODTraits<unsigned short> {};
00106 template<> struct Traits<unsigned int>                  : public PODTraits<unsigned int> {};
00107 template<> struct Traits<unsigned long>                 : public PODTraits<unsigned long> {};
00108 template<> struct Traits<unsigned long long>    : public PODTraits<unsigned long long> {};
00109 
00110 
00111 template<typename T, size_t N> struct Traits< ::boost::array<T,N> > {
00112         typedef const ::boost::array<T,N>& parameter_type;
00113 
00114         static size_t serializedLength() {
00115                 return N * Traits<T>::serializedLength();
00116         }
00117 
00118         static void serialize(parameter_type source, char* dest) {
00119                 for (size_t i = 0; i < N; ++i) {
00120                         Traits<T>::serialize(source[i], dest);
00121                         dest += Traits<T>::serializedLength();
00122                 }
00123         }
00124 
00125         static ::boost::array<T,N> unserialize(char* source) {
00126                 ::boost::array<T,N> dest;
00127                 for (size_t i = 0; i < N; ++i) {
00128                         dest[i] = Traits<T>::unserialize(source);
00129                         source += Traits<T>::serializedLength();
00130                 }
00131                 return dest;
00132         }
00133 
00134         static void asCSV(parameter_type source, std::ostream& os) {
00135                 detail::arrayAsCSV(os, source, N);
00136         }
00137 };
00138 
00139 
00140 template<typename Scalar> struct Traits<Eigen::Quaternion<Scalar> > {
00141         typedef Eigen::Quaternion<Scalar> T;
00142         typedef const T& parameter_type;
00143 
00144         static size_t serializedLength() {
00145                 return sizeof(Scalar) * 4;
00146         }
00147 
00148         static void serialize(parameter_type source, char* dest) {
00149                 std::memcpy(dest, source.coeffs().data(), serializedLength());
00150         }
00151 
00152         static T unserialize(char* source) {
00153                 T q;
00154                 std::memcpy(q.coeffs().data(), source, serializedLength());
00155                 return q;
00156         }
00157 
00158         static void asCSV(parameter_type source, std::ostream& os) {
00159                 os << source.w() << "," << source.x() << "," << source.y() << "," << source.z();
00160         }
00161 };
00162 
00163 
00164 //template<typename TraitsDerived> struct Traits<Eigen::MatrixBase<TraitsDerived> > :
00165 //              public DefaultTraits<Eigen::MatrixBase<TraitsDerived> > {
00166 //      typedef typename DefaultTraits<Eigen::MatrixBase<TraitsDerived> >::parameter_type parameter_type;
00167 //      static void asCSV(parameter_type source, std::ostream& os) {
00168 //              os << source;
00169 //      }
00170 //};
00171 //
00172 //template<int R, int C, typename Units> struct Traits<math::Matrix<R,C, Units> > :
00173 //              public Traits<Eigen::MatrixBase<typename math::Matrix<R,C, Units>::Base> > {};
00174 
00175 template<int R, int C, typename Units> struct Traits<math::Matrix<R,C, Units> > :
00176                 public DefaultTraits<math::Matrix<R,C, Units> > {
00177 
00178         typedef typename DefaultTraits<math::Matrix<R,C, Units> >::parameter_type parameter_type;
00179 
00180         static void asCSV(parameter_type source, std::ostream& os) {
00181                 detail::arrayAsCSV(os, source, source.size());
00182         }
00183 };
00184 
00185 
00186 template<
00187         typename T0, typename T1, typename T2, typename T3, typename T4,
00188         typename T5, typename T6, typename T7, typename T8, typename T9>
00189 struct Traits<boost::tuple<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> > {
00190 
00191         typedef boost::tuple<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> tuple_type;
00192         static const size_t NUM_INPUTS = boost::tuples::length<tuple_type>::value;
00193 
00194         typedef detail::TupleTraitsHelper<NUM_INPUTS, Traits<tuple_type> > tuple_traits_helper;
00195 
00196         typedef const tuple_type& parameter_type;
00197 
00198         static size_t serializedLength() {
00199                 return tuple_traits_helper::serializedLength();
00200         }
00201 
00202         static void serialize(parameter_type source, char* dest) {
00203                 tuple_traits_helper::serialize(source, dest);
00204         }
00205 
00206         static tuple_type unserialize(char* source) {
00207                 tuple_type t;
00208                 tuple_traits_helper::unserialize(source, &t);
00209                 return t;
00210         }
00211 
00212         static void asCSV(parameter_type source, std::ostream& os) {
00213                 tuple_traits_helper::asCSV(source, os);
00214         }
00215 };
00216 
00217 
00218 }
00219 }
00220 
00221 
00222 #endif /* BARRETT_LOG_TRAITS_H_ */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Defines