mirror of
https://github.com/SFML/SFML.git
synced 2024-12-01 15:51:04 +08:00
Implemented the sf::Packet to bool conversion using the safe-bool idiom
This commit is contained in:
parent
43d4d0bb28
commit
94ab420dd3
@ -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
|
||||
///
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user