$include_dir="/home/hyper-archives/boost-users/include"; include("$include_dir/msg-header.inc") ?>
Subject: Re: [Boost-users] Can boost help me organize/parse lots of binary data?
From: Larry Evans (cppljevans_at_[hidden])
Date: 2013-02-21 12:04:47
On 02/20/13 19:21, Chris Stankevitz wrote:
> On Wed, Feb 20, 2013 at 3:27 PM, Richard
> <legalize+jeeves_at_[hidden]> wrote:
>> Boost.Spirit.Qi to parse the stream into data structures
>> Boost.Spirit.Karma to emit the necessary output from the data structures
> 
> Richard,
> 
> Thank you.  I am unfamiliar with Spirit and Karma.  Your comment not
> only introduced me to them but highlighted just how vague my question
> was.  I was thinking less about the actual decoding (who would have
> known based on what I wrote) and more on how I would "generically" and
> "extensibly" handle the many fields I am about to receive.
> 
> For example, consider the following horrible code.  Is there a "boost"
> way to do this kind of thing... (perhaps Spirit/Karma is the answer
> and I am just more out of touch than I imagine):
> 
> Good things about the classes below:
> It is easy to add a new parameter.  Just push one onto the back of
> EmployeePacket::Items
> The class EmployeePacket doesn't have hundreds of data members such as
> "string name_, int age_"
> 
> Bad things about the classes below:
> Extracting data from the EmployeePacket requires hideous dynamic_casts
> and hard-coded vector indices
> 
> Thank you again for your comments/criticisms/suggestions,
> 
> Chris
> 
> ===
> 
>  class EmployeePacket
>   {
>     std::vector<Item*> Items;
> 
>     EmployeePacket()
>     {
>       Items.push_back(new String("name", "John Doe"));
>       Items.push_back(new Double("salary", "USD", 1, 1));
>     }
> 
>     void Decode(istream& Stream)
>     {
>       for (auto pItem : Items) pItem->Decode(Stream)
>     }
> 
>     double GetSalary()
>     {
>       return dynamic_cast<Double*>(Items[1])->value;
>     }
>   }
> 
> class Item
> {
>   virtual void Decode(istream& Stream) = 0;
> }
> 
> class Double : public Item
> {
>   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 : public Item
>   {
>     string name;
>     string value;
>     void Decode(istream& Stream);
>   }
Hi Chris,
How do you decide whether the Stream argument to Decode contains a
String or a Double?  Is that already known?  IOW, if you open the
istream, do you *know* what types it contains already and only need
to fill in the values?
-regards,
Larry