Skip to content

Commit 0fd063c

Browse files
committed
More hints for proper use of Delegate to optimize performance.
1 parent 099fc86 commit 0fd063c

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

libraries/esp8266/examples/DelegatePerf/DelegatePerf.ino

+6-2
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@ std::function<bool(int)> f5;
3131

3232
Delegate<bool(int)> f6;
3333
Delegate<bool(int)> f7;
34+
3435
Delegate<bool(int)> f8;
3536

3637
Delegate<bool(int), Foo*> f9;
3738
Delegate<bool(int), Foo*> f10;
39+
3840
Delegate<bool(int), Foo*> f11;
3941
Delegate<bool(int), Foo*> f12;
4042

@@ -48,6 +50,7 @@ void set_f5(const std::function<bool(int)>& _f) { f5 = _f; }
4850

4951
void set_f6(const Delegate<bool(int)>& _f) { f6 = _f; }
5052
void set_f7(const Delegate<bool(int)>& _f) { f7 = _f; }
53+
5154
void set_f8(const Delegate<bool(int)>& _f) { f8 = _f; }
5255

5356
void set_f9(const Delegate<bool(int), Foo*>& _f) { f9 = _f; }
@@ -97,8 +100,9 @@ void loop()
97100
case F4: f4(42); break; // [o](int result) -> bool { return o->cb(result); }
98101
case F5: f5(42); break; // std::bind(Foo::cbwObj, o, std::placeholders::_1)
99102

100-
case F6: f6(42); break; // [o](int result) -> bool { return o->cb(result); }
101-
case F7: f7(42); break; // std::bind(Foo::cbwObj, o, std::placeholders::_1)
103+
case F6: f6(42); break; // [o](int result) -> bool { return o->cb(result); } <==== antipattern for Delegate, use f11 instead
104+
case F7: f7(42); break; // std::bind(Foo::cbwObj, o, std::placeholders::_1) <==== antipattern for Delegate, use f11 instead
105+
102106
case F8: f8(42); break; // [](int result) -> bool { return cbCPtr(result); }
103107

104108
case F9: f9(42); break; // [o](int result) -> bool { return o->cb(result); } <==== antipattern for Delegate, use f11 instead

libraries/esp8266/examples/DelegatePerf/TestPrep.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ extern void set_f5(const std::function<bool(int)>& f);
2323

2424
extern void set_f6(const Delegate<bool(int)>&);
2525
extern void set_f7(const Delegate<bool(int)>&);
26+
2627
extern void set_f8(const Delegate<bool(int)>&);
2728

2829
extern void set_f9(const Delegate<bool(int), Foo*>& f);
@@ -43,6 +44,7 @@ void testPrep() {
4344

4445
set_f6([o](int result) -> bool { return o->cb(result); });
4546
set_f7(std::bind(Foo::cbwObj, o.get(), std::placeholders::_1));
47+
4648
set_f8([](int result) -> bool { return cbCPtr(result); });
4749

4850
set_f9([o](int result) -> bool { return o->cb(result); });

0 commit comments

Comments
 (0)