Skip to content

Commit f634399

Browse files
committed
fix(mac): Add initializer list constructor
1 parent fb49b6d commit f634399

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

cores/esp32/MacAddress.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,24 @@ MacAddress::MacAddress(const String &macstr) {
3232
fromString(macstr.c_str());
3333
}
3434

35+
#ifdef __GXX_EXPERIMENTAL_CXX0X__
36+
MacAddress::MacAddress(std::initializer_list<uint8_t> list) {
37+
size_t size = list.size();
38+
memset(_mac.bytes, 0, sizeof(_mac.bytes));
39+
if (size == 6) {
40+
_type = MAC6;
41+
} else if (size == 8) {
42+
_type = MAC8;
43+
} else {
44+
// Default to MAC6 and keep the rest of the bytes as 0
45+
_type = MAC6;
46+
return;
47+
}
48+
49+
memcpy(_mac.bytes, list.begin(), size);
50+
}
51+
#endif
52+
3553
MacAddress::MacAddress(uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4, uint8_t b5, uint8_t b6) {
3654
_type = MAC6;
3755
memset(_mac.bytes, 0, sizeof(_mac.bytes));

cores/esp32/MacAddress.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
#include <stdint.h>
2424
#include <WString.h>
2525
#include <Printable.h>
26+
#ifdef __GXX_EXPERIMENTAL_CXX0X__
27+
#include <initializer_list>
28+
#endif
2629

2730
enum MACType {
2831
MAC6,
@@ -56,6 +59,12 @@ class MacAddress : public Printable {
5659
MacAddress(const char *macstr);
5760
MacAddress(const String &macstr);
5861

62+
#ifdef __GXX_EXPERIMENTAL_CXX0X__
63+
// Initializer list constructor for {0xAA, 0xBB, ...} syntax
64+
// This has higher priority than String conversion, preventing ambiguity
65+
MacAddress(std::initializer_list<uint8_t> list);
66+
#endif
67+
5968
virtual ~MacAddress() {}
6069

6170
bool fromString(const char *buf);

cores/esp32/WString.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@
2929
#include <stdint.h>
3030
#include <string.h>
3131
#include <ctype.h>
32+
#ifdef __GXX_EXPERIMENTAL_CXX0X__
3233
#include <initializer_list>
34+
#endif
3335

3436
// A pure abstract class forward used as a means to proide a unique pointer type
3537
// but really is never defined.

0 commit comments

Comments
 (0)