/*

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/ex.hpp>

#include <oess_1/defs/log/h/err.hpp>

#include <oess_1/scheme/h/errno.hpp>
#include <oess_1/scheme/h/scheme.hpp>

namespace oess_1 {

namespace scheme {

//
// attr_editor_t
//

attr_editor_t::~attr_editor_t()
{
}


//
// cpp_mapping_editor_t
//

cpp_mapping_editor_t::~cpp_mapping_editor_t()
{
}


//
// type_editor_t
//

type_editor_t::~type_editor_t()
{
}

//
// scheme_t
//

scheme_t::~scheme_t()
{
}

//
// Эти функции реализованы в impl/scheme_impl.cpp
//
namespace impl {

scheme_t *
create_std_scheme();

void
destroy_std_scheme( scheme_t * ptr );

} /* namespace impl */

//
// scheme_auto_ptr_t
//

scheme_auto_ptr_t::scheme_auto_ptr_t() :
	m_scheme( 0 )
{
	m_scheme = impl::create_std_scheme();
}

scheme_auto_ptr_t::~scheme_auto_ptr_t()
{
	impl::destroy_std_scheme( m_scheme );
}

scheme_t &
scheme_auto_ptr_t::operator*()
{
	return *m_scheme;
}

const scheme_t &
scheme_auto_ptr_t::operator*() const
{
	return *m_scheme;
}

scheme_t *
scheme_auto_ptr_t::operator->()
{
	return m_scheme;
}

const scheme_t *
scheme_auto_ptr_t::operator->() const
{
	return m_scheme;
}

scheme_t *
scheme_auto_ptr_t::query()
{
	return m_scheme;
}

const scheme_t *
scheme_auto_ptr_t::query() const
{
	return m_scheme;
}

//
// attr_editor_auto_ptr_t
//

attr_editor_auto_ptr_t::attr_editor_auto_ptr_t(
	type_editor_t & type_editor,
	const std::string & attr_name,
	const std::string & type_name ) :
	m_type_editor( type_editor ),
	m_attr_editor( 0 )
{
	m_attr_editor = &( type_editor.start_attr_def(
		attr_name, type_name ) );
}

attr_editor_auto_ptr_t::~attr_editor_auto_ptr_t()
{
	if( m_attr_editor ) {
		try {
			m_type_editor.rollback_attr_def();
		}
		catch( ex_t & x ) {
			OESS_1_LOG_ERR( x.query_err_code() << std::endl )
		}
	}
}

attr_editor_t &
attr_editor_auto_ptr_t::operator*()
{
	return *m_attr_editor;
}

const attr_editor_t &
attr_editor_auto_ptr_t::operator*() const
{
	return *m_attr_editor;
}

attr_editor_t *
attr_editor_auto_ptr_t::operator->()
{
	return m_attr_editor;
}

const attr_editor_t *
attr_editor_auto_ptr_t::operator->() const
{
	return m_attr_editor;
}

attr_editor_t *
attr_editor_auto_ptr_t::query()
{
	return m_attr_editor;
}

const attr_editor_t *
attr_editor_auto_ptr_t::query() const
{
	return m_attr_editor;
}

void
attr_editor_auto_ptr_t::commit()
{
	if( m_attr_editor ) {
		// Защита против исключений.
		m_attr_editor = 0;

		m_type_editor.commit_attr_def();
	}
	else
		OESS_THROW_PHYSIC( oess_1::scheme::err::c_zero_pointer,
			"attr_editor_auto_ptr_t::commit()" )
}

void
attr_editor_auto_ptr_t::rollback()
{
	if( m_attr_editor ) {
		// Защита против исключений.
		m_attr_editor = 0;

		m_type_editor.rollback_attr_def();
	}
	else
		OESS_THROW_PHYSIC( oess_1::scheme::err::c_zero_pointer,
			"attr_editor_auto_ptr_t::rollback()" )
}

//
// type_editor_auto_ptr_t
//

type_editor_auto_ptr_t::type_editor_auto_ptr_t(
	scheme_t & scheme,
	const std::string & type_name ) :
	m_scheme( scheme ),
	m_type_editor( 0 )
{
	m_type_editor = &( m_scheme.start_type_def( type_name ) );
}

type_editor_auto_ptr_t::~type_editor_auto_ptr_t()
{
	if( m_type_editor ) {
		try {
			m_scheme.rollback_type_def();
		}
		catch( ex_t & x ) {
			OESS_1_LOG_ERR( x.query_err_code() << std::endl )
		}
	}
}

type_editor_t &
type_editor_auto_ptr_t::operator*()
{
	return *m_type_editor;
}

const type_editor_t &
type_editor_auto_ptr_t::operator*() const
{
	return *m_type_editor;
}

type_editor_t *
type_editor_auto_ptr_t::operator->()
{
	return m_type_editor;
}

const type_editor_t *
type_editor_auto_ptr_t::operator->() const
{
	return m_type_editor;
}

type_editor_t *
type_editor_auto_ptr_t::query()
{
	return m_type_editor;
}

const type_editor_t *
type_editor_auto_ptr_t::query() const
{
	return m_type_editor;
}

void
type_editor_auto_ptr_t::commit()
{
	if( m_type_editor ) {
		// Защита против исключений.
		m_type_editor = 0;

		m_scheme.commit_type_def();
	}
	else
		OESS_THROW_PHYSIC( oess_1::scheme::err::c_zero_pointer,
			"type_editor_auto_ptr_t::commit()" )
}

void
type_editor_auto_ptr_t::rollback()
{
	if( m_type_editor ) {
		// Защита против исключений.
		m_type_editor = 0;

		m_scheme.rollback_type_def();
	}
	else
		OESS_THROW_PHYSIC( oess_1::scheme::err::c_zero_pointer,
			"type_editor_auto_ptr_t::rollback()" )
}

} /* scheme */

} /* namespace oess_1 */

