7#include <boost/numeric/conversion/cast.hpp>
15 template<
typename SourceType,
typename DestType>
22 using round_style = boost::mpl::integral_c<std::float_round_style, std::round_to_nearest>;
26 boost::numeric::converter<DestType, SourceType, boost::numeric::conversion_traits<DestType, SourceType>,
30 template<
typename SourceType>
33 static Void convert(__attribute__((unused)) SourceType s) {
return {}; }
37 template<
typename DestType>
40 static DestType
convert(__attribute__((unused))
Void s) {
return 0.0; }
48 template<
typename CookedType,
typename RAW_ITERATOR,
typename COOKED_ITERATOR>
50 const RAW_ITERATOR& raw_begin,
const RAW_ITERATOR& raw_end,
const COOKED_ITERATOR& cooked_begin)
const {
52 assert((std::is_same<
typename std::iterator_traits<RAW_ITERATOR>::value_type, int32_t>::value));
53 static_assert(std::is_same<typename std::iterator_traits<RAW_ITERATOR>::value_type, int8_t>::value ||
54 std::is_same<typename std::iterator_traits<RAW_ITERATOR>::value_type, int16_t>::value ||
55 std::is_same<typename std::iterator_traits<RAW_ITERATOR>::value_type, int32_t>::value,
56 "RAW_ITERATOR template argument must be an iterator with value type equal to int8_t, int16_t or int32_t.");
57 static_assert(std::is_same<typename std::iterator_traits<COOKED_ITERATOR>::value_type, CookedType>::value,
58 "COOKED_ITERATOR template argument must be an iterator with value type equal to the CookedType template "
63 template<
typename CookedType,
typename RAW_ITERATOR,
typename COOKED_ITERATOR>
65 static void impl(
const RAW_ITERATOR& raw_begin,
const RAW_ITERATOR& raw_end, COOKED_ITERATOR cooked_begin);
69 template<
typename CookedType>
72 vectorToCooked<CookedType>(&
raw, (&
raw) + 1, &cooked);
76 template<
typename CookedType>
77 uint32_t
toRaw(CookedType cookedValue)
const;
86 template<
typename CookedType,
typename RAW_ITERATOR,
typename COOKED_ITERATOR>
88 const RAW_ITERATOR& raw_begin,
const RAW_ITERATOR& raw_end, COOKED_ITERATOR cooked_begin) {
89 for(
auto it = raw_begin; it != raw_end; ++it) {
91 float genericRepresentation;
92 memcpy(&genericRepresentation, &(*it),
sizeof(
float));
100 template<
typename CookedType>
104 float genericRepresentation;
108 catch(boost::numeric::positive_overflow&) {
109 genericRepresentation = FLT_MAX;
111 catch(boost::numeric::negative_overflow&) {
112 genericRepresentation = -FLT_MAX;
116 void* warningAvoider = &genericRepresentation;
118 int32_t rawValue = *(
reinterpret_cast<int32_t*
>(warningAvoider));
123 template<
typename RAW_ITERATOR,
typename COOKED_ITERATOR>
125 static void impl(
const RAW_ITERATOR& raw_begin,
const RAW_ITERATOR& raw_end, COOKED_ITERATOR cooked_begin) {
126 for(
auto it = raw_begin; it != raw_end; ++it) {
129 float genericRepresentation = *(
reinterpret_cast<const float*
>(&(*it)));
@ raw
Raw access: disable any possible conversion from the original hardware data type into the given UserT...
std::string to_string(const std::string &v)
static void impl(const RAW_ITERATOR &raw_begin, const RAW_ITERATOR &raw_end, COOKED_ITERATOR cooked_begin)
static void impl(const RAW_ITERATOR &raw_begin, const RAW_ITERATOR &raw_end, COOKED_ITERATOR cooked_begin)
Needs to have the same interface as FixedPointConverter, except for the constructor.
IEEE754_SingleConverter(const std::string &="")
bool operator!=(const IEEE754_SingleConverter &) const
CookedType scalarToCooked(int32_t const &raw) const
Inefficient convenience function for converting a single value to cooked.
uint32_t toRaw(CookedType cookedValue) const
bool operator==(const IEEE754_SingleConverter &) const
void vectorToCooked(const RAW_ITERATOR &raw_begin, const RAW_ITERATOR &raw_end, const COOKED_ITERATOR &cooked_begin) const
define round type for the boost::numeric::converter
boost::mpl::integral_c< std::float_round_style, std::round_to_nearest > round_style
static Void convert(__attribute__((unused)) SourceType s)
static DestType convert(__attribute__((unused)) Void s)
boost::numeric::converter< DestType, SourceType, boost::numeric::conversion_traits< DestType, SourceType >, boost::numeric::def_overflow_handler, Round< SourceType > > converter