// Copyright (C) 2017 Jérôme Leclercq // This file is part of the "Nazara Engine - Core module" // For conditions of distribution and use, see copyright notice in Config.hpp #include #include #include #include namespace Nz { /*! * \ingroup core * \class Nz::ObjectHandle * \brief Core class that represents a object handle */ /*! * \brief Constructs a ObjectHandle object by default */ template ObjectHandle::ObjectHandle() : m_object(nullptr) { } /*! * \brief Constructs a ObjectHandle object with a pointer to an object * * \param object Pointer to handle like an object (can be nullptr) */ template ObjectHandle::ObjectHandle(T* object) : ObjectHandle() { Reset(object); } /*! * \brief Constructs a ObjectHandle object by assignation * * \param handle ObjectHandle to assign into this */ template ObjectHandle::ObjectHandle(const ObjectHandle& handle) : ObjectHandle() { Reset(handle); } /*! * \brief Constructs a ObjectHandle object by move semantic * * \param handle ObjectHandle to move into this */ template ObjectHandle::ObjectHandle(ObjectHandle&& handle) noexcept : ObjectHandle() { Reset(std::move(handle)); } /*! * \brief Destructs the object and calls reset with nullptr * * \see Reset */ template ObjectHandle::~ObjectHandle() { Reset(nullptr); } /*! * \brief Gets the underlying object * \return Underlying object */ template T* ObjectHandle::GetObject() const { return m_object; } /*! * \brief Checks whether the object is valid * \return true if object is not nullptr */ template bool ObjectHandle::IsValid() const { return m_object != nullptr; } /*! * \brief Resets the content of the ObjectHandle with another object * * \param object Object to handle */ template void ObjectHandle::Reset(T* object) { // If we already have an entity, we must alert it that we are not pointing to it anymore if (m_object) m_object->UnregisterHandle(this); m_object = object; if (m_object) // We alert the new entity that we are pointing to it m_object->RegisterHandle(this); } /*! * \brief Resets the content of this with another object * * \param handle New object to handle */ template void ObjectHandle::Reset(const ObjectHandle& handle) { Reset(handle.GetObject()); } /*! * \brief Resets the content of this with another object by move semantic * * \param handle New object to handle to move into this */ template void ObjectHandle::Reset(ObjectHandle&& handle) noexcept { if (this == &handle) return; if (m_object) m_object->UnregisterHandle(this); if (T* object = handle.GetObject()) { m_object = handle.m_object; handle.m_object = nullptr; object->UpdateHandle(&handle, this); } } /*! * \brief Swaps the content of the two ObjectHandle * \return A reference to this * * \param handle ObjectHandle to swap */ template ObjectHandle& ObjectHandle::Swap(ObjectHandle& handle) { // As we swap the two handles, we must alert the entities // The default version with swap (move) would be working, // but will register handles one more time (due to temporary copy). if (m_object) { m_object->UnregisterHandle(this); m_object->RegisterHandle(&handle); } if (handle.m_object) { handle.m_object->UnregisterHandle(&handle); handle.m_object->RegisterHandle(this); } // We do the swap std::swap(m_object, handle.m_object); return *this; } /*! * \brief Gives a string representation * \return A string representation of the object "ObjectHandle(object representation) or Null" */ template Nz::String ObjectHandle::ToString() const { Nz::StringStream ss; ss << "ObjectHandle("; if (IsValid()) ss << m_object->ToString(); else ss << "Null"; ss << ')'; return ss; } /*! * \brief Converts the ObjectHandle to bool * \return true if reference is not nullptr * * \see IsValid */ template ObjectHandle::operator bool() const { return IsValid(); } /*! * \brief Dereferences the ObjectHandle * \return Underlying pointer */ template ObjectHandle::operator T*() const { return m_object; } /*! * \brief Dereferences the ObjectHandle * \return Underlying pointer */ template T* ObjectHandle::operator->() const { return m_object; } /*! * \brief Assigns the entity into this * \return A reference to this * * \param entity Pointer to handle like an object (can be nullptr) */ template ObjectHandle& ObjectHandle::operator=(T* entity) { Reset(entity); return *this; } /*! * \brief Sets the handle of the ObjectHandle with the handle from another * \return A reference to this * * \param handle The other ObjectHandle */ template ObjectHandle& ObjectHandle::operator=(const ObjectHandle& handle) { Reset(handle); return *this; } /*! * \brief Moves the ObjectHandle into this * \return A reference to this * * \param handle ObjectHandle to move in this */ template ObjectHandle& ObjectHandle::operator=(ObjectHandle&& handle) noexcept { Reset(std::move(handle)); return *this; } /*! * \brief Action to do on object destruction */ template void ObjectHandle::OnObjectDestroyed() noexcept { // Shortcut m_object = nullptr; } /*! * \brief Action to do on object move */ template void ObjectHandle::OnObjectMoved(T* newObject) noexcept { // The object has been moved, update our pointer m_object = newObject; } /*! * \brief Output operator * \return The stream * * \param out The stream * \param handle The ObjectHandle to output */ template std::ostream& operator<<(std::ostream& out, const ObjectHandle& handle) { out << handle.ToString(); return out; } /*! * \brief Checks whether the first object handle is equal to the second object handle * \return true if it is the case * * \param lhs ObjectHandle to compare in left hand side * \param rhs ObjectHandle to compare in right hand side */ template bool operator==(const ObjectHandle& lhs, const ObjectHandle& rhs) { return lhs.GetObject() == rhs.GetObject(); } /*! * \brief Checks whether the object is equal to the second object handle * \return true if it is the case * * \param lhs Object to compare in left hand side * \param rhs ObjectHandle to compare in right hand side */ template bool operator==(const T& lhs, const ObjectHandle& rhs) { return &lhs == rhs.GetObject(); } /*! * \brief Checks whether the object handle is equal to the second object * \return true if it is the case * * \param lhs ObjectHandle to compare in left hand side * \param rhs Object to compare in right hand side */ template bool operator==(const ObjectHandle& lhs, const T& rhs) { return lhs.GetObject() == &rhs; } /*! * \brief Checks whether the first object handle is equal to the second object handle * \return false if it is the case * * \param lhs ObjectHandle to compare in left hand side * \param rhs ObjectHandle to compare in right hand side */ template bool operator!=(const ObjectHandle& lhs, const ObjectHandle& rhs) { return !(lhs == rhs); } /*! * \brief Checks whether the object is equal to the second object handle * \return false if it is the case * * \param lhs Object to compare in left hand side * \param rhs ObjectHandle to compare in right hand side */ template bool operator!=(const T& lhs, const ObjectHandle& rhs) { return !(lhs == rhs); } /*! * \brief Checks whether the object handle is equal to the second object * \return false if it is the case * * \param lhs ObjectHandle to compare in left hand side * \param rhs Object to compare in right hand side */ template bool operator!=(const ObjectHandle& lhs, const T& rhs) { return !(lhs == rhs); } /*! * \brief Checks whether the first object handle is less than the second object handle * \return true if it is the case * * \param lhs ObjectHandle to compare in left hand side * \param rhs ObjectHandle to compare in right hand side */ template bool operator<(const ObjectHandle& lhs, const ObjectHandle& rhs) { return lhs.m_object < rhs.m_object; } /*! * \brief Checks whether the first object handle is less than the second object handle * \return true if it is the case * * \param lhs ObjectHandle to compare in left hand side * \param rhs ObjectHandle to compare in right hand side */ template bool operator<(const T& lhs, const ObjectHandle& rhs) { return &lhs < rhs.m_object; } /*! * \brief Checks whether the first object handle is less than the second object handle * \return true if it is the case * * \param lhs ObjectHandle to compare in left hand side * \param rhs ObjectHandle to compare in right hand side */ template bool operator<(const ObjectHandle& lhs, const T& rhs) { return lhs.m_object < &rhs; } /*! * \brief Checks whether the first object handle is less or equal than the second object handle * \return true if it is the case * * \param lhs ObjectHandle to compare in left hand side * \param rhs ObjectHandle to compare in right hand side */ template bool operator<=(const ObjectHandle& lhs, const ObjectHandle& rhs) { return !(lhs > rhs); } /*! * \brief Checks whether the first object handle is less or equal than the second object handle * \return true if it is the case * * \param lhs ObjectHandle to compare in left hand side * \param rhs ObjectHandle to compare in right hand side */ template bool operator<=(const T& lhs, const ObjectHandle& rhs) { return !(lhs > rhs); } /*! * \brief Checks whether the first object handle is less or equal than the second object handle * \return true if it is the case * * \param lhs ObjectHandle to compare in left hand side * \param rhs ObjectHandle to compare in right hand side */ template bool operator<=(const ObjectHandle& lhs, const T& rhs) { return !(lhs > rhs); } /*! * \brief Checks whether the first object handle is greather than the second object handle * \return true if it is the case * * \param lhs ObjectHandle to compare in left hand side * \param rhs ObjectHandle to compare in right hand side */ template bool operator>(const ObjectHandle& lhs, const ObjectHandle& rhs) { return rhs < lhs; } /*! * \brief Checks whether the first object handle is greather than the second object handle * \return true if it is the case * * \param lhs ObjectHandle to compare in left hand side * \param rhs ObjectHandle to compare in right hand side */ template bool operator>(const T& lhs, const ObjectHandle& rhs) { return rhs < lhs; } /*! * \brief Checks whether the first object handle is greather than the second object handle * \return true if it is the case * * \param lhs ObjectHandle to compare in left hand side * \param rhs ObjectHandle to compare in right hand side */ template bool operator>(const ObjectHandle& lhs, const T& rhs) { return rhs < lhs; } /*! * \brief Checks whether the first object handle is greather or equal than the second object handle * \return true if it is the case * * \param lhs ObjectHandle to compare in left hand side * \param rhs ObjectHandle to compare in right hand side */ template bool operator>=(const ObjectHandle& lhs, const ObjectHandle& rhs) { return !(lhs < rhs); } /*! * \brief Checks whether the first object handle is greather or equal than the second object handle * \return true if it is the case * * \param lhs ObjectHandle to compare in left hand side * \param rhs ObjectHandle to compare in right hand side */ template bool operator>=(const T& lhs, const ObjectHandle& rhs) { return !(lhs < rhs); } /*! * \brief Checks whether the first object handle is greather or equal than the second object handle * \return true if it is the case * * \param lhs ObjectHandle to compare in left hand side * \param rhs ObjectHandle to compare in right hand side */ template bool operator>=(const ObjectHandle& lhs, const T& rhs) { return !(lhs < rhs); } template const ObjectHandle ObjectHandle::InvalidHandle; } namespace std { /*! * \brief Swaps two ObjectHandle, specialisation of std * * \param lhs First object handle * \param rhs Second object handle */ template void swap(Nz::ObjectHandle& lhs, Nz::ObjectHandle& rhs) { lhs.Swap(rhs); } }