/*

Copyright (c) 2002-2005, Yauheni Akhotnikau
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

- Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

- Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.

- The name of the author may not be used to endorse or promote products derived
from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.

*/

/*!
	\file
	\brief Стандартная реализация интерфейса client_map_t
*/
#include <set>

#include <oess_1/defs/h/ex.hpp>

#include <oess_1/db/impl/db_content/h/errno.hpp>
#include <oess_1/db/impl/db_content/h/client_map.hpp>

namespace oess_1 {

namespace db {

namespace impl {

namespace db_content {

//
// std_client_map_t
//

/*!
	\brief Стандартная реализация интерфейса client_map_t
*/
class	std_client_map_t :
	public client_map_t {
	public :
		std_client_map_t();
		virtual ~std_client_map_t();

		virtual void
		attach(
			const std::string & client_id );

		virtual void
		detach(
			const std::string & client_id );

		virtual oess_1::uint_t
		count() const;

		virtual void
		find(
			const std::string & client_id ) const;

	private :
		//! Тип множества имен подключенных клиентов.
		typedef std::set< std::string >	str_set_t;

		//! Множество имен подключенных клиентов.
		str_set_t	m_clients;

		//! Проверить возможность подключения клиента.
		/*!	Если такой клиент уже подключен, то порождается
			исключение. */
		void
		is_client_can_be_attached(
			const std::string & client_id );
};

std_client_map_t::std_client_map_t()
{
}

std_client_map_t::~std_client_map_t()
{
}

void
std_client_map_t::attach(
	const std::string & client_id )
{
	is_client_can_be_attached( client_id );

	// Если исключения возникло, значит клиента можно
	// подключать.
	m_clients.insert( client_id );
}

void
std_client_map_t::detach(
	const std::string & client_id )
{
	m_clients.erase( client_id );
}

oess_1::uint_t
std_client_map_t::count() const
{
	return m_clients.size();
}

void
std_client_map_t::find(
	const std::string & client_id ) const
{
	if( m_clients.find( client_id ) == m_clients.end() )
		OESS_THROW_LOGIC( err::c_client_not_attached,
			"client_id: " << client_id )
}

void
std_client_map_t::is_client_can_be_attached(
	const std::string & client_id )
{
	if( m_clients.find( client_id ) != m_clients.end() )
		OESS_THROW_LOGIC( err::c_client_attached,
			"client_id: " << client_id )
}

//
// client_map_t
//

client_map_t::client_map_t()
{
}

client_map_t::~client_map_t()
{
}

//
// create_std_client_map
//

std::auto_ptr< client_map_t >
create_std_client_map()
{
	return std::auto_ptr< client_map_t >(
			new std_client_map_t() );
}

} /* namespace db_content */

} /* namespace impl */

} /* namespace db */

} /* namespace oess_1 */
