STL内存基本处理工具(整理)

来源:互联网 发布:慕恋的喜欢lofter乐乎 编辑:程序博客网 时间:2024/05/29 17:55


STL定义有五个全局函数,作用于未初始化空间上。分别为用于构造的construct()和用于析构的destroy()。另外三个分别为uninitialized_copy(),uninitialized_fill(),uninitialized_fill_n()

 

construct()与destroy()

//#include "stl_construct.h"

#include <new.h>

 

template <class T1, class T2>

inline void construct(T1* p, const T2& value)

{

        new (p) T1(value);//使用placement new 在分配好的内存上,根据T2的值构造一个T1对象

}

 

template <class T1>

inline void construct(T1 *p)

{

        new (p) T1();

}

 

template <class T1>

inline void destory(T1 *p)

{

         P->~T1();

}

 

//destory()函数接受两个迭代器作为参数的版本。此函数设法找出元素的数值型别。

//进而利用_type_traits<>求取最适当的措施

template <class ForwardIterator>

inline void destroy(ForwardIterator first, ForwardIterator last)

{

        _destroy(first, last, value_type(first));//value_type() 全局函数??它的返回值是什么??

}

 

//根据元素的型别判断是否有trival destructor

//trival desructor是无关紧要的析构函数,例如某些内置类型的析构函数,实际并没有执行什么动作,调用的话会浪费时间

template <class ForwardIterator, class T>

inline void _destroy(ForwardIterator first, ForwardIterator last, T*)

{

        typedef typename _type_traits<T>::has_trival_destructor trivial_destructor;//这一句什么意思

        _destroy_aux(first, last, trivial_destructor());

}

 

//如果元素型别有non-trival destructor...

//也就是说,析构函数做了些有价值的事情,不能省略,需要遍历调用

template <class ForwardIterator>

inline void _destroy_aux(ForwardIterator first, ForwardIterator last, _false_type)//最后一个参数的指定,不甚明了????

{

        for (; first < last; ++first)

        {

                 destroy(first);

        }

}

 

//如果元素型别有trival-destructor...

//也就是说,析构函数实际什么也没做,调用这些析构函数除了浪费时间就是浪费时间

template <class ForwardIterator>

inline void _destroy_aux(ForwardIterator first, ForwardIterator last, _true_type) {}

 

//以下是destroy()第二版本针对迭代器为char*wchar_t*的特化版

inline void destroy(char*, char*) {}

inline void destroy(wchar_t*, wchar_t*) {}

关于value_type()和_type_traits<>的疑问,请看:

 

Traits编程技法—STL源代码的门钥匙

Traits编程技法大量运用于STL实现品中。它利用”内嵌型别“的编程技巧与编译器的template参数推导功能,增强C++未能提供的关于型别认证方面的能力,弥补C++不为强型别(strong type)语言的遗憾。

//<stl_iterator.h>

struct input_iterator_tag {};

struct output_iterator_tag {};

struct forward_iterator_tag :public input_iterator_tag {};

struct bidirectional_iterator_tag :public forward_iterator_tag {};

struct random_access_iterator_tag :public bidirectional_iterator_tag {};

 

//为了避免迭代器细节遗漏,自行开发迭代器的时候最好继承

//下面这个std::iterator

template <class Category, class T, class Distance=ptrdiff_t, 

          class Pointer=T*, class Reference=T&>

struct iterator

{

        typedef Category  iterator_category;

        typedef T         value_type;

        typedef Distance  difference_type;

        typedef Pointer   pointer;

        typedef Reference reference;

};

 

//特性榨取traits

template <class Iterator>

struct iterator_traits

{

        typedef typename Iterator::iterator_category iterator_category;

        typedef typename Iterator::value_type        value_type;

        typedef typename Iterator::difference_type   difference_type;

        typedef typename Iterator::pointer           pointer;

        typedef typename Iterator::reference         reference;

};

 

//针对原生指针(native pointer)有特化的traits

template <class T>

struct iterator_traits<T*>

{

        typedef typename random_access_iterator_tag iterator_category;

        typedef typename T                          value_type;

        typedef typename ptrdiff_t                  difference_type;

        typedef typename T*                         pointer;

        typedef typename T&                         reference;

};

 

template <class T>

struct iterator_traits<const T*>

{

        typedef typename random_access_iterator_tag iterator_category;

        typedef typename T                          value_type;

        typedef typename ptrdiff_t                  difference_type;

        typedef typename T*                         pointer;

        typedef typename T&                         reference;

};

 

//返回迭代器类型

template <class Iterator>

inline typename iterator_traits<Iterator>::iterator_category

iterator_category(const Iterator&)

{

        typedef typename iterator_traits<Iterator>::iterator_category category;

        return category();//初始化一个迭代器内typedef的迭代器类型结构

}

 

//为什么返回迭代器的distance_type* 便可获得distance_type, 省去了具体对象的复制

template <class Iterator>

inline typename iterator_traits<Iterator>::distance_type*

distance_type(const Iterator&)

{

        return static_cast<typename iterator_traits<Iterator>::distance_type*>(0);

}

 

//返回迭代器value_type

template <class Iterator>

inline typename iterator_traits<Iterator>::value_type*

value_type(const Iterator&)

{

        return static_cast<typename iterator_traits<Iterator>::value_type*>(0);

}

uninitialized_copy(),uninitialized_fill(),uninitialized_fill_n()

//以下三个函数,都是在已经申请好,但是未初始化的内存中构造对象(使用placement new

//是我们能够将内存的配置与对象的构造行为分离开

//它们都是commit or rollback, 要么构造所有,要么一个也不构造

 

//利用x,从first开始构造n个对象

template <class ForwardIterator, class Size, class T>

inline ForwardIterator uninitialized_fill_n(ForwardIterator first,

        Size n, const T& x)

{

        //_uninitialized_fill_n(first, n, x, value_type(first))根据first所指型别判断该型别是不是标量型别

        //从而采取不同的策略构造策略

        return _uninitialized_fill_n(first, n, x, value_type(first));

}

 

//区间复制构造

//有针对char*wchar_t*的特化版本

template <class InputIterator, class ForwardIterator>

inline ForwardIterator uninitialized_copy(InputIterator first,

InputIterator last, ForwardIterator result)

{//根据所指类型是否为标量型别,选择不同的策略

        return _uninitialized_copy(first, last, result, 

value_type(ForwardIterator));

}

 

//利用x[first, last)区间内构造对象

template <class ForwardIterator, class T>

inline void uninitialized_fill(ForwardIterator first, ForwardIterator last, const T& x)

{//根据所指类型是否为标量型别,选择不同的策略

         _uninitialized_fill(first, last, x, value_type(first));

}

 

0 0
原创粉丝点击