ATLAS Offline Software
Loading...
Searching...
No Matches
TransientConstSharedPtr.h
Go to the documentation of this file.
1
2/*
3 * Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration.
4 */
5#ifndef GEOMODELUTILITIES_TRANSIENTCONSTSHAREDPTR_H
6#define GEOMODELUTILITIES_TRANSIENTCONSTSHAREDPTR_H
7
8#include <memory>
9namespace GeoModel {
13 template <typename Obj> class TransientConstSharedPtr {
14 public:
15 template <typename DerivType> friend class GeoModel::TransientConstSharedPtr;
17 using element_type = Obj;
19 Obj* get() { return m_ptr.get(); }
20 Obj* operator->() { return get(); }
22 const Obj* get() const { return m_ptr.get(); }
23 const Obj* operator->() const { return get(); }
25 const Obj& operator*() const { return *m_ptr; }
26 Obj& operator*() { return *m_ptr; }
27
29 template <typename DerivType>
30 TransientConstSharedPtr(std::unique_ptr<DerivType> ptr)
31 requires(std::is_base_of_v<Obj, DerivType>): m_ptr{std::move(ptr)} {}
32
33 template <typename DerivType>
34 TransientConstSharedPtr(std::shared_ptr<DerivType> ptr)
35 requires(std::is_base_of_v<Obj, DerivType>) : m_ptr{std::move(ptr)} {}
36
37 TransientConstSharedPtr(Obj* ptr) : m_ptr{ptr} {}
38
42 template <typename DerivType>
44 requires(std::is_base_of_v<Obj, DerivType>) :
45 m_ptr{other.m_ptr}{}
46
47 template <typename DerivType>
49 requires(std::is_base_of_v<Obj, DerivType>) :
50 m_ptr{std::move(other.m_ptr)}{}
51
53 template <typename DerivType>
55 requires(std::is_base_of_v<Obj, DerivType>) {
56 if (&other != this) {
57 m_ptr = other.m_ptr;
58 }
59 return *this;
60 }
61 template <typename DerivType>
62 TransientConstSharedPtr& operator=(const std::shared_ptr<DerivType>& other)
63 requires(std::is_base_of_v<Obj, DerivType>) {
64 m_ptr = other.m_ptr;
65 return *this;
66 }
67
68 template <typename DerivType>
70 requires(std::is_base_of_v<Obj, DerivType>) {
71 m_ptr = std::move(other.m_ptr);
72 return *this;
73 }
74 template <typename DerivType>
75 TransientConstSharedPtr& operator=(std::unique_ptr<DerivType>&& other)
76 requires(std::is_base_of_v<Obj, DerivType>) {
77 m_ptr = std::move(other);
78 return *this;
79 }
80 template <typename DerivType>
81 TransientConstSharedPtr& operator=(std::shared_ptr<DerivType>&& other)
82 requires(std::is_base_of_v<Obj, DerivType>) {
83 m_ptr = std::move(other);
84 return *this;
85 }
86
87 template <typename DerivType>
88 void reset(std::unique_ptr<DerivType> newObj)
89 requires(std::is_base_of_v<Obj, DerivType>) {
90 m_ptr = std::move(newObj);
91 }
92 template <typename DerivType>
93 void reset(std::shared_ptr<DerivType>&& newObj)
94 requires(std::is_base_of_v<Obj, DerivType>) {
95 m_ptr = std::move(newObj);
96 }
97 template <typename DerivType>
98 void reset(const std::shared_ptr<DerivType>& newObj)
99 requires(std::is_base_of_v<Obj, DerivType>) {
100 m_ptr = std::move(newObj);
101 }
102 void reset() { m_ptr.reset(); }
104 std::shared_ptr<const Obj> release() { return std::move(m_ptr); }
106 operator bool() const { return m_ptr.get() != nullptr; }
107 bool operator!() const { return !m_ptr; }
109 size_t use_count() const { return m_ptr.use_count(); }
110
112 bool operator<(const TransientConstSharedPtr& other) const {
113 return get() < other.get();
114 }
115
116 bool operator ==(const TransientConstSharedPtr& other) const {
117 return other.get() == get();
118 }
119 bool operator !=(const TransientConstSharedPtr& other) const {
120 return other.get() != get();
121 }
122
123 private:
124 std::shared_ptr<Obj> m_ptr{};
125 };
126} // namespace GeoModel
127#endif
The TransientConstSharedPtr allows non-const access if the pointer itself is non-const but in the con...
bool operator<(const TransientConstSharedPtr &other) const
Smaller operator to insert the pointer into sets.
TransientConstSharedPtr(Obj *ptr)
Constructor from raw ptr.
TransientConstSharedPtr & operator=(const std::shared_ptr< DerivType > &other)
bool operator!=(const TransientConstSharedPtr &other) const
TransientConstSharedPtr & operator=(const TransientConstSharedPtr< DerivType > &other)
Assignment operator.
void reset(std::unique_ptr< DerivType > newObj)
Overload the pointer.
Obj * get()
Get (non-const) access to the underlying object.
void reset(const std::shared_ptr< DerivType > &newObj)
TransientConstSharedPtr & operator=(TransientConstSharedPtr< DerivType > &&other)
Move assignment operator.
TransientConstSharedPtr()=default
Standard constructor.
TransientConstSharedPtr(std::unique_ptr< DerivType > ptr)
Constructor from a unique_ptr.
const Obj & operator*() const
Dereference operator.
TransientConstSharedPtr(TransientConstSharedPtr< DerivType > &&other)
Standard move constructor.
bool operator==(const TransientConstSharedPtr &other) const
Equal operator.
size_t use_count() const
How many clients does the pointer have.
TransientConstSharedPtr & operator=(std::shared_ptr< DerivType > &&other)
std::shared_ptr< const Obj > release()
Release the memory.
TransientConstSharedPtr & operator=(std::unique_ptr< DerivType > &&other)
const Obj * get() const
Get const access to the underlying object.
TransientConstSharedPtr(std::shared_ptr< DerivType > ptr)
Constructor from shared_ptr.
void reset(std::shared_ptr< DerivType > &&newObj)
TransientConstSharedPtr(const TransientConstSharedPtr< DerivType > &other)
Delete the copy constructor if the object is const.
STL namespace.