📄 mstl_temp_buffer.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_TEMPORARY_BUFFER_HEADER_FILE__
#define __MACRO_CPLUSPLUS_MINI_STL_TEMPORARY_BUFFER_HEADER_FILE__
//-----------------------------------------------------------------------------
#include <cstdlib>
#include <climits>
#include "mstl_pair.hpp"
#include "mstl_type_traits.hpp"
#include "mstl_initialization.hpp"
//-----------------------------------------------------------------------------
__MACRO_CPLUSPLUS_MINI_STL_BEGIN_NAMESPACE__
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
template< typename T >
pair<T*, def_ptrdiff_t> get_temporary_buffer( def_ptrdiff_t len, T *p )
{
def_ptrdiff_t max = std:: numeric_limits<def_ptrdiff_t>::max();
if( len > def_ptrdiff_t(max / sizeof(T)) )
len = max / sizeof(T);
while( len > 0 )
{
T* temp = (T*)std::malloc( len * sizeof(T) );
if( temp )
return pair<T*, def_ptrdiff_t>( temp, len );
len /= 2;
}
return pair<T*, def_ptrdiff_t>( (T*)NULL_POINTER, 0 );
}
template< typename T >
inline void return_temporary_buffer( T *p )
{
if( p )
std::free(p);
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
template< typename ForwardIterator, typename T >
class temporary_buffer
{
public:
temporary_buffer( ForwardIterator first, ForwardIterator last );
~temporary_buffer()
{
destroy( buffer, buffer + len );
std::free( buffer );
}
ptrdiff_t size() const { return len; }
ptrdiff_t requested_size() const { return original_len; }
T* begin() { return buffer; }
T* end() { return buffer + len; }
private:
def_ptrdiff_t original_len;
def_ptrdiff_t len;
T *buffer;
temporary_buffer( const temporary_buffer& );
temporary_buffer& operator=( const temporary_buffer& );
void init_buffer( const T&, true_type ) {}
void init_buffer( const T& value, false_type )
{ init_fill_n( buffer, len, value ); }
void allocate_buffer();
}; //end class
template< typename ForwardIterator, typename T >
temporary_buffer<ForwardIterator, T>::
temporary_buffer( ForwardIterator first, ForwardIterator last )
: original_len(0)
{
typedef typename type_traits<T>::has_trivial_default_constructor dc;
len = distance( first, last );
try
{
allocate_buffer();
if( len > 0 )
init_buffer( *first, dc() );
}
catch(...)
{
std::free( buffer );
buffer = NULL_POINTER;
len = 0;
}
}
template< typename ForwardIterator, typename T >
void temporary_buffer<ForwardIterator, T>::allocate_buffer()
{
original_len = len;
buffer = NULL_POINTER;
if( len > def_ptrdiff_t(INT_MAX / sizeof(T)) )
len = INT_MAX / sizeof(T);
while( len > 0 )
{
buffer = (T*)std::malloc( len * sizeof(T) );
if( buffer )
return;
len /= 2;
}
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
__MACRO_CPLUSPLUS_MINI_STL_END_NAMESPACE__
#endif
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -