Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-06-02 08:17:03

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
0008 
0009 #pragma once
0010 
0011 #include <memory>
0012 #include <utility>
0013 
0014 namespace ActsExamples {
0015 
0016 /// Helper that keeps ROOT branch payload alive via unique_ptr while also
0017 /// providing the raw pointer/pointer-to-pointer interface ROOT expects.
0018 template <typename T>
0019 class RootBranchPtr {
0020  public:
0021   using value_type = T;
0022 
0023   RootBranchPtr() = default;
0024   explicit RootBranchPtr(std::nullptr_t) {}
0025 
0026   RootBranchPtr(const RootBranchPtr&) = delete;
0027   RootBranchPtr& operator=(const RootBranchPtr&) = delete;
0028 
0029   RootBranchPtr(RootBranchPtr&& other) noexcept { *this = std::move(other); }
0030 
0031   RootBranchPtr& operator=(RootBranchPtr&& other) noexcept {
0032     if (this != &other) {
0033       m_storage = std::move(other.m_storage);
0034       m_pointer = m_storage ? m_storage.get() : nullptr;
0035       other.m_pointer = nullptr;
0036     }
0037     return *this;
0038   }
0039 
0040   /// Allocate owned storage if not already available.
0041   void allocate() {
0042     if (!m_storage) {
0043       m_storage = std::make_unique<T>();
0044       m_pointer = m_storage.get();
0045     }
0046   }
0047 
0048   /// Release owned storage and clear the raw pointer.
0049   void reset() {
0050     m_storage.reset();
0051     m_pointer = nullptr;
0052   }
0053 
0054   bool hasValue() const { return m_pointer != nullptr; }
0055 
0056   T& operator*() { return *m_pointer; }
0057   const T& operator*() const { return *m_pointer; }
0058 
0059   T* operator->() { return m_pointer; }
0060   const T* operator->() const { return m_pointer; }
0061 
0062   /// Returns a reference to the managed pointer so callers can take its
0063   /// address when interfacing with ROOT.
0064   T*& get() { return m_pointer; }
0065   T* const& get() const { return m_pointer; }
0066 
0067  private:
0068   std::unique_ptr<T> m_storage = nullptr;
0069   T* m_pointer = nullptr;
0070 };
0071 
0072 }  // namespace ActsExamples