| 
									
										
										
										
											2018-11-16 15:53:39 +08:00
										 |  |  | // Copyright (c) 2016-2018 Easy2D - Nomango
 | 
					
						
							|  |  |  | // 
 | 
					
						
							|  |  |  | // Permission is hereby granted, free of charge, to any person obtaining lhs copy
 | 
					
						
							|  |  |  | // of this software and associated documentation files (the "Software"), to deal
 | 
					
						
							|  |  |  | // in the Software without restriction, including without limitation the rights
 | 
					
						
							|  |  |  | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 | 
					
						
							|  |  |  | // copies of the Software, and to permit persons to whom the Software is
 | 
					
						
							|  |  |  | // furnished to do so, subject to the following conditions:
 | 
					
						
							|  |  |  | // 
 | 
					
						
							|  |  |  | // The above copyright notice and this permission notice shall be included in
 | 
					
						
							|  |  |  | // all copies or substantial portions of the Software.
 | 
					
						
							|  |  |  | // 
 | 
					
						
							|  |  |  | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 | 
					
						
							|  |  |  | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 | 
					
						
							|  |  |  | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 | 
					
						
							|  |  |  | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 | 
					
						
							|  |  |  | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 | 
					
						
							|  |  |  | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
					
						
							|  |  |  | // THE SOFTWARE.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							| 
									
										
										
										
											2018-11-17 17:15:32 +08:00
										 |  |  | #include "../macros.h"
 | 
					
						
							| 
									
										
										
										
											2018-11-16 15:53:39 +08:00
										 |  |  | #include <utility>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace easy2d | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	namespace intrusive | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		template <typename T> | 
					
						
							|  |  |  | 		class SmartPointer | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			T* ptr_{ nullptr }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		public: | 
					
						
							|  |  |  | 			using Type = T; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-17 17:15:32 +08:00
										 |  |  | 			SmartPointer() E2D_NOEXCEPT {} | 
					
						
							| 
									
										
										
										
											2018-11-16 15:53:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-17 17:15:32 +08:00
										 |  |  | 			SmartPointer(nullptr_t) E2D_NOEXCEPT {} | 
					
						
							| 
									
										
										
										
											2018-11-16 15:53:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-17 17:15:32 +08:00
										 |  |  | 			SmartPointer(Type* p) E2D_NOEXCEPT : ptr_(p) | 
					
						
							| 
									
										
										
										
											2018-11-16 15:53:39 +08:00
										 |  |  | 			{ | 
					
						
							|  |  |  | 				IntrusivePtrAddRef(ptr_); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-17 17:15:32 +08:00
										 |  |  | 			SmartPointer(const SmartPointer& other) E2D_NOEXCEPT : ptr_(other.ptr_) | 
					
						
							| 
									
										
										
										
											2018-11-16 15:53:39 +08:00
										 |  |  | 			{ | 
					
						
							|  |  |  | 				IntrusivePtrAddRef(ptr_); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			template <typename U> | 
					
						
							| 
									
										
										
										
											2018-11-17 17:15:32 +08:00
										 |  |  | 			SmartPointer(const SmartPointer<U>& other) E2D_NOEXCEPT : ptr_(other.Get()) | 
					
						
							| 
									
										
										
										
											2018-11-16 15:53:39 +08:00
										 |  |  | 			{ | 
					
						
							|  |  |  | 				IntrusivePtrAddRef(ptr_); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-17 17:15:32 +08:00
										 |  |  | 			SmartPointer(SmartPointer&& other) E2D_NOEXCEPT | 
					
						
							| 
									
										
										
										
											2018-11-16 15:53:39 +08:00
										 |  |  | 			{ | 
					
						
							|  |  |  | 				ptr_ = other.ptr_; | 
					
						
							|  |  |  | 				other.ptr_ = nullptr; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-17 17:15:32 +08:00
										 |  |  | 			~SmartPointer() E2D_NOEXCEPT | 
					
						
							| 
									
										
										
										
											2018-11-16 15:53:39 +08:00
										 |  |  | 			{ | 
					
						
							|  |  |  | 				IntrusivePtrRelease(ptr_); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-17 17:15:32 +08:00
										 |  |  | 			inline Type* Get() const E2D_NOEXCEPT { return ptr_; } | 
					
						
							| 
									
										
										
										
											2018-11-16 15:53:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-17 17:15:32 +08:00
										 |  |  | 			inline void Swap(SmartPointer& other) E2D_NOEXCEPT | 
					
						
							| 
									
										
										
										
											2018-11-16 15:53:39 +08:00
										 |  |  | 			{ | 
					
						
							|  |  |  | 				std::swap(ptr_, other.ptr_); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			inline Type* operator ->() const | 
					
						
							|  |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2018-11-23 14:55:03 +08:00
										 |  |  | 				E2D_ASSERT(ptr_ != nullptr && "Invalid pointer"); | 
					
						
							| 
									
										
										
										
											2018-11-16 15:53:39 +08:00
										 |  |  | 				return ptr_; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			inline Type& operator *() const | 
					
						
							|  |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2018-11-23 14:55:03 +08:00
										 |  |  | 				E2D_ASSERT(ptr_ != nullptr && "Invalid pointer"); | 
					
						
							| 
									
										
										
										
											2018-11-16 15:53:39 +08:00
										 |  |  | 				return *ptr_; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-17 17:15:32 +08:00
										 |  |  | 			inline Type** operator &() | 
					
						
							|  |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2018-11-23 14:55:03 +08:00
										 |  |  | 				E2D_ASSERT(ptr_ == nullptr && "Memory leak"); | 
					
						
							| 
									
										
										
										
											2018-11-17 17:15:32 +08:00
										 |  |  | 				return &ptr_; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			inline operator bool() const E2D_NOEXCEPT { return ptr_ != nullptr; } | 
					
						
							| 
									
										
										
										
											2018-11-16 15:53:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-17 17:15:32 +08:00
										 |  |  | 			inline bool operator !() const E2D_NOEXCEPT { return ptr_ == 0; } | 
					
						
							| 
									
										
										
										
											2018-11-16 15:53:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-17 17:15:32 +08:00
										 |  |  | 			inline SmartPointer& operator =(const SmartPointer& other) E2D_NOEXCEPT | 
					
						
							| 
									
										
										
										
											2018-11-16 15:53:39 +08:00
										 |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2018-11-17 17:15:32 +08:00
										 |  |  | 				if (other.ptr_ != ptr_) | 
					
						
							|  |  |  | 					SmartPointer(other).Swap(*this); | 
					
						
							| 
									
										
										
										
											2018-11-16 15:53:39 +08:00
										 |  |  | 				return *this; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-17 17:15:32 +08:00
										 |  |  | 			inline SmartPointer& operator =(SmartPointer&& other) E2D_NOEXCEPT | 
					
						
							| 
									
										
										
										
											2018-11-16 15:53:39 +08:00
										 |  |  | 			{ | 
					
						
							|  |  |  | 				IntrusivePtrRelease(ptr_); | 
					
						
							|  |  |  | 				ptr_ = other.ptr_; | 
					
						
							|  |  |  | 				other.ptr_ = nullptr; | 
					
						
							|  |  |  | 				return *this; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-17 17:15:32 +08:00
										 |  |  | 			inline SmartPointer& operator =(Type* p) E2D_NOEXCEPT | 
					
						
							| 
									
										
										
										
											2018-11-16 15:53:39 +08:00
										 |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2018-11-17 17:15:32 +08:00
										 |  |  | 				if (p != ptr_) | 
					
						
							|  |  |  | 					SmartPointer(p).Swap(*this); | 
					
						
							| 
									
										
										
										
											2018-11-16 15:53:39 +08:00
										 |  |  | 				return *this; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-17 17:15:32 +08:00
										 |  |  | 			inline SmartPointer& operator =(nullptr_t) E2D_NOEXCEPT | 
					
						
							| 
									
										
										
										
											2018-11-16 15:53:39 +08:00
										 |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2018-11-17 17:15:32 +08:00
										 |  |  | 				if (nullptr != ptr_) | 
					
						
							|  |  |  | 					SmartPointer{}.Swap(*this); | 
					
						
							| 
									
										
										
										
											2018-11-16 15:53:39 +08:00
										 |  |  | 				return *this; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		template<class T, class U> | 
					
						
							| 
									
										
										
										
											2018-11-17 17:15:32 +08:00
										 |  |  | 		inline bool operator==(SmartPointer<T> const& lhs, SmartPointer<U> const& rhs) E2D_NOEXCEPT | 
					
						
							| 
									
										
										
										
											2018-11-16 15:53:39 +08:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			return lhs.Get() == rhs.Get(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		template<class T, class U> | 
					
						
							| 
									
										
										
										
											2018-11-17 17:15:32 +08:00
										 |  |  | 		inline bool operator!=(SmartPointer<T> const& lhs, SmartPointer<U> const& rhs) E2D_NOEXCEPT | 
					
						
							| 
									
										
										
										
											2018-11-16 15:53:39 +08:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			return lhs.Get() != rhs.Get(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		template<class T, class U> | 
					
						
							| 
									
										
										
										
											2018-11-17 17:15:32 +08:00
										 |  |  | 		inline bool operator<(SmartPointer<T> const& lhs, SmartPointer<U> const& rhs) E2D_NOEXCEPT | 
					
						
							| 
									
										
										
										
											2018-11-16 15:53:39 +08:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			return lhs.Get() < rhs.Get(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		template<class T> | 
					
						
							| 
									
										
										
										
											2018-11-17 17:15:32 +08:00
										 |  |  | 		inline bool operator==(SmartPointer<T> const& lhs, T* rhs) E2D_NOEXCEPT | 
					
						
							| 
									
										
										
										
											2018-11-16 15:53:39 +08:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			return lhs.Get() == rhs; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		template<class T> | 
					
						
							| 
									
										
										
										
											2018-11-17 17:15:32 +08:00
										 |  |  | 		inline bool operator!=(SmartPointer<T> const& lhs, T* rhs) E2D_NOEXCEPT | 
					
						
							| 
									
										
										
										
											2018-11-16 15:53:39 +08:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			return lhs.Get() != rhs; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		template<class T> | 
					
						
							| 
									
										
										
										
											2018-11-17 17:15:32 +08:00
										 |  |  | 		inline bool operator==(T* lhs, SmartPointer<T> const& rhs) E2D_NOEXCEPT | 
					
						
							| 
									
										
										
										
											2018-11-16 15:53:39 +08:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			return lhs == rhs.Get(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		template<class T> | 
					
						
							| 
									
										
										
										
											2018-11-17 17:15:32 +08:00
										 |  |  | 		inline bool operator!=(T* lhs, SmartPointer<T> const& rhs) E2D_NOEXCEPT | 
					
						
							| 
									
										
										
										
											2018-11-16 15:53:39 +08:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			return lhs != rhs.Get(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		template<class T> | 
					
						
							| 
									
										
										
										
											2018-11-17 17:15:32 +08:00
										 |  |  | 		inline bool operator==(SmartPointer<T> const& lhs, nullptr_t) E2D_NOEXCEPT | 
					
						
							| 
									
										
										
										
											2018-11-16 15:53:39 +08:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			return !static_cast<bool>(lhs); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		template<class T> | 
					
						
							| 
									
										
										
										
											2018-11-17 17:15:32 +08:00
										 |  |  | 		inline bool operator!=(SmartPointer<T> const& lhs, nullptr_t) E2D_NOEXCEPT | 
					
						
							| 
									
										
										
										
											2018-11-16 15:53:39 +08:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			return static_cast<bool>(lhs); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		template<class T> | 
					
						
							| 
									
										
										
										
											2018-11-17 17:15:32 +08:00
										 |  |  | 		inline bool operator==(nullptr_t, SmartPointer<T> const& rhs) E2D_NOEXCEPT | 
					
						
							| 
									
										
										
										
											2018-11-16 15:53:39 +08:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			return !static_cast<bool>(rhs); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		template<class T> | 
					
						
							| 
									
										
										
										
											2018-11-17 17:15:32 +08:00
										 |  |  | 		inline bool operator!=(nullptr_t, SmartPointer<T> const& rhs) E2D_NOEXCEPT | 
					
						
							| 
									
										
										
										
											2018-11-16 15:53:39 +08:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			return static_cast<bool>(rhs); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-11-16 16:16:16 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-11-16 15:53:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-16 16:16:16 +08:00
										 |  |  | 	// template class cannot specialize std::swap,
 | 
					
						
							|  |  |  | 	// so implement a swap function in easy2d namespace
 | 
					
						
							|  |  |  | 	template<class T> | 
					
						
							| 
									
										
										
										
											2018-11-17 17:15:32 +08:00
										 |  |  | 	inline void swap(intrusive::SmartPointer<T>& lhs, intrusive::SmartPointer<T>& rhs) E2D_NOEXCEPT | 
					
						
							| 
									
										
										
										
											2018-11-16 16:16:16 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		lhs.Swap(rhs); | 
					
						
							| 
									
										
										
										
											2018-11-16 15:53:39 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-01-20 12:07:59 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	class SmartMaker | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 	public: | 
					
						
							|  |  |  | 		static inline SmartMaker const& Instance() | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			static SmartMaker maker; | 
					
						
							|  |  |  | 			return maker; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		template<typename T> | 
					
						
							|  |  |  | 		inline intrusive::SmartPointer<T> operator -(T* ptr) const | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			return intrusive::SmartPointer<T>(ptr); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef NEW
 | 
					
						
							|  |  |  | #	undef NEW
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define NEW ::easy2d::SmartMaker::Instance() - new (std::nothrow)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-16 15:53:39 +08:00
										 |  |  | } |