TGUI 1.11
Loading...
Searching...
No Matches
CopiedPtr.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2025 Bruno Van de Velde (vdv_b@tgui.eu)
5//
6// This software is provided 'as-is', without any express or implied warranty.
7// In no event will the authors be held liable for any damages arising from the use of this software.
8//
9// Permission is granted to anyone to use this software for any purpose,
10// including commercial applications, and to alter it and redistribute it freely,
11// subject to the following restrictions:
12//
13// 1. The origin of this software must not be misrepresented;
14// you must not claim that you wrote the original software.
15// If you use this software in a product, an acknowledgment
16// in the product documentation would be appreciated but is not required.
17//
18// 2. Altered source versions must be plainly marked as such,
19// and must not be misrepresented as being the original software.
20//
21// 3. This notice may not be removed or altered from any source distribution.
22//
24
25#ifndef TGUI_COPIED_PTR_HPP
26#define TGUI_COPIED_PTR_HPP
27
29
30#include <TGUI/Config.hpp>
31
32#include <memory>
33
35
36namespace tgui
37{
42 struct CopiedPtrEmplaceTag { explicit CopiedPtrEmplaceTag() = default; };
43
50 template <typename T>
51 class CopiedPtr
52 {
53 public:
54
59 {
60 }
61
65 CopiedPtr(std::nullptr_t)
66 {
67 }
68
74 template <typename... Args>
75 CopiedPtr(CopiedPtrEmplaceTag, Args&&... args) :
76 m_ptr(std::make_unique<T>(args...)),
77 m_cloner([](const void* val) -> void* { return new T(*static_cast<const T*>(val)); })
78 {
79 }
80
84 CopiedPtr(const CopiedPtr& other) :
85 m_ptr(other.m_ptr ? static_cast<T*>(other.m_cloner(other.m_ptr.get())) : nullptr),
86 m_cloner(other.m_cloner)
87 {
88 }
89
93 template <typename U>
94 CopiedPtr(const CopiedPtr<U>& other) :
95 m_ptr(other.m_ptr ? static_cast<T*>(other.m_cloner(other.m_ptr.get())) : nullptr),
96 m_cloner(other.m_cloner)
97 {
98 }
99
103 CopiedPtr(CopiedPtr&& other) :
104 m_ptr(std::move(other.m_ptr)),
105 m_cloner(std::move(other.m_cloner))
106 {
107 }
108
112 template <typename U>
113 CopiedPtr(CopiedPtr&& other) :
114 m_ptr(std::move(other.m_ptr)),
115 m_cloner(std::move(other.m_cloner))
116 {
117 }
118
122 void swap(CopiedPtr& other)
123 {
124 std::swap(m_ptr, other.m_ptr);
125 std::swap(m_cloner, other.m_cloner);
126 }
127
131 CopiedPtr& operator=(const CopiedPtr& other)
132 {
133 if (this != &other)
134 CopiedPtr(other).swap(*this);
135 return *this;
136 }
137
141 template <typename U>
142 CopiedPtr& operator=(const CopiedPtr<U>& other)
143 {
144 CopiedPtr(other).swap(*this);
145 return *this;
146 }
147
151 CopiedPtr& operator=(CopiedPtr&& other)
152 {
153 if (this != &other)
154 CopiedPtr(std::move(other)).swap(*this);
155 return *this;
156 }
157
161 template <typename U>
162 CopiedPtr& operator=(CopiedPtr<U>&& other)
163 {
164 CopiedPtr(std::move(other)).swap(*this);
165 return *this;
166 }
167
172 {
173 return *m_ptr;
174 }
175
179 T* operator->() const
180 {
181 return m_ptr.get();
182 }
183
187 operator bool() const
188 {
189 return m_ptr != nullptr;
190 }
191
195 T* get() const
196 {
197 return m_ptr.get();
198 }
199
203 void reset()
204 {
205 m_ptr.reset();
206 m_cloner = nullptr;
207 }
208
209 private:
210 std::unique_ptr<T> m_ptr;
211
212 // When m_ptr contains a pointer to a base class, we still need some way to copy the derived class
213 void*(*m_cloner)(const void*) = nullptr;
214
215 // The copy constructor needs access to classes with a different template parameter
216 template <typename U>
217 friend class CopiedPtr;
218 };
219
223 template <typename T, typename... Args>
224 CopiedPtr<T> makeCopied(Args&&... args)
225 {
226 return CopiedPtr<T>(CopiedPtrEmplaceTag{}, std::forward<Args>(args)...);
227 }
228
230}
231
233
234#endif // TGUI_COPIED_PTR_HPP
Copyable smart pointer template.
Definition CopiedPtr.hpp:52
CopiedPtr & operator=(const CopiedPtr< U > &other)
Copy assignment operator for a pointer to a derived object.
Definition CopiedPtr.hpp:142
CopiedPtr & operator=(CopiedPtr< U > &&other)
Move assignment operator for a pointer to a derived object.
Definition CopiedPtr.hpp:162
CopiedPtr()
Default constructor that initializes the object to a nullptr.
Definition CopiedPtr.hpp:58
T * operator->() const
Dereferences the pointer for member access.
Definition CopiedPtr.hpp:179
CopiedPtr(CopiedPtr &&other)
Move constructor that takes a pointer to a derived object.
Definition CopiedPtr.hpp:113
CopiedPtr(std::nullptr_t)
Constructor that accepts a nullptr as parameter.
Definition CopiedPtr.hpp:65
CopiedPtr(CopiedPtr &&other)
Move constructor.
Definition CopiedPtr.hpp:103
CopiedPtr(const CopiedPtr< U > &other)
Copy constructor that takes a pointer to a derived object.
Definition CopiedPtr.hpp:94
CopiedPtr & operator=(const CopiedPtr &other)
Copy assignment operator.
Definition CopiedPtr.hpp:131
void reset()
Reset the pointer to a nullptr.
Definition CopiedPtr.hpp:203
CopiedPtr(CopiedPtrEmplaceTag, Args &&... args)
Constructor that creates a new object.
Definition CopiedPtr.hpp:75
CopiedPtr(const CopiedPtr &other)
Copy constructor.
Definition CopiedPtr.hpp:84
CopiedPtr & operator=(CopiedPtr &&other)
Move assignment operator.
Definition CopiedPtr.hpp:151
T & operator*()
Dereferences the pointer.
Definition CopiedPtr.hpp:171
T * get() const
Returns a raw pointer to the underlying object.
Definition CopiedPtr.hpp:195
void swap(CopiedPtr &other)
Exchanges the values of *this and other.
Definition CopiedPtr.hpp:122
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:36
CopiedPtr< T > makeCopied(Args &&... args)
Creates a CopiedPtr object that contains a value.
Definition CopiedPtr.hpp:224
Definition CopiedPtr.hpp:42