Libbarrett  1.2.4
include/barrett/log/detail/traits-helper.h
Go to the documentation of this file.
00001 
00033 #ifndef BARRETT_LOG_DETAIL_TRAITS_HELPER_H_
00034 #define BARRETT_LOG_DETAIL_TRAITS_HELPER_H_
00035 
00036 
00037 #include <ostream>
00038 #include <boost/tuple/tuple.hpp>
00039 
00040 
00041 namespace barrett {
00042 namespace log {
00043 
00044 
00045 // forward declaration from "../traits.h"
00046 template<typename T> struct Traits;
00047 
00048 
00049 namespace detail {
00050 
00051 
00052 template<typename T>
00053 inline void arrayAsCSV(std::ostream& os, const T& array, const size_t size)
00054 {
00055         for (size_t i = 0; i < (size - 1); ++i) {
00056                 os << array[i] << ",";
00057         }
00058         os << array[size - 1];
00059 }
00060 
00061 
00062 template<size_t N, typename TraitsType>
00063 struct TupleTraitsHelper {
00064 
00065         typedef typename TraitsType::tuple_type tuple_type;
00066         typedef typename TraitsType::parameter_type parameter_type;
00067 
00068         static const size_t INDEX = TraitsType::NUM_INPUTS - N;
00069         typedef Traits<typename boost::tuples::element<INDEX, tuple_type>::type> element_traits;
00070         typedef TupleTraitsHelper<(N - 1), TraitsType> next_helper;
00071 
00072 
00073         static size_t serializedLength() {
00074                 return element_traits::serializedLength() + next_helper::serializedLength();
00075         }
00076 
00077         static void serialize(parameter_type source, char* dest) {
00078                 element_traits::serialize(boost::get<INDEX>(source), dest);
00079                 next_helper::serialize(source, dest + element_traits::serializedLength());
00080         }
00081 
00082         static void unserialize(char* source, tuple_type* t) {
00083                 boost::get<INDEX>(*t) = element_traits::unserialize(source);
00084                 next_helper::unserialize(source + element_traits::serializedLength(), t);
00085         }
00086 
00087         static void asCSV(parameter_type source, std::ostream& os) {
00088                 if (INDEX != 0) {
00089                         os << ",";
00090                 }
00091 
00092                 element_traits::asCSV(boost::get<INDEX>(source), os);
00093                 next_helper::asCSV(source, os);
00094         }
00095 };
00096 
00097 // base-case specialization (N == 0)
00098 template<typename TraitsType>
00099 struct TupleTraitsHelper<0, TraitsType> {
00100 
00101         typedef typename TraitsType::tuple_type tuple_type;
00102         typedef typename TraitsType::parameter_type parameter_type;
00103 
00104         static size_t serializedLength() {  return 0;  }
00105         static void serialize(parameter_type source, char* dest) {}
00106         static void unserialize(char* source, tuple_type* t) {}
00107         static void asCSV(parameter_type source, std::ostream& os) {}
00108 };
00109 
00110 
00111 }
00112 }
00113 }
00114 
00115 
00116 #endif /* BARRETT_LOG_DETAIL_TRAITS_HELPER_H_ */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Defines