ATLAS Offline Software
Loading...
Searching...
No Matches
RefCountedPtr.h
Go to the documentation of this file.
1// This file's extension implies that it's C, but it's really -*- C++ -*-.
2/*
3 * Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration.
4 */
11
12
13#ifndef CXXUTILS_REFCOUNTEDPTR_H
14#define CXXUTILS_REFCOUNTEDPTR_H
15
16
17#include "CxxUtils/concepts.h"
18#include <memory> // For unique_ptr
19
20
21namespace CxxUtils {
22
23
37template <CxxUtils::detail::RefCounted T>
39{
40public:
41 using element_type = T;
42
44 RefCountedPtr() = default;
45
48
50 template <CxxUtils::detail::RefCounted OTHER>
51 requires std::convertible_to<OTHER*, T*>
52 RefCountedPtr (std::unique_ptr<OTHER>&& other);
53
56
59
61 template <CxxUtils::detail::RefCounted OTHER>
62 requires std::convertible_to<OTHER*, T*>
64
66 template <CxxUtils::detail::RefCounted OTHER>
67 requires std::convertible_to<OTHER*, T*>
69
72
75
78
80 template <CxxUtils::detail::RefCounted OTHER>
81 requires std::convertible_to<OTHER*, T*>
83
85 template <CxxUtils::detail::RefCounted OTHER>
86 requires std::convertible_to<OTHER*, T*>
88
91
93 void reset (T* ptr = nullptr);
94
96 bool isValid() const;
97
99 explicit operator bool() const;
100
102 bool operator!() const;
103
105 T* get();
106 const T* get() const;
107
109 operator T*();
110 operator const T*() const;
111
114 const T* operator->() const;
115
118 const T& operator*() const;
119
120
121private:
122 template <CxxUtils::detail::RefCounted OTHER> friend class RefCountedPtr;
123
124 // Return the pointer and give up ownership.
126
128 T* m_ptr = nullptr;
129};
130
131
132} // namespace CxxUtils
133
134
136
137
138#endif // not CXXUTILS_REFCOUNTEDPTR_H
RefCountedPtr(RefCountedPtr< OTHER > &&other)
Move from another RefCountedPtr, with a different type.
const T * operator->() const
T & operator*()
Dereference.
bool isValid() const
Check if the pointer is valid.
RefCountedPtr & operator=(const RefCountedPtr &other)
Assignment.
RefCountedPtr()=default
Default constructor.
void reset(T *ptr=nullptr)
Change the pointer.
RefCountedPtr(const RefCountedPtr< OTHER > &other)
Constructor from another RefCountedPtr, with a different type.
~RefCountedPtr()
Destructor.
const T & operator*() const
bool operator!() const
Check if the pointer is invalid.
RefCountedPtr(T *p)
Constructor from pointer. Adds to the refcount.
RefCountedPtr(std::unique_ptr< OTHER > &&other)
Constructor from unique_ptr. Adds to the refcount.
const T * get() const
T * get()
Get the pointer.
T * operator->()
Dereference.
RefCountedPtr(const RefCountedPtr &other)
Copy constructor.
RefCountedPtr(RefCountedPtr &&other)
Move constructor.
A couple standard-library related concepts.