Skip to content

Commit 912ff0f

Browse files
committed
network: modernize Mac[8,16,48,64]Address
1 parent 162ea85 commit 912ff0f

File tree

8 files changed

+99
-238
lines changed

8 files changed

+99
-238
lines changed

src/network/utils/mac16-address.cc

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@ void
5151
Mac16Address::CopyFrom(const uint8_t buffer[2])
5252
{
5353
NS_LOG_FUNCTION(this << &buffer);
54-
memcpy(m_address, buffer, 2);
54+
std::copy(buffer, buffer + 2, m_address.begin());
5555
}
5656

5757
void
5858
Mac16Address::CopyTo(uint8_t buffer[2]) const
5959
{
6060
NS_LOG_FUNCTION(this << &buffer);
61-
memcpy(buffer, m_address, 2);
61+
std::copy(m_address.begin(), m_address.end(), buffer);
6262
}
6363

6464
bool
@@ -80,15 +80,15 @@ Mac16Address::ConvertFrom(const Address& address)
8080
NS_LOG_FUNCTION(address);
8181
NS_ASSERT(address.CheckCompatible(GetType(), 2));
8282
Mac16Address retval;
83-
address.CopyTo(retval.m_address);
83+
address.CopyTo(retval.m_address.data());
8484
return retval;
8585
}
8686

8787
Address
8888
Mac16Address::ConvertTo() const
8989
{
9090
NS_LOG_FUNCTION(this);
91-
return Address(GetType(), m_address, 2);
91+
return Address(GetType(), m_address.data(), 2);
9292
}
9393

9494
uint16_t
@@ -180,19 +180,16 @@ Mac16Address::IsMulticast() const
180180
std::ostream&
181181
operator<<(std::ostream& os, const Mac16Address& address)
182182
{
183-
uint8_t ad[2];
184-
address.CopyTo(ad);
185-
183+
std::ios_base::fmtflags ff = os.flags();
186184
os.setf(std::ios::hex, std::ios::basefield);
187-
os.fill('0');
188-
for (uint8_t i = 0; i < 1; i++)
189-
{
190-
os << std::setw(2) << (uint32_t)ad[i] << ":";
191-
}
192-
// Final byte not suffixed by ":"
193-
os << std::setw(2) << (uint32_t)ad[1];
194-
os.setf(std::ios::dec, std::ios::basefield);
195-
os.fill(' ');
185+
auto fill = os.fill('0');
186+
187+
os << std::setw(2) << static_cast<uint32_t>(address.m_address[0]) << ":";
188+
os << std::setw(2) << static_cast<uint32_t>(address.m_address[1]);
189+
190+
os.flags(ff); // Restore stream flags
191+
os.fill(fill);
192+
196193
return os;
197194
}
198195

src/network/utils/mac16-address.h

Lines changed: 13 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#include "ns3/attribute-helper.h"
1515
#include "ns3/attribute.h"
1616

17+
#include <array>
18+
#include <compare>
1719
#include <ostream>
1820
#include <stdint.h>
1921

@@ -165,39 +167,21 @@ class Mac16Address
165167
*/
166168
bool IsMulticast() const;
167169

168-
private:
169-
/**
170-
* @brief Return the Type of address.
171-
* @return type of address
172-
*/
173-
static uint8_t GetType();
174-
175-
/**
176-
* @brief Equal to operator.
177-
*
178-
* @param a the first operand
179-
* @param b the first operand
180-
* @returns true if the operands are equal
181-
*/
182-
friend bool operator==(const Mac16Address& a, const Mac16Address& b);
183-
184170
/**
185-
* @brief Not equal to operator.
171+
* Spaceship comparison operator. All the other comparison operators
172+
* are automatically generated from this one.
186173
*
187-
* @param a the first operand
188-
* @param b the first operand
189-
* @returns true if the operands are not equal
174+
* @param other address to compare to this one
175+
* @returns The result of the comparison.
190176
*/
191-
friend bool operator!=(const Mac16Address& a, const Mac16Address& b);
177+
constexpr std::strong_ordering operator<=>(const Mac16Address& other) const = default;
192178

179+
private:
193180
/**
194-
* @brief Less than operator.
195-
*
196-
* @param a the first operand
197-
* @param b the first operand
198-
* @returns true if the operand a is less than operand b
181+
* @brief Return the Type of address.
182+
* @return type of address
199183
*/
200-
friend bool operator<(const Mac16Address& a, const Mac16Address& b);
184+
static uint8_t GetType();
201185

202186
/**
203187
* @brief Stream insertion operator.
@@ -217,30 +201,12 @@ class Mac16Address
217201
*/
218202
friend std::istream& operator>>(std::istream& is, Mac16Address& address);
219203

220-
static uint64_t m_allocationIndex; //!< Address allocation index
221-
uint8_t m_address[2]{0}; //!< Address value
204+
static uint64_t m_allocationIndex; //!< Address allocation index
205+
std::array<uint8_t, 2> m_address{}; //!< Address value
222206
};
223207

224208
ATTRIBUTE_HELPER_HEADER(Mac16Address);
225209

226-
inline bool
227-
operator==(const Mac16Address& a, const Mac16Address& b)
228-
{
229-
return memcmp(a.m_address, b.m_address, 2) == 0;
230-
}
231-
232-
inline bool
233-
operator!=(const Mac16Address& a, const Mac16Address& b)
234-
{
235-
return memcmp(a.m_address, b.m_address, 2) != 0;
236-
}
237-
238-
inline bool
239-
operator<(const Mac16Address& a, const Mac16Address& b)
240-
{
241-
return memcmp(a.m_address, b.m_address, 2) < 0;
242-
}
243-
244210
std::ostream& operator<<(std::ostream& os, const Mac16Address& address);
245211
std::istream& operator>>(std::istream& is, Mac16Address& address);
246212

src/network/utils/mac48-address.cc

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@ void
5151
Mac48Address::CopyFrom(const uint8_t buffer[6])
5252
{
5353
NS_LOG_FUNCTION(this << &buffer);
54-
std::memcpy(m_address, buffer, 6);
54+
std::copy(buffer, buffer + 6, m_address.begin());
5555
}
5656

5757
void
5858
Mac48Address::CopyTo(uint8_t buffer[6]) const
5959
{
6060
NS_LOG_FUNCTION(this << &buffer);
61-
std::memcpy(buffer, m_address, 6);
61+
std::copy(m_address.begin(), m_address.end(), buffer);
6262
}
6363

6464
bool
@@ -78,7 +78,7 @@ Address
7878
Mac48Address::ConvertTo() const
7979
{
8080
NS_LOG_FUNCTION(this);
81-
return Address(GetType(), m_address, 6);
81+
return Address(GetType(), m_address.data(), 6);
8282
}
8383

8484
Mac48Address
@@ -87,7 +87,7 @@ Mac48Address::ConvertFrom(const Address& address)
8787
NS_LOG_FUNCTION(&address);
8888
NS_ASSERT(address.CheckCompatible(GetType(), 6));
8989
Mac48Address retval;
90-
address.CopyTo(retval.m_address);
90+
address.CopyTo(retval.m_address.data());
9191
return retval;
9292
}
9393

@@ -229,19 +229,20 @@ Mac48Address::GetMulticast(Ipv6Address addr)
229229
std::ostream&
230230
operator<<(std::ostream& os, const Mac48Address& address)
231231
{
232-
uint8_t ad[6];
233-
address.CopyTo(ad);
234-
232+
std::ios_base::fmtflags ff = os.flags();
235233
os.setf(std::ios::hex, std::ios::basefield);
236-
os.fill('0');
237-
for (uint8_t i = 0; i < 5; i++)
238-
{
239-
os << std::setw(2) << (uint32_t)ad[i] << ":";
240-
}
241-
// Final byte not suffixed by ":"
242-
os << std::setw(2) << (uint32_t)ad[5];
243-
os.setf(std::ios::dec, std::ios::basefield);
244-
os.fill(' ');
234+
auto fill = os.fill('0');
235+
236+
os << std::setw(2) << static_cast<uint32_t>(address.m_address[0]) << ":";
237+
os << std::setw(2) << static_cast<uint32_t>(address.m_address[1]) << ":";
238+
os << std::setw(2) << static_cast<uint32_t>(address.m_address[2]) << ":";
239+
os << std::setw(2) << static_cast<uint32_t>(address.m_address[3]) << ":";
240+
os << std::setw(2) << static_cast<uint32_t>(address.m_address[4]) << ":";
241+
os << std::setw(2) << static_cast<uint32_t>(address.m_address[5]);
242+
243+
os.flags(ff); // Restore stream flags
244+
os.fill(fill);
245+
245246
return os;
246247
}
247248

src/network/utils/mac48-address.h

Lines changed: 13 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#include "ns3/attribute-helper.h"
1515
#include "ns3/attribute.h"
1616

17+
#include <array>
18+
#include <compare>
1719
#include <ostream>
1820
#include <stdint.h>
1921

@@ -149,39 +151,21 @@ class Mac48Address
149151
*/
150152
typedef void (*TracedCallback)(Mac48Address value);
151153

152-
private:
153-
/**
154-
* @brief Return the Type of address.
155-
* @return type of address
156-
*/
157-
static uint8_t GetType();
158-
159-
/**
160-
* @brief Equal to operator.
161-
*
162-
* @param a the first operand
163-
* @param b the first operand
164-
* @returns true if the operands are equal
165-
*/
166-
friend bool operator==(const Mac48Address& a, const Mac48Address& b);
167-
168154
/**
169-
* @brief Not equal to operator.
155+
* Spaceship comparison operator. All the other comparison operators
156+
* are automatically generated from this one.
170157
*
171-
* @param a the first operand
172-
* @param b the first operand
173-
* @returns true if the operands are not equal
158+
* @param other address to compare to this one
159+
* @returns The result of the comparison.
174160
*/
175-
friend bool operator!=(const Mac48Address& a, const Mac48Address& b);
161+
constexpr std::strong_ordering operator<=>(const Mac48Address& other) const = default;
176162

163+
private:
177164
/**
178-
* @brief Less than operator.
179-
*
180-
* @param a the first operand
181-
* @param b the first operand
182-
* @returns true if the operand a is less than operand b
165+
* @brief Return the Type of address.
166+
* @return type of address
183167
*/
184-
friend bool operator<(const Mac48Address& a, const Mac48Address& b);
168+
static uint8_t GetType();
185169

186170
/**
187171
* @brief Stream insertion operator.
@@ -201,30 +185,12 @@ class Mac48Address
201185
*/
202186
friend std::istream& operator>>(std::istream& is, Mac48Address& address);
203187

204-
static uint64_t m_allocationIndex; //!< Address allocation index
205-
uint8_t m_address[6]{0}; //!< Address value
188+
static uint64_t m_allocationIndex; //!< Address allocation index
189+
std::array<uint8_t, 6> m_address{}; //!< Address value
206190
};
207191

208192
ATTRIBUTE_HELPER_HEADER(Mac48Address);
209193

210-
inline bool
211-
operator==(const Mac48Address& a, const Mac48Address& b)
212-
{
213-
return memcmp(a.m_address, b.m_address, 6) == 0;
214-
}
215-
216-
inline bool
217-
operator!=(const Mac48Address& a, const Mac48Address& b)
218-
{
219-
return memcmp(a.m_address, b.m_address, 6) != 0;
220-
}
221-
222-
inline bool
223-
operator<(const Mac48Address& a, const Mac48Address& b)
224-
{
225-
return memcmp(a.m_address, b.m_address, 6) < 0;
226-
}
227-
228194
std::ostream& operator<<(std::ostream& os, const Mac48Address& address);
229195
std::istream& operator>>(std::istream& is, Mac48Address& address);
230196

src/network/utils/mac64-address.cc

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,14 @@ void
6666
Mac64Address::CopyFrom(const uint8_t buffer[8])
6767
{
6868
NS_LOG_FUNCTION(this << &buffer);
69-
std::memcpy(m_address, buffer, 8);
69+
std::copy(buffer, buffer + 8, m_address.begin());
7070
}
7171

7272
void
7373
Mac64Address::CopyTo(uint8_t buffer[8]) const
7474
{
7575
NS_LOG_FUNCTION(this << &buffer);
76-
std::memcpy(buffer, m_address, 8);
76+
std::copy(m_address.begin(), m_address.end(), buffer);
7777
}
7878

7979
bool
@@ -95,15 +95,15 @@ Mac64Address::ConvertFrom(const Address& address)
9595
NS_LOG_FUNCTION(address);
9696
NS_ASSERT(address.CheckCompatible(GetType(), 8));
9797
Mac64Address retval;
98-
address.CopyTo(retval.m_address);
98+
address.CopyTo(retval.m_address.data());
9999
return retval;
100100
}
101101

102102
Address
103103
Mac64Address::ConvertTo() const
104104
{
105105
NS_LOG_FUNCTION(this);
106-
return Address(GetType(), m_address, 8);
106+
return Address(GetType(), m_address.data(), 8);
107107
}
108108

109109
uint64_t
@@ -164,19 +164,22 @@ Mac64Address::GetType()
164164
std::ostream&
165165
operator<<(std::ostream& os, const Mac64Address& address)
166166
{
167-
uint8_t ad[8];
168-
address.CopyTo(ad);
169-
167+
std::ios_base::fmtflags ff = os.flags();
170168
os.setf(std::ios::hex, std::ios::basefield);
171-
os.fill('0');
172-
for (uint8_t i = 0; i < 7; i++)
173-
{
174-
os << std::setw(2) << (uint32_t)ad[i] << ":";
175-
}
176-
// Final byte not suffixed by ":"
177-
os << std::setw(2) << (uint32_t)ad[7];
178-
os.setf(std::ios::dec, std::ios::basefield);
179-
os.fill(' ');
169+
auto fill = os.fill('0');
170+
171+
os << std::setw(2) << static_cast<uint32_t>(address.m_address[0]) << ":";
172+
os << std::setw(2) << static_cast<uint32_t>(address.m_address[1]) << ":";
173+
os << std::setw(2) << static_cast<uint32_t>(address.m_address[2]) << ":";
174+
os << std::setw(2) << static_cast<uint32_t>(address.m_address[3]) << ":";
175+
os << std::setw(2) << static_cast<uint32_t>(address.m_address[4]) << ":";
176+
os << std::setw(2) << static_cast<uint32_t>(address.m_address[5]) << ":";
177+
os << std::setw(2) << static_cast<uint32_t>(address.m_address[6]) << ":";
178+
os << std::setw(2) << static_cast<uint32_t>(address.m_address[7]);
179+
180+
os.flags(ff); // Restore stream flags
181+
os.fill(fill);
182+
180183
return os;
181184
}
182185

0 commit comments

Comments
 (0)