Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:28:11

0001 // SPDX-License-Identifier: LGPL-3.0-or-later
0002 // Copyright (C) 2022 Wouter Deconinck, Sylvester Joosten
0003 //
0004 // Type traits used for argument deduction for input and output arguments.
0005 // It allows to distinguish Vector and Optional arguments from regular arguments,
0006 // and to select the appropriate underlying pointer type for each of the argument types.
0007 //
0008 #pragma once
0009 
0010 #include <gsl/gsl>
0011 #include <optional>
0012 #include <type_traits>
0013 #include <vector>
0014 
0015 // Useful type traits for generically translation framework specific algorithms into
0016 // using `algorithms'
0017 
0018 namespace algorithms {
0019 // Make it easy to handle the two special data types for algorithms: std::optional<T>
0020 // and std::vector<R> where needed
0021 template <class T> struct is_vector : std::false_type {};
0022 template <class T, class A> struct is_vector<std::vector<T, A>> : std::true_type {};
0023 template <class T> constexpr bool is_vector_v = is_vector<T>::value;
0024 
0025 template <class T> struct is_optional : std::false_type {};
0026 template <class T> struct is_optional<std::optional<T>> : std::true_type {};
0027 template <class T> constexpr bool is_optional_v = is_optional<T>::value;
0028 
0029 // Get the underlying type for each of the 3 supported cases
0030 template <class T> struct data_type { using type = T; };
0031 template <class T, class A> struct data_type<std::vector<T, A>> { using type = T; };
0032 template <class T> struct data_type<std::optional<T>> { using type = T; };
0033 template <class T> using data_type_t = typename data_type<T>::type;
0034 
0035 // Deduce inptu and output value types
0036 template <class T> struct deduce_type {
0037   using input_type  = gsl::not_null<const T*>;
0038   using output_type = gsl::not_null<T*>;
0039 };
0040 template <class T, class A> struct deduce_type<std::vector<T, A>> {
0041   using input_type  = const std::vector<gsl::not_null<const T*>>;
0042   using output_type = const std::vector<gsl::not_null<T*>>;
0043 };
0044 template <class T> struct deduce_type<std::optional<T>> {
0045   using input_type  = const T*;
0046   using output_type = T*;
0047 };
0048 
0049 template <class T> using input_type_t  = typename deduce_type<T>::input_type;
0050 template <class T> using output_type_t = typename deduce_type<T>::output_type;
0051 
0052 } // namespace algorithms
0053