|
| 1 | +/* |
| 2 | + ESP8266 Delegate class template test by Dirk O. Kaar |
| 3 | + This example code is in the public domain |
| 4 | +*/ |
| 5 | + |
| 6 | +#include "Foo.h" |
| 7 | +#include <Delegate.h> |
| 8 | +#include <MultiDelegate.h> |
| 9 | + |
| 10 | +constexpr long unsigned MAXCNT = 100000UL; |
| 11 | +const String TESTCASE = "F"; |
| 12 | +const String LATENCY = "Latency/cycles = "; |
| 13 | +uint32_t cycles; |
| 14 | +uint32_t cnt; |
| 15 | + |
| 16 | +enum TestCase { F0 = 0, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12 }; |
| 17 | +TestCase testCases[] = { F0, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12 }; |
| 18 | + |
| 19 | +using Fp0 = bool (*)(Foo*, int); |
| 20 | +using Fp2 = bool(*)(int); |
| 21 | + |
| 22 | +Foo* o0; |
| 23 | +Fp0 f0; |
| 24 | +Foo* o1; |
| 25 | +Fp0 f1; |
| 26 | +Delegate<bool(int)> f2; |
| 27 | +Delegate<bool(int), Foo*> f3; |
| 28 | + |
| 29 | +std::function<bool(int)> f4; |
| 30 | +std::function<bool(int)> f5; |
| 31 | + |
| 32 | +Delegate<bool(int)> f6; |
| 33 | +Delegate<bool(int)> f7; |
| 34 | +Delegate<bool(int)> f8; |
| 35 | + |
| 36 | +Delegate<bool(int), Foo*> f9; |
| 37 | +Delegate<bool(int), Foo*> f10; |
| 38 | +Delegate<bool(int), Foo*> f11; |
| 39 | +Delegate<bool(int), Foo*> f12; |
| 40 | + |
| 41 | +void set_f0(Fp0 _f, Foo* _o) { f0 = _f; o0 = _o; } |
| 42 | +void set_f1(Fp0 _f, Foo* _o) { f1 = _f; o1 = _o; } |
| 43 | +void set_f2(Fp2 _f) { f2 = { _f }; } |
| 44 | +void set_f3(Fp2 _f) { f3 = { _f }; } |
| 45 | + |
| 46 | +void set_f4(const std::function<bool(int)>& _f) { f4 = _f; } |
| 47 | +void set_f5(const std::function<bool(int)>& _f) { f5 = _f; } |
| 48 | + |
| 49 | +void set_f6(const Delegate<bool(int)>& _f) { f6 = _f; } |
| 50 | +void set_f7(const Delegate<bool(int)>& _f) { f7 = _f; } |
| 51 | +void set_f8(const Delegate<bool(int)>& _f) { f8 = _f; } |
| 52 | + |
| 53 | +void set_f9(const Delegate<bool(int), Foo*>& _f) { f9 = _f; } |
| 54 | +void set_f10(const Delegate<bool(int), Foo*>& _f) { f10 = _f; } |
| 55 | + |
| 56 | +void set_f11(const Delegate<bool(int), Foo*>& _f) { f11 = _f; } |
| 57 | +void set_f12(const Delegate<bool(int), Foo*>& _f) { f12 = _f; } |
| 58 | + |
| 59 | +extern void testPrep(); |
| 60 | + |
| 61 | +void stopWatch() |
| 62 | +{ |
| 63 | + if (MAXCNT == cnt) |
| 64 | + { |
| 65 | + Serial.print(LATENCY); |
| 66 | + Serial.println(cycles / MAXCNT); |
| 67 | + cycles = 0; |
| 68 | + cnt = 0; |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +void setup() |
| 73 | +{ |
| 74 | + Serial.begin(115200); |
| 75 | + testPrep(); |
| 76 | + |
| 77 | + cycles = 0; |
| 78 | + cnt = 0; |
| 79 | +} |
| 80 | + |
| 81 | +// Add the main program code into the continuous loop() function |
| 82 | +void loop() |
| 83 | +{ |
| 84 | + for (auto tc : testCases) { |
| 85 | + Serial.print(TESTCASE); |
| 86 | + Serial.print(tc); |
| 87 | + Serial.print(": "); |
| 88 | + for (unsigned i = 0; i < MAXCNT; ++i) |
| 89 | + { |
| 90 | + auto start = ESP.getCycleCount(); |
| 91 | + switch (tc) { |
| 92 | + case F0: f0(o0, 42); break; |
| 93 | + case F1: f1(o1, 42); break; |
| 94 | + case F2: f2(42); break; // { cbCPtr } |
| 95 | + case F3: f3(42); break; // { cbCPtr } |
| 96 | + |
| 97 | + case F4: f4(42); break; // [o](int result) -> bool { return o->cb(result); } |
| 98 | + case F5: f5(42); break; // std::bind(Foo::cbwObj, o, std::placeholders::_1) |
| 99 | + |
| 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) |
| 102 | + case F8: f8(42); break; // [](int result) -> bool { return cbCPtr(result); } |
| 103 | + |
| 104 | + case F9: f9(42); break; // [o](int result) -> bool { return o->cb(result); } <==== antipattern for Delegate, use f11 instead |
| 105 | + case F10: f10(42); break; // std::bind(Foo::cbwObj, o, std::placeholders::_1) <==== antipattern for Delegate, use f11 instead |
| 106 | + |
| 107 | + case F11: f11(42); break; // [](Foo* o, int result) -> bool { return o->cb(result); }, o.get() }) |
| 108 | + case F12: f12(42); break; // { Foo::cbwObj, o.get() } |
| 109 | + } |
| 110 | + cycles += (ESP.getCycleCount() - start); |
| 111 | + stopWatch(); |
| 112 | + } |
| 113 | + yield(); |
| 114 | + } |
| 115 | + delay(16000); |
| 116 | +} |
0 commit comments