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