468,513 Members | 1,403 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,513 developers. It's quick & easy.

STL without memcpy

2
I wanted to write a quick test program to send a message with variable length fields in the message. Typically I would use some sort of dynamically allocated c-like buffer and just memcpy the elements of different sized into their proper positions. For example:

Expand|Select|Wrap|Line Numbers
  1. char Buffer[8192];
  2. unsigned short count = 512;
  3. char temp_char = 3, temp_char2 = 255;
  4.  
  5. *(unsigned short*)(&Buffer[0]) = 1;  
  6. *(unsigned short*)(&Buffer[2]) = count;
  7. Buffer[4] = temp_char;
  8. Buffer[5] = temp_char2;
  9.  
I would like to use the STL to do this a little more elegantly and c++-ish, but I don't know how to go about doing it the best way. I could do something like this:
Expand|Select|Wrap|Line Numbers
  1. deque<char> Buffer(8192);
  2. deque<char>::iterator buff_iter = Buffer.begin();
  3. unsigned short count = 512;
  4. char temp_char = 3, temp_char2 = 255;
  5.  
  6. *buff_iter = 0; // High Byte
  7. buff_iter++;
  8. *buff_iter = 1; // Low Byte
  9. buff_iter++;
  10.  
  11. *buff_iter = ((count >> 8) & 0xFF); // High Byte
  12. buff_iter++;
  13. *buff_iter = (count & 0xFF); // Low Byte
  14. buff_iter++;
  15.  
  16. *buff_iter = temp_char;
  17. buff_iter++;
  18. *buff_iter = temp_char2;
  19.  
However, that seems worse to me. Could someone give me some ideas on a good way to handle packing data words of different sizes into an STL container?

Thanks!

Btw... I didn't actually attempt to compile this so it may not be 100% syntactically correct.
Sep 5 '06 #1
2 3741
Banfa
9,056 Expert Mod 8TB
Rather than just instanciating a variable of type deque(char) you could create a subclass of that type and add you own methods to insert data

Expand|Select|Wrap|Line Numbers
  1. class message : public deque<char>
  2. {
  3. public:
  4.     message();
  5.     message(int defaultBufferSize);
  6.     ~message();
  7.  
  8.     AddChar(unsigned char data);
  9.     AddShort(unsigned short data);
  10.     AddString(string data);
  11.  
  12.     // etc
  13. }
  14.  
Then your main cod becomes

Expand|Select|Wrap|Line Numbers
  1. message Buffer(8192);
  2. unsigned short count = 512;
  3. char temp_char = 3, temp_char2 = 255;
  4.  
  5. Buffer.AddShort(1);
  6. Buffer.AddShort(count);
  7. Buffer.AddChar(temp_char);
  8. Buffer.AddChar(temp_char2);
  9.  
If you wished rather than add methods you could override the += operator giving

Expand|Select|Wrap|Line Numbers
  1. message Buffer(8192);
  2. unsigned short count = 512;
  3. char temp_char = 3, temp_char2 = 255;
  4.  
  5. Buffer += (unsigned short)1;
  6. Buffer += count;
  7. Buffer += temp_char;
  8. Buffer += temp_char2;
  9.  
I have left you to actually implement the functions/operators.
Sep 5 '06 #2
Esam
2
Hey that's a good idea, thanks!
Sep 6 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

13 posts views Thread by franky.backeljauw | last post: by
16 posts views Thread by Delali Dzirasa | last post: by
6 posts views Thread by Samee Zahur | last post: by
5 posts views Thread by manya | last post: by
35 posts views Thread by Christopher Benson-Manica | last post: by
16 posts views Thread by Amarendra GODBOLE | last post: by
33 posts views Thread by Case | last post: by
6 posts views Thread by novice | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.