bux API Reference
1.12.6
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.
Toggle main menu visibility
Loading...
Searching...
No Matches
include
bux
XAutoPtr.h
Go to the documentation of this file.
1
#pragma once
2
3
#include <new>
// placement new
4
#include <utility>
// std::forward<>
5
6
namespace
bux
{
7
8
//
9
// Types
10
//
11
template
<
class
T>
12
class
C_AutoNode
13
{
14
public
:
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
; }
24
~C_AutoNode
();
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
44
private
:
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
54
struct
C_Void
{};
55
56
template
<
class
T>
57
class
C_NewNode
:
public
C_AutoNode
<T>
58
{
59
protected
:
60
constexpr
C_NewNode
(
C_Void
)
noexcept
:
C_AutoNode<T>
(
nullptr
,
false
) {}
61
62
public
:
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
//
71
template
<
class
T>
72
C_AutoNode<T>::~C_AutoNode
()
73
{
74
if
(m_Owned)
75
delete
m_Ptr;
76
}
77
78
template
<
class
T>
79
void
C_AutoNode<T>::operator=
(
C_AutoNode
&another)
80
{
81
this->
~C_AutoNode<T>
();
82
new
(
this
)
C_AutoNode
(another);
83
}
84
85
template
<
class
T>
86
template
<
class
T2>
87
void
C_AutoNode<T>::operator=
(
C_AutoNode<T2>
&another)
88
{
89
this->
~C_AutoNode<T>
();
90
new
(
this
)
C_AutoNode
(another);
91
}
92
93
template
<
class
T>
94
void
C_AutoNode<T>::assign
(T *ptr,
bool
owned
)
95
{
96
this->
~C_AutoNode<T>
();
97
new
(
this
)
C_AutoNode
(ptr,
owned
);
98
}
99
100
template
<
class
T>
101
T *
C_AutoNode<T>::disown
() noexcept
102
{
103
if
(m_Owned)
104
{
105
m_Owned =
false
;
106
return
m_Ptr;
107
}
108
return
nullptr
;
109
}
110
111
template
<
class
T>
112
void
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
123
template
<
class
T>
124
template
<
class
T2>
125
bool
C_AutoNode<T>::takeOver
(
C_AutoNode<T2>
&another)
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
bool
bux::C_AutoNode::operator=
void operator=(C_AutoNode< T2 > &another)
Definition
XAutoPtr.h:87
bux::C_AutoNode::C_AutoNode
constexpr C_AutoNode(T *p, bool owned) noexcept
Definition
XAutoPtr.h:18
bux::C_AutoNode::reset
void reset(T *ptr) noexcept
Definition
XAutoPtr.h:35
bux::C_AutoNode::~C_AutoNode
~C_AutoNode()
Definition
XAutoPtr.h:72
bux::C_AutoNode::C_AutoNode
C_AutoNode(C_AutoNode< T2 > &another) noexcept
Definition
XAutoPtr.h:22
bux::C_AutoNode::clear
void clear() noexcept
Definition
XAutoPtr.h:36
bux::C_AutoNode::C_AutoNode
C_AutoNode(C_AutoNode &another) noexcept
Definition
XAutoPtr.h:19
bux::C_AutoNode< I_LexAttr >::owned
bool owned() const noexcept
Definition
XAutoPtr.h:39
bux::C_AutoNode::operator=
void operator=(C_AutoNode &another)
Definition
XAutoPtr.h:79
bux::C_AutoNode::operator&
T ** operator&() noexcept
Definition
XAutoPtr.h:32
bux::C_AutoNode::C_AutoNode
friend class C_AutoNode
Definition
XAutoPtr.h:51
bux::C_AutoNode::C_AutoNode
constexpr C_AutoNode() noexcept
Definition
XAutoPtr.h:17
bux::C_AutoNode::takeOver
bool takeOver(C_AutoNode< T2 > &another)
Definition
XAutoPtr.h:125
bux::C_AutoNode::swap
void swap(C_AutoNode &another) noexcept
Definition
XAutoPtr.h:112
bux::C_AutoNode< I_LexAttr >::get
I_LexAttr * get() const noexcept
Definition
XAutoPtr.h:38
bux::C_AutoNode< I_LexAttr >::assign
void assign(I_LexAttr *ptr, bool owned)
bux::C_AutoNode::operator->
T * operator->() const noexcept
Definition
XAutoPtr.h:31
bux::C_AutoNode::operator*
T & operator*() const noexcept
Definition
XAutoPtr.h:33
bux::C_AutoNode::disown
T * disown() noexcept
Definition
XAutoPtr.h:101
bux::C_NewNode::C_NewNode
constexpr C_NewNode(C_Void) noexcept
Definition
XAutoPtr.h:60
bux::C_NewNode::C_NewNode
C_NewNode()
Definition
XAutoPtr.h:63
bux::C_NewNode::C_NewNode
C_NewNode(T_Args &&...args)
Definition
XAutoPtr.h:65
bux
THE common namespace of bux library.
Definition
AtomiX.cpp:3
bux::C_Void
Definition
XAutoPtr.h:54
Generated by
1.18.0