Line data Source code
1 0 : // Distributed under the MIT License.
2 : // See LICENSE.txt for details.
3 :
4 : #pragma once
5 :
6 : #include <type_traits>
7 :
8 : /*!
9 : * \ingroup TypeTraitsGroup
10 : * \brief Generate a metafunction that will retrieve the specified
11 : * `static constexpr` member variable, or, if not present, a default.
12 : *
13 : * \warning Please use defaults responsibly. In many cases it is
14 : * better to require the static member variable is present so the
15 : * functionality isn't hidden. Also make sure to document the
16 : * compile-time interface, e.g. with a \ref protocols "protocol".
17 : */
18 1 : #define CREATE_GET_STATIC_MEMBER_VARIABLE_OR_DEFAULT(VARIABLE_NAME) \
19 : template <typename CheckingType, auto Default, typename = std::void_t<>> \
20 : struct get_##VARIABLE_NAME##_or_default { \
21 : static constexpr auto value = Default; \
22 : }; \
23 : template <typename CheckingType, auto Default> \
24 : struct get_##VARIABLE_NAME##_or_default< \
25 : CheckingType, Default, \
26 : std::void_t<decltype(CheckingType::VARIABLE_NAME)>> { \
27 : static_assert( \
28 : std::is_same_v<std::decay_t<decltype(Default)>, \
29 : std::decay_t<decltype(CheckingType::VARIABLE_NAME)>>, \
30 : "Types of default and found value are not the same."); \
31 : static constexpr auto value = CheckingType::VARIABLE_NAME; \
32 : }; \
33 : template <typename CheckingType, auto Default> \
34 : constexpr auto get_##VARIABLE_NAME##_or_default_v = \
35 : get_##VARIABLE_NAME##_or_default<CheckingType, Default>::value;
|