ProtocolHelpers.hpp
1 // Distributed under the MIT License.
2 // See LICENSE.txt for details.
3 
4 #pragma once
5 
6 #include <type_traits>
7 
8 #include "Utilities/TypeTraits.hpp"
9 
10 namespace tt {
11 
12 /*!
13  * \ingroup ProtocolsGroup
14  * \brief Indicate a class conforms to the `Protocol`.
15  *
16  * (Publicly) inherit classes from this class to indicate they conform to the
17  * `Protocol`.
18  *
19  * \see Documentation on \ref protocols
20  */
21 template <template <class> class Protocol>
22 struct ConformsTo {};
23 
24 // Note that std::is_convertible is used in the following type aliases as it
25 // will not match private base classes (unlike std::is_base_of)
26 
27 // @{
28 /*!
29  * \ingroup ProtocolsGroup
30  * \brief Checks if the `ConformingType` conforms to the `Protocol`.
31  *
32  * By default, only checks if the class derives off the protocol to reduce
33  * compile time. Protocol conformance is tested rigorously in the unit tests
34  * instead. Set the `SPECTRE_ALWAYS_CHECK_PROTOCOL_CONFORMANCE` CMake option to
35  * always enable rigorous protocol conformance checks.
36  *
37  * \see Documentation on \ref protocols
38  */
39 #ifdef SPECTRE_ALWAYS_CHECK_PROTOCOL_CONFORMANCE
40 template <typename ConformingType, template <class> class Protocol>
41 constexpr bool conforms_to_v =
42  cpp17::is_convertible_v<ConformingType*, ConformsTo<Protocol>*>and
43  Protocol<ConformingType>::value;
44 template <typename ConformingType, template <class> class Protocol>
45 using conforms_to =
47 #else // SPECTRE_ALWAYS_CHECK_PROTOCOL_CONFORMANCE
48 template <typename ConformingType, template <class> class Protocol>
49 using conforms_to =
51 template <typename ConformingType, template <class> class Protocol>
52 constexpr bool conforms_to_v =
53  cpp17::is_convertible_v<ConformingType*, ConformsTo<Protocol>*>;
54 #endif // SPECTRE_ALWAYS_CHECK_PROTOCOL_CONFORMANCE
55 // @}
56 
57 } // namespace tt
Indicate a class conforms to the Protocol.
Definition: ProtocolHelpers.hpp:22
A collection of useful type traits.
Definition: TensorExpression.hpp:113
typename std::is_convertible< ConformingType *, ConformsTo< Protocol > * > conforms_to
Checks if the ConformingType conforms to the Protocol.
Definition: ProtocolHelpers.hpp:50
constexpr bool conforms_to_v
Checks if the ConformingType conforms to the Protocol.
Definition: ProtocolHelpers.hpp:52