Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
proview
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Esteban Blanc
proview
Commits
2e643250
Commit
2e643250
authored
Feb 07, 2019
by
Christoffer Ackelman
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Simplified co_smart_ptr
parent
a98a8196
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
15 additions
and
80 deletions
+15
-80
src/lib/co/src/co_smart_ptr.h
src/lib/co/src/co_smart_ptr.h
+15
-80
No files found.
src/lib/co/src/co_smart_ptr.h
View file @
2e643250
...
@@ -34,68 +34,24 @@
...
@@ -34,68 +34,24 @@
* General Public License plus this exception.
* General Public License plus this exception.
*/
*/
// This file contains unique_ptr
and smart_ptr
from C++11 backported to C++03.
// This file contains unique_ptr from C++11 backported to C++03.
#ifndef CO_UNIQUE_PTR
#ifndef CO_UNIQUE_PTR
#define CO_UNIQUE_PTR
#define CO_UNIQUE_PTR
#include "co_debug.h"
#include "co_debug.h"
// std::remove_reference
template
<
typename
T
>
struct
remove_reference
{
typedef
T
type
;
};
template
<
typename
T
>
struct
remove_reference
<
T
&>
{
typedef
T
type
;
};
template
<
typename
T
>
struct
remove_reference
<
T
&&>
{
typedef
T
type
;
};
// std::forward
template
<
typename
T
>
T
&&
forward
(
typename
remove_reference
<
T
>::
type
&
x
)
{
return
static_cast
<
T
&&>
(
x
);
}
template
<
typename
T
>
T
&&
forward
(
typename
remove_reference
<
T
>::
type
&&
x
)
{
return
static_cast
<
T
&&>
(
x
);
}
// std::move
template
<
typename
T
>
typename
remove_reference
<
T
>::
type
&&
move
(
T
&&
x
)
{
return
((
typename
remove_reference
<
T
>::
type
&&
)
x
);
}
template
<
typename
T
>
class
default_delete
{
public:
void
operator
()(
T
*
ptr
)
const
{
delete
ptr
;
}
};
template
<
typename
T
>
class
default_delete
<
T
[]
>
{
public:
void
operator
()(
T
*
ptr
)
const
{
delete
[]
ptr
;
}
};
// std::unique_ptr for single objects -> free memory with 'delete'
// std::unique_ptr for single objects -> free memory with 'delete'
template
<
typename
T
,
typename
Deleter
=
default_delete
<
T
>
>
template
<
typename
T
>
class
unique_ptr
{
class
unique_ptr
{
public:
public:
typedef
unique_ptr
<
T
,
Deleter
>
this_type
;
unique_ptr
(
T
*
p
)
:
value
(
p
)
{
unique_ptr
(
T
*
p
)
:
value
(
p
)
{
}
}
unique_ptr
(
T
*
p
,
Deleter
d
)
:
value
(
p
),
del
(
d
)
{
unique_ptr
(
unique_ptr
&&
x
)
:
value
(
x
.
release
())
{
}
unique_ptr
(
this_type
&&
x
)
:
value
(
x
.
release
()),
del
(
forward
<
Deleter
>
(
x
.
get_deleter
()))
{
}
}
this_type
&
operator
=
(
this_type
&&
x
)
{
unique_ptr
&
operator
=
(
unique_ptr
&&
x
)
{
reset
(
x
.
release
());
reset
(
x
.
release
());
del
=
move
(
forward
<
Deleter
>
(
x
.
get_deleter
()));
return
*
this
;
return
*
this
;
}
}
...
@@ -105,7 +61,7 @@ public:
...
@@ -105,7 +61,7 @@ public:
void
reset
(
T
*
p
=
NULL
)
{
void
reset
(
T
*
p
=
NULL
)
{
if
(
p
!=
value
)
{
if
(
p
!=
value
)
{
get_deleter
()(
value
)
;
delete
value
;
value
=
p
;
value
=
p
;
}
}
}
}
...
@@ -127,37 +83,25 @@ public:
...
@@ -127,37 +83,25 @@ public:
T
*
get
()
const
{
T
*
get
()
const
{
return
value
;
return
value
;
}
}
Deleter
&
get_deleter
()
{
return
del
;
}
const
Deleter
&
get_deleter
()
const
{
return
del
;
}
protected:
protected:
T
*
value
;
T
*
value
;
Deleter
del
;
unique_ptr
(
const
this_type
&
);
unique_ptr
(
const
unique_ptr
&
);
unique_ptr
&
operator
=
(
const
this_type
&
);
unique_ptr
&
operator
=
(
const
unique_ptr
&
);
unique_ptr
&
operator
=
(
T
*
p
);
unique_ptr
&
operator
=
(
T
*
p
);
};
};
// std::unique_ptr for arrays -> free memory with 'delete[]'
// std::unique_ptr for arrays -> free memory with 'delete[]'
template
<
typename
T
,
typename
Deleter
>
template
<
typename
T
>
class
unique_ptr
<
T
[]
,
Deleter
>
{
class
unique_ptr
<
T
[]
>
{
public:
public:
typedef
unique_ptr
<
T
[],
Deleter
>
this_type
;
unique_ptr
(
T
*
p
)
:
value
(
p
)
{
unique_ptr
(
T
*
p
,
Deleter
d
=
default_delete
<
T
[]
>
())
:
value
(
p
),
del
(
d
)
{
}
}
unique_ptr
(
this_type
&&
x
)
:
value
(
x
.
release
()),
del
(
forward
<
Deleter
>
(
x
.
get_deleter
()
))
{
unique_ptr
(
unique_ptr
&&
x
)
:
value
(
x
.
release
(
))
{
}
}
this_type
&
operator
=
(
this_type
&&
x
)
{
unique_ptr
&
operator
=
(
unique_ptr
&&
x
)
{
reset
(
x
.
release
());
reset
(
x
.
release
());
del
=
move
(
forward
<
Deleter
>
(
x
.
get_deleter
()));
return
*
this
;
return
*
this
;
}
}
...
@@ -167,7 +111,7 @@ public:
...
@@ -167,7 +111,7 @@ public:
void
reset
(
T
*
p
=
NULL
)
{
void
reset
(
T
*
p
=
NULL
)
{
if
(
p
!=
value
)
{
if
(
p
!=
value
)
{
get_deleter
()(
value
)
;
delete
[]
value
;
value
=
p
;
value
=
p
;
}
}
}
}
...
@@ -185,20 +129,11 @@ public:
...
@@ -185,20 +129,11 @@ public:
T
*
get
()
const
{
T
*
get
()
const
{
return
value
;
return
value
;
}
}
Deleter
&
get_deleter
()
{
return
del
;
}
const
Deleter
&
get_deleter
()
const
{
return
del
;
}
protected:
protected:
T
*
value
;
T
*
value
;
Deleter
del
;
unique_ptr
(
const
this_type
&
);
unique_ptr
(
const
unique_ptr
&
);
unique_ptr
&
operator
=
(
const
this_type
&
);
unique_ptr
&
operator
=
(
const
unique_ptr
&
);
unique_ptr
&
operator
=
(
T
*
p
);
unique_ptr
&
operator
=
(
T
*
p
);
};
};
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment