Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SmallObjectCache.hpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file SmallObjectCache.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 
11 #include <array>
12 #include <cassert>
13 #include <cstddef>
14 #include <memory>
15 
16 namespace Acts {
17 
18 namespace detail {
21  public:
22  template <typename T, typename... Args>
23  static SmallObjectCache make(Args&&... args) {
24  SmallObjectCache cache{};
25 
26  static_assert(std::is_same_v<T, std::decay_t<T>>,
27  "Please pass the raw type, no const or ref");
28  static_assert(sizeof(T) <= sizeof(cache.m_data),
29  "Passed type is too large");
30  static_assert(
31  std::is_move_assignable_v<T> && std::is_move_constructible_v<T>,
32  "Type needs to be move assignable and move constructible");
33 
34  /*T* ptr =*/new (cache.m_data.data()) T(std::forward<Args>(args)...);
35  static const Handler<T> static_handler{};
36  cache.m_handler = &static_handler;
37 
38  return cache;
39  }
40 
41  template <typename T>
42  T& get() {
43  static_assert(std::is_same_v<T, std::decay_t<T>>,
44  "Please pass the raw type, no const or ref");
45  static_assert(sizeof(T) <= sizeof(m_data), "Passed type is too large");
46  return *reinterpret_cast<T*>(m_data.data());
47  }
48 
49  template <typename T>
50  const T& get() const {
51  static_assert(std::is_same_v<T, std::decay_t<T>>,
52  "Please pass the raw type, no const or ref");
53  static_assert(sizeof(T) <= sizeof(m_data), "Passed type is too large");
54  return *reinterpret_cast<const T*>(m_data.data());
55  }
56 
58  assert(m_handler && "Handler cannot be dead");
59  m_handler->destroy(m_data.data());
60  }
61 
63  m_handler = other.m_handler;
64  assert(m_handler && "Handler is null");
65  m_handler->moveConstruct(other.m_data.data(), m_data.data());
66  }
67 
69  m_handler = other.m_handler;
70  assert(m_handler && "Handler is null");
71  m_handler->move(other.m_data.data(), m_data.data());
72  return *this;
73  }
74 
75  private:
76  SmallObjectCache() = default;
77 
78  struct HandlerBase {
79  virtual void destroy(void* ptr) const = 0;
80  virtual void moveConstruct(void* from, void* to) const = 0;
81  virtual void move(void* from, void* to) const = 0;
82  virtual ~HandlerBase() = default;
83  };
84 
85  template <typename T>
86  struct Handler final : public HandlerBase {
87  void destroy(void* ptr) const override {
88  assert(ptr != nullptr && "Address to destroy is nullptr");
89  T* obj = static_cast<T*>(ptr);
90  obj->~T();
91  }
92 
93  void moveConstruct(void* from, void* to) const override {
94  assert(from != nullptr && "Source is null");
95  assert(to != nullptr && "Target is null");
96  T* fromValue = static_cast<T*>(from);
97  /*T* ptr =*/new (to) T(std::move(*fromValue));
98  }
99 
100  void move(void* from, void* to) const override {
101  assert(from != nullptr && "Source is null");
102  assert(to != nullptr && "Target is null");
103 
104  T* fromValue = static_cast<T*>(from);
105  T* toValue = static_cast<T*>(to);
106 
107  (*toValue) = std::move(*fromValue);
108  }
109  };
110 
111  alignas(std::max_align_t) std::array<char, 512> m_data{};
112  const HandlerBase* m_handler{nullptr};
113 };
114 
115 } // namespace detail
116 } // namespace Acts