C++ young 程序库——y_allocator.hpp 、y_construct.hpp 和 y_exception.hpp

来源:互联网 发布:sql的功能包括 编辑:程序博客网 时间:2024/06/06 02:41

文件位置:young/y_allocator.hpp

/*
The young Library
Copyright (c) 2005 by 杨桓

Permission to use, copy, modify, distribute and sell this software for any
purpose is hereby granted without fee, provided that the above copyright
notice appear in all copies and that both that copyright notice and this
permission notice appear in supporting documentation.
The author make no representations about the suitability of this software
for any purpose. It is provided "as is" without express or implied warranty.
*/

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
#ifndef __MACRO_CPLUSPLUS_YOUNG_LIBRARY_ALLOCATOR_HEADER_FILE__
#define __MACRO_CPLUSPLUS_YOUNG_LIBRARY_ALLOCATOR_HEADER_FILE__
//-----------------------------------------------------------------------------
#include <new>
#include "y_define.hpp"
//-----------------------------------------------------------------------------
__MACRO_CPLUSPLUS_YOUNG_LIBRARY_BEGIN_NAMESPACE__
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------

template< typename T >
class allocator
{
public:
    typedef  T                  value_type;
    typedef  value_type&        reference;
    typedef  const value_type&  const_reference;
    typedef  value_type*        pointer;
    typedef  const value_type*  const_pointer;
    typedef  value_type*        iterator;
    typedef  const value_type*  const_iterator;
    typedef  def_size_t         size_type;
    typedef  def_ptrdiff_t      difference_type;
    typedef  allocator<T>       self;

    template< typename U >
    struct rebind
    {
        typedef  allocator<U>  other;
    };

    template< typename U >
    allocator( const allocator<U>& ) throw()  {}

    template< typename U >
    self& operator=( const allocator<U>& ) throw()  {  return *this;  }

    allocator() throw()  {}

    allocator( const self& ) throw()  {}

    self& operator=( const self& ) throw()  {  return *this;  }

    ~allocator() throw()  {}

    pointer address( reference X ) throw()  {  return ( &X );  }

    const_pointer address( const_reference X ) throw()  {  return ( &X );  }

    size_type max_size()
    {
        return size_type( size_t_max / sizeof(T) );
    }

    pointer allocate( size_type n, const void* = NULL_POINTER )
    {
        return (pointer)( operator new( sizeof(T) * n ) );
    }

    void deallocate( pointer ptr, size_type n = 0 )
    {
        operator delete( ptr );
    }

    void construct( pointer ptr, const T& value )
    {
        new(ptr) T(value);
    }

    void destroy( pointer ptr )
    {
        ptr->~T();
    }

};  //end allocator

template<>
class allocator<void>
{
public:
    typedef  void*            pointer;
    typedef  const void*      const_pointer;
    typedef  void             value_type;
    typedef  def_ptrdiff_t    difference_type;
    typedef  allocator<void>  self;

    template< typename U >
    struct rebind
    {
        typedef  allocator<U>  other;
    };
};

template< typename T >
inline bool operator==( const allocator<T>&, const allocator<T>& ) throw()
{
    return true;
}

template< typename T >
inline bool operator!=( const allocator<T>&, const allocator<T>& ) throw()
{
    return false;
}

template< typename T, typename U >
inline bool operator==( const allocator<T>&, const allocator<U>& ) throw()
{
    return false;
}

template< typename T, typename U >
inline bool operator!=( const allocator<T>&, const allocator<U>& ) throw()
{
    return true;
}

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
__MACRO_CPLUSPLUS_YOUNG_LIBRARY_END_NAMESPACE__
#endif
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------

文件位置:young/y_construct.hpp

/*
The young Library
Copyright (c) 2005 by 杨桓

Permission to use, copy, modify, distribute and sell this software for any
purpose is hereby granted without fee, provided that the above copyright
notice appear in all copies and that both that copyright notice and this
permission notice appear in supporting documentation.
The author make no representations about the suitability of this software
for any purpose. It is provided "as is" without express or implied warranty.
*/

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
#ifndef __MACRO_CPLUSPLUS_YOUNG_LIBRARY_CONSTRUCT_HEADER_FILE__
#define __MACRO_CPLUSPLUS_YOUNG_LIBRARY_CONSTRUCT_HEADER_FILE__
//-----------------------------------------------------------------------------
#include "y_iterator.hpp"
//-----------------------------------------------------------------------------
__MACRO_CPLUSPLUS_YOUNG_LIBRARY_BEGIN_NAMESPACE__
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------

template< typename T1, typename T2 >
inline void construct( T1* ptr, const T2& value )
{
    typedef  typename type_traits<T1>::is_POD_type  POD;
    construct_aux( ptr, value, POD() );
}

template< typename T1, typename T2 >
inline void construct_aux( T1* ptr, const T2& value, false_type )
{
    new(ptr) T1(value);
}

template< typename T1, typename T2 >
inline void construct_aux( T1* ptr, const T2& value, true_type )
{
    *ptr = value;
}

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------

template< typename T >
inline void destroy( T* ptr )
{
    typedef  typename type_traits<T>::has_trivial_destructor  destructor;
    destroy_aux( ptr, destructor() );
}

template< typename T >
inline void destroy_aux( T* ptr, true_type )  {}

template< typename T >
inline void destroy_aux( T* ptr, false_type )  {  ptr->~T();  }

template< typename ForwardIterator >
inline void destroy( ForwardIterator first, ForwardIterator last )
{
    typedef  typename iterator_traits<ForwardIterator>::iterator_category
             cate;
    typedef  typename iterator_traits<ForwardIterator>::value_type  value_t;
    typedef  typename type_traits<value_t>::has_trivial_destructor  destructor;

    destroy_aux( first, last, destructor(), cate() );
}

template< typename ForwardIterator >
inline void destroy_aux( ForwardIterator first, ForwardIterator last,
                         true_type, forward_iterator_tag )  {}

template< typename ForwardIterator >
void destroy_aux( ForwardIterator first, ForwardIterator last,
                  false_type x, forward_iterator_tag )
{
    ForwardIterator temp;
    while( first != last )
    {
        temp = first;
        ++first;
        destroy_aux( &(*temp), x );
    }
}

template< typename ForwardIterator >
void destroy_aux( ForwardIterator first, ForwardIterator last,
                  false_type x, bidirectional_iterator_tag )
{
     if( first != last )
    {
        ForwardIterator temp = --last;
        while( last != first )
        {
            --last;
            destroy_aux( &(*temp), x );
            temp = last;
        }
        destroy_aux( &(*first), x );
    }
}

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
__MACRO_CPLUSPLUS_YOUNG_LIBRARY_END_NAMESPACE__
#endif
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------



文件位置:young/y_exception.hpp

/*
The young Library
Copyright (c) 2005 by 杨桓

Permission to use, copy, modify, distribute and sell this software for any
purpose is hereby granted without fee, provided that the above copyright
notice appear in all copies and that both that copyright notice and this
permission notice appear in supporting documentation.
The author make no representations about the suitability of this software
for any purpose. It is provided "as is" without express or implied warranty.
*/

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
#ifndef __MACRO_CPLUSPLUS_YOUNG_LIBRARY_EXCEPTION_HEADER_FILE__
#define __MACRO_CPLUSPLUS_YOUNG_LIBRARY_EXCEPTION_HEADER_FILE__
//-----------------------------------------------------------------------------
#include <stdexcept>
#include "y_define.hpp"
//-----------------------------------------------------------------------------
__MACRO_CPLUSPLUS_YOUNG_LIBRARY_BEGIN_NAMESPACE__
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------

inline void throw_logic_error( const char* error_info )
{
    throw std::logic_error( error_info );
}


inline void throw_domain_error( const char* error_info )
{
    throw std::domain_error( error_info );
}


inline void throw_invalid_argument( const char* error_info )
{
    throw std::invalid_argument( error_info );
}


inline void throw_length_error( const char* error_info )
{
    throw std::length_error( error_info );
}


inline void throw_out_of_range( const char* error_info )
{
    throw std::out_of_range( error_info );
}


inline void throw_runtime_error( const char* error_info )
{
    throw std::runtime_error( error_info );
}


inline void throw_overflow_error( const char* error_info )
{
    throw std::overflow_error( error_info );
}


inline void throw_range_error( const char* error_info )
{
    throw std::range_error( error_info );
}


inline void throw_underflow_error( const char* error_info )
{
    throw std::underflow_error( error_info );
}

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
__MACRO_CPLUSPLUS_YOUNG_LIBRARY_END_NAMESPACE__
#endif
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------


原创粉丝点击