突发其想,也是预谋已久,把我想到方法贴出来和大家分享,有兴趣看看,水平有限,多指教。
代码:
// TEMPLATE FUNCTION _Allocate
template<class _Ty> inline
_Ty _FARQ *POD_Allocate(_SIZT _Count, _Ty _FARQ *)
{ // allocate storage for _Count elements of type _Ty
return ((_Ty _FARQ *)appMemAlloc( _Count * sizeof (_Ty), typeid(_Ty).name(), __FILE__, __LINE__ ));
}
// TEMPLATE FUNCTION _Construct
template<class _T1,
class _T2> inline
void POD_Construct(_T1 _FARQ *_Ptr, const _T2& _Val)
{ // construct object at _Ptr with value _Val
new ((void _FARQ *)_Ptr) _T1(_Val);
}
// TEMPLATE FUNCTION _Destroy
template<class _Ty> inline
void POD_Destroy(_Ty _FARQ *_Ptr)
{ // destroy POD object at _Ptr (do nothing)
}
template <class _Ty> class POD_allocator : public _Allocator_base<_Ty>
{
public:
typedef _Allocator_base<_Ty> _Mybase;
typedef typename _Mybase::value_type value_type;
typedef value_type _FARQ *pointer;
typedef value_type _FARQ& reference;
typedef const value_type _FARQ *const_pointer;
typedef const value_type _FARQ& const_reference;
typedef _SIZT size_type;
typedef _PDFT difference_type;
public:
template<class _Other>
struct rebind
{ // convert an allocator<_Ty> to an allocator <_Other>
typedef POD_allocator<_Other> other;
};
public:
pointer address(reference _Val) const
{ // return address of mutable _Val
return (&_Val);
}
const_pointer address(const_reference _Val) const
{ // return address of nonmutable _Val
return (&_Val);
}
pointer allocate(size_type _Count)
{ // allocate array of _Count elements
return (POD_Allocate(_Count, (pointer)0));
}
pointer allocate(size_type _Count, const void _FARQ *)
{ // allocate array of _Count elements, ignore hint
return (allocate(_Count));
}
void deallocate(pointer _Ptr, size_type)
{ // deallocate object at _Ptr, ignore size
appMemFree(_Ptr);
}
void construct(pointer _Ptr, const _Ty& _Val)
{ // construct object at _Ptr with value _Val
POD_Construct(_Ptr, _Val);
}
void destroy(pointer _Ptr)
{ // destroy object at _Ptr
POD_Destroy(_Ptr);
}
_SIZT max_size() const
{ // estimate maximum array size
_SIZT _Count = (_SIZT)(-1) / sizeof (_Ty);
return (0 < _Count ? _Count : 1);
}
public:
POD_allocator()
{ // construct default allocator (do nothing)
}
POD_allocator(const POD_allocator<_Ty>&)
{ // construct by copying (do nothing)
}
template<class _Other>
POD_allocator(const POD_allocator<_Other>&)
{ // construct from a related allocator (do nothing)
}
template<class _Other>
POD_allocator<_Ty>& operator=(const POD_allocator<_Other>&)
{ // assign from a related allocator (do nothing)
return (*this);
}
};
// allocator TEMPLATE OPERATORS
template<class _Ty,
class _Other> inline
bool operator==(const POD_allocator<_Ty>&, const POD_allocator<_Other>&)
{ // test for allocator equality (always true)
return (true);
}
template<class _Ty,
class _Other> inline
bool operator!=(const POD_allocator<_Ty>&, const POD_allocator<_Other>&)
{ // test for allocator inequality (always false)
return (false);
}
// CLASS allocator<void>
template<> class _CRTIMP2 POD_allocator<void>
{ // generic allocator for type void
public:
typedef void _Ty;
typedef _Ty _FARQ *pointer;
typedef const _Ty _FARQ *const_pointer;
typedef _Ty value_type;
template<class _Other>
struct rebind
{ // convert an allocator<void> to an allocator <_Other>
typedef POD_allocator<_Other> other;
};
POD_allocator()
{ // construct default allocator (do nothing)
}
POD_allocator(const POD_allocator<_Ty>&)
{ // construct by copying (do nothing)
}
template<class _Other>
POD_allocator(const POD_allocator<_Other>&)
{ // construct from related allocator (do nothing)
}
template<class _Other>
POD_allocator<_Ty>& operator=(const POD_allocator<_Other>&)
{ // assign from a related allocator (do nothing)
return (*this);
}
};
// TEMPLATE FUNCTION _Destroy_range
template<class _Ty> inline
void _Destroy_range(_Ty *_First, _Ty *_Last, POD_allocator<_Ty>& _Al)
{ // destroy [_First, _Last), POD type (do nothing)
}
//! This macro help to define PodVector which will not call dtor when elements destroy
#ifdef __USE_CRT_ALLOCATOR__
# define PodVector(_Ty) std::vector< _Ty >
#else
# define PodVector(_Ty) std::vector< _Ty, std::POD_allocator<_Ty> >
#endif
//! This macro help to define PodList which will not call dtor when elements destroy
#ifdef __USE_CRT_ALLOCATOR__
# define PodList(_Ty) std::list< _Ty >
#else
# define PodList(_Ty) std::list< _Ty, std::POD_allocator<_Ty> >
#endif
//! This macro help to define PodMap which will not call dtor when elements destroy
#ifdef __USE_CRT_ALLOCATOR__
# define PodMap(_K, _Ty) std::map< _K, _Ty, std::less<_K> >
#else
# define PodMap(_K, _Ty) std::map< _K, _Ty, std::less<_K>, std::POD_allocator<_Ty> >
#endif
代码:
// TEMPLATE FUNCTION _Allocate
template<class _Ty> inline
_Ty _FARQ *OBJ_Allocate(_SIZT _Count, _Ty _FARQ *)
{ // allocate storage for _Count elements of type _Ty
return ((_Ty _FARQ *)appMemAlloc( _Count * sizeof (_Ty), typeid(_Ty).name(), __FILE__, __LINE__ ));
}
// TEMPLATE FUNCTION _Construct
template<class _T1,
class _T2> inline
void OBJ_Construct(_T1 _FARQ *_Ptr, const _T2& _Val)
{ // construct object at _Ptr with value _Val
new ((void _FARQ *)_Ptr) _T1(_Val);
}
// TEMPLATE FUNCTION _Destroy
template<class _Ty> inline
void OBJ_Destroy(_Ty _FARQ *_Ptr)
{ // destroy OBJ object at _Ptr
_DESTRUCTOR(_Ty, _Ptr);
}
template <class _Ty> class OBJ_allocator : public _Allocator_base<_Ty>
{
public:
typedef _Allocator_base<_Ty> _Mybase;
typedef typename _Mybase::value_type value_type;
typedef value_type _FARQ *pointer;
typedef value_type _FARQ& reference;
typedef const value_type _FARQ *const_pointer;
typedef const value_type _FARQ& const_reference;
typedef _SIZT size_type;
typedef _PDFT difference_type;
public:
template<class _Other>
struct rebind
{ // convert an allocator<_Ty> to an allocator <_Other>
typedef OBJ_allocator<_Other> other;
};
public:
pointer address(reference _Val) const
{ // return address of mutable _Val
return (&_Val);
}
const_pointer address(const_reference _Val) const
{ // return address of nonmutable _Val
return (&_Val);
}
pointer allocate(size_type _Count)
{ // allocate array of _Count elements
return (OBJ_Allocate(_Count, (pointer)0));
}
pointer allocate(size_type _Count, const void _FARQ *)
{ // allocate array of _Count elements, ignore hint
return (allocate(_Count));
}
void deallocate(pointer _Ptr, size_type)
{ // deallocate object at _Ptr, ignore size
appMemFree(_Ptr);
}
void construct(pointer _Ptr, const _Ty& _Val)
{ // construct object at _Ptr with value _Val
OBJ_Construct(_Ptr, _Val);
}
void destroy(pointer _Ptr)
{ // destroy object at _Ptr
OBJ_Destroy(_Ptr);
}
_SIZT max_size() const
{ // estimate maximum array size
_SIZT _Count = (_SIZT)(-1) / sizeof (_Ty);
return (0 < _Count ? _Count : 1);
}
public:
OBJ_allocator()
{ // construct default allocator (do nothing)
}
OBJ_allocator(const OBJ_allocator<_Ty>&)
{ // construct by copying (do nothing)
}
template<class _Other>
OBJ_allocator(const OBJ_allocator<_Other>&)
{ // construct from a related allocator (do nothing)
}
template<class _Other>
OBJ_allocator<_Ty>& operator=(const OBJ_allocator<_Other>&)
{ // assign from a related allocator (do nothing)
return (*this);
}
};
// allocator TEMPLATE OPERATORS
template<class _Ty,
class _Other> inline
bool operator==(const OBJ_allocator<_Ty>&, const OBJ_allocator<_Other>&)
{ // test for allocator equality (always true)
return (true);
}
template<class _Ty,
class _Other> inline
bool operator!=(const OBJ_allocator<_Ty>&, const OBJ_allocator<_Other>&)
{ // test for allocator inequality (always false)
return (false);
}
// CLASS allocator<void>
template<> class _CRTIMP2 OBJ_allocator<void>
{ // generic allocator for type void
public:
typedef void _Ty;
typedef _Ty _FARQ *pointer;
typedef const _Ty _FARQ *const_pointer;
typedef _Ty value_type;
template<class _Other>
struct rebind
{ // convert an allocator<void> to an allocator <_Other>
typedef OBJ_allocator<_Other> other;
};
OBJ_allocator()
{ // construct default allocator (do nothing)
}
OBJ_allocator(const OBJ_allocator<_Ty>&)
{ // construct by copying (do nothing)
}
template<class _Other>
OBJ_allocator(const OBJ_allocator<_Other>&)
{ // construct from related allocator (do nothing)
}
template<class _Other>
OBJ_allocator<_Ty>& operator=(const OBJ_allocator<_Other>&)
{ // assign from a related allocator (do nothing)
return (*this);
}
};
// TEMPLATE FUNCTION _Destroy_range
template<class _Ty> inline
void _Destroy_range(_Ty *_First, _Ty *_Last, OBJ_allocator<_Ty>& _Al)
{ // destroy [_First, _Last), OBJ type
for (; _First != _Last; ++_First)
_Al.destroy(_First);
}
//! This macro help to define PodVector which will call dtor when elements destroy
#ifdef __USE_CRT_ALLOCATOR__
# define ObjVector(_Ty) std::vector< _Ty >
#else
# define ObjVector(_Ty) std::vector< _Ty, std::OBJ_allocator<_Ty> >
#endif
//! This macro help to define ObjList which will call dtor when elements destroy
#ifdef __USE_CRT_ALLOCATOR__
# define ObjList(_Ty) std::list< _Ty >
#else
# define ObjList(_Ty) std::list< _Ty, std::OBJ_allocator<_Ty> >
#endif
//! This macro help to define ObjMap which will call dtor when elements destroy
#ifdef __USE_CRT_ALLOCATOR__
# define ObjMap(_K, _Ty) std::map< _K, _Ty, std::less<_K> >
#else
# define ObjMap(_K, _Ty) std::map< _K, _Ty, std::less<_K>, std::OBJ_allocator<_Ty> >
#endif