File indexing completed on 2026-06-02 08:17:03
0001
0002
0003
0004
0005
0006
0007
0008
0009 #pragma once
0010
0011 #include <memory>
0012 #include <utility>
0013
0014 namespace ActsExamples {
0015
0016
0017
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
0041 void allocate() {
0042 if (!m_storage) {
0043 m_storage = std::make_unique<T>();
0044 m_pointer = m_storage.get();
0045 }
0046 }
0047
0048
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
0063
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 }