-=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- (c) WidthPadding Industries 1987 0|699|0 -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=-
SoCoder -> Snippet Home -> Misc


 
Afr0
Created : 19 September 2009
Edited : 20 September 2009
System : Nintendo DS
Language : Monkey

BinaryStream

Binary (memory) stream in C++





Using this class should be fairly self-explanatory. Very handy for reading packets when working with network code for instance!

 

Comments


Saturday, 19 September 2009, 16:58
JL235
I believe you should use memcpy for WriteBytes, as it'll be far more efficient then a for loop.
Sunday, 20 September 2009, 01:53
Afr0

These appear to be straight C-style functions wrapped in a class (why?)

The C++ way would be to overload the insertion "<<" and extraction ">>" operator for each data type. This is already done for you in the standard stringstream class (I see you've included <sstream> but aren't actually using it). All you need to do is instantiate with the "binary" flag.


I tried using stringstream to read binary data from a char array, but using it was really confusing to me. So I came up with this instead, which is closer to the C# interface.
You're right about memory allocation though! The constructor should allocate it and the destructor should release it. I'll get around to changing it right away!
Sunday, 20 September 2009, 04:14
Phoenix
1. Why do you want the BinaryStream's constructor parameter to be passed as an array of char pointers, as opposed to just a char pointer?
2. You can't place a template's definition in a separate source file, because it will lead to linker errors. Try calling WritePODType to see why. There is also nothing to guarantee that the type passed is in fact a POD type.
3. The standard library already has plenty of functionality for dealing with this. Don't skip it because it's confusing.
4. To avoid the memory leak Agent Smith mentioned without having someone tell you about it, use smart pointers. That way, dynamically allocated memory is automatically cleaned up. Almost like what you're used to in C#.
Sunday, 20 September 2009, 05:32
Afr0
Thanks for the feedback Phoenix! I did get a linker error, but I had no idea why!
I haven't learned about smart pointers yet, though I've heard about them.

Anyways, I updated the original code.
I kept the original constructor, but added an additional one.
Reason being that I can pass already existing char arrays containing received packets right into a BinaryStream object, like so:


Monday, 21 September 2009, 07:16
Afr0

Why don't you just use a struct for your packet data?
Then you could access the fields directly.

This seems like a very inefficient way to go about it.


What? Do you mean when I'm receiving it? No I can't, the recvfrom() function only takes a char* array as a parameter for the buffer to receive into.
Monday, 21 September 2009, 07:32
Afr0
o_O

Wouldn't that require more than a cast?
The memory wouldn't be automagically organized in the right way, would it?