Boost logo

Boost Users :

Subject: Re: [Boost-users] Can boost help me organize/parse lots of binary data?
From: Larry Evans (cppljevans_at_[hidden])
Date: 2013-02-21 11:47:14


On 02/21/13 08:15, Antony Polukhin wrote:
> This looks like a perfect place for Boost.Variant. Here is some pseudo code:
>
> #include <boost/variant.hpp>
>
> struct DecoderVisitor: boost ::static_vizitor<> {
> istream& Stream;
>
> explicit DecoderVisitor(istream& Stream): Stream(Stream){}
>
> void operator()(String& s) const {
> s.Decode(Stream);
> }
>
> void operator()(Double& d) const {
> d.Decode(Stream);
> }
>
> // or you may just write
> // template <class T>
> // void operator()(T& val) const {
> // val.Decode(Stream);
> //}
> };
>
>
> class EmployeePacket
> {
> typedef boost::variant<String, Double> variant_t;
> std::vector<variant_t> Items;
>
> EmployeePacket()
> {
> Items.push_back(String("name", "John Doe"));
> Items.push_back(Double("salary", "USD", 1, 1));
> }
>
> void Decode(istream& Stream)
> {
> for (auto pItem : Items)
> boost::apply_visitor(DecoderVisitor(Stream), pItem)
> }
>
> double GetSalary()
> {
> return boost::get<Double>(Items[1]).value;
> }
> }
>
> class Double
> {
> string name;
> string units;
> // double decode_conversion_scale;
> // double decode_conversion_translate;
> // unsigned number_of_bits_used_to_encode
> // double scale_factor
> double value;
> void Decode(istream& Stream)
> {
> Stream.read(&value, 8);
> value = value * decode_conversion_scale + decode_conversion_translate;
> }
>
> class String
> {
> string name;
> string value;
> void Decode(istream& Stream);
> }
>
>
Hi Anthony,

I haven't a clear idea how this would work because I assumed that
the binary input stream would have to have something that
1st would be decoded into the tag for the variant.
Then, if your variant visitor solution is used, some
code (not shown) would create some default constructed value for
the bounded type corresponding to that tag and then call the visitor.

All the above is just off the top of my head, but, as I said,
I'm not real sure the variant visitor solution would work.

Could you provide more pseudo code to clarify.

Also, Chris, could you provide more details about this
binary input stream?

-regards,
Larry


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