From ff0bbc2e0517bdb724e05ddbcee69013ee78072c Mon Sep 17 00:00:00 2001 From: Giulio Eulisse <10544+ktf@users.noreply.github.com> Date: Wed, 9 Jun 2021 11:50:24 +0200 Subject: [PATCH] DPL: extend call_if_defined to have a fallback if not defined --- Framework/Core/CMakeLists.txt | 1 + Framework/Core/test/test_CheckTypes.cxx | 45 +++++++++++++++++++ .../Foundation/include/Framework/CheckTypes.h | 17 +++++-- 3 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 Framework/Core/test/test_CheckTypes.cxx diff --git a/Framework/Core/CMakeLists.txt b/Framework/Core/CMakeLists.txt index 7bee2f9f02682..d40108210260d 100644 --- a/Framework/Core/CMakeLists.txt +++ b/Framework/Core/CMakeLists.txt @@ -153,6 +153,7 @@ foreach(t ConfigurationOptionsRetriever CallbackRegistry ChannelSpecHelpers + CheckTypes CompletionPolicy ComputingResourceHelpers ConfigParamStore diff --git a/Framework/Core/test/test_CheckTypes.cxx b/Framework/Core/test/test_CheckTypes.cxx new file mode 100644 index 0000000000000..f884f117e9ba3 --- /dev/null +++ b/Framework/Core/test/test_CheckTypes.cxx @@ -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 +#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([&shouldBeCalled](auto) { shouldBeCalled = true; }); + call_if_defined([&shouldNotBeCalled](auto) { shouldNotBeCalled = true; }); + BOOST_REQUIRE_EQUAL(shouldBeCalled, true); + BOOST_REQUIRE_EQUAL(shouldNotBeCalled, false); + + shouldBeCalled = false; + shouldNotBeCalled = false; + shouldBeCalledOnUndefined = false; + + call_if_defined_full([&shouldNotBeCalled](auto) { shouldNotBeCalled = true; }, []() {}); + BOOST_REQUIRE_EQUAL(shouldNotBeCalled, false); + BOOST_REQUIRE_EQUAL(shouldBeCalledOnUndefined, false); + call_if_defined_full([&shouldNotBeCalled](auto) { shouldNotBeCalled = true; }, [&shouldBeCalledOnUndefined]() { shouldBeCalledOnUndefined = true; }); + BOOST_REQUIRE_EQUAL(shouldNotBeCalled, false); + BOOST_REQUIRE_EQUAL(shouldBeCalledOnUndefined, true); +} diff --git a/Framework/Foundation/include/Framework/CheckTypes.h b/Framework/Foundation/include/Framework/CheckTypes.h index 681a787d071b9..fac3ad26bd81d 100644 --- a/Framework/Foundation/include/Framework/CheckTypes.h +++ b/Framework/Foundation/include/Framework/CheckTypes.h @@ -21,16 +21,25 @@ constexpr bool is_type_complete_v = false; template constexpr bool is_type_complete_v> = 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 -void call_if_defined(TLambda&& lambda) +template +void call_if_defined_full(TDefined&& onDefined, TUndefined&& onUndefined) { if constexpr (is_type_complete_v) { - lambda(static_cast(nullptr)); + onDefined(static_cast(nullptr)); + } else { + onUndefined(); } } +template +void call_if_defined(TDefined&& onDefined) +{ + call_if_defined_full(onDefined, []() -> void {}); +} + } // namespace o2::framework #endif // O2_FRAMEWORK_CHECKTYPES_H_