diff --git a/ememory/UniquePtr.hpp b/ememory/UniquePtr.hpp new file mode 100644 index 0000000..18a181e --- /dev/null +++ b/ememory/UniquePtr.hpp @@ -0,0 +1,97 @@ +#ifndef UniquePtr_HPP_INCLUDED +#define UniquePtr_HPP_INCLUDED + +namespace ememory { + template + class UniquePtr { + private: + EMEM_UPTR_TYPE* m_pointer; + template UniquePtr(UniquePtr &); + template UniquePtr &operator=(UniquePtr &); + 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 + UniquePtr& operator=(UniquePtr _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 + UniquePtr makeUniquePtr(EMEM_UPTR_ARG ... _obj) { + return ememory::UniquePtr(new EMEM_UPTR_TYPE(_obj...)); + } + + template + inline void swap(UniquePtr &_obj1, UniquePtr &_obj2) { + _obj1.swap(_obj2); + } + /* + template + bool operator==(const UniquePtr &x, const UniquePtr &y) { + return x.get() == y.get(); + } + template + bool operator!=(const UniquePtr &x, const UniquePtr &y) { + return x.get() != y.get(); + } + template + bool operator<(const UniquePtr &x, const UniquePtr &y) { + return x.get() < y.get(); + } + template + bool operator<=(const UniquePtr &x, const UniquePtr &y) { + return x.get() <= y.get(); + } + template + bool operator>(const UniquePtr &x, const UniquePtr &y) { + return x.get() > y.get(); + } + template + bool operator>=(const UniquePtr &x, const UniquePtr &y) { + return x.get() >= y.get(); + } + */ +} + diff --git a/lutin_ememory-test.py b/lutin_ememory-test.py index b1842c0..1f341b8 100644 --- a/lutin_ememory-test.py +++ b/lutin_ememory-test.py @@ -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', diff --git a/lutin_ememory.py b/lutin_ememory.py index 8ca5eaf..b7ac91e 100644 --- a/lutin_ememory.py +++ b/lutin_ememory.py @@ -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', diff --git a/test/testUnique.cpp b/test/testUnique.cpp new file mode 100644 index 0000000..e69de29