// Copyright (C) 2015 Jérôme Leclercq // This file is part of the "Nazara Development Kit" // For conditions of distribution and use, see copyright notice in Prerequesites.hpp #include #include #include #include #include namespace Nz { template HandledObject::HandledObject(const HandledObject& object) { // Don't copy anything, we're a copy of the object, we have no handle right now } template HandledObject::HandledObject(HandledObject&& object) : m_handles(std::move(object.m_handles)) { for (ObjectHandle* handle : m_handles) handle->OnObjectMoved(static_cast(this)); } template HandledObject::~HandledObject() { UnregisterAllHandles(); } template ObjectHandle HandledObject::CreateHandle() { return ObjectHandle(static_cast(this)); } template HandledObject& HandledObject::operator=(const HandledObject& object) { // Nothing to do return *this; } template HandledObject& HandledObject::operator=(HandledObject&& object) { m_handles = std::move(object.m_handles); for (ObjectHandle* handle : m_handles) handle->OnObjectMoved(static_cast(this)); return *this; } template void HandledObject::RegisterHandle(ObjectHandle* handle) { ///DOC: Un handle ne doit être enregistré qu'une fois, des erreurs se produisent s'il l'est plus d'une fois m_handles.push_back(handle); } template void HandledObject::UnregisterAllHandles() { // Tell every handle we got destroyed, to null them for (ObjectHandle* handle : m_handles) handle->OnObjectDestroyed(); m_handles.clear(); } template void HandledObject::UnregisterHandle(ObjectHandle* handle) noexcept { ///DOC: Un handle ne doit être libéré qu'une fois, et doit faire partie de la liste, sous peine de crash auto it = std::find(m_handles.begin(), m_handles.end(), handle); NazaraAssert(it != m_handles.end(), "Handle not registered"); // Swap and pop idiom, more efficient than vector::erase std::swap(*it, m_handles.back()); m_handles.pop_back(); } template void HandledObject::UpdateHandle(ObjectHandle* oldHandle, ObjectHandle* newHandle) noexcept { auto it = std::find(m_handles.begin(), m_handles.end(), oldHandle); NazaraAssert(it != m_handles.end(), "Handle not registered"); // Simply update the handle *it = newHandle; } }