/*******************************************************************************
***  This file is part of Dux Rei Nummariae.                                 ***
***                                                                          ***
***  Copyright (C) 2017, 2018, 2019, 2020, 2021                              ***
***  John Schneiderman <Licensing _AT_ Schneiderman _DOT_ me>                ***
***                                                                          ***
***  This program is free software: you can redistribute it and/or modify it ***
***  under the terms of the GNU Affero General Public License as published   ***
***  by the Free Software Foundation, either version 3 of the License, or    ***
***  (at your option) any later version.                                     ***
***                                                                          ***
***  This program is distributed in the hope that it will be useful, but     ***
***  WITHOUT ANY WARRANTY; without even the implied warranty of              ***
***  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                    ***
***  See the GNU Affero General Public License for more details.             ***
***                                                                          ***
***  You should have received a copy of the GNU Affero General Public License***
***  along with this program. If not, see <http://www.gnu.org/licenses/>.    ***
*******************************************************************************/
#ifndef DRN_FOUNDATION_OBSERVERPTR_HPP_
#define DRN_FOUNDATION_OBSERVERPTR_HPP_

#include <memory>
#include <ostream>


namespace drn
{
namespace foundation
{

/**
 * @brief A smart pointer for observing an object. This means that it preforms no memory management
 * on the lifetime of the observed object.
 *
 * @tparm ObservedType The type the smart pointer is observing.
 */
template<typename ObservedType>
class ObserverPtr
{
	ObservedType* observed_;

public:
	ObserverPtr(ObservedType* const observe = nullptr);
	ObserverPtr(const std::unique_ptr<ObservedType>& observe);
	ObserverPtr(const ObserverPtr&) = default;
	ObserverPtr(ObserverPtr&&) = default;
	~ObserverPtr() = default;
	ObserverPtr& operator=(const ObserverPtr&) = default;
	ObserverPtr& operator=(ObserverPtr&&) = default;
	ObserverPtr& operator=(ObservedType* const observe);
	bool operator==(const std::nullptr_t) const;
	bool operator==(const ObserverPtr& other) const;
	bool operator==(const ObservedType* observed) const;
	bool operator!=(const std::nullptr_t) const;
	bool operator!=(const ObserverPtr& other) const;
	bool operator!=(const ObservedType* observed) const;
	bool operator<(const ObservedType* observed) const = delete;
	bool operator<(const ObserverPtr& other) const = delete;
	bool operator<(const std::nullptr_t) const = delete;
	ObservedType& operator*();
	ObservedType& operator*() const;
	ObservedType* operator->();
	ObservedType* operator->() const;
	explicit operator bool() const noexcept;
	explicit operator ObservedType*() const noexcept;
	void operator delete(void*) = delete;
	/**
	 * @brief An accessor to the object being observed.
	 */
	ObservedType* observed() const;
};

template<typename ObservedType>
bool operator==(std::nullptr_t, const ObserverPtr<ObservedType>& rhs);
template<typename ObservedType, typename ObservedPtrType>
bool operator==(const ObservedType* lhs, const ObserverPtr<ObservedPtrType>& rhs);
template<typename ObservedType>
bool operator!=(std::nullptr_t, const ObserverPtr<ObservedType>& rhs);
template<typename ObservedType, typename ObservedPtrType>
bool operator!=(const ObservedType* lhs, const ObserverPtr<ObservedPtrType>& rhs);
template<typename ObservedType>
std::ostream& operator<<(std::ostream& o, const ObserverPtr<ObservedType>& observer);

}}

template<typename ObservedType>
drn::foundation::ObserverPtr<ObservedType>::ObserverPtr(ObservedType* const observe) :
	observed_{observe}
{}

template<typename ObservedType>
drn::foundation::ObserverPtr<ObservedType>::ObserverPtr(
	const std::unique_ptr<ObservedType>& observe
) :
	ObserverPtr{observe.get()}
{}

template<typename ObservedType>
drn::foundation::ObserverPtr<ObservedType>& drn::foundation::ObserverPtr<ObservedType>::operator=(
	ObservedType* const observe
)
{
	this->observed_ = observe;
	return *this;
}

template<typename ObservedType>
bool drn::foundation::ObserverPtr<ObservedType>::operator==(const std::nullptr_t) const
{
	return this->observed_ == nullptr;
}

template<typename ObservedType>
bool drn::foundation::ObserverPtr<ObservedType>::operator==(const ObserverPtr& other) const
{
	return *this == other.observed_;
}

template<typename ObservedType>
bool drn::foundation::ObserverPtr<ObservedType>::operator==(const ObservedType* observed) const
{
	return this->observed_ == observed;
}

template<typename ObservedType>
bool drn::foundation::ObserverPtr<ObservedType>::operator!=(const std::nullptr_t) const
{
	return ! (*this == nullptr);
}

template<typename ObservedType>
bool drn::foundation::ObserverPtr<ObservedType>::operator!=(const ObserverPtr& other) const
{
	return ! (*this == other);
}

template<typename ObservedType>
bool drn::foundation::ObserverPtr<ObservedType>::operator!=(const ObservedType* observed) const
{
	return ! (*this == observed);
}

template<typename ObservedType>
ObservedType* drn::foundation::ObserverPtr<ObservedType>::observed() const
{
	return this->observed_;
}

template<typename ObservedType>
ObservedType& drn::foundation::ObserverPtr<ObservedType>::operator*()
{
	assert(this->observed_ != nullptr && "Should not access the pointed object when it's null.");
	return *this->observed_;
}

template<typename ObservedType>
ObservedType& drn::foundation::ObserverPtr<ObservedType>::operator*() const
{
	assert(this->observed_ != nullptr && "Should not access the pointed object when it's null.");
	return *this->observed_;
}

template<typename ObservedType>
ObservedType* drn::foundation::ObserverPtr<ObservedType>::operator->()
{
	assert(this->observed_ != nullptr && "Should not access the pointed object when it's null.");
	return this->observed_;
}

template<typename ObservedType>
ObservedType* drn::foundation::ObserverPtr<ObservedType>::operator->() const
{
	assert(this->observed_ != nullptr && "Should not access the pointed object when it's null.");
	return this->observed_;
}

template<typename ObservedType>
drn::foundation::ObserverPtr<ObservedType>::operator bool() const noexcept
{
	return this->observed_ != nullptr;
}

template<typename ObservedType>
drn::foundation::ObserverPtr<ObservedType>::operator ObservedType*() const noexcept
{
	return this->observed_;
}

template<typename ObservedType>
bool drn::foundation::operator==(std::nullptr_t, const ObserverPtr<ObservedType>& rhs)
{
	return rhs == nullptr;
}

template<typename ObservedType, typename ObservedPtrType>
bool drn::foundation::operator==(const ObservedType* lhs, const ObserverPtr<ObservedPtrType>& rhs)
{
	return rhs == lhs;
}

template<typename ObservedType>
bool drn::foundation::operator!=(std::nullptr_t, const ObserverPtr<ObservedType>& rhs)
{
	return ! (nullptr == rhs);
}

template<typename ObservedType, typename ObservedPtrType>
bool drn::foundation::operator!=(const ObservedType* lhs, const ObserverPtr<ObservedPtrType>& rhs)
{
	return ! (lhs == rhs);
}

template<typename ObservedType>
std::ostream& drn::foundation::operator<<(
	std::ostream& o,
	const ObserverPtr<ObservedType>& observer
)
{
	if (observer == nullptr)
		return o << "<nullptr>";
	return o << *observer;
}

#endif
