/*
	Copyright 2016, 2017 Michele "King_DuckZ" Santullo

	This file is part of MyCurry.

	MyCurry is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	MyCurry 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 General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with MyCurry.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "worldsizenotifiable.hpp"
#include "worldgrid.hpp"
#include <exception>
#include <cassert>
#include <ciso646>

namespace curry {
	WorldSizeNotifiable::WorldSizeNotifiable (const DeferredRegister& parDeferredRegister) :
		m_ticket(cloonel::ObserversManager<WorldSizeNotifiable*>::Ticket_Null),
		m_world(parDeferredRegister.world())
	{
		assert(m_world);
	}

	WorldSizeNotifiable::~WorldSizeNotifiable() noexcept {
		m_world->unregister_observer(m_ticket);
	}

	WorldSizeNotifiable::DeferredRegister::DeferredRegister (WorldGrid* parWorld, WorldSizeNotifiable* parObj) noexcept :
		m_notifiable(parObj),
		m_world(parWorld)
	{
		assert(m_notifiable);
		assert(m_world);
	}

	WorldSizeNotifiable::DeferredRegister::~DeferredRegister() {
		assert(m_notifiable);
		assert(m_world);
		if (not std::uncaught_exception())
			m_notifiable->register_to_world(m_world);
	}

	void WorldSizeNotifiable::register_to_world (WorldGrid* parWorld) {
		assert(cloonel::ObserversManager<WorldSizeNotifiable*>::Ticket_Null == m_ticket);
		m_ticket = parWorld->register_observer(this);
	}
} //namespace curry
