From 94ab420dd37cb538669ce2ac1d52e69df25b21ae Mon Sep 17 00:00:00 2001 From: Laurent Gomila Date: Wed, 25 May 2011 20:45:16 +0200 Subject: [PATCH] Implemented the sf::Packet to bool conversion using the safe-bool idiom --- include/SFML/Network/Packet.hpp | 18 +++++++++++++++++- src/SFML/Network/Packet.cpp | 4 ++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/include/SFML/Network/Packet.hpp b/include/SFML/Network/Packet.hpp index a4a2cb732..61baa2443 100644 --- a/include/SFML/Network/Packet.hpp +++ b/include/SFML/Network/Packet.hpp @@ -46,6 +46,9 @@ class UdpSocket; //////////////////////////////////////////////////////////// class SFML_API Packet { + // A bool-like type that cannot be converted to integer or pointer types + typedef bool (Packet::*BoolType)(std::size_t); + public : //////////////////////////////////////////////////////////// @@ -125,6 +128,8 @@ public : //////////////////////////////////////////////////////////// bool EndOfPacket() const; +public: + //////////////////////////////////////////////////////////// /// \brief Test the validity of the packet, for reading /// @@ -154,12 +159,16 @@ public : /// } /// \endcode /// + /// Don't focus on the return type, it's equivalent to bool but + /// it disallows unwanted implicit conversions to integer or + /// pointer types. + /// /// \return True if last data extraction from packet was successful /// /// \see EndOfPacket /// //////////////////////////////////////////////////////////// - operator void*() const; + operator BoolType() const; //////////////////////////////////////////////////////////// /// Overloads of operator >> to read data from the packet @@ -204,6 +213,13 @@ private : friend class TcpSocket; friend class UdpSocket; + //////////////////////////////////////////////////////////// + /// Disallow comparisons between packets + /// + //////////////////////////////////////////////////////////// + bool operator ==(const Packet& right) const; + bool operator !=(const Packet& right) const; + //////////////////////////////////////////////////////////// /// \brief Check if the packet can extract a given number of bytes /// diff --git a/src/SFML/Network/Packet.cpp b/src/SFML/Network/Packet.cpp index 740ca39f5..0a7473605 100644 --- a/src/SFML/Network/Packet.cpp +++ b/src/SFML/Network/Packet.cpp @@ -92,9 +92,9 @@ bool Packet::EndOfPacket() const //////////////////////////////////////////////////////////// -Packet::operator void*() const +Packet::operator BoolType() const { - return myIsValid ? const_cast(this) : NULL; + return myIsValid ? &Packet::CheckSize : NULL; }