Boost logo

Boost Users :

Subject: Re: [Boost-users] Can boost help me organize/parse lots of binary data?
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2013-02-20 22:24:55


AMDG

On 02/20/2013 03:05 PM, Chris Stankevitz wrote:
> I am receiving a "live" stream of binary data that represents the
> states of various objects. Sort of like this:
> Car
> Speed (m/s): float32
> Temperature (deg): uint16
> ...
>
> My real packets have hundreds of items in them. I will be decoding
> many of these "packets" and doing various things with them like
> printing their values to console, etc.
>
> My plan is just to make classes:
>
> class Car
> {
> public:
> void Decode(istream&);
> private:
> float speed;
> unsigned temperature
> ...
> };
>
> But it will not be very extendable.
>
> Unfocused open-ended question: Does boost offer something that will
> help me with this?
>

I suppose that you could try something
with Boost.Fusion (warning untested):

BOOST_FUSION_DEFINE_STRUCT_INLINE(struct Car,
  (float, speed)
  (unsigned, temperature)
)

struct decoder {
  typedef void result_type;
  template<class T>
  void operator()(T& t, istream& is) {
    decode(t, is);
  }
};

// recursively decode nested structs
template<class T>
typename boost::enable_if<
  boost::fusion::traits::is_sequence<T>
>::type decode(T& t, istream& is) {
  boost::fusion::for_each(t, boost::phoenix::bind(decoder(), _1, is));
}

// load arithmetic types directly
template<class T>
typename boost::enable_if<
  boost::fusion::traits::is_arithmetic<T>
>::type decode(T& t, istream& is) {
  ...
}

Anyway, the idea is that Boost.Fusion allows you
to iterate over the members of a struct.

In Christ,
Steven Watanabe


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net