TGUI  1.3-dev
Loading...
Searching...
No Matches
CopiedSharedPtr.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2024 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
26#ifndef TGUI_COPIED_SHARED_PTR_HPP
27#define TGUI_COPIED_SHARED_PTR_HPP
28
29
30#include <TGUI/Config.hpp>
31
32
33#if !TGUI_EXPERIMENTAL_USE_STD_MODULE
34 #include <memory>
35#endif
36
38
39TGUI_MODULE_EXPORT namespace tgui
40{
42
43 template <typename WidgetType>
45 {
46 public:
47
48 template <typename... Args>
49 CopiedSharedPtr(Args&&... args) noexcept
50 : m_WidgetPtr{std::make_shared<WidgetType>(std::forward<Args>(args)...)}
51 {
52 }
53
54 CopiedSharedPtr(const CopiedSharedPtr& other) noexcept
55 : m_WidgetPtr{std::make_shared<WidgetType>(*other.m_WidgetPtr)}
56 {
57 }
58
59 CopiedSharedPtr(CopiedSharedPtr&& other) noexcept
60 : m_WidgetPtr{std::move(other.m_WidgetPtr)}
61 {
62 }
63
64 CopiedSharedPtr& operator=(const CopiedSharedPtr& other) noexcept
65 {
66 if (&other != this)
67 m_WidgetPtr = std::make_shared<WidgetType>(*other.m_WidgetPtr);
68
69 return *this;
70 }
71
72 CopiedSharedPtr& operator=(CopiedSharedPtr&& other) noexcept
73 {
74 if (&other != this)
75 m_WidgetPtr = std::move(other.m_WidgetPtr);
76
77 return *this;
78 }
79
80 operator bool() const noexcept
81 {
82 return (m_WidgetPtr != nullptr);
83 }
84
85 WidgetType& operator*() const noexcept
86 {
87 return *m_WidgetPtr;
88 }
89
90 WidgetType* operator->() const noexcept
91 {
92 return m_WidgetPtr.get();
93 }
94
95 TGUI_NODISCARD WidgetType* get() const noexcept
96 {
97 return m_WidgetPtr.get();
98 }
99
101 private:
102
103 std::shared_ptr<WidgetType> m_WidgetPtr;
104 };
105
106
108}
109
111
112#endif // TGUI_COPIED_SHARED_PTR_HPP
Definition CopiedSharedPtr.hpp:45
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:39