diff --git a/lib/glacier/memory/move.h b/lib/glacier/memory/move.h new file mode 100644 index 0000000..f8faa3d --- /dev/null +++ b/lib/glacier/memory/move.h @@ -0,0 +1,25 @@ +#pragma once + +namespace glcr { + +template +struct RemoveReference { + typedef T type; +}; + +template +struct RemoveReference { + typedef T type; +}; + +template +struct RemoveReference { + typedef T type; +}; + +template +typename RemoveReference::type&& Move(T&& arg) { + return static_cast::type&&>(arg); +} + +} // namespace glcr diff --git a/lib/glacier/memory/unique_ptr.h b/lib/glacier/memory/unique_ptr.h new file mode 100644 index 0000000..3eb631d --- /dev/null +++ b/lib/glacier/memory/unique_ptr.h @@ -0,0 +1,51 @@ +#pragma once + +namespace glcr { + +template +class UniquePtr { + public: + UniquePtr() : ptr_(nullptr) {} + UniquePtr(decltype(nullptr)) : ptr_(nullptr) {} + UniquePtr(T* ptr) : ptr_(ptr) {} + + UniquePtr(const UniquePtr&) = delete; + UniquePtr& operator=(const UniquePtr&) = delete; + + UniquePtr(UniquePtr&& other) : ptr_(other.ptr_) { other.ptr_ = nullptr; } + UniquePtr& operator=(UniquePtr&& other) { + T* temp = ptr_; + ptr_ = other.ptr_; + other.ptr_ = temp; + } + + ~UniquePtr() { + if (ptr_ != nullptr) { + delete ptr_; + } + } + + explicit operator bool() const { return ptr_ != nullptr; } + bool empty() const { return ptr_ == nullptr; } + + T* get() const { return ptr_; } + + T* operator->() const { return ptr_; } + T& operator*() const { return *ptr_; } + + T* release() { + T* tmp = ptr_; + ptr_ = nullptr; + return tmp; + } + + private: + T* ptr_; +}; + +template +UniquePtr MakeUnique(Args... args) { + return UniquePtr(new T(args...)); +} + +} // namespace glcr