
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. 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