Loading...
Searching...
No Matches
IpAddress.hpp
Go to the documentation of this file.
1
2//
3// SFML - Simple and Fast Multimedia Library
4// Copyright (C) 2007-2024 Laurent Gomila (laurent@sfml-dev.org)
5//
6// This software is provided 'as-is', without any express or implied warranty.
7// In no event will the authors be held liable for any damages arising from the use of this software.
8//
9// Permission is granted to anyone to use this software for any purpose,
10// including commercial applications, and to alter it and redistribute it freely,
11// subject to the following restrictions:
12//
13// 1. The origin of this software must not be misrepresented;
14// you must not claim that you wrote the original software.
15// If you use this software in a product, an acknowledgment
16// in the product documentation would be appreciated but is not required.
17//
18// 2. Altered source versions must be plainly marked as such,
19// and must not be misrepresented as being the original software.
20//
21// 3. This notice may not be removed or altered from any source distribution.
22//
24
25#pragma once
26
28// Headers
31
32#include <SFML/System/Time.hpp>
33
34#include <iosfwd>
35#include <optional>
36#include <string>
37#include <string_view>
38
39#include <cstdint>
40
41
42namespace sf
43{
49{
50public:
62 [[nodiscard]] static std::optional<IpAddress> resolve(std::string_view address);
63
77 IpAddress(std::uint8_t byte0, std::uint8_t byte1, std::uint8_t byte2, std::uint8_t byte3);
78
92 explicit IpAddress(std::uint32_t address);
93
106 [[nodiscard]] std::string toString() const;
107
122 [[nodiscard]] std::uint32_t toInteger() const;
123
138 [[nodiscard]] static std::optional<IpAddress> getLocalAddress();
139
162 [[nodiscard]] static std::optional<IpAddress> getPublicAddress(Time timeout = Time::Zero);
163
165 // Static member data
167 // NOLINTBEGIN(readability-identifier-naming)
168 static const IpAddress Any;
169 static const IpAddress LocalHost;
170 static const IpAddress Broadcast;
171 // NOLINTEND(readability-identifier-naming)
172
173private:
175
177 // Member data
179 std::uint32_t m_address;
180};
181
191[[nodiscard]] SFML_NETWORK_API bool operator==(IpAddress left, IpAddress right);
192
202[[nodiscard]] SFML_NETWORK_API bool operator!=(IpAddress left, IpAddress right);
203
213[[nodiscard]] SFML_NETWORK_API bool operator<(IpAddress left, IpAddress right);
214
224[[nodiscard]] SFML_NETWORK_API bool operator>(IpAddress left, IpAddress right);
225
235[[nodiscard]] SFML_NETWORK_API bool operator<=(IpAddress left, IpAddress right);
236
246[[nodiscard]] SFML_NETWORK_API bool operator>=(IpAddress left, IpAddress right);
247
257SFML_NETWORK_API std::istream& operator>>(std::istream& stream, std::optional<IpAddress>& address);
258
268SFML_NETWORK_API std::ostream& operator<<(std::ostream& stream, IpAddress address);
269
270} // namespace sf
271
272
#define SFML_NETWORK_API
Encapsulate an IPv4 network address.
Definition IpAddress.hpp:49
static std::optional< IpAddress > getLocalAddress()
Get the computer's local address.
static std::optional< IpAddress > getPublicAddress(Time timeout=Time::Zero)
Get the computer's public address.
static const IpAddress Any
Value representing any address (0.0.0.0)
static std::optional< IpAddress > resolve(std::string_view address)
Construct the address from a null-terminated string view.
std::uint32_t toInteger() const
Get an integer representation of the address.
IpAddress(std::uint32_t address)
Construct the address from a 32-bits integer.
static const IpAddress LocalHost
The "localhost" address (for connecting a computer to itself locally)
std::string toString() const
Get a string representation of the address.
static const IpAddress Broadcast
The "broadcast" address (for sending UDP messages to everyone on a local network)
friend bool operator<(IpAddress left, IpAddress right)
Overload of operator< to compare two IP addresses.
IpAddress(std::uint8_t byte0, std::uint8_t byte1, std::uint8_t byte2, std::uint8_t byte3)
Construct the address from 4 bytes.
Represents a time value.
Definition Time.hpp:42
bool operator!=(IpAddress left, IpAddress right)
Overload of operator!= to compare two IP addresses.
bool operator<(IpAddress left, IpAddress right)
Overload of operator< to compare two IP addresses.
bool operator==(IpAddress left, IpAddress right)
Overload of operator== to compare two IP addresses.
bool operator>(IpAddress left, IpAddress right)
Overload of operator> to compare two IP addresses.
bool operator>=(IpAddress left, IpAddress right)
Overload of operator>= to compare two IP addresses.
std::istream & operator>>(std::istream &stream, std::optional< IpAddress > &address)
Overload of operator>> to extract an IP address from an input stream.
bool operator<=(IpAddress left, IpAddress right)
Overload of operator<= to compare two IP addresses.
std::ostream & operator<<(std::ostream &stream, IpAddress address)
Overload of operator<< to print an IP address to an output stream.