
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
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
variant_t; std::vector 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_in... 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]