|
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 14:42:35
On 02/21/13 12:53, Chris Stankevitz wrote:
> On Thu, Feb 21, 2013 at 9:04 AM, Larry Evans <cppljevans_at_[hidden]> wrote:
>> 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?
>
>
> Thank you all for your interest and help. I LOVE boost and have been
> using it for almost a year now. I cannot live without signals2,
> thread, and shared_ptr. They have changed the way I. I am eager to
> learn about the other components.
>
> I created a simple program that basically does what I want to do. Of
> course in my real application the objects are more complicated and
> this is just one small piece of everything I need to do.
>
> What I hope to highlight with this sample cpp is:
>
> 1. I am decoding data. The format is about as simple and "fixed" as
> you can imagine.
>
> 2. The data is not "POD-encoded" values. Sometimes I have to perform
> conversions on the values. For example "temperature" in the example.
>
> 3. I would like to keep track of some static known-at-compile-time
> meta-data for each of the values such as the units they are in. This
> I use for display purposes.
>
> 4. Some values have meta data that other values do not, such as
> floating point values which have a "number of significant digits after
> the decimal point" which I illustrate with the "temperature" example.
>
> 5. I will have many values. ~60. In my example I attempt to
> highlight what a mess the class will turn into if I make no attempt to
> use boost::variant, boost::any, polymorphism, etc.
>
> 6. An external class "TCTimer" in my example will want access to
> Get/Set values in the class.
>
> 7. I appear to be confusing/merging several concepts: 1) decoding 2)
> tracking meta data for members 3)
> replace-lots-of-members-and-with-a-vector-of-something-like-boost::variant
This 7th feature is not represented by your example code. The example
code suggests the type being read is already known. OTOH, your
previous example code had a vector of pointers to an abstract class:
class EmployeePacket
{
std::vector<Item*> Items;
suggesting that *maybe* the concrete classes are only known at runtime.
OTOH, that same code had a CTOR:
EmployeePacket()
{
Items.push_back(new String("name", "John Doe"));
Items.push_back(new Double("salary", "USD", 1, 1));
}
suggesting the actual values were really known and there was really
no need for the abstract Item class or, AFAICT, any need for something
like boost::variant. Could you please clarify?
Also, the code:
std::istringstream Stream(Bytes.substr(0, ByteCount));
unsigned short Temperature;
Stream.read(reinterpret_cast<char*>(&Temperature), 2);
Stream.read(reinterpret_cast<char*>(&mAge), 4);
worries me because it assumes 2 bytes are used to store a short
and 4 bytes are used to store an int. I'm not sure how portable this is.
In addition, the comments:
// First two bytes are a temperature encoded as an uint16.
// 0=>25 DegF
// 2^16=>125 DegF
//
// Next four bytes Age is encoded as an int32
//
// Byte Value
// 0 Temperature LSB
// 1 Temperature MSB
// 2 Age LSB
// 3 Age ...
// 4 Age ...
// 5 Age MSB
//
// Total bytes: 6
suggest that there's a specific encoding of the values which implies
that there needs to be a specific decoding of the bit's in
the bytes in the Byte argument. IOW, I don't think the reinterpret_cast
and using of istream.read will give you what you want according to
the comments.
HTH.
-regards,
Larry
>
> Again, thank you,
>
> Chris
>
>
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
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