algorithms
2.2.0
Framework-independent algorithms for the EIC
Toggle main menu visibility
Loading...
Searching...
No Matches
type_traits.h
Go to the documentation of this file.
1
// SPDX-License-Identifier: LGPL-3.0-or-later
2
// Copyright (C) 2022 Wouter Deconinck, Sylvester Joosten
3
//
4
// Type traits used for argument deduction for input and output arguments.
5
// It allows to distinguish Vector, Optional, and Vector-of-Optional arguments from regular
6
// arguments, and to select the appropriate underlying pointer type for each of the argument types.
7
//
8
#pragma once
9
10
#include <gsl/gsl>
11
#include <optional>
12
#include <type_traits>
13
#include <vector>
14
15
// Useful type traits for generically translation framework specific algorithms into
16
// using `algorithms'
17
18
namespace
algorithms
{
19
// Make it easy to handle the special data types for algorithms: std::optional<T>,
20
// std::vector<R>, and std::vector<std::optional<R>> where needed
21
template
<
class
T>
struct
is_vector
: std::false_type {};
22
template
<
class
T,
class
A>
struct
is_vector
<std::vector<T, A>> : std::true_type {};
23
template
<
class
T>
constexpr
bool
is_vector_v
=
is_vector<T>::value
;
24
25
template
<
class
T>
struct
is_optional
: std::false_type {};
26
template
<
class
T>
struct
is_optional
<std::optional<T>> : std::true_type {};
27
template
<
class
T>
constexpr
bool
is_optional_v
=
is_optional<T>::value
;
28
29
// A std::vector<std::optional<T>> is a vector (is_vector_v is true) whose entries may be absent
30
template
<
class
T>
struct
is_vector_of_optional
: std::false_type {};
31
template
<
class
T,
class
A>
32
struct
is_vector_of_optional
<std::vector<std::optional<T>, A>> : std::true_type {};
33
template
<
class
T>
constexpr
bool
is_vector_of_optional_v
=
is_vector_of_optional<T>::value
;
34
35
// Get the underlying type for each of the 4 supported cases
36
template
<
class
T>
struct
data_type
{
using
type
= T; };
37
template
<
class
T,
class
A>
struct
data_type
<std::vector<T, A>> {
using
type
= T; };
38
template
<
class
T>
struct
data_type
<std::optional<T>> {
using
type
= T; };
39
template
<
class
T,
class
A>
struct
data_type
<std::vector<std::optional<T>, A>> {
using
type
= T; };
40
template
<
class
T>
using
data_type_t
=
typename
data_type<T>::type
;
41
42
// Deduce inptu and output value types
43
template
<
class
T>
struct
deduce_type
{
44
using
input_type
= gsl::not_null<const T*>;
45
using
output_type
= gsl::not_null<T*>;
46
};
47
template
<
class
T,
class
A>
struct
deduce_type
<std::vector<T, A>> {
48
using
input_type
=
const
std::vector<gsl::not_null<const T*>>;
49
using
output_type
=
const
std::vector<gsl::not_null<T*>>;
50
};
51
template
<
class
T>
struct
deduce_type
<std::optional<T>> {
52
using
input_type
=
const
T*;
53
using
output_type
= T*;
54
};
55
template
<
class
T,
class
A>
struct
deduce_type
<std::vector<std::optional<T>, A>> {
56
using
input_type
=
const
std::vector<const T*>;
57
using
output_type
=
const
std::vector<T*>;
58
};
59
60
template
<
class
T>
using
input_type_t
=
typename
deduce_type<T>::input_type
;
61
template
<
class
T>
using
output_type_t
=
typename
deduce_type<T>::output_type
;
62
63
}
// namespace algorithms
64
algorithms
Definition
algorithm.h:52
algorithms::is_vector_v
constexpr bool is_vector_v
Definition
type_traits.h:23
algorithms::data_type_t
typename data_type< T >::type data_type_t
Definition
type_traits.h:40
algorithms::output_type_t
typename deduce_type< T >::output_type output_type_t
Definition
type_traits.h:61
algorithms::is_optional_v
constexpr bool is_optional_v
Definition
type_traits.h:27
algorithms::is_vector_of_optional_v
constexpr bool is_vector_of_optional_v
Definition
type_traits.h:33
algorithms::input_type_t
typename deduce_type< T >::input_type input_type_t
Definition
type_traits.h:60
algorithms::data_type< std::optional< T > >::type
T type
Definition
type_traits.h:38
algorithms::data_type< std::vector< T, A > >::type
T type
Definition
type_traits.h:37
algorithms::data_type< std::vector< std::optional< T >, A > >::type
T type
Definition
type_traits.h:39
algorithms::data_type
Definition
type_traits.h:36
algorithms::data_type::type
T type
Definition
type_traits.h:36
algorithms::deduce_type< std::optional< T > >::output_type
T * output_type
Definition
type_traits.h:53
algorithms::deduce_type< std::optional< T > >::input_type
const T * input_type
Definition
type_traits.h:52
algorithms::deduce_type< std::vector< T, A > >::output_type
const std::vector< gsl::not_null< T * > > output_type
Definition
type_traits.h:49
algorithms::deduce_type< std::vector< T, A > >::input_type
const std::vector< gsl::not_null< const T * > > input_type
Definition
type_traits.h:48
algorithms::deduce_type< std::vector< std::optional< T >, A > >::input_type
const std::vector< const T * > input_type
Definition
type_traits.h:56
algorithms::deduce_type< std::vector< std::optional< T >, A > >::output_type
const std::vector< T * > output_type
Definition
type_traits.h:57
algorithms::deduce_type
Definition
type_traits.h:43
algorithms::deduce_type::output_type
gsl::not_null< T * > output_type
Definition
type_traits.h:45
algorithms::deduce_type::input_type
gsl::not_null< const T * > input_type
Definition
type_traits.h:44
algorithms::is_optional
Definition
type_traits.h:25
algorithms::is_vector_of_optional
Definition
type_traits.h:30
algorithms::is_vector
Definition
type_traits.h:21
core
include
algorithms
type_traits.h
Generated by
1.17.0