Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Holders.hpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file Holders.hpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2023 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 <utility>
12 
13 namespace Acts::detail {
14 
16 template <typename T>
17 struct RefHolder {
18  T* ptr;
19 
20  explicit RefHolder(T* _ptr) : ptr{_ptr} {}
21  explicit RefHolder(T& ref) : ptr{&ref} {}
22 
23  const T& operator*() const { return *ptr; }
24  T& operator*() { return *ptr; }
25 
26  const T* operator->() const { return ptr; }
27  T* operator->() { return ptr; }
28 };
29 
32 template <typename T>
34  const T* ptr;
35 
36  explicit ConstRefHolder(const T* _ptr) : ptr{_ptr} {}
37  explicit ConstRefHolder(const T& ref) : ptr{&ref} {}
38 
39  const T& operator*() const { return *ptr; }
40 
41  const T* operator->() const { return ptr; }
42 };
43 
45 template <typename T>
46 struct ValueHolder {
47  T val;
48 
49  // Let's be clear with the user that we take the ownership
50  // Only require rvalues and avoid hidden copies
51  ValueHolder(T& _val) = delete;
52  // @FIXME: Ideally we want this to be explicit, but cannot be explicit,
53  // because using an explicit constructor and a deduction guide leads to
54  // a SEGFAULT in GCC11 (an up?). Re-evaluate down the line
55  /* explicit */ ValueHolder(T&& _val) : val{std::move(_val)} {}
56 
57  // Does it make sense to allow copy operations?
58 
59  const T& operator*() const { return val; }
60  T& operator*() { return val; }
61 
62  const T* operator->() const { return &val; }
63  T* operator->() { return &val; }
64 };
65 
66 } // namespace Acts::detail