Boost logo

Boost Users :

Subject: Re: [Boost-users] Can boost help me organize/parse lots of binary data?
From: Chris Stankevitz (chrisstankevitz_at_[hidden])
Date: 2013-02-20 20:21:55


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);
  }


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