@@ -28,47 +28,41 @@ class LambdaBridge
28
28
public:
29
29
// Use initialize instead of constructor because this class has to be trivial
30
30
template <typename Lambda>
31
- void Initialize (const Lambda & lambda)
31
+ void Initialize (const Lambda & lambda, CHIP_ERROR * Error_Value = nullptr )
32
32
{
33
+ // LambdaBridge accepts either Lambdas without arguments or those with `CHIP_ERROR *` as argument.
34
+ mpError = Error_Value;
35
+
33
36
// memcpy is used to move the lambda into the event queue, so it must be trivially copyable
34
37
static_assert (std::is_trivially_copyable<Lambda>::value, " lambda must be trivially copyable" );
35
38
static_assert (sizeof (Lambda) <= CHIP_CONFIG_LAMBDA_EVENT_SIZE, " lambda too large" );
36
39
static_assert (CHIP_CONFIG_LAMBDA_EVENT_ALIGN % alignof (Lambda) == 0 , " lambda align too large" );
37
40
38
41
// Implicit cast a capture-less lambda into a raw function pointer.
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
- }
42
+ mLambdaProxy = [](const LambdaStorage & body, CHIP_ERROR * apError) {
43
+ // Check if lambda has CHIP_ERROR * as argument, if not, call it without arguments
44
+ if constexpr (std::is_invocable<Lambda, CHIP_ERROR *>::value)
45
+ {
46
+ // Call the lambda with CHIP_ERROR* argument
47
+ (*reinterpret_cast <const Lambda *>(&body))(apError);
48
+ }
49
+ else
50
+ {
51
+ // Call the lambda with no arguments
52
+ (*reinterpret_cast <const Lambda *>(&body))();
53
+ }
54
+ };
47
55
48
56
::memcpy (&mLambdaBody , &lambda, sizeof (Lambda));
49
57
}
50
58
51
- void operator ()() const
52
- {
53
- if (mLambdaVoidProxy != nullptr )
54
- {
55
- mLambdaVoidProxy (mLambdaBody );
56
- }
57
- }
58
- CHIP_ERROR CallLambdaWithErrorReturn () const
59
- {
60
- if (mLambdaProxy != nullptr )
61
- {
62
- return mLambdaProxy (mLambdaBody );
63
- }
64
- return CHIP_ERROR_INTERNAL; // Return an error if the proxy is not for CHIP_ERROR
65
- }
59
+ void operator ()() const { mLambdaProxy (mLambdaBody , mpError); }
66
60
67
61
private:
68
62
using LambdaStorage = std::aligned_storage_t <CHIP_CONFIG_LAMBDA_EVENT_SIZE, CHIP_CONFIG_LAMBDA_EVENT_ALIGN>;
69
- void (*mLambdaVoidProxy )(const LambdaStorage & body);
70
- CHIP_ERROR (*mLambdaProxy )(const LambdaStorage & body);
63
+ void (*mLambdaProxy )(const LambdaStorage & body, CHIP_ERROR * mpError);
71
64
LambdaStorage mLambdaBody ;
65
+ CHIP_ERROR * mpError;
72
66
};
73
67
74
68
static_assert (std::is_trivial<LambdaBridge>::value, " LambdaBridge is not trivial" );
0 commit comments