Geant4 11.2.2
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
Globals.hh
Go to the documentation of this file.
1//
2// MIT License
3// Copyright (c) 2020 Jonathan R. Madsen
4// Permission is hereby granted, free of charge, to any person obtaining a copy
5// of this software and associated documentation files (the "Software"), to deal
6// in the Software without restriction, including without limitation the rights
7// to use, copy, modify, merge, publish, distribute, sublicense, and
8// copies of the Software, and to permit persons to whom the Software is
9// furnished to do so, subject to the following conditions:
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED
12// "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
13// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
15// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
16// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
17// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18//
19
20#pragma once
21
22#include <algorithm> // Retrieve definitions of min/max
23
24// Include base types
25#include "PTL/Types.hh"
26
27// Global utility functions
28#include "PTL/Utility.hh"
29
30#include <initializer_list>
31#include <tuple>
32#include <type_traits>
33#include <utility>
34
35#if !defined(PTL_NO_SANITIZE_THREAD)
36// expect that sanitizer is from compiler which supports __has_attribute
37# if defined(__has_attribute)
38# if __has_attribute(no_sanitize)
39# define PTL_NO_SANITIZE_THREAD __attribute__((no_sanitize("thread")))
40# else
41# define PTL_NO_SANITIZE_THREAD
42# endif
43# elif defined(__clang__) || defined(__GNUC__)
44# define PTL_NO_SANITIZE_THREAD __attribute__((no_sanitize("thread")))
45# else
46// otherwise, make blank
47# define PTL_NO_SANITIZE_THREAD
48# endif
49#endif
50
51namespace PTL
52{
53template <typename T>
54using decay_t = typename std::decay<T>::type;
55
56template <bool B, typename T = void>
57using enable_if_t = typename std::enable_if<B, T>::type;
58
59// for pre-C++14 tuple expansion to arguments
60namespace mpl
61{
62//--------------------------------------------------------------------------------------//
63
64namespace impl
65{
66//--------------------------------------------------------------------------------------//
67// Stores a tuple of indices. Used by tuple and pair, and by bind() to
68// extract the elements in a tuple.
69template <size_t... Indexes>
71{};
72
73// Concatenates two Index_tuples.
74template <typename Itup1, typename Itup2>
75struct Itup_cat;
76
77template <size_t... Ind1, size_t... Ind2>
78struct Itup_cat<Index_tuple<Ind1...>, Index_tuple<Ind2...>>
79{
80 using __type = Index_tuple<Ind1..., (Ind2 + sizeof...(Ind1))...>;
81};
82
83// Builds an Index_tuple<0, 1, 2, ..., NumT-1>.
84template <size_t NumT>
86: Itup_cat<typename Build_index_tuple<NumT / 2>::__type,
87 typename Build_index_tuple<NumT - NumT / 2>::__type>
88{};
89
90template <>
92{
94};
95
96template <>
98{
100};
101
102/// Class template integer_sequence
103template <typename Tp, Tp... Idx>
105{
106 using value_type = Tp;
107 static constexpr size_t size() noexcept { return sizeof...(Idx); }
108};
109
110template <typename Tp, Tp NumT, typename ISeq = typename Build_index_tuple<NumT>::__type>
112
113template <typename Tp, Tp NumT, size_t... Idx>
114struct Make_integer_sequence<Tp, NumT, Index_tuple<Idx...>>
115{
116 static_assert(NumT >= 0, "Cannot make integer sequence of negative length");
117
119};
120
121/// Alias template make_integer_sequence
122template <typename Tp, Tp NumT>
124
125/// Alias template index_sequence
126template <size_t... Idx>
127using index_sequence = integer_sequence<size_t, Idx...>;
128
129/// Alias template make_index_sequence
130template <size_t NumT>
132
133/// Alias template index_sequence_for
134template <typename... Types>
135using index_sequence_for = make_index_sequence<sizeof...(Types)>;
136
137template <size_t Idx, typename Tup>
138using index_type_t = decay_t<decltype(std::get<Idx>(std::declval<Tup>()))>;
139
140template <typename FnT, typename TupleT, size_t... Idx>
141static inline auto
142apply(FnT&& _func, TupleT _args, impl::index_sequence<Idx...>)
143 -> decltype(std::forward<FnT>(_func)(std::get<Idx>(std::move(_args))...))
144{
145 // GCC 5.3 warns about unused variable _args when the index sequence is empty
146#if defined(__GNUC__) && (__GNUC__ < 6)
147 if(sizeof...(Idx) == 0)
148 {
149 ConsumeParameters(_args);
150 }
151#endif
152 return std::forward<FnT>(_func)(std::get<Idx>(std::move(_args))...);
153}
154
155//--------------------------------------------------------------------------------------//
156
157} // namespace impl
158
159//--------------------------------------------------------------------------------------//
160
161/// Alias template index_sequence
162template <size_t... Idx>
164
165/// Alias template make_index_sequence
166template <size_t NumT>
168
169/// Alias template index_sequence_for
170template <typename... Types>
172
173template <typename FnT, typename TupleT>
174static inline void
175apply(FnT&& _func, TupleT&& _args)
176{
177 using tuple_type = typename std::decay<TupleT>::type;
178 constexpr auto N = std::tuple_size<tuple_type>::value;
179 impl::apply(std::forward<FnT>(_func), std::forward<TupleT>(_args),
181}
182
183//--------------------------------------------------------------------------------------//
184
185} // namespace mpl
186
187} // namespace PTL
#define N
Definition crc32.c:57
decay_t< decltype(std::get< Idx >(std::declval< Tup >()))> index_type_t
Definition Globals.hh:138
make_integer_sequence< size_t, NumT > make_index_sequence
Alias template make_index_sequence.
Definition Globals.hh:131
typename Make_integer_sequence< Tp, NumT >::__type make_integer_sequence
Alias template make_integer_sequence.
Definition Globals.hh:123
make_index_sequence< sizeof...(Types)> index_sequence_for
Alias template index_sequence_for.
Definition Globals.hh:135
impl::make_integer_sequence< size_t, NumT > make_index_sequence
Alias template make_index_sequence.
Definition Globals.hh:167
impl::make_index_sequence< sizeof...(Types)> index_sequence_for
Alias template index_sequence_for.
Definition Globals.hh:171
void ConsumeParameters(Args &&...)
Definition Utility.hh:44
typename std::enable_if< B, T >::type enable_if_t
Definition Globals.hh:57
typename std::decay< T >::type decay_t
Definition Globals.hh:54
Class template integer_sequence.
Definition Globals.hh:105
static constexpr size_t size() noexcept
Definition Globals.hh:107