PLCnext API Documentation  22.3.0.20
IpAddress.hpp
1 //
3 // Copyright PHOENIX CONTACT Electronics GmbH
4 //
6 #pragma once
7 #include "Arp/System/Core/Arp.h"
8 
9 namespace Arp { namespace System { namespace Commons { namespace Net
10 {
11 
13 class IpAddress
14 {
15 public: // typedefs
16 
18  using IpV4Value = uint32;
19 
20 public: // construction/destruction
22  IpAddress(void) = default;
23 
26  IpAddress(uint32 ip4address);
27 
29  IpAddress(const IpAddress& arg) = default;
30 
32  IpAddress& operator=(const IpAddress& arg) = default;
33 
35  ~IpAddress(void) = default;
36 
37 public: // operators
38 
41  void operator=(Arp::uint32 ip4Address);
42 
43 public: // static properties
44  static IpAddress Empty;
45 
46 public: // static operations
47 
52  static IpAddress Parse(const String& input);
53 
58  static bool TryParse(const String& input, IpAddress& result);
59 
60 public: // setter/getter operations
63  bool IsEmpty(void)const;
64 
67  IpV4Value GetIpv4Value(void)const;
68 
69 public: // operations
70  String ToString(void)const;
71 
72 private: // static methods
73  static bool IsLocalhostString(const String& input);
74 
75 private: // fields
76  IpV4Value ipV4Value = 0;
77 
78 private: // static fields
79  static const char* localhostString1;
80  static const char* localhostString2;
81  static const char* localhostString3;
82  static const char* localhostAddress;
83 };
84 
86 // inline methods of class IpAddress
87 inline IpAddress::IpAddress(uint32 ip4Address) : ipV4Value(ip4Address)
88 {
89 }
90 
91 inline bool IpAddress::IsEmpty()const
92 {
93  return this->ipV4Value == 0;
94 }
95 
97 {
98  return this->ipV4Value;
99 }
100 
101 inline void IpAddress::operator=(Arp::uint32 ip4Address)
102 {
103  this->ipV4Value = ip4Address;
104 }
105 
107 inline std::ostream& operator<<(std::ostream& os, const IpAddress& ipAddress)
108 {
109  os << ipAddress.ToString();
110  return os;
111 }
112 
114 inline std::istream& operator>>(std::istream& is, IpAddress& ipAddress)
115 {
116  std::string s;
117  is >> s;
118  ipAddress = IpAddress::Parse(s);
119  return is;
120 }
121 
122 }}}} // end of namespace Arp::System::Commons::Net
bool IsEmpty(void) const
Determines if this instance is empty, e.g. the address value is zero.
Definition: IpAddress.hpp:91
std::uint32_t uint32
The Arp unsigned integer type of 4 byte size.
Definition: PrimitiveTypes.hpp:35
IpAddress & operator=(const IpAddress &arg)=default
Assignment operator.
uint32 IpV4Value
Value-type-definition for ip addresses using version 4 scheme.
Definition: IpAddress.hpp:18
IpV4Value GetIpv4Value(void) const
Returns the ip address in ip v4 address scheme.
Definition: IpAddress.hpp:96
std::ostream & operator<<(std::ostream &os, const IpAddress &ipAddress)
The ostream operator is used for logging and string formatting.
Definition: IpAddress.hpp:107
Root namespace for the PLCnext API
std::istream & operator>>(std::istream &is, IpAddress &ipAddress)
The istream operator is used for string parsing.
Definition: IpAddress.hpp:114
static IpAddress Parse(const String &input)
Parses an ip address from string bases representation, e.g. "127.0.0.1".
Unified representation for ip address schemes.
Definition: IpAddress.hpp:13
System components used by the System, Device, Plc or Io domains.
~IpAddress(void)=default
Destructs this instance and frees all resources.
static bool TryParse(const String &input, IpAddress &result)
Tries to parse an address from string based representation.
IpAddress(void)=default
Constructs an IpAddress instance.