📄 mstl_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.
*/
/*
* This file is derived from software bearing the following
* restrictions:
*
* Copyright (c) 1994
* Hewlett-Packard Company
*
* Permission to use, copy, modify, distribute and sell this
* software and its documentation 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.
* Hewlett-Packard Company makes no representations about the
* suitability of this software for any purpose. It is provided
* "as is" without express or implied warranty.
*/
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
#ifndef __MACRO_CPLUSPLUS_MINI_STL_ALLOCATOR_HEADER_FILE__
#define __MACRO_CPLUSPLUS_MINI_STL_ALLOCATOR_HEADER_FILE__
//-----------------------------------------------------------------------------
#include <new>
#include "mstl_define.hpp"
//-----------------------------------------------------------------------------
__MACRO_CPLUSPLUS_MINI_STL_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;
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
template< typename T >
class pool_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 pool_allocator<T> self;
template< typename U >
struct rebind
{
typedef allocator<U> other;
};
template< typename U >
struct rebind_pool
{
typedef pool_allocator<U> other;
};
template< typename U >
pool_allocator( const pool_allocator<U>& rhs ) : obj_count(0) {}
template< typename U >
self& operator=( const pool_allocator<U>& rhs ) { return *this; }
pool_allocator() : obj_count(0) {}
pool_allocator( const self& rhs ) : obj_count(0) {}
self& operator=( const self& rhs ) { return *this; }
~pool_allocator() {}
pointer address( reference X ) { return ( &X ); }
const_pointer address( const_reference X ) { return ( &X ); }
size_type max_size()
{
return size_type( size_t_max / sizeof(T) );
}
private:
size_type obj_count;
}; //end pool_allocator
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
__MACRO_CPLUSPLUS_MINI_STL_END_NAMESPACE__
#endif
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -