Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SourceLink.hpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file SourceLink.hpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2021 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
9 #pragma once
10 
12 #include "Acts/Utilities/Any.hpp"
15 
16 #include <cassert>
17 #include <iostream>
18 #include <type_traits>
19 #include <utility>
20 
21 #if !defined(ACTS_SOURCELINK_SBO_SIZE)
22 // Do not set this in code, use CMake!
23 #define ACTS_SOURCELINK_SBO_SIZE 16
24 #endif
25 
26 namespace Acts {
27 
28 class SourceLink final {
30 
31  public:
32  SourceLink(const SourceLink& other) = default;
33  SourceLink(SourceLink&& other) = default;
34  SourceLink& operator=(const SourceLink& other) = default;
35  SourceLink& operator=(SourceLink&& other) = default;
36 
40  template <typename T, typename = std::enable_if_t<
41  !std::is_same_v<std::decay_t<T>, SourceLink>>>
42  explicit SourceLink(T&& upstream) {
43  static_assert(!std::is_same_v<std::decay_t<T>, SourceLink>,
44  "Cannot wrap SourceLink in SourceLink");
45 
46  if constexpr (std::is_same_v<T, std::decay_t<T>>) {
47  m_upstream = any_type{std::move(upstream)};
48  } else {
49  m_upstream = any_type{static_cast<std::decay_t<T>>(upstream)};
50  }
51  }
52 
56  template <typename T>
57  T& get() {
58  return m_upstream.as<T>();
59  }
60 
64  template <typename T>
65  const T& get() const {
66  return m_upstream.as<T>();
67  }
68 
69  private:
71 };
72 
73 template <typename T>
75  using BaseIterator = T;
76 
77  using iterator_category = typename BaseIterator::iterator_category;
79  using difference_type = typename BaseIterator::difference_type;
80  using pointer = typename BaseIterator::pointer;
81  using reference = typename BaseIterator::reference;
82 
83  explicit SourceLinkAdapterIterator(T iterator) : m_iterator{iterator} {}
84 
85  SourceLinkAdapterIterator& operator++() {
86  ++m_iterator;
87  return *this;
88  }
89 
90  bool operator==(const SourceLinkAdapterIterator& other) const {
91  return m_iterator == other.m_iterator;
92  }
93 
94  bool operator!=(const SourceLinkAdapterIterator& other) const {
95  return !(*this == other);
96  }
97 
98  Acts::SourceLink operator*() const { return Acts::SourceLink{*m_iterator}; }
99 
100  auto operator-(const SourceLinkAdapterIterator& other) const {
101  return m_iterator - other.m_iterator;
102  }
103 
104  BaseIterator m_iterator;
105 };
106 
109 
110 } // namespace Acts