Skip to content

Commit 3be024d

Browse files
authored
DPL: extend call_if_defined to have a fallback if not defined (#6354)
1 parent ef7c674 commit 3be024d

3 files changed

Lines changed: 59 additions & 4 deletions

File tree

Framework/Core/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ foreach(t
153153
ConfigurationOptionsRetriever
154154
CallbackRegistry
155155
ChannelSpecHelpers
156+
CheckTypes
156157
CompletionPolicy
157158
ComputingResourceHelpers
158159
ConfigParamStore
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright CERN and copyright holders of ALICE O2. This software is
2+
// distributed under the terms of the GNU General Public License v3 (GPL
3+
// Version 3), copied verbatim in the file "COPYING".
4+
//
5+
// See http://alice-o2.web.cern.ch/license for full licensing information.
6+
//
7+
// In applying this license CERN does not waive the privileges and immunities
8+
// granted to it by virtue of its status as an Intergovernmental Organization
9+
// or submit itself to any jurisdiction.
10+
11+
#define BOOST_TEST_MODULE Test Framework Traits
12+
#define BOOST_TEST_MAIN
13+
#define BOOST_TEST_DYN_LINK
14+
15+
#include <boost/test/unit_test.hpp>
16+
#include "Framework/CheckTypes.h"
17+
18+
using namespace o2::framework;
19+
20+
struct Foo {
21+
bool foo;
22+
};
23+
24+
BOOST_AUTO_TEST_CASE(CallIfUndefined)
25+
{
26+
bool shouldBeCalled = false;
27+
bool shouldNotBeCalled = false;
28+
bool shouldBeCalledOnUndefined = false;
29+
30+
call_if_defined<struct Foo>([&shouldBeCalled](auto) { shouldBeCalled = true; });
31+
call_if_defined<struct Bar>([&shouldNotBeCalled](auto) { shouldNotBeCalled = true; });
32+
BOOST_REQUIRE_EQUAL(shouldBeCalled, true);
33+
BOOST_REQUIRE_EQUAL(shouldNotBeCalled, false);
34+
35+
shouldBeCalled = false;
36+
shouldNotBeCalled = false;
37+
shouldBeCalledOnUndefined = false;
38+
39+
call_if_defined_full<struct Bar>([&shouldNotBeCalled](auto) { shouldNotBeCalled = true; }, []() {});
40+
BOOST_REQUIRE_EQUAL(shouldNotBeCalled, false);
41+
BOOST_REQUIRE_EQUAL(shouldBeCalledOnUndefined, false);
42+
call_if_defined_full<struct Bar>([&shouldNotBeCalled](auto) { shouldNotBeCalled = true; }, [&shouldBeCalledOnUndefined]() { shouldBeCalledOnUndefined = true; });
43+
BOOST_REQUIRE_EQUAL(shouldNotBeCalled, false);
44+
BOOST_REQUIRE_EQUAL(shouldBeCalledOnUndefined, true);
45+
}

Framework/Foundation/include/Framework/CheckTypes.h

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,25 @@ constexpr bool is_type_complete_v = false;
2121
template <typename T>
2222
constexpr bool is_type_complete_v<T, std::void_t<decltype(sizeof(T))>> = true;
2323

24-
/// Helper which will invoke lambda if the type T is actually available.
24+
/// Helper which will invoke @a onDefined if the type T is actually available
25+
/// or @a onUndefined if the type T is a forward declaration.
2526
/// Can be used to check for existence or not of a given type.
26-
template <typename T, typename TLambda>
27-
void call_if_defined(TLambda&& lambda)
27+
template <typename T, typename TDefined, typename TUndefined>
28+
void call_if_defined_full(TDefined&& onDefined, TUndefined&& onUndefined)
2829
{
2930
if constexpr (is_type_complete_v<T>) {
30-
lambda(static_cast<T*>(nullptr));
31+
onDefined(static_cast<T*>(nullptr));
32+
} else {
33+
onUndefined();
3134
}
3235
}
3336

37+
template <typename T, typename TDefined>
38+
void call_if_defined(TDefined&& onDefined)
39+
{
40+
call_if_defined_full<T>(onDefined, []() -> void {});
41+
}
42+
3443
} // namespace o2::framework
3544

3645
#endif // O2_FRAMEWORK_CHECKTYPES_H_

0 commit comments

Comments
 (0)