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 13:10:24


On 02/21/13 10:47, Larry Evans wrote:
> 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.
Or, something like the serialization library's "external key":

http://www.boost.org/doc/libs/1_53_0/libs/serialization/doc/extended_type_info.html

AFAICT, such an external key serves a purpose similar
to variant's tag in that it's used to tag which value
is stored in some "storage medium'. In the case of variant,
the "storage medium" is a character buffer inside variant.
In the case of the serialization library, it's the
serialization's archive file.

Of course maybe I'm oversimplifying.
[snip]


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