FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
c-plus-plus-examples/std_shared_ptr_wrapper/main.cpp at master · goblinhack/c-plus-plus-examples · GitHub
goblinhack
/
c-plus-plus-examples
Public
Notifications
You must be signed in to change notification settings
Fork
6
Star
21
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
c-plus-plus-examples
/
std_shared_ptr_wrapper
/
main.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
117 lines (98 loc) · 2.77 KB
Breadcrumbs
c-plus-plus-examples
/
std_shared_ptr_wrapper
/
main.cpp
Copy path
File metadata and controls
117 lines (98 loc) · 2.77 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#
include
"
../common/common.h
"
#
include
<
iostream
>
#
include
<
memory
>
#
include
<
sstream
>
#
include
<
string
>
template
<
typename
T >
class
MySharedPtr
{
private:
std::shared_ptr< T > sptr;
std::string name {
"
nullptr
"
};
void
debug
(
const
std::string &what) { std::cout << name <<
"
:
"
<< what <<
"
"
<<
to_string
() << std::endl; }
std::string
to_string
(
void
)
{
auto
address =
static_cast
<
const
void
*>(
this
);
std::stringstream ss;
ss << address;
if
(sptr) {
return
"
MySharedPtr(
"
+ ss.
str
() +
"
,
"
+ sptr->
to_string
() +
"
)
"
;
}
else
{
return
"
MySharedPtr(
"
+ ss.
str
() +
"
)
"
;
}
}
public:
//
explicit means constructor must match exactly
template
<
typename
...
ARGS
>
explicit
MySharedPtr
(
const
std::string &name,
ARGS
... a) : name(name)
{
sptr = std::make_shared< T >(a...);
debug
(
"
MySharedPtr::make_shared
"
);
}
explicit
MySharedPtr
(
const
std::string &name) : name(name)
{
sptr = std::make_shared< T > ();
debug
(
"
MySharedPtr::make_shared
"
);
}
explicit
MySharedPtr
(
void
) {
debug
(
"
MySharedPtr::default constructor
"
); }
~MySharedPtr
() {
debug
(
"
MySharedPtr::delete
"
); }
T *
const
operator
->()
{
debug
(
"
MySharedPtr::-> dereference
"
);
return
sptr.
operator
->();
}
T *
get
()
const
{
debug
(
"
MySharedPtr::get ptr
"
);
return
sptr.
get
();
}
T &
operator
*()
{
debug
(
"
MySharedPtr::* ptr
"
);
return
*sptr;
}
const
T &
operator
*()
const
{
debug
(
"
MySharedPtr::const * ptr
"
);
return
*sptr;
}
operator
bool
()
const
{
debug
(
"
MySharedPtr::bool
"
);
return
(
bool
) sptr;
}
size_t
use_count
(
void
)
const
{
return
sptr.
use_count
(); }
void
reset
()
{
debug
(
"
MySharedPtr::reset
"
);
sptr.
reset
();
}
};
typedef
MySharedPtr<
class
Foo
> Foop;
class
Foo
{
private:
std::string data;
void
debug
(
const
std::string &what) { std::cout << what <<
"
"
<<
to_string
() << std::endl; }
public:
Foo
(std::string data) : data(data) {
debug
(
"
new
"
); }
~Foo
() {
debug
(
"
delete
"
); }
std::string
to_string
(
void
)
{
auto
address =
static_cast
<
const
void
*>(
this
);
std::stringstream ss;
ss << address;
return
"
Foo(
"
+ ss.
str
() +
"
, data=
"
+ data +
"
)
"
;
}
};
int
main
(
void
)
{
DOC
(
"
create a class and share it between two pointers:
"
);
auto
sptr1 = MySharedPtr<
class
Foo
>(
"
[foo1]
"
,
Foo
(
"
foo1-data
"
));
std::cout <<
"
sptr1 ref count now
"
<< sptr1.
use_count
() << std::endl;
auto
sptr2 = sptr1;
std::cout <<
"
sptr2 ref count now
"
<< sptr2.
use_count
() << std::endl;
DOC
(
"
release the shared sptrs, expect foo1 to be destroyed:
"
);
sptr1.
reset
();
std::cout <<
"
sptr1 ref count now
"
<< sptr1.
use_count
() << std::endl;
sptr2.
reset
();
std::cout <<
"
sptr2 ref count now
"
<< sptr2.
use_count
() << std::endl;
}
Back
|
FazBrowse Home
|
New Git URL