Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Framework/Core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ foreach(t
ConfigurationOptionsRetriever
CallbackRegistry
ChannelSpecHelpers
CheckTypes
CompletionPolicy
ComputingResourceHelpers
ConfigParamStore
Expand Down
45 changes: 45 additions & 0 deletions Framework/Core/test/test_CheckTypes.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright CERN and copyright holders of ALICE O2. This software is
// distributed under the terms of the GNU General Public License v3 (GPL
// Version 3), copied verbatim in the file "COPYING".
//
// See http://alice-o2.web.cern.ch/license for full licensing information.
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.

#define BOOST_TEST_MODULE Test Framework Traits
#define BOOST_TEST_MAIN
#define BOOST_TEST_DYN_LINK

#include <boost/test/unit_test.hpp>
#include "Framework/CheckTypes.h"

using namespace o2::framework;

struct Foo {
bool foo;
};

BOOST_AUTO_TEST_CASE(CallIfUndefined)
{
bool shouldBeCalled = false;
bool shouldNotBeCalled = false;
bool shouldBeCalledOnUndefined = false;

call_if_defined<struct Foo>([&shouldBeCalled](auto) { shouldBeCalled = true; });
call_if_defined<struct Bar>([&shouldNotBeCalled](auto) { shouldNotBeCalled = true; });
BOOST_REQUIRE_EQUAL(shouldBeCalled, true);
BOOST_REQUIRE_EQUAL(shouldNotBeCalled, false);

shouldBeCalled = false;
shouldNotBeCalled = false;
shouldBeCalledOnUndefined = false;

call_if_defined_full<struct Bar>([&shouldNotBeCalled](auto) { shouldNotBeCalled = true; }, []() {});
BOOST_REQUIRE_EQUAL(shouldNotBeCalled, false);
BOOST_REQUIRE_EQUAL(shouldBeCalledOnUndefined, false);
call_if_defined_full<struct Bar>([&shouldNotBeCalled](auto) { shouldNotBeCalled = true; }, [&shouldBeCalledOnUndefined]() { shouldBeCalledOnUndefined = true; });
BOOST_REQUIRE_EQUAL(shouldNotBeCalled, false);
BOOST_REQUIRE_EQUAL(shouldBeCalledOnUndefined, true);
}
17 changes: 13 additions & 4 deletions Framework/Foundation/include/Framework/CheckTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,25 @@ constexpr bool is_type_complete_v = false;
template <typename T>
constexpr bool is_type_complete_v<T, std::void_t<decltype(sizeof(T))>> = true;

/// Helper which will invoke lambda if the type T is actually available.
/// Helper which will invoke @a onDefined if the type T is actually available
/// or @a onUndefined if the type T is a forward declaration.
/// Can be used to check for existence or not of a given type.
template <typename T, typename TLambda>
void call_if_defined(TLambda&& lambda)
template <typename T, typename TDefined, typename TUndefined>
void call_if_defined_full(TDefined&& onDefined, TUndefined&& onUndefined)
{
if constexpr (is_type_complete_v<T>) {
lambda(static_cast<T*>(nullptr));
onDefined(static_cast<T*>(nullptr));
} else {
onUndefined();
}
}

template <typename T, typename TDefined>
void call_if_defined(TDefined&& onDefined)
{
call_if_defined_full<T>(onDefined, []() -> void {});
}

} // namespace o2::framework

#endif // O2_FRAMEWORK_CHECKTYPES_H_