
/*
 * Copyright 2011-2022 Cuberite Contributors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


#pragma once


/** An RAII wrapper for std::atomic<T*>. */
template <typename T>
class cAtomicUniquePtr
{
public:
	static_assert(!std::is_array<T>::value, "cAtomicUniquePtr does not support arrays");
	DISALLOW_COPY_AND_ASSIGN(cAtomicUniquePtr);

	cAtomicUniquePtr() noexcept:
		m_Ptr(nullptr)
	{
	}


	cAtomicUniquePtr(std::unique_ptr<T> a_Ptr) noexcept:
		m_Ptr(a_Ptr.release())
	{
	}

	cAtomicUniquePtr & operator = (std::unique_ptr<T> a_Ptr) noexcept
	{
		store(std::move(a_Ptr));
		return *this;
	}

	~cAtomicUniquePtr() noexcept
	{
		delete load();
	}

	operator T * () const noexcept
	{
		return load();
	}

	bool compare_exchange_weak(T *& a_Expected, std::unique_ptr<T> && a_Desired, std::memory_order a_Order = std::memory_order_seq_cst) noexcept
	{
		bool DidExchange = m_Ptr.compare_exchange_weak(a_Expected, a_Desired.get(), a_Order);
		if (DidExchange)
		{
			// Only release ownership from the caller if the exchange occurred
			a_Desired.release();
		}
		return DidExchange;
	}

	bool compare_exchange_strong(T *& a_Expected, std::unique_ptr<T> && a_Desired, std::memory_order a_Order = std::memory_order_seq_cst) noexcept
	{
		bool DidExchange = m_Ptr.compare_exchange_strong(a_Expected, a_Desired.get(), a_Order);
		if (DidExchange)
		{
			// Only release ownership from the caller if the exchange occurred
			a_Desired.release();
		}
		return DidExchange;
	}

	std::unique_ptr<T> exchange(std::unique_ptr<T> a_Ptr, std::memory_order a_Order = std::memory_order_seq_cst) noexcept
	{
		return std::unique_ptr<T>{ m_Ptr.exchange(a_Ptr.release(), a_Order) };
	}

	T * load(std::memory_order a_Order = std::memory_order_seq_cst) const noexcept
	{
		return m_Ptr.load(a_Order);
	}

	void store(std::unique_ptr<T> a_Ptr, std::memory_order a_Order = std::memory_order_seq_cst) noexcept
	{
		// Store new value and delete old value
		delete m_Ptr.exchange(a_Ptr.release(), a_Order);
	}

private:
	std::atomic<T*> m_Ptr;
};
