分享
 
 
 

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

王朝c/c++·作者佚名  2006-01-10
窄屏简体版  字體: |||超大  

文件位置: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 )

{

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

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

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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有