Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: No support for serializing a single char?  (Read 3150 times)

0 Members and 1 Guest are viewing this topic.

P@u1

  • Jr. Member
  • **
  • Posts: 83
    • View Profile
No support for serializing a single char?
« on: August 04, 2011, 12:14:29 pm »
Hi,

I recognized that sf::Packet offers operator<< and operator>> for
sf::Int8 and sf::Uint8
which are at my platform typedefs for
signed char   and
unsigned char

But if I use
char
Then it won't compile, as it is a different type.

I know that there is support for 0-terminated char-arrays, but that's not what I need.
Laurent could you please add support for char?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
No support for serializing a single char?
« Reply #1 on: August 04, 2011, 12:17:13 pm »
sf::Packet only supports fixed size types (strings are provided only for convenience), you should serialize your char as a Uint8 to make sure that it has the same size on both ends of the socket.
Laurent Gomila - SFML developer

P@u1

  • Jr. Member
  • **
  • Posts: 83
    • View Profile
No support for serializing a single char?
« Reply #2 on: August 04, 2011, 12:27:36 pm »
Isn't char always 1 byte?

What I'm trying to achieve is to take a part of a packet and transfer it to another packet.
And sf::Packet stores it's data in an std::vector<char>.
So it wanted to extract it char-wise, which is not possible.

I more or less solved it by reading to unsigned chars and casting to char, but I'm not 100% sure, if this is portable without problems.

Why there is no operator<< or operator>> for char?

Edit:
That's the code I used:
Code: [Select]

int size;
packet >> size;
if(size == 0)
{
return;
}
char * data = new char[size];
for(int i = 0; i < size; ++i)
{
unsigned char c;
packet >> c;
data[i] = static_cast<char>(c);
}
data_.Append(data, size);
delete[] data;


I can't do it just with a single append, because I can't get the current readpos (as it's private)
Note that the "string" here is not zero terminated and may contain zeros in the middle

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
No support for serializing a single char?
« Reply #3 on: August 04, 2011, 12:33:21 pm »
Quote
Isn't char always 1 byte?

Yes, but a byte can be more than 8 bits.

Quote
Why there is no operator<< or operator>> for char?

Because its size is not constant across platforms. Same for all other native types (int, short, ...).

Quote
What I'm trying to achieve is to take a part of a packet and transfer it to another packet.

It seems like a common problem people have. I should make the read position public, or add a custom type for extracting / inserting raw data directly.
Laurent Gomila - SFML developer

P@u1

  • Jr. Member
  • **
  • Posts: 83
    • View Profile
No support for serializing a single char?
« Reply #4 on: August 04, 2011, 12:34:16 pm »
Quote from: "Laurent"
It seems like a common problem people have. I should make the read position public, or add a custom type for extracting / inserting raw data directly.


That's a very good idea!
Thanks

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
No support for serializing a single char?
« Reply #5 on: August 04, 2011, 01:46:03 pm »
Quote from: "Laurent"
It seems like a common problem people have. I should make the read position public, or add a custom type for extracting / inserting raw data directly.
Don't GetData() and GetDataSize() already cover most cases? If the user is certain about the internal representation, he can extract the parts himself... Or does the problem only arise at variable-size data, such as strings?

You should probably be careful with the addition of specific operations. When you make the read position public, chances are that soon other users request more features, and you will end up with a bloated stream class ;)

But if you find an elegant approach (like custom type) providing the necessary flexibility, that would be nice. Otherwise, << and >> alone cover many cases and there is no problem of mixing formatted with unformatted input/output.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
No support for serializing a single char?
« Reply #6 on: August 04, 2011, 01:51:03 pm »
Quote
Don't GetData() and GetDataSize() already cover most cases?

No, because:
Quote from: "P@u1"
I can't do it just with a single append, because I can't get the current readpos (as it's private)


Quote
You should probably be careful with the addition of specific operations. When you make the read position public, chances are that soon other users request more features, and you will end up with a bloated stream class

In fact I think that a Read(char*, size_t) function would be enough. And renaming Append with Write of course.
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
No support for serializing a single char?
« Reply #7 on: August 04, 2011, 02:19:31 pm »
Quote from: "Laurent"
In fact I think that a Read(char*, size_t) function would be enough. And renaming Append with Write of course.
Sounds good. I also think Write() should take const char* instead of const void* for symmetry reasons. User code will need a reinterpret_cast, but that makes clear that an object is read byte for byte.

By the way, is it safe to serialize float and double with sf::Packet? As far as I know, the C++ standard doesn't dictate their sizes either...
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
No support for serializing a single char?
« Reply #8 on: August 04, 2011, 02:35:19 pm »
Quote
By the way, is it safe to serialize float and double with sf::Packet? As far as I know, the C++ standard doesn't dictate their sizes either...

Ah ah :D
You're right, and this topic has already been discussed here.
See http://www.sfml-dev.org/forum/viewtopic.php?t=5059
Laurent Gomila - SFML developer

 

anything