mirror of
https://github.com/wichtounet/thor-os.git
synced 2025-09-18 01:01:57 -04:00
Remove the overhead of unique_ptr when using default_deleter
This commit is contained in:
parent
1c4f8413e0
commit
11318343c7
@ -8,6 +8,8 @@
|
|||||||
#ifndef UNIQUE_PTR_H
|
#ifndef UNIQUE_PTR_H
|
||||||
#define UNIQUE_PTR_H
|
#define UNIQUE_PTR_H
|
||||||
|
|
||||||
|
#include <tuple.hpp>
|
||||||
|
|
||||||
#include "thor.hpp"
|
#include "thor.hpp"
|
||||||
|
|
||||||
namespace std {
|
namespace std {
|
||||||
@ -33,15 +35,19 @@ public:
|
|||||||
typedef D deleter_type;
|
typedef D deleter_type;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
pointer_type pointer;
|
typedef tuple<pointer_type, deleter_type> data_impl;
|
||||||
deleter_type deleter;
|
|
||||||
|
data_impl _data;
|
||||||
|
|
||||||
|
//pointer_type pointer;
|
||||||
|
//deleter_type deleter;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
unique_ptr() : pointer(pointer_type()), deleter(deleter_type()) {}
|
unique_ptr() : _data(make_tuple(pointer_type(), deleter_type())) {}
|
||||||
|
|
||||||
explicit unique_ptr(pointer_type p) : pointer(p), deleter(deleter_type()) {}
|
explicit unique_ptr(pointer_type p) : _data(make_tuple(p, deleter_type())) {}
|
||||||
|
|
||||||
unique_ptr(unique_ptr&& u) : pointer(u.release()), deleter(u.get_deleter()) {}
|
unique_ptr(unique_ptr&& u) : _data(make_tuple(u.release(), u.get_deleter())) {}
|
||||||
unique_ptr& operator=(unique_ptr&& u){
|
unique_ptr& operator=(unique_ptr&& u){
|
||||||
reset(u.release());
|
reset(u.release());
|
||||||
return *this;
|
return *this;
|
||||||
@ -64,11 +70,11 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
pointer_type get() const {
|
pointer_type get() const {
|
||||||
return pointer;
|
return std::get<0>(_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
deleter_type get_deleter() const {
|
deleter_type get_deleter() const {
|
||||||
return deleter;
|
return std::get<1>(_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
operator bool() const {
|
operator bool() const {
|
||||||
@ -76,19 +82,21 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
pointer_type release(){
|
pointer_type release(){
|
||||||
pointer_type p = pointer;
|
pointer_type p = get();
|
||||||
pointer = nullptr;
|
std::get<0>(_data) = nullptr;
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
void reset(pointer_type p = pointer_type()){
|
void reset(pointer_type p = pointer_type()){
|
||||||
if(pointer != p){
|
if(get() != p){
|
||||||
get_deleter()(pointer);
|
get_deleter()(get());
|
||||||
pointer = nullptr;
|
std::get<0>(_data) = nullptr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static_assert(sizeof(unique_ptr<long>) == sizeof(long), "unique_ptr must have zero overhead with default deleter");
|
||||||
|
|
||||||
} //end of namespace std
|
} //end of namespace std
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user