Skip to content

Commit a01e705

Browse files
authored
Add IP and Mac user defined literals. (#1999)
* Added mac and ip literals for quick creation. * Fixed the static MacValues to be const.
1 parent 6387499 commit a01e705

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

Common++/header/IpAddress.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,19 @@ namespace pcpp
883883
std::unique_ptr<IPv6Network> m_IPv6Network;
884884
};
885885

886+
namespace literals
887+
{
888+
inline IPv4Address operator""_ipv4(const char* addrString, std::size_t size)
889+
{
890+
return IPv4Address(std::string(addrString, size));
891+
}
892+
893+
inline IPv6Address operator""_ipv6(const char* addrString, std::size_t size)
894+
{
895+
return IPv6Address(std::string(addrString, size));
896+
}
897+
} // namespace literals
898+
886899
inline std::ostream& operator<<(std::ostream& oss, const pcpp::IPv4Address& ipv4Address)
887900
{
888901
oss << ipv4Address.toString();

Common++/header/MacAddress.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,14 +177,22 @@ namespace pcpp
177177
bool copyToNewBuffer(uint8_t** buffer, size_t& size) const;
178178

179179
/// A static value representing a zero value of MAC address, meaning address of value "00:00:00:00:00:00"
180-
static MacAddress Zero;
180+
static const MacAddress Zero;
181181
/// A static value representing a broadcast MAC address, meaning address of value "ff:ff:ff:ff:ff:ff"
182-
static MacAddress Broadcast;
182+
static const MacAddress Broadcast;
183183

184184
private:
185185
std::array<uint8_t, 6> m_Address{};
186186
};
187187

188+
namespace literals
189+
{
190+
inline MacAddress operator""_mac(const char* addrString, size_t size)
191+
{
192+
return MacAddress(std::string(addrString, size));
193+
}
194+
} // namespace literals
195+
188196
inline std::ostream& operator<<(std::ostream& oss, const pcpp::MacAddress& macAddress)
189197
{
190198
oss << macAddress.toString();

Common++/src/MacAddress.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
namespace pcpp
44
{
55

6-
MacAddress MacAddress::Zero(0, 0, 0, 0, 0, 0);
6+
const MacAddress MacAddress::Zero(0, 0, 0, 0, 0, 0);
77

8-
MacAddress MacAddress::Broadcast(0xff, 0xff, 0xff, 0xff, 0xff, 0xff);
8+
const MacAddress MacAddress::Broadcast(0xff, 0xff, 0xff, 0xff, 0xff, 0xff);
99

1010
std::string MacAddress::toString() const
1111
{

0 commit comments

Comments
 (0)