bux API Reference 1.9.0
Static library of whatever are seen required in general purpose but not directly supported from Modern C++. Or whatever reusable originated from my side projects.
Loading...
Searching...
No Matches
XAutoPtr.h
Go to the documentation of this file.
1#pragma once
2
3#include <new> // placement new
4#include <utility> // std::forward<>
5
6namespace bux {
7
8//
9// Types
10//
11template<class T>
13{
14public:
15
16 // Nonvirtuals
17 constexpr C_AutoNode() noexcept: m_Ptr(nullptr), m_Owned(false) {}
18 constexpr C_AutoNode(T *p, bool owned) noexcept: m_Ptr(p), m_Owned(owned) {}
19 explicit C_AutoNode(C_AutoNode &another) noexcept:
20 m_Ptr(another.m_Ptr), m_Owned(another.m_Owned) { another.m_Owned = false; }
21 template<class T2>
22 explicit C_AutoNode(C_AutoNode<T2> &another) noexcept:
23 m_Ptr(static_cast<T*>(another.m_Ptr)), m_Owned(another.m_Owned) { another.m_Owned = false; }
25 void operator=(C_AutoNode &another);
26 template<class T2>
27 void operator=(C_AutoNode<T2> &another);
28 operator bool() const noexcept { return m_Ptr != nullptr; }
29 template<class T2>
30 operator T2*() const noexcept { return dynamic_cast<T2*>(m_Ptr); }
31 T *operator->() const noexcept { return m_Ptr; }
32 T **operator&() noexcept { assign(nullptr,true); return &m_Ptr; }
33 T &operator*() const noexcept { return *m_Ptr; }
34 void assign(T *ptr, bool owned);
35 void reset(T *ptr) noexcept { assign(ptr, true); }
36 void clear() noexcept { assign(nullptr,false); }
37 T *disown() noexcept;
38 T *get() const noexcept { return m_Ptr; }
39 bool owned() const noexcept { return m_Owned; }
40 void swap(C_AutoNode &another) noexcept ;
41 template<class T2>
42 bool takeOver(C_AutoNode<T2> &another);
43
44private:
45
46 // Data
47 T *m_Ptr;
48 bool m_Owned;
49
50 // Friend of my kind
51 template<class T2> friend class C_AutoNode;
52};
53
54struct C_Void {};
55
56template<class T>
57class C_NewNode: public C_AutoNode<T>
58{
59protected:
60 constexpr C_NewNode(C_Void) noexcept: C_AutoNode<T>(nullptr,false) {}
61
62public:
63 C_NewNode(): C_AutoNode<T>(new T, true) {}
64 template<class...T_Args>
65 C_NewNode(T_Args&&...args): C_AutoNode<T>(new T{std::forward<T_Args>(args)...}, true) {}
66};
67
68//
69// Implementation of Class Templates
70//
71template<class T>
73{
74 if (m_Owned)
75 delete m_Ptr;
76}
77
78template<class T>
80{
81 this->~C_AutoNode<T>();
82 new(this)C_AutoNode(another);
83}
84
85template<class T>
86template<class T2>
88{
89 this->~C_AutoNode<T>();
90 new(this)C_AutoNode(another);
91}
92
93template<class T>
94void C_AutoNode<T>::assign(T *ptr, bool owned)
95{
96 this->~C_AutoNode<T>();
97 new(this)C_AutoNode(ptr, owned);
98}
99
100template<class T>
102{
103 if (m_Owned)
104 {
105 m_Owned = false;
106 return m_Ptr;
107 }
108 return nullptr;
109}
110
111template<class T>
112void C_AutoNode<T>::swap(C_AutoNode &another) noexcept
113{
114 const auto p = m_Ptr;
115 m_Ptr = another.m_Ptr;
116 another.m_Ptr = p;
117 //------------------------
118 const auto y = m_Owned;
119 m_Owned = another.m_Owned;
120 another.m_Owned = y;
121}
122
123template<class T>
124template<class T2>
126{
127 if (auto ptr = dynamic_cast<T*>(another.get()))
128 {
129 assign(ptr, another.m_Owned);
130 another.m_Owned = false;
131 return true;
132 }
133 return false;
134}
135
136} // namespace bux
void operator=(C_AutoNode< T2 > &another)
Definition XAutoPtr.h:87
constexpr C_AutoNode(T *p, bool owned) noexcept
Definition XAutoPtr.h:18
void reset(T *ptr) noexcept
Definition XAutoPtr.h:35
C_AutoNode(C_AutoNode< T2 > &another) noexcept
Definition XAutoPtr.h:22
void clear() noexcept
Definition XAutoPtr.h:36
C_AutoNode(C_AutoNode &another) noexcept
Definition XAutoPtr.h:19
bool owned() const noexcept
Definition XAutoPtr.h:39
void operator=(C_AutoNode &another)
Definition XAutoPtr.h:79
T ** operator&() noexcept
Definition XAutoPtr.h:32
friend class C_AutoNode
Definition XAutoPtr.h:51
constexpr C_AutoNode() noexcept
Definition XAutoPtr.h:17
bool takeOver(C_AutoNode< T2 > &another)
Definition XAutoPtr.h:125
void swap(C_AutoNode &another) noexcept
Definition XAutoPtr.h:112
T * get() const noexcept
Definition XAutoPtr.h:38
void assign(T *ptr, bool owned)
Definition XAutoPtr.h:94
T * operator->() const noexcept
Definition XAutoPtr.h:31
T & operator*() const noexcept
Definition XAutoPtr.h:33
T * disown() noexcept
Definition XAutoPtr.h:101
constexpr C_NewNode(C_Void) noexcept
Definition XAutoPtr.h:60
C_NewNode(T_Args &&...args)
Definition XAutoPtr.h:65
THE common namespace of bux library.
Definition AtomiX.cpp:3