Implemented the sf::Packet to bool conversion using the safe-bool idiom

This commit is contained in:
Laurent Gomila 2011-05-25 20:45:16 +02:00
parent 43d4d0bb28
commit 94ab420dd3
2 changed files with 19 additions and 3 deletions

View File

@ -46,6 +46,9 @@ class UdpSocket;
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
class SFML_API Packet 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 : public :
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
@ -125,6 +128,8 @@ public :
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
bool EndOfPacket() const; bool EndOfPacket() const;
public:
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \brief Test the validity of the packet, for reading /// \brief Test the validity of the packet, for reading
/// ///
@ -154,12 +159,16 @@ public :
/// } /// }
/// \endcode /// \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 /// \return True if last data extraction from packet was successful
/// ///
/// \see EndOfPacket /// \see EndOfPacket
/// ///
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
operator void*() const; operator BoolType() const;
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// Overloads of operator >> to read data from the packet /// Overloads of operator >> to read data from the packet
@ -204,6 +213,13 @@ private :
friend class TcpSocket; friend class TcpSocket;
friend class UdpSocket; 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 /// \brief Check if the packet can extract a given number of bytes
/// ///

View File

@ -92,9 +92,9 @@ bool Packet::EndOfPacket() const
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
Packet::operator void*() const Packet::operator BoolType() const
{ {
return myIsValid ? const_cast<Packet*>(this) : NULL; return myIsValid ? &Packet::CheckSize : NULL;
} }