Geant4-11
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#ifndef FALSE
23# define FALSE 0
24#endif
25
26#ifndef TRUE
27# define TRUE 1
28#endif
29
30#include <algorithm> // Retrieve definitions of min/max
31
32// Include base types
33#include "PTL/Types.hh"
34
35// Global utility functions
36#include "PTL/Utility.hh"
37
38#include <initializer_list>
39#include <tuple>
40#include <type_traits>
41#include <utility>
42
43#if !defined(PTL_FOLD_EXPRESSION)
44# define PTL_FOLD_EXPRESSION(...) \
45 ::PTL::mpl::consume_parameters( \
46 ::std::initializer_list<int>{ (__VA_ARGS__, 0)... })
47#endif
48
49#if !defined(PTL_NO_SANITIZE_THREAD)
50// expect that sanitizer is from compiler which supports __has_attribute
51# if defined(__has_attribute)
52# if __has_attribute(no_sanitize)
53# define PTL_NO_SANITIZE_THREAD __attribute__((no_sanitize("thread")))
54# else
55# define PTL_NO_SANITIZE_THREAD
56# endif
57# elif defined(__clang__) || defined(__GNUC__)
58# define PTL_NO_SANITIZE_THREAD __attribute__((no_sanitize("thread")))
59# else
60// otherwise, make blank
61# define PTL_NO_SANITIZE_THREAD
62# endif
63#endif
64
65namespace PTL
66{
67template <typename T>
68using decay_t = typename std::decay<T>::type;
69
70template <bool B, typename T = void>
71using enable_if_t = typename std::enable_if<B, T>::type;
72
73// for pre-C++14 tuple expansion to arguments
74namespace mpl
75{
76//--------------------------------------------------------------------------------------//
77
78template <typename... Args>
79void
81{}
82
83//--------------------------------------------------------------------------------------//
84
85namespace impl
86{
87//--------------------------------------------------------------------------------------//
88// Stores a tuple of indices. Used by tuple and pair, and by bind() to
89// extract the elements in a tuple.
90template <size_t... Indexes>
92{};
93
94// Concatenates two Index_tuples.
95template <typename Itup1, typename Itup2>
96struct Itup_cat;
97
98template <size_t... Ind1, size_t... Ind2>
99struct Itup_cat<Index_tuple<Ind1...>, Index_tuple<Ind2...>>
100{
101 using __type = Index_tuple<Ind1..., (Ind2 + sizeof...(Ind1))...>;
102};
103
104// Builds an Index_tuple<0, 1, 2, ..., NumT-1>.
105template <size_t NumT>
107: Itup_cat<typename Build_index_tuple<NumT / 2>::__type,
108 typename Build_index_tuple<NumT - NumT / 2>::__type>
109{};
110
111template <>
113{
115};
116
117template <>
119{
121};
122
124template <typename Tp, Tp... Idx>
126{
127 typedef Tp value_type;
128 static constexpr size_t size() noexcept { return sizeof...(Idx); }
129};
130
131template <typename Tp, Tp NumT, typename ISeq = typename Build_index_tuple<NumT>::__type>
133
134template <typename Tp, Tp NumT, size_t... Idx>
135struct Make_integer_sequence<Tp, NumT, Index_tuple<Idx...>>
136{
137 static_assert(NumT >= 0, "Cannot make integer sequence of negative length");
138
140};
141
143template <typename Tp, Tp NumT>
145
147template <size_t... Idx>
148using index_sequence = integer_sequence<size_t, Idx...>;
149
151template <size_t NumT>
153
155template <typename... Types>
156using index_sequence_for = make_index_sequence<sizeof...(Types)>;
157
158template <size_t Idx, typename Tup>
159using index_type_t = decay_t<decltype(std::get<Idx>(std::declval<Tup>()))>;
160
161template <typename FnT, typename TupleT, size_t... Idx>
162static inline auto
163apply(FnT&& _func, TupleT _args, impl::index_sequence<Idx...>)
164 -> decltype(std::forward<FnT>(_func)(std::get<Idx>(std::move(_args))...))
165{
166 // GCC 5.3 warns about unused variable _args when the index sequence is empty
167#if defined(__GNUC__) && (__GNUC__ < 6)
168 if(sizeof...(Idx) == 0)
169 {
170 consume_parameters(_args);
171 }
172#endif
173 return std::forward<FnT>(_func)(std::get<Idx>(std::move(_args))...);
174}
175
176//--------------------------------------------------------------------------------------//
177
178} // namespace impl
179
180//--------------------------------------------------------------------------------------//
181
183template <size_t... Idx>
185
187template <size_t NumT>
189
191template <typename... Types>
193
194template <typename FnT, typename TupleT>
195static inline void
196apply(FnT&& _func, TupleT&& _args)
197{
198 using tuple_type = typename std::decay<TupleT>::type;
199 constexpr auto N = std::tuple_size<tuple_type>::value;
200 impl::apply(std::forward<FnT>(_func), std::forward<TupleT>(_args),
202}
203
204//--------------------------------------------------------------------------------------//
205
206} // namespace mpl
207
208namespace thread_pool
209{
210namespace state
211{
212static const short STARTED = 0;
213static const short PARTIAL = 1;
214static const short STOPPED = 2;
215static const short NONINIT = 3;
216
217} // namespace state
218} // namespace thread_pool
219
220//--------------------------------------------------------------------------------------//
221
222} // namespace PTL
typename Make_integer_sequence< Tp, NumT >::__type make_integer_sequence
Alias template make_integer_sequence.
Definition: Globals.hh:144
make_integer_sequence< size_t, NumT > make_index_sequence
Alias template make_index_sequence.
Definition: Globals.hh:152
decay_t< decltype(std::get< Idx >(std::declval< Tup >()))> index_type_t
Definition: Globals.hh:159
make_index_sequence< sizeof...(Types)> index_sequence_for
Alias template index_sequence_for.
Definition: Globals.hh:156
static auto apply(FnT &&_func, TupleT _args, impl::index_sequence< Idx... >) -> decltype(std::forward< FnT >(_func)(std::get< Idx >(std::move(_args))...))
Definition: Globals.hh:163
void consume_parameters(Args &&...)
Definition: Globals.hh:80
static void apply(FnT &&_func, TupleT &&_args)
Definition: Globals.hh:196
impl::make_integer_sequence< size_t, NumT > make_index_sequence
Alias template make_index_sequence.
Definition: Globals.hh:188
impl::make_index_sequence< sizeof...(Types)> index_sequence_for
Alias template index_sequence_for.
Definition: Globals.hh:192
static const short PARTIAL
Definition: Globals.hh:213
static const short NONINIT
Definition: Globals.hh:215
static const short STOPPED
Definition: Globals.hh:214
static const short STARTED
Definition: Globals.hh:212
Definition: AutoLock.hh:254
typename std::decay< T >::type decay_t
Definition: Globals.hh:68
typename std::enable_if< B, T >::type enable_if_t
Definition: Globals.hh:71
integer_sequence< Tp, static_cast< Tp >(Idx)... > __type
Definition: Globals.hh:137
Class template integer_sequence.
Definition: Globals.hh:126
static constexpr size_t size() noexcept
Definition: Globals.hh:128