Skip to content

Commit 158fe6a

Browse files
bemerybmw4og
authored andcommitted
Support generic callables in ScopedOperation
In order to use a scoped function (or any other type of callable) with ScopedOperation, we template ScopedOperation with the callable rather than making the call always an amp::callback. GIT_ORIGIN_SPP_REV_ID: 38a0277ccd8183c91a11b7e97d2923e035e22365
1 parent f5a544e commit 158fe6a

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

score/utils/BUILD

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ cc_gtest_unit_test(
6161
name = "scoped_operation_test",
6262
srcs = ["test/scoped_operation_test.cpp"],
6363
features = COMPILER_WARNING_FEATURES,
64-
deps = [":scoped_operation"],
64+
deps = [
65+
":scoped_operation",
66+
"@score_baselibs//score/language/safecpp/scoped_function:move_only_scoped_function",
67+
],
6568
)
6669

6770
cc_unit_test_suites_for_host_and_qnx(

score/utils/src/scoped_operation.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
#include <score/callback.hpp>
1717

18+
#include <type_traits>
19+
1820
namespace score
1921
{
2022
namespace utils
@@ -24,12 +26,10 @@ namespace utils
2426
///
2527
/// @tparam Capacity The capacity to use by callback in storage
2628
/// @tparam Alignment The alignment of callback in storage
27-
template <std::size_t Capacity = score::cpp::callback<void()>::capacity_t::value,
28-
std::size_t Alignment = score::cpp::callback<void()>::alignment_t::value>
29+
template <typename CallbackType = score::cpp::callback<void()>>
2930
class ScopedOperation final
3031
{
31-
private:
32-
using CallbackType = score::cpp::callback<void(), Capacity, Alignment>;
32+
static_assert(std::is_invocable_v<CallbackType>);
3333

3434
public:
3535
explicit ScopedOperation(CallbackType fn) : fn_{std::move(fn)} {}

score/utils/test/scoped_operation_test.cpp

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
********************************************************************************/
1313
#include "score/utils/src/scoped_operation.h"
1414

15+
#include "score/language/safecpp/scoped_function/move_only_scoped_function.h"
16+
1517
#include <gmock/gmock.h>
1618
#include <gtest/gtest.h>
1719

@@ -24,7 +26,7 @@ namespace ifwi
2426
namespace
2527
{
2628

27-
TEST(ScopedOperationTest, callFunctionOnDestruction)
29+
TEST(ScopedOperationTest, CallDefaultFunctionOnDestruction)
2830
{
2931
bool functionCalled = false;
3032
{
@@ -36,6 +38,32 @@ TEST(ScopedOperationTest, callFunctionOnDestruction)
3638
ASSERT_TRUE(functionCalled);
3739
}
3840

41+
TEST(ScopedOperationTest, CallAmpCallbackOnDestruction)
42+
{
43+
bool functionCalled = false;
44+
{
45+
utils::ScopedOperation<score::cpp::callback<void()>> scopedOperation{[&functionCalled]() noexcept {
46+
functionCalled = true;
47+
}};
48+
functionCalled = false;
49+
}
50+
ASSERT_TRUE(functionCalled);
51+
}
52+
53+
TEST(ScopedOperationTest, CallScopedFunctionOnDestruction)
54+
{
55+
safecpp::Scope<> scope{};
56+
bool functionCalled = false;
57+
{
58+
utils::ScopedOperation<safecpp::MoveOnlyScopedFunction<void()>> scopedOperation{
59+
{scope, [&functionCalled]() noexcept {
60+
functionCalled = true;
61+
}}};
62+
functionCalled = false;
63+
}
64+
ASSERT_TRUE(functionCalled);
65+
}
66+
3967
} // namespace
4068
} // namespace ifwi
4169
} // namespace platform

0 commit comments

Comments
 (0)