/**
 * @file
 * $Id$
 * $Revision$
 * $Author$
 * $Date$
 *
 * This file is part of The iWear Framework.
 *
 * The iWear Framework is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by the
 * Free Software Foundation as in version 2 of the License.
 * 
 * The iWear Framework 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.  See the GNU General Public License for
 * more details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * The iWear Framework; if not, write to the Free Software Foundation, Inc., 59
 * Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#ifndef __IWEAR_RELATIVE_PTR_H
#define __IWEAR_RELATIVE_PTR_H

#include <cstddef>

namespace iwear
{

template<class T, class OFF_T = ptrdiff_t>
class relative_ptr 
{
private:
    typedef unsigned char* offset_ptr_t;
    typedef const unsigned char* const_offset_ptr_t;
    OFF_T offset;
    T* get_ptr( );
    const T* get_ptr( ) const;
protected:
public:
   relative_ptr( T* );

   relative_ptr( const relative_ptr<T,OFF_T>& );

   template<class S>
   relative_ptr<T,OFF_T>& operator=( const relative_ptr<S>& );
   relative_ptr<T,OFF_T>& operator=( const relative_ptr<T,OFF_T>& );

   T* operator->() { return get_ptr(); }
   T& operator*() { return *get_ptr(); }
   operator T* () { return get_ptr(); }
   operator const T* () const { return get_ptr(); }
};

template<class T, class OFF_T>
inline
relative_ptr<T,OFF_T>::relative_ptr( const relative_ptr<T,OFF_T>& t )
   : offset( reinterpret_cast<const_offset_ptr_t>(t.get_ptr()) - reinterpret_cast<offset_ptr_t>(this) )
{
//    std::cout << __PRETTY_FUNCTION__ << std::endl;
    if( sizeof(OFF_T) < sizeof(void*) && t.offset == 0)
    {
	offset = 0;
    }
}

template<class T, class OFF_T>
template<class S>
inline
relative_ptr<T,OFF_T>& relative_ptr<T,OFF_T>::operator=( const relative_ptr<S>& t )
{
//    std::cout << __PRETTY_FUNCTION__ << std::endl;
    const S* tmp = t;
    if( sizeof(OFF_T) < sizeof(void*) && t.offset == 0)
    {
	offset = 0;
    }
    else
    {
	offset = reinterpret_cast<offset_ptr_t>(tmp) - reinterpret_cast<offset_ptr_t>(this);
    }
   return *this;
}

template<class T, class OFF_T>
inline
relative_ptr<T,OFF_T>& relative_ptr<T,OFF_T>::operator=( const relative_ptr<T,OFF_T>& t )
{
//    std::cout << __PRETTY_FUNCTION__ << std::endl;
    const T* tmp = t;
    if( sizeof(OFF_T) < sizeof(void*) && t.offset == 0)
    {
	offset = 0;
    }
    else
    {
	offset = reinterpret_cast<const_offset_ptr_t>(tmp) - reinterpret_cast<offset_ptr_t>(this);
    }
   return *this;
}

template<class T, class OFF_T>
inline
relative_ptr<T,OFF_T>::relative_ptr( T* t)
   : offset( reinterpret_cast<offset_ptr_t>(t) - reinterpret_cast<offset_ptr_t>(this) )
{
    if( sizeof(OFF_T) < sizeof(void*) && t == 0)
    {
	offset = 0;
    }
}

template<class T, class OFF_T>
inline
T* relative_ptr<T,OFF_T>::get_ptr( )
{
//    std::cout << __PRETTY_FUNCTION__ << std::endl;
    if( sizeof(OFF_T) < sizeof(void*) && offset == 0 )
    {
	return 0;
    }
    return reinterpret_cast<T*>( reinterpret_cast<offset_ptr_t>(this) + offset );
}

template<class T, class OFF_T>
inline
const T* relative_ptr<T,OFF_T>::get_ptr( ) const
{
//    std::cout << __PRETTY_FUNCTION__ << std::endl;
    if( sizeof(OFF_T) < sizeof(void*) && offset == 0 )
    {
	return 0;
    }
    return reinterpret_cast<const T*>( reinterpret_cast<const_offset_ptr_t>(this) + offset );
}

}

#endif
