Skip to content

Commit bbdad4b

Browse files
committed
adding support for Lambda that returns a CHIP_ERROR to LambdaBridge
1 parent d9f7861 commit bbdad4b

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

src/lib/support/LambdaBridge.h

+27-3
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,39 @@ class LambdaBridge
3636
static_assert(CHIP_CONFIG_LAMBDA_EVENT_ALIGN % alignof(Lambda) == 0, "lambda align too large");
3737

3838
// 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+
4049
::memcpy(&mLambdaBody, &lambda, sizeof(Lambda));
4150
}
4251

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+
}
4467

4568
private:
4669
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);
4872
LambdaStorage mLambdaBody;
4973
};
5074

0 commit comments

Comments
 (0)