pairinteraction
A Rydberg Interaction Calculator
traits.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2024 Pairinteraction Developers
2// SPDX-License-Identifier: LGPL-3.0-or-later
3
4#pragma once
5
7
8#include <complex>
9#include <functional>
10#include <type_traits>
11
13
14/**
15 * @struct CrtpTraits
16 *
17 * @brief Helper struct to extract types from a derived basis type. Must be specialized for each
18 * derived basis type.
19 *
20 * @tparam Derived The derived basis type from which to extract types.
21 */
22
23template <typename Derived>
25
26/**
27 * @struct NumTraits
28 *
29 * @brief Helper struct to extract types from a numerical type.
30 *
31 * @tparam Numeric The numerical type from which to extract types.
32 */
33
34template <typename Numeric>
35struct NumTraits {
36 static_assert(std::is_arithmetic_v<Numeric>);
37
38 using real_t = Numeric;
39 static constexpr bool is_complex_v = false;
40 static constexpr bool from_floating_point_v = std::is_floating_point<Numeric>::value;
41 static constexpr bool from_integral_v = std::is_integral<Numeric>::value;
42};
43
44template <typename Numeric>
45struct NumTraits<std::complex<Numeric>> {
46 static_assert(std::is_arithmetic_v<Numeric>);
47
48 using real_t = Numeric;
49 static constexpr bool is_complex_v = true;
50 static constexpr bool from_floating_point_v = std::is_floating_point<Numeric>::value;
51 static constexpr bool from_integral_v = std::is_integral<Numeric>::value;
52};
53
54/**
55 * @struct OpTraits
56 *
57 * @brief Helper struct to extract whether a type supports certain operations.
58 *
59 * @tparam T The type for which the existence of operations is to be checked.
60 */
61
62template <typename T>
63struct OpTraits {
64 static constexpr bool has_equal_v = std::is_invocable_r_v<bool, std::equal_to<>, T, T>;
65 static constexpr bool has_less_v = std::is_invocable_r_v<bool, std::less<>, T, T>;
66};
67
68/**
69 * @struct FPTypeTraits
70 *
71 * @brief Helper struct to extract the type from a floating point precision enum.
72 *
73 * @tparam T The enum value for which the type is to be extracted.
74 */
75
76template <FloatType T>
78
79template <>
81 using type = float;
82};
83
84template <>
86 using type = double;
87};
88
89template <typename Scalar, FloatType FP>
90using restricted_t = std::conditional_t<traits::NumTraits<Scalar>::is_complex_v,
91 std::complex<typename FPTypeTraits<FP>::type>,
92 typename FPTypeTraits<FP>::type>;
93
94} // namespace pairinteraction::traits
std::conditional_t< traits::NumTraits< Scalar >::is_complex_v, std::complex< typename FPTypeTraits< FP >::type >, typename FPTypeTraits< FP >::type > restricted_t
Definition: traits.hpp:92
Helper struct to extract types from a derived basis type. Must be specialized for each derived basis ...
Definition: traits.hpp:24
Helper struct to extract the type from a floating point precision enum.
Definition: traits.hpp:77
Helper struct to extract types from a numerical type.
Definition: traits.hpp:35
static constexpr bool is_complex_v
Definition: traits.hpp:39
static constexpr bool from_floating_point_v
Definition: traits.hpp:40
static constexpr bool from_integral_v
Definition: traits.hpp:41
Helper struct to extract whether a type supports certain operations.
Definition: traits.hpp:63
static constexpr bool has_less_v
Definition: traits.hpp:65
static constexpr bool has_equal_v
Definition: traits.hpp:64