views:

88

answers:

2

Language : C++

I am working on Bit Packing (Extracting the required bits from the given data and packing them in a char*) . My code currently supports : - Integers - Characters - Strings

Now if I have to store the required bits of a structure, how should I go about it ? I mean what should I expect as input parameters for a generalized code w.r.t structures ?

The question may be vague and I am not expecting direct answers, even hints and pointers are appreciated.

+1  A: 

Have a look at this for a very packed format or use an standard marshalling format such as json, xml, boost serialization,... and save yourself the grey hair.

piotr
Thank you for the informative link. The option of using the external libraries will be considered at the end when I feel there is no other way to do it.
anishakaul
You shouldn't (wait until there is no other way). Except for learning how to do it there are very few reasons to write a library on your own, especially when you are new to the domain. So considering external libraries should be the first thing to do, and writing yourself should be the last option, especially for such complex basic components (Just IMVHO)
Fabio Fracassi
Fabio Fracassi, Thanks, I shall consider your opinion and read about those external libraries.
anishakaul
I just found **bitmagic** library: http://bmagic.sourceforge.net/ Can this be used w.r.t above problem ? Has it got any serious limitations ?
anishakaul
But what are you tring to do, bitsets as flags? or Marshalling data into a buffer? it's not clear from your question. If you just want bitset, use that container from the STL.
piotr
Piotr, Thanks for following up ! I actually **do not know** which one of the above 3, is *better* for packing structures and whether there is some standard algorithm to be used for bit packing ? I saw Huffman coding algorithm. but That's not related to this, is it ? I'll be greatful if some one can give some more pointers on all this.
anishakaul
This "packing structures" is called marshalling or serialization in computing jargon. Please use one of the two, because your questions are ambigous. Huffman coding is a technique for data compression. Could you explain futher what is that you are trying to do? Saving structures to disk for example? that is marshalling, and you'd probably be better to use boost-serialization for example.
piotr
A: 

As piotr already suggested: try using existing libraries to marshall.

However, since your already doing it yourself:
If your supported primitives are representable as bytes, then you shouldn't be bit packing (there might be some confusion about the term), otherwise consider using std::bitset.
Because C++ doesn't support reflection, there is no help in C++ to byte pack structures in a generic, safe and portable way, so be prepared to write a function per structure to pack each primitive and each member structure.
Overloading does help to call these functions in a generic way, so packing of containers (vector ...) can be done generically. However, if you want this, then prefer free functions over member functions, to avoid having a difference between packing primitives and packing structures.

Example:

void Pack(const MyStruct& str, Packer& pack)
{
  Pack(str.int1, pack);
  Pack(str.string1, pack);
  Pack(str.otherStruct, pack);
}  
stefaanv
Thank you for the detailed response. I am not a native English speaker. Kindly explain what does "supported primitives" mean here. As you said : "so be prepared to write a function per structure to pack each primitive and each member structure." That's not feasible I IMO because this bit-packing function of mine is supposed to handle any type of structure not just one !
anishakaul
and I am just checking what "bitset" does. Thanks for the information.
anishakaul
@anishakaul: You mentioned that your implementation already support integers, characters and strings. These are the supported primitives. Unsupported primitives include doubles, boolean, ... Structures and containers (and strictly speaking strings too) are compounds made up of primitives.
stefaanv