BioDynaMo  v1.03.58-27764645
tuple.h
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------
2 //
3 // Copyright (C) 2021 CERN & University of Surrey for the benefit of the
4 // BioDynaMo collaboration. All Rights Reserved.
5 //
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 //
9 // See the LICENSE file distributed with this work for details.
10 // See the NOTICE file distributed with this work for additional information
11 // regarding copyright ownership.
12 //
13 // -----------------------------------------------------------------------------
14 
15 #ifndef CORE_UTIL_TUPLE_H_
16 #define CORE_UTIL_TUPLE_H_
17 
18 #include <type_traits>
19 #include <utility>
20 
21 namespace bdm {
22 
23 using std::size_t;
24 
25 namespace detail {
26 // Inspiration taken from:
27 // https://stackoverflow.com/questions/21062864/optimal-way-to-access-stdtuple-element-in-runtime-by-index
28 
33 template <typename TTuple, typename TFunction, size_t TIndex>
34 void ApplyImpl(TTuple* t, TFunction&& function) {
35  function(&std::get<TIndex>(*t));
36 }
37 
41 template <typename TTuple, typename TFunction, size_t... TIndices>
42 void Apply(TTuple* t, size_t index, TFunction&& f,
43  std::index_sequence<TIndices...>) {
44  using ApplyImplSignature = void(TTuple*, TFunction &&); // NOLINT
45  // create lookup table that maps runtime index to right ApplyImpl function
46  static constexpr ApplyImplSignature* kLut[] = {
47  &ApplyImpl<TTuple, TFunction, TIndices>...};
48  kLut[index](t, f);
49 }
50 
51 template <typename T, size_t Counter, typename... Types>
52 struct GetIndexImpl;
53 
54 template <typename T, size_t Counter, typename FirstType>
55 struct GetIndexImpl<T, Counter, FirstType> {
56  int static constexpr GetValue() {
57  if (std::is_same<T, FirstType>::value) {
58  return Counter;
59  } else {
60  return -1;
61  }
62  }
63 };
64 
65 template <typename T, size_t Counter, typename FirstType,
66  typename... RemainingTypes>
67 struct GetIndexImpl<T, Counter, FirstType, RemainingTypes...> {
68  int static constexpr GetValue() {
69  // TODO(lukas) after changing to c++17: change to if constexpr (...)
70  if (std::is_same<T, FirstType>::value) {
71  return Counter;
72  } else {
74  }
75  }
76 };
77 
78 } // namespace detail
79 
87 template <typename TTuple, typename TFunction>
88 void Apply(TTuple* t, size_t index, TFunction&& f) {
89  detail::Apply(t, index, f,
90  std::make_index_sequence<std::tuple_size<TTuple>::value>());
91 }
92 
95 template <typename T, typename... Types>
96 inline constexpr int GetIndex() {
97  return bdm::detail::template GetIndexImpl<T, 0, Types...>::GetValue();
98 }
99 
100 } // namespace bdm
101 
102 #endif // CORE_UTIL_TUPLE_H_
bdm::detail::GetIndexImpl< T, Counter, FirstType, RemainingTypes... >::GetValue
static constexpr int GetValue()
Definition: tuple.h:68
bdm
Definition: agent.cc:39
bdm::detail::Apply
void Apply(TTuple *t, size_t index, TFunction &&f, std::index_sequence< TIndices... >)
Definition: tuple.h:42
bdm::Apply
void Apply(TTuple *t, size_t index, TFunction &&f)
Definition: tuple.h:88
bdm::detail::ApplyImpl
void ApplyImpl(TTuple *t, TFunction &&function)
Definition: tuple.h:34
bdm::detail::GetIndexImpl
Definition: tuple.h:52
bdm::GetIndex
constexpr int GetIndex()
Definition: tuple.h:96
bdm::detail::GetIndexImpl< T, Counter, FirstType >::GetValue
static constexpr int GetValue()
Definition: tuple.h:56