• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            szwolf

            專注于C++技術(shù),再用1年的時間努力學(xué)C++!
            隨筆 - 2, 文章 - 0, 評論 - 2, 引用 - 0
            數(shù)據(jù)加載中……

            STL學(xué)習(xí)之二:STL內(nèi)存工具(一)

            ??????用Traits進(jìn)行類別推導(dǎo),將類別選擇工作放到編譯期進(jìn)行,利用重載提高效率。
            ??????下面的type_traits.h是我直接從SGI STL里Copy出來的。。。

            #ifndef?TYPE_TRAITS_H
            #define ?TYPE_TRAITS_H

            // 為了了重載確定那些類弄型是不用析構(gòu)的SGI?STl用了type_traits
            // 并將一些Scale?type進(jìn)行特例化
            struct ?__true_type? {
            }
            ;

            struct ?__false_type? {
            }
            ;

            template?
            < class ?_Tp >
            struct ?__type_traits? {?
            ???typedef?__true_type?????this_dummy_member_must_be_first;

            ???typedef?__false_type????has_trivial_default_constructor;
            ???typedef?__false_type????has_trivial_copy_constructor;
            ???typedef?__false_type????has_trivial_assignment_operator;
            ???typedef?__false_type????has_trivial_destructor;
            ???typedef?__false_type????is_POD_type;
            }
            ;

            template
            <>
            struct ?__type_traits < bool > ? {
            ???typedef?__true_type????has_trivial_default_constructor;
            ???typedef?__true_type????has_trivial_copy_constructor;
            ???typedef?__true_type????has_trivial_assignment_operator;
            ???typedef?__true_type????has_trivial_destructor;
            ???typedef?__true_type????is_POD_type;
            }
            ;
            template
            <>
            ?
            struct ?__type_traits < char > ? {
            ???typedef?__true_type????has_trivial_default_constructor;
            ???typedef?__true_type????has_trivial_copy_constructor;
            ???typedef?__true_type????has_trivial_assignment_operator;
            ???typedef?__true_type????has_trivial_destructor;
            ???typedef?__true_type????is_POD_type;
            }
            ;
            template
            <>
            ?
            struct ?__type_traits < signed? char > ? {
            ???typedef?__true_type????has_trivial_default_constructor;
            ???typedef?__true_type????has_trivial_copy_constructor;
            ???typedef?__true_type????has_trivial_assignment_operator;
            ???typedef?__true_type????has_trivial_destructor;
            ???typedef?__true_type????is_POD_type;
            }
            ;
            template
            <>
            ?
            struct ?__type_traits < unsigned? char > ? {
            ???typedef?__true_type????has_trivial_default_constructor;
            ???typedef?__true_type????has_trivial_copy_constructor;
            ???typedef?__true_type????has_trivial_assignment_operator;
            ???typedef?__true_type????has_trivial_destructor;
            ???typedef?__true_type????is_POD_type;
            }
            ;
            template
            <>
            ?
            struct ?__type_traits < wchar_t > ? {
            ???typedef?__true_type????has_trivial_default_constructor;
            ???typedef?__true_type????has_trivial_copy_constructor;
            ???typedef?__true_type????has_trivial_assignment_operator;
            ???typedef?__true_type????has_trivial_destructor;
            ???typedef?__true_type????is_POD_type;
            }
            ;

            template
            <>
            ?
            struct ?__type_traits < short > ? {
            ???typedef?__true_type????has_trivial_default_constructor;
            ???typedef?__true_type????has_trivial_copy_constructor;
            ???typedef?__true_type????has_trivial_assignment_operator;
            ???typedef?__true_type????has_trivial_destructor;
            ???typedef?__true_type????is_POD_type;
            }
            ;
            template
            <>
            ?
            struct ?__type_traits < unsigned? short > ? {
            ???typedef?__true_type????has_trivial_default_constructor;
            ???typedef?__true_type????has_trivial_copy_constructor;
            ???typedef?__true_type????has_trivial_assignment_operator;
            ???typedef?__true_type????has_trivial_destructor;
            ???typedef?__true_type????is_POD_type;
            }
            ;
            template
            <>
            ?
            struct ?__type_traits < int > ? {
            ???typedef?__true_type????has_trivial_default_constructor;
            ???typedef?__true_type????has_trivial_copy_constructor;
            ???typedef?__true_type????has_trivial_assignment_operator;
            ???typedef?__true_type????has_trivial_destructor;
            ???typedef?__true_type????is_POD_type;
            }
            ;
            template
            <>
            ?
            struct ?__type_traits < unsigned? int > ? {
            ???typedef?__true_type????has_trivial_default_constructor;
            ???typedef?__true_type????has_trivial_copy_constructor;
            ???typedef?__true_type????has_trivial_assignment_operator;
            ???typedef?__true_type????has_trivial_destructor;
            ???typedef?__true_type????is_POD_type;
            }
            ;
            template
            <>
            ?
            struct ?__type_traits < long > ? {
            ???typedef?__true_type????has_trivial_default_constructor;
            ???typedef?__true_type????has_trivial_copy_constructor;
            ???typedef?__true_type????has_trivial_assignment_operator;
            ???typedef?__true_type????has_trivial_destructor;
            ???typedef?__true_type????is_POD_type;
            }
            ;
            template
            <>
            ?
            struct ?__type_traits < unsigned? long > ? {
            ???typedef?__true_type????has_trivial_default_constructor;
            ???typedef?__true_type????has_trivial_copy_constructor;
            ???typedef?__true_type????has_trivial_assignment_operator;
            ???typedef?__true_type????has_trivial_destructor;
            ???typedef?__true_type????is_POD_type;
            }
            ;

            #ifdef?__STL_LONG_LONG
            template
            <>
            ?
            struct ?__type_traits < long ? long > ? {
            ???typedef?__true_type????has_trivial_default_constructor;
            ???typedef?__true_type????has_trivial_copy_constructor;
            ???typedef?__true_type????has_trivial_assignment_operator;
            ???typedef?__true_type????has_trivial_destructor;
            ???typedef?__true_type????is_POD_type;
            }
            ;

            ?
            struct ?__type_traits < unsigned? long ? long > ? {
            ???typedef?__true_type????has_trivial_default_constructor;
            ???typedef?__true_type????has_trivial_copy_constructor;
            ???typedef?__true_type????has_trivial_assignment_operator;
            ???typedef?__true_type????has_trivial_destructor;
            ???typedef?__true_type????is_POD_type;
            }
            ;

            #endif ?/*?__STL_LONG_LONG?*/
            template
            <>
            ?
            struct ?__type_traits < float > ? {
            ???typedef?__true_type????has_trivial_default_constructor;
            ???typedef?__true_type????has_trivial_copy_constructor;
            ???typedef?__true_type????has_trivial_assignment_operator;
            ???typedef?__true_type????has_trivial_destructor;
            ???typedef?__true_type????is_POD_type;
            }
            ;
            template
            <>
            ?
            struct ?__type_traits < double > ? {
            ???typedef?__true_type????has_trivial_default_constructor;
            ???typedef?__true_type????has_trivial_copy_constructor;
            ???typedef?__true_type????has_trivial_assignment_operator;
            ???typedef?__true_type????has_trivial_destructor;
            ???typedef?__true_type????is_POD_type;
            }
            ;
            template
            <>
            ?
            struct ?__type_traits < long ? double > ? {
            ???typedef?__true_type????has_trivial_default_constructor;
            ???typedef?__true_type????has_trivial_copy_constructor;
            ???typedef?__true_type????has_trivial_assignment_operator;
            ???typedef?__true_type????has_trivial_destructor;
            ???typedef?__true_type????is_POD_type;
            }
            ;

            template?
            < class ?_Tp >
            struct ?__type_traits < _Tp *> ? {
            ???typedef?__true_type????has_trivial_default_constructor;
            ???typedef?__true_type????has_trivial_copy_constructor;
            ???typedef?__true_type????has_trivial_assignment_operator;
            ???typedef?__true_type????has_trivial_destructor;
            ???typedef?__true_type????is_POD_type;
            }
            ;
            template
            <>
            ?
            struct ?__type_traits < char *> ? {
            ???typedef?__true_type????has_trivial_default_constructor;
            ???typedef?__true_type????has_trivial_copy_constructor;
            ???typedef?__true_type????has_trivial_assignment_operator;
            ???typedef?__true_type????has_trivial_destructor;
            ???typedef?__true_type????is_POD_type;
            }
            ;
            template
            <>
            ?
            struct ?__type_traits < signed? char *> ? {
            ???typedef?__true_type????has_trivial_default_constructor;
            ???typedef?__true_type????has_trivial_copy_constructor;
            ???typedef?__true_type????has_trivial_assignment_operator;
            ???typedef?__true_type????has_trivial_destructor;
            ???typedef?__true_type????is_POD_type;
            }
            ;
            template
            <>
            ?
            struct ?__type_traits < unsigned? char *> ? {
            ???typedef?__true_type????has_trivial_default_constructor;
            ???typedef?__true_type????has_trivial_copy_constructor;
            ???typedef?__true_type????has_trivial_assignment_operator;
            ???typedef?__true_type????has_trivial_destructor;
            ???typedef?__true_type????is_POD_type;
            }
            ;
            template
            <>
            ?
            struct ?__type_traits < const ? char *> ? {
            ???typedef?__true_type????has_trivial_default_constructor;
            ???typedef?__true_type????has_trivial_copy_constructor;
            ???typedef?__true_type????has_trivial_assignment_operator;
            ???typedef?__true_type????has_trivial_destructor;
            ???typedef?__true_type????is_POD_type;
            }
            ;
            template
            <>
            ?
            struct ?__type_traits < const ?signed? char *> ? {
            ???typedef?__true_type????has_trivial_default_constructor;
            ???typedef?__true_type????has_trivial_copy_constructor;
            ???typedef?__true_type????has_trivial_assignment_operator;
            ???typedef?__true_type????has_trivial_destructor;
            ???typedef?__true_type????is_POD_type;
            }
            ;
            template
            <>
            ?
            struct ?__type_traits < const ?unsigned? char *> ? {
            ???typedef?__true_type????has_trivial_default_constructor;
            ???typedef?__true_type????has_trivial_copy_constructor;
            ???typedef?__true_type????has_trivial_assignment_operator;
            ???typedef?__true_type????has_trivial_destructor;
            ???typedef?__true_type????is_POD_type;
            }
            ;

            #endif

            以下是construct 和 destroy的實(shí)現(xiàn)
            #ifndef?MEM_TOOlS_H
            #define?MEM_TOOlS_H
            #include?
            "type_traits.h"
            using?namespace?std;
            namespace?SGI
            {
            ????template
            <typename?T1,?typename?T2>
            ????inline?
            void?construct(T1*?p,?const?T2&?value)
            ????
            {
            ????????
            new(p)T1(value);
            ????}

            ????
            ????template
            <typename?T>
            ????inline?
            void?construct(T*?p)
            ????
            {
            ????????
            new(p)T();
            ????}


            ????template
            <typename?T>
            ????
            void?destroy(T*?p)
            ????
            {
            ????????p
            ->~T();
            ????}


            ????template
            <typename?ForwardIterator>
            ????inline?
            void?destroy_aux(ForwardIterator?first,?ForwardIterator?last,?__false_type)
            ????
            {
            ????????
            for?(;?first?<?last;?++first)
            ????????????destroy(
            &*first);

            ????????cout?
            <<?"false?type"?<<?endl;
            ????}


            ????template
            <typename?ForwardIterator>
            ????inline?
            void?destroy_aux(ForwardIterator?first,?ForwardIterator?last,?__true_type)
            ????
            {
            ????????cout?
            <<?"true?type"?<<?endl;
            ????}


            ????template
            <typename?ForwardIterator>
            ????inline?
            void?destroy(ForwardIterator?first,?ForwardIterator?last)
            ????
            {
            ????????_destroy(first,?last,?value_type(
            *first));
            ????}


            ????template
            <typename?ForwardIterator,?typename?T>
            ????inline?
            void?_destroy(ForwardIterator?first,?ForwardIterator?last,?T*)
            ????
            {
            ????????typedef?__type_traits
            <T>::has_trivial_destructor?trivial_destructor;
            ????????destroy_aux(first,?last,?trivial_destructor());
            ????}


            ????inline?
            void?destroy(char*,?char*)?
            ????
            {
            ????????cout?
            <<?"char*?type"?<<?endl;
            ????}

            ????inline?
            void?destroy(wchar_t*,?wchar_t*)?
            ????
            {
            ????????cout?
            <<?"wchar_t*?type"?<<?endl;
            ????}
            ????

            }


            #endif

            在main中對construct,destroy時行測試

            //?mem_tools.cpp?:?定義控制臺應(yīng)用程序的入口點(diǎn)。
            //
            /*
            ?*????模仿實(shí)現(xiàn)SGI?STL中的內(nèi)存工具
            ?*??szwolf?@?szu
            ?*??2006.08.07
            ?
            */

            #include?
            "stdafx.h"
            #include?
            "sgi_allocator.h"
            #include?
            <iostream>
            #include?
            <vector>
            #include?
            <algorithm>
            #include?
            "mem_tools.h"

            class?mem_test
            {
            public:
            ????mem_test(
            const?char*?msg)
            ????
            {
            ????????cout?
            <<?msg?<<?endl;
            ????}


            ????
            ~mem_test()
            ????
            {
            ????????cout?
            <<?"dead~~"?<<?endl;
            ????}

            }
            ;

            int?_tmain(int?argc,?_TCHAR*?argv[])
            {
            ????
            //????很郁悶為什么下面的代碼是錯的?
            ????
            //????typedef?simple_alloc<mem_test,?SGI::alloc>?data_allcator;
            ????
            //????mem_test*?t?=?data_allocator::allocate();

            ????SGI::simple_alloc
            <mem_test,?SGI::alloc>?data_allocator;
            ????
            ????mem_test
            *?t?=?data_allocator.allocate();

            ????SGI::construct(t,?
            "come to live!");
            ????SGI::destroy(
            &*t);

            ????SGI::simple_alloc
            <char,?SGI::alloc>?char_alloc;
            ????
            char?*p?=?char_alloc.allocate(133);
            ????strcpy(p,?
            "hellow!");
            ????SGI::destroy(p,p
            +8);
            ????char_alloc.deallocate(p,?
            133);

            ????system(
            "pause");
            ????
            return?0;
            }
            實(shí)中用到的simple_alloc及sgi_allocator.h是上一篇隨筆里的東西。。。

            posted on 2006-08-07 01:47 szwolf 閱讀(1050) 評論(0)  編輯 收藏 引用


            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            国产精品久久久久影视不卡| 久久久精品国产亚洲成人满18免费网站| 无码人妻精品一区二区三区久久| 久久99亚洲网美利坚合众国| 伊人丁香狠狠色综合久久| 久久久WWW成人免费毛片| 午夜精品久久久久久久久| 久久久久四虎国产精品| 久久久久波多野结衣高潮| 91精品久久久久久无码| 午夜精品久久久久久99热| 久久久久国产| 久久精品国产99国产电影网 | 日本久久久久久中文字幕| 久久精品一区二区影院| 久久99精品国产99久久| 97精品依人久久久大香线蕉97| 狠狠综合久久综合中文88| 精品国产VA久久久久久久冰| 一级a性色生活片久久无| 伊人久久大香线焦综合四虎| 无码伊人66久久大杳蕉网站谷歌| 精品国产热久久久福利| 国产成人精品久久二区二区| 人妻无码αv中文字幕久久琪琪布 人妻无码久久一区二区三区免费 人妻无码中文久久久久专区 | 色综合久久中文综合网| 久久精品中文闷骚内射| 久久国产免费直播| 欧美黑人激情性久久| 久久天天躁狠狠躁夜夜2020| 嫩草影院久久99| 久久综合综合久久狠狠狠97色88| 国产美女久久精品香蕉69| 久久影院综合精品| 久久久久亚洲精品天堂| 欧美一区二区三区久久综 | 女同久久| 九九热久久免费视频| 久久人妻少妇嫩草AV无码蜜桃| 精品国产热久久久福利| 久久久久久青草大香综合精品|