// Copyright (C) 2017 Jérôme Leclercq // This file is part of the "Nazara Development Kit" // For conditions of distribution and use, see copyright notice in Prerequisites.hpp #include #include #include #include #include #include namespace Nz { /*! * \ingroup core * \class Nz::HandledObject * \brief Core class that represents a handled object */ /*! * \brief Constructs a HandledObject object by assignation * * \param object HandledObject to assign into this */ template HandledObject::HandledObject(const HandledObject& object) { NazaraUnused(object); // Don't copy anything, we're a copy of the object, we have no handle right now } /*! * \brief Constructs a HandledObject object by move semantic * * \param object HandledObject to move into this */ template HandledObject::HandledObject(HandledObject&& object) noexcept : m_handleData(std::move(object.m_handleData)) { if (m_handleData) m_handleData->object = static_cast(this); } /*! * \brief Destructs the object and calls UnregisterAllHandles * * \see UnregisterAllHandles */ template HandledObject::~HandledObject() { UnregisterAllHandles(); } /*! * \brief Creates a ObjectHandle for this * \return ObjectHandle to this */ template ObjectHandle HandledObject::CreateHandle() { return ObjectHandle(static_cast(this)); } /*! * \brief Sets the reference of the HandledObject with the handle from another * \return A reference to this * * \param object The other HandledObject */ template HandledObject& HandledObject::operator=(const HandledObject& object) { NazaraUnused(object); // Nothing to do return *this; } /*! * \brief Moves the HandledObject into this * \return A reference to this * * \param object HandledObject to move in this */ template HandledObject& HandledObject::operator=(HandledObject&& object) noexcept { UnregisterAllHandles(); m_handleData = std::move(object.m_handleData); if (m_handleData) m_handleData->object = static_cast(this); return *this; } /*! * \brief Unregisters all handles */ template void HandledObject::UnregisterAllHandles() noexcept { if (m_handleData) { OnHandledObjectDestruction(this); m_handleData->object = nullptr; m_handleData.reset(); } } template std::shared_ptr HandledObject::GetHandleData() { if (!m_handleData) InitHandleData(); return std::shared_ptr(m_handleData); } template void HandledObject::InitHandleData() { assert(!m_handleData); m_handleData = std::make_shared(); m_handleData->object = static_cast(this); } }