@@ -36,15 +36,39 @@ class LambdaBridge
36
36
static_assert (CHIP_CONFIG_LAMBDA_EVENT_ALIGN % alignof (Lambda) == 0 , " lambda align too large" );
37
37
38
38
// Implicit cast a capture-less lambda into a raw function pointer.
39
- mLambdaProxy = [](const LambdaStorage & body) { (*reinterpret_cast <const Lambda *>(&body))(); };
39
+ if constexpr (std::is_same_v<decltype (lambda ()), void >)
40
+ {
41
+ mLambdaVoidProxy = [](const LambdaStorage & body) { (*reinterpret_cast <const Lambda *>(&body))(); };
42
+ }
43
+ else if constexpr (std::is_same_v<decltype (lambda ()), CHIP_ERROR>)
44
+ {
45
+ mLambdaProxy = [](const LambdaStorage & body) { return (*reinterpret_cast <const Lambda *>(&body))(); };
46
+ }
47
+
48
+
40
49
::memcpy (&mLambdaBody , &lambda, sizeof (Lambda));
41
50
}
42
51
43
- void operator ()() const { mLambdaProxy (mLambdaBody ); }
52
+ void operator ()() const
53
+ {
54
+ if (mLambdaVoidProxy != nullptr )
55
+ {
56
+ mLambdaVoidProxy (mLambdaBody );
57
+ }
58
+ }
59
+ CHIP_ERROR CallLambdaWithErrorReturn () const
60
+ {
61
+ if (mLambdaProxy != nullptr )
62
+ {
63
+ return mLambdaProxy (mLambdaBody );
64
+ }
65
+ return CHIP_ERROR_INTERNAL; // Return an error if the proxy is not for CHIP_ERROR
66
+ }
44
67
45
68
private:
46
69
using LambdaStorage = std::aligned_storage_t <CHIP_CONFIG_LAMBDA_EVENT_SIZE, CHIP_CONFIG_LAMBDA_EVENT_ALIGN>;
47
- void (*mLambdaProxy )(const LambdaStorage & body);
70
+ void (*mLambdaVoidProxy )(const LambdaStorage & body);
71
+ CHIP_ERROR (*mLambdaProxy )(const LambdaStorage & body);
48
72
LambdaStorage mLambdaBody ;
49
73
};
50
74
0 commit comments