Commit 2e643250 authored by Christoffer Ackelman's avatar Christoffer Ackelman

Simplified co_smart_ptr

parent a98a8196
......@@ -34,68 +34,24 @@
* 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
#define CO_UNIQUE_PTR
#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'
template<typename T, typename Deleter = default_delete<T>>
template<typename T>
class unique_ptr {
public:
typedef unique_ptr<T, Deleter> this_type;
unique_ptr(T* p) : value(p) {
}
unique_ptr(T* p, Deleter d) : 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());
del = move(forward<Deleter>(x.get_deleter()));
return *this;
}
......@@ -105,7 +61,7 @@ public:
void reset(T* p = NULL) {
if (p != value) {
get_deleter()(value);
delete value;
value = p;
}
}
......@@ -127,37 +83,25 @@ public:
T* get() const {
return value;
}
Deleter& get_deleter() {
return del;
}
const Deleter& get_deleter() const {
return del;
}
protected:
T* value;
Deleter del;
unique_ptr(const this_type&);
unique_ptr& operator=(const this_type&);
unique_ptr(const unique_ptr&);
unique_ptr& operator=(const unique_ptr&);
unique_ptr& operator=(T* p);
};
// std::unique_ptr for arrays -> free memory with 'delete[]'
template<typename T, typename Deleter>
class unique_ptr<T[], Deleter> {
template<typename T>
class unique_ptr<T[]> {
public:
typedef unique_ptr<T[], Deleter> this_type;
unique_ptr(T* p, Deleter d = default_delete<T[]>()) : value(p), del(d) {
unique_ptr(T* p) : value(p) {
}
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());
del = move(forward<Deleter>(x.get_deleter()));
return *this;
}
......@@ -167,7 +111,7 @@ public:
void reset(T* p = NULL) {
if (p != value) {
get_deleter()(value);
delete[] value;
value = p;
}
}
......@@ -185,20 +129,11 @@ public:
T* get() const {
return value;
}
Deleter& get_deleter() {
return del;
}
const Deleter& get_deleter() const {
return del;
}
protected:
T* value;
Deleter del;
unique_ptr(const this_type&);
unique_ptr& operator=(const this_type&);
unique_ptr(const unique_ptr&);
unique_ptr& operator=(const unique_ptr&);
unique_ptr& operator=(T* p);
};
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment