• <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++技術,再用1年的時間努力學C++!
            隨筆 - 2, 文章 - 0, 評論 - 2, 引用 - 0
            數據加載中……

            STL學習之二:STL內存工具(一)

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

            #ifndef?TYPE_TRAITS_H
            #define ?TYPE_TRAITS_H

            // 為了了重載確定那些類弄型是不用析構的SGI?STl用了type_traits
            // 并將一些Scale?type進行特例化
            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的實現
            #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?:?定義控制臺應用程序的入口點。
            //
            /*
            ?*????模仿實現SGI?STL中的內存工具
            ?*??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;
            }
            實中用到的simple_alloc及sgi_allocator.h是上一篇隨筆里的東西。。。

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

            国产成人精品久久亚洲高清不卡 国产成人精品久久亚洲高清不卡 国产成人精品久久亚洲 | 精品久久人人做人人爽综合| 青青青国产成人久久111网站| 国产精品99久久精品爆乳| 久久人人超碰精品CAOPOREN| 无码任你躁久久久久久老妇App| 久久精品国产亚洲AV无码麻豆 | 伊人 久久 精品| 久久婷婷国产综合精品| 99久久成人18免费网站| 亚洲日本va午夜中文字幕久久| 国产Av激情久久无码天堂| 久久成人精品| 久久精品中文字幕久久| 久久99九九国产免费看小说| 国产精品久久波多野结衣| 国产精品中文久久久久久久| 亚洲欧美精品伊人久久| 色欲综合久久躁天天躁蜜桃| 怡红院日本一道日本久久 | 久久精品国产69国产精品亚洲| 日日狠狠久久偷偷色综合96蜜桃| 99久久er这里只有精品18| 麻豆精品久久久久久久99蜜桃| 99久久国产综合精品网成人影院| 国产A三级久久精品| 少妇人妻综合久久中文字幕| 久久中文精品无码中文字幕| 久久亚洲高清观看| 久久青草国产精品一区| 国产精品99久久免费观看| 三上悠亚久久精品| 热re99久久精品国99热| 亚洲va久久久噜噜噜久久男同| 国产成人精品综合久久久久| 中文字幕无码久久人妻| 区亚洲欧美一级久久精品亚洲精品成人网久久久久 | 久久涩综合| 久久久久亚洲AV成人网人人网站| 国产精品日韩深夜福利久久| 99久久婷婷国产一区二区|