
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