/*

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.

*/

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

namespace oess_1 {

//
// Аналогом NULL является нулевой идентификатор раздела.
// Идентификатор сущности так же устанавливается в 0, но главное,
// что не может быть раздела с нулевым идентификатором.

//
// ent_id_t
//

ent_id_t::ent_id_t() :
	m_slice_id( 0 ),
	m_ent_id( 0 )
{
}

ent_id_t::ent_id_t(
	oess_1::uint_t slice_id,
	oess_1::uint_t ent_id ) :
	m_slice_id( slice_id ),
	m_ent_id( ent_id )
{
}

ent_id_t::ent_id_t(
	const ent_id_t & o ) :
	m_slice_id( 0 ),
	m_ent_id( 0 )
{
	(*this) = o;
}

ent_id_t::~ent_id_t()
{
}

ent_id_t &
ent_id_t::operator=(
	const ent_id_t & o )
{
	if( &o != this ) {
		m_slice_id = o.m_slice_id;
		m_ent_id = o.m_ent_id;
	}

	return *this;
}

ent_id_t::operator bool() const
{
	return ( 0 != m_slice_id );
}

bool
ent_id_t::operator!() const
{
	return ( 0 == m_slice_id );
}

bool
ent_id_t::operator==( const ent_id_t & o ) const
{
	return ( m_slice_id == o.m_slice_id &&
		m_ent_id == o.m_ent_id );
}

bool
ent_id_t::operator!=( const ent_id_t & o ) const
{
	return ( m_slice_id != o.m_slice_id ||
		m_ent_id != o.m_ent_id );
}

bool
ent_id_t::operator<( const ent_id_t & o ) const
{
	return ( m_slice_id < o.m_slice_id ||
		( m_slice_id == o.m_slice_id &&
		m_ent_id < o.m_ent_id ) );
}

void
ent_id_t::make_null()
{
	m_slice_id = 0;
	m_ent_id = 0;
}

// Формат представления ent_id в ostream/istream:
// <slice_id-dec>:<ent_id-dec>

const char	c_separator = ':';

OESS_1__DEFS__FUNC( std::ostream & )
ent_id_to_std_ostream( std::ostream & s, const ent_id_t & o )
{
	std::ios::fmtflags old_format = s.flags();
	s.setf( std::ios::dec, std::ios::basefield );
	s << o.m_slice_id << c_separator << o.m_ent_id;
	s.flags( old_format );

	return s;
}

OESS_1__DEFS__FUNC( std::istream & )
ent_id_from_std_ostream( std::istream & s, ent_id_t & o )
{
	std::ios::fmtflags old_format = s.flags();
	s.setf( std::ios::dec, std::ios::basefield );

	char separator;
	s >> o.m_slice_id >> separator >> o.m_ent_id;
	s.flags( old_format );

	if( c_separator != separator )
		o.make_null();

	return s;
}

} /* namespace oess_1 */
