algorithms  2.2.0
Framework-independent algorithms for the EIC
Loading...
Searching...
No Matches
upcast.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <string>
5#include <type_traits>
6
7// Safe type conversions (without narrowing) to be used for properties,
8// gets arround the limitations of type-erasure without explicitly needing
9// to specify template arguments in setProperty etc.
10
11namespace algorithms::detail {
12template <typename T, typename Enable = void> struct upcast_type { using type = T; };
13template <typename UInt>
14struct upcast_type<UInt, std::enable_if_t<std::is_integral_v<UInt> && !std::is_signed_v<UInt>>> {
15 using type = uint64_t;
16};
17template <typename Int>
18struct upcast_type<Int, std::enable_if_t<std::is_integral_v<Int> && std::is_signed_v<Int>>> {
19 using type = int64_t;
20};
21template <typename Enum> struct upcast_type<Enum, std::enable_if_t<std::is_enum_v<Enum>>> {
22 using type = int64_t;
23};
24template <typename Float>
25struct upcast_type<Float, std::enable_if_t<std::is_floating_point_v<Float>>> {
26 using type = double;
27};
28template <typename String>
29struct upcast_type<String, std::enable_if_t<std::is_convertible_v<String, std::string>>> {
30 using type = std::string;
31};
32template <> struct upcast_type<bool> { using type = bool; };
33
34// references and cv-qualifiers are removed first, so lvalues upcast as their value type
35template <class T> using upcast_type_t = typename upcast_type<std::remove_cvref_t<T>>::type;
36
37template <class T> upcast_type_t<T> upcast(T&& value) { return value; }
38
39} // namespace algorithms::detail
40
upcast_type_t< T > upcast(T &&value)
Definition upcast.h:37
typename upcast_type< std::remove_cvref_t< T > >::type upcast_type_t
Definition upcast.h:35