[DEV] try develop UniquePtr

This commit is contained in:
Edouard DUPIN 2017-08-28 21:05:29 +02:00
parent fa16d76623
commit ee9a5de786
4 changed files with 99 additions and 0 deletions

97
ememory/UniquePtr.hpp Normal file
View File

@ -0,0 +1,97 @@
#ifndef UniquePtr_HPP_INCLUDED
#define UniquePtr_HPP_INCLUDED
namespace ememory {
template <class EMEM_UPTR_TYPE>
class UniquePtr {
private:
EMEM_UPTR_TYPE* m_pointer;
template <class EMEM_UPTR_TYPE> UniquePtr(UniquePtr<EMEM_UPTR_TYPE> &);
template <class EMEM_UPTR_TYPE> UniquePtr &operator=(UniquePtr<EMEM_UPTR_TYPE> &);
public:
UniquePtr() :
m_pointer(nullptr) {
}
UniquePtr(nullptr) :
m_pointer(nullptr) {
}
explicit UniquePtr(EMEM_UPTR_TYPE* _obj) :
m_pointer(_obj)
{
}
~UniquePtr() {
reset();
}
UniquePtr& operator=(UniquePtr _obj) {
reset(_obj.release());
return *this;
}
template <class EMEM_UPTR_TYPE_2>
UniquePtr& operator=(UniquePtr<EMEM_UPTR_TYPE_2, EMEM_UPTR_DELETER> _obj){
reset(_obj.release());
m_pointer = etk::move(_obj.pointer)
return *this;
}
EMEM_UPTR_TYPE operator*() const{
return *m_pointer;
}
EMEM_UPTR_TYPE *operator->() const{
return m_pointer;
}
EMEM_UPTR_TYPE *get() const{
return *m_pointer;
}
EMEM_UPTR_TYPE *release(){
T *tmp = m_pointer;
m_pointer = 0;
return tmp;
}
void reset(){
delete m_pointer;
m_pointer = nullptr;
}
void swap(UniquePtr &_obj){
std::swap(m_pointer, _obj.m_pointer);
}
};
template<class EMEM_UPTR_TYPE>
UniquePtr<EMEM_UPTR_TYPE> makeUniquePtr(EMEM_UPTR_ARG ... _obj) {
return ememory::UniquePtr<EMEM_UPTR_TYPE>(new EMEM_UPTR_TYPE(_obj...));
}
template <class EMEM_UPTR_TYPE>
inline void swap(UniquePtr<EMEM_UPTR_TYPE> &_obj1, UniquePtr<EMEM_UPTR_TYPE> &_obj2) {
_obj1.swap(_obj2);
}
/*
template <class EMEM_UPTR_TYPE_1, class EMEM_UPTR_DELETER_1, class EMEM_UPTR_TYPE_2, class EMEM_UPTR_DELETER_2>
bool operator==(const UniquePtr<EMEM_UPTR_TYPE_1, EMEM_UPTR_DELETER_1> &x, const UniquePtr<EMEM_UPTR_TYPE_2, EMEM_UPTR_DELETER_2> &y) {
return x.get() == y.get();
}
template <class EMEM_UPTR_TYPE_1, class EMEM_UPTR_DELETER_1, class EMEM_UPTR_TYPE_2, class EMEM_UPTR_DELETER_2>
bool operator!=(const UniquePtr<EMEM_UPTR_TYPE_1, EMEM_UPTR_DELETER_1> &x, const UniquePtr<EMEM_UPTR_TYPE_2, EMEM_UPTR_DELETER_2> &y) {
return x.get() != y.get();
}
template <class EMEM_UPTR_TYPE_1, class EMEM_UPTR_DELETER_1, class EMEM_UPTR_TYPE_2, class EMEM_UPTR_DELETER_2>
bool operator<(const UniquePtr<EMEM_UPTR_TYPE_1, EMEM_UPTR_DELETER_1> &x, const UniquePtr<EMEM_UPTR_TYPE_2, EMEM_UPTR_DELETER_2> &y) {
return x.get() < y.get();
}
template <class EMEM_UPTR_TYPE_1, class EMEM_UPTR_DELETER_1, class EMEM_UPTR_TYPE_2, class EMEM_UPTR_DELETER_2>
bool operator<=(const UniquePtr<EMEM_UPTR_TYPE_1, EMEM_UPTR_DELETER_1> &x, const UniquePtr<EMEM_UPTR_TYPE_2, EMEM_UPTR_DELETER_2> &y) {
return x.get() <= y.get();
}
template <class EMEM_UPTR_TYPE_1, class EMEM_UPTR_DELETER_1, class EMEM_UPTR_TYPE_2, class EMEM_UPTR_DELETER_2>
bool operator>(const UniquePtr<EMEM_UPTR_TYPE_1, EMEM_UPTR_DELETER_1> &x, const UniquePtr<EMEM_UPTR_TYPE_2, EMEM_UPTR_DELETER_2> &y) {
return x.get() > y.get();
}
template <class EMEM_UPTR_TYPE_1, class EMEM_UPTR_DELETER_1, class EMEM_UPTR_TYPE_2, class EMEM_UPTR_DELETER_2>
bool operator>=(const UniquePtr<EMEM_UPTR_TYPE_1, EMEM_UPTR_DELETER_1> &x, const UniquePtr<EMEM_UPTR_TYPE_2, EMEM_UPTR_DELETER_2> &y) {
return x.get() >= y.get();
}
*/
}

View File

@ -27,6 +27,7 @@ def get_maintainer():
def configure(target, my_module):
my_module.add_src_file([
'test/main.cpp',
'test/testUnique.cpp',
'test/testShared.cpp',
'test/testWeak.cpp',
'test/testEnableSharedFromThis.cpp',

View File

@ -37,6 +37,7 @@ def configure(target, my_module):
'ememory/memory.hpp',
'ememory/Counter.hpp',
'ememory/SharedPtr.hpp',
'ememory/UniquePtr.hpp',
'ememory/WeakPtr.hpp',
'ememory/EnableSharedFromThis.hpp',
'ememory/details/memory.hxx',

0
test/testUnique.cpp Normal file
View File