/*

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 oess_1/defs/h/ex.hpp
	\brief Классы исключений.
*/

#if !defined( _OESS_1__DEFS__EX_HPP_ )
#define _OESS_1__DEFS__EX_HPP_

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

#include <sstream>
#include <exception>

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

namespace oess_1 {

//
// ex_t
//

/*!
	Базовый класс для всех исключений.
*/
class	OESS_1__DEFS__TYPE	ex_t :
	public std::exception
{
	public :
		ex_t( const err_code_t & err );
		ex_t( const ex_t & x );
		virtual ~ex_t() throw();

		ex_t &
		operator=( const ex_t & x );

		const err_code_t &
		query_err_code() const;

		/*! Описание исключения.
			Для совместимости с STL исключениями. */
		virtual const char *
		what() const throw();

	private :
		err_code_t	m_err;

		/*! Текстовое описание иключения для метода what(). */
		std::string	m_text_representation;

		/*! Создание текстового представления для what(). */
		void
		make_text_representation() throw();
};

//
// logic_ex_t
//

/*!
	Класс для исключений,
	вызванных логическими ошибками.
*/
class	OESS_1__DEFS__TYPE	logic_ex_t : public ex_t {
	public :
		logic_ex_t( const err_code_t & err );
		logic_ex_t( const logic_ex_t & x );
		virtual ~logic_ex_t() throw();

		logic_ex_t &
		operator=( const logic_ex_t & x );
};

//
// physic_ex_t
//

/*!
	Класс для исключений,
	вызванных физическими (фатальными) ошибками.
*/
class	OESS_1__DEFS__TYPE	physic_ex_t : public ex_t {
	public :
		physic_ex_t( const err_code_t & err );
		physic_ex_t( const physic_ex_t & x );
		virtual ~physic_ex_t() throw();

		physic_ex_t &
		operator=( const physic_ex_t & x );
};

//
// Макросы для более легкого информирования об ошибках
//

/*!
	Сгенерировать логическое исключение.

	\param	c	Целочисленный код ошибки.
	\param	m	Выражение, формирующее описание ошибки
		(например: "file not found: " << file_name << ", reason: "
		<< error_reason).
*/
#define OESS_THROW_LOGIC( c, m )\
{ \
	std::ostringstream msg; \
	msg << m; \
	throw oess_1::logic_ex_t( oess_1_make_err( c, msg.str() ) ); \
}

/*!
	Сгенерировать физическое исключение.

	\param	c	Целочисленный код ошибки.
	\param	m	Выражение, формирующее описание ошибки
		(например: "file not found: " << file_name << ", reason: "
		<< error_reason).
*/
#define OESS_THROW_PHYSIC( c, m )\
{ \
	std::ostringstream msg; \
	msg << m; \
	throw oess_1::physic_ex_t( oess_1_make_err( c, msg.str() ) ); \
}

} /* namespace oess_1 */

#endif
