Skip to content

Commit 6c16c41

Browse files
committed
Employ black-macro-magic to achieve 'macro overloading'
Same macro name, different macro parameter.
1 parent 48a7ff7 commit 6c16c41

File tree

1 file changed

+29
-11
lines changed

1 file changed

+29
-11
lines changed

Diff for: src/Arduino_Threads.h

+29-11
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,37 @@ public: \
4444
Source<type> name; \
4545
private:
4646

47-
#define SINK(name, type) \
47+
/* We need to call the SinkBlocking<T>(size_t const size)
48+
* non-default constructor using size as parameter.
49+
50+
* This is achieved via
51+
* SinkBlocking<type> name{size};
52+
* instead of
53+
* SinkBlocking<type> name(size);
54+
* otherwise the compiler will read it as a declaration
55+
* of a method called "name" and we get a syntax error.
56+
*
57+
* This is called "C++11 uniform init" (using "{}" instead
58+
* of "()" without "="... yikes!)
59+
* https://chromium.googlesource.com/chromium/src/+/master/styleguide/c++/c++-dos-and-donts.md
60+
*/
61+
62+
#define SINK_2_ARG(name, type) \
63+
public: \
64+
SinkBlocking<type> name{1}; \
65+
private:
66+
67+
#define SINK_3_ARG(name, type, size) \
4868
public: \
49-
SinkBlocking<type> name; \
69+
SinkBlocking<type> name{size}; \
5070
private:
51-
// we need to call the Sink<T>(int size) non-default constructor using size as parameter.
52-
// This is done by writing
53-
// Sink<type> name{size};
54-
// instead of:
55-
// Sink<type> name(size);
56-
// otherwise the compiler will read it as a declaration of a method called "name" and we
57-
// get a syntax error.
58-
// This is called "C++11 uniform init" (using "{}" instead of "()" without "="... yikes!)
59-
// https://chromium.googlesource.com/chromium/src/+/master/styleguide/c++/c++-dos-and-donts.md
71+
72+
/* Black C macro magic enabling "macro overloading"
73+
* with same name macro, but multiple arguments.
74+
* https://stackoverflow.com/questions/11761703/overloading-macro-on-number-of-arguments
75+
*/
76+
#define GET_SINK_MACRO(_1,_2,_3,NAME,...) NAME
77+
#define SINK(...) GET_SINK_MACRO(__VA_ARGS__, SINK_3_ARG, SINK_2_ARG)(__VA_ARGS__)
6078

6179
#define SHARED(name, type) \
6280
Shared<type> name;

0 commit comments

Comments
 (0)