/*

smart_ref_3: Smart Reference library
Yauheni A. Akhotnikau (C) 2001-2003
eao197@yahoo.com
-------------------------------------------------

Permission is granted to anyone to use this software for any purpose on any
computer system, and to redistribute it freely, subject to the following
restrictions:

1. This software 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.

2. The origin of this software must not be misrepresented, either by
   explicit claim or by omission.

3. Altered versions must be plainly marked as such, and must not be
   misrepresented as being the original software.

-------------------------------------------------

*/

/*!
	\file
	\brief Основные классы и макросы.
*/

#include <assert.h>

//#define SMART_PTR_VERBOSE

#if defined( SMART_PTR_VERBOSE )
#include <stdio.h>
#endif

#include <smart_ref_3/h/smart_ref.hpp>

namespace smart_ref_3 {

//
// ref_data_t
//
ref_data_t::ref_data_t(
	void * ptr,
	pfn_destroyer_t destroyer ) :
	m_ptr( ptr ),
	m_ref_count( 1 ),
	m_destroyer( destroyer ) {
	assert( ptr );

#if defined( SMART_PTR_VERBOSE )
	printf( "%p\tsmart_ref_2::ref_data_t( %p, %lu, %p )\n",
		this, m_ptr, m_ref_count, m_destroyer );
#endif
}

ref_data_t::~ref_data_t() {
#if defined( SMART_PTR_VERBOSE )
	printf( "%p\tsmart_ref_2::~ref_data_t( %p, %lu, %p )\n",
		this, m_ptr, m_ref_count, m_destroyer );
#endif

	if( m_ptr ) {
		// Defense aganist exceptions
		register void * ptr = m_ptr;
		m_ptr = 0;
		m_ref_count = 0;

		(*m_destroyer)( ptr );
	}
}

ref_data_t *
ref_data_t::dec_ref_count(
	ref_data_t * ref_data ) {
	if( ref_data ) {
#if defined( SMART_PTR_VERBOSE )
		printf( "dec_ref_count( %p, %lu, %p )\n",
			ref_data, ref_data->m_ref_count,
			ref_data->m_destroyer );
#endif

		if( ref_data->m_ref_count )
			--ref_data->m_ref_count;
		if( !ref_data->m_ref_count ) {
			delete ref_data;
			ref_data = 0;
		}
	}

	return ref_data;
}

ref_data_t *
ref_data_t::inc_ref_count(
	ref_data_t * ref_data ) {
	if( ref_data ) {
#if defined( SMART_PTR_VERBOSE )
		printf( "inc_ref_count( %p, %lu, %p )\n",
			ref_data, ref_data->m_ref_count,
			ref_data->m_destroyer );
#endif

		++ref_data->m_ref_count;
	}

	return ref_data;
}

//
// ref_t
//

ref_t::ref_t(
	const ref_t & o ) :
	m_ref_data( 0 ) {
	m_ref_data = ref_data_t::inc_ref_count( o.m_ref_data );

#if defined( SMART_PTR_VERBOSE )
	printf( "%p\tsmart_ref_2::ref_t( %p )\n",
		this, m_ref_data );
#endif
}

ref_t::ref_t() :
	m_ref_data( 0 ) {

#if defined( SMART_PTR_VERBOSE )
	printf( "%p\tsmart_ref_2::ref_t()\n", this );
#endif
}

ref_t::~ref_t() {
#if defined( SMART_PTR_VERBOSE )
	printf( "%p\tsmart_ref_2::~ref_t()\n",	this );
#endif

	ref_data_t::dec_ref_count( m_ref_data );
}

void
ref_t::set_ref_data(
	ref_data_t * ref_data ) {
	assert( !m_ref_data );
	assert( ref_data );

	m_ref_data = ref_data;

#if defined( SMART_PTR_VERBOSE )
	printf( "%p\tset_ref_data( %p )\n",
		this, m_ref_data );
#endif
}

ref_count_t
ref_t::query_ref_count() const {
	if( m_ref_data )
		return m_ref_data->query_ref_count();

	return 0;
}

void
ref_t::destroy() {
	if( m_ref_data ) {
#if defined( SMART_PTR_VERBOSE )
		printf( "%p\tdestroy( %p )\n",
			this, m_ref_data );
#endif

		// Defense aganist exceptions
		register ref_data_t * tmp = m_ref_data;
	    m_ref_data = 0;
		ref_data_t::dec_ref_count( tmp );
	}
}

} /* namespace smart_ref_2 */
