• <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>

            大規模高性能網絡服務器編程 大型游戲服務器編程


            大規模高性能網絡服務器編程 大型游戲服務器編程 完成端口模型 TCP UDP P2P 網絡編程

                       C++博客 | 首頁 | 發新隨筆 | 發新文章 | 聯系 | 聚合 | 管理

                          

            類型檢測和根據結構體中的變量地址取結構體地址


            請各位不吝指教,謝謝!

            typeof.h:
             1 /************************************************************************
             2 * Detect a variable's type or kind
             3 * by kusamba@126.com 2010-12-28
             4 */
             5 #pragma once
             6 
             7 #include <typeinfo.h>
             8 #include <string.h>
             9 #include <assert.h>
            10 
            11 #ifdef  __cplusplus
            12 
            13 /** detect static type/class */
            14 #define IS_TYPE_OF(TYPE, variable)    assert( 0 == _stricmp( typeid(TYPE).name(), typeid(variable).name() ) )
            15 
            16 /** detect polymorphic type */
            17 #define IS_KIND_OF(TYPE, ptr)        assert( NULL != dynamic_cast<TYPE*>(ptr) )
            18 
            19 #else
            20 
            21 /** detect static type/class */
            22 #define IS_TYPE_OF(TYPE, variable)    assert( 0 == _stricmp( typeid(TYPE).name(), typeid(variable).name() ) )
            23 
            24 /** detect polymorphic type */
            25 #define IS_KIND_OF(TYPE, ptr)        assert( 0 && "C Not Support Inheritance!" )
            26 
            27 #endif

            containerof.h
             1 /************************************************************************
             2 * Obtain the struct/class address by it's member address
             3 * by Kusamba@126.com 2010-12-28
             4 */
             5 #pragma once
             6 
             7 #include <stddef.h>
             8 
             9 /** Linux Kernel.h GCC Define */
            10 /**
            11 #define container_of(ptr, type, member) ({                    \
            12     const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
            13     (type *)( (char *)__mptr - offsetof(type,member) );})
            14 */
            15 
            16 #ifdef __cplusplus
            17 
            18 #ifdef  _WIN64
            19 #define CONTAINER_OF(ptr, TYPE, MEMBER)    reinterpret_cast<TYPE*>( (ptrdiff_t)( reinterpret_cast<char*>(ptr) - offsetof(TYPE, MEMBER) ) )
            20 #else
            21 #define CONTAINER_OF(ptr, TYPE, MEMBER)    reinterpret_cast<TYPE*>( ( reinterpret_cast<char*>(ptr) - offsetof(TYPE, MEMBER) ) )
            22 #endif
            23 
            24 #else
            25 
            26 #ifdef  _WIN64
            27 #define CONTAINER_OF(ptr, TYPE, MEMBER)    (TYPE*)( (ptrdiff_t)( (char*)(ptr) - offsetof(TYPE, MEMBER) ) )
            28 #else
            29 #define CONTAINER_OF(ptr, TYPE, MEMBER)    (TYPE*)( ( (char*)(ptr) - offsetof(TYPE, MEMBER) ) )
            30 #endif
            31 
            32 #endif    /* __cplusplus */
            33 

            測試代碼:
             1 #include "stdafx.h"
             2 #include "typeof.h"
             3 #include "containerof.h"
             4 
             5 //////////////////////////////////////////////////////////////////////////
             6 // Test Kind/Type of
             7 //////////////////////////////////////////////////////////////////////////
             8 namespace _typeof_
             9 {
            10     class CA {};
            11     class CB {};
            12 
            13     struct SA {};
            14     struct SB {};
            15 
            16     class CP {};
            17     class CC : public CP {};
            18 
            19     void Test()
            20     {
            21         int int_a;
            22         IS_TYPE_OF(int, int_a);
            23         //IS_TYPE_OF(float, int_a);
            24 
            25         int* pint_a;
            26         IS_TYPE_OF(int*, pint_a);
            27         //IS_TYPE_OF(int, pint_a);
            28         //IS_TYPE_OF(float*, pint_a);
            29         //IS_TYPE_OF(float, pint_a);
            30 
            31         CA ca;
            32         CA* pca;
            33         IS_TYPE_OF(CA, ca);
            34         IS_TYPE_OF(CA*, pca);
            35         //IS_TYPE_OF(CA, pca);
            36 
            37         //IS_TYPE_OF(CB, ca);
            38 
            39         IS_KIND_OF(CA, &ca);
            40 
            41         CP parent;
            42         CC child;
            43         IS_TYPE_OF(CP, parent);
            44         //IS_TYPE_OF(CC, parent);
            45         //IS_TYPE_OF(CP, child);
            46         IS_TYPE_OF(CC, child);
            47 
            48         IS_KIND_OF(CP, &parent);
            49         //IS_KIND_OF(CC, &parent);
            50         IS_KIND_OF(CP, &child);
            51         IS_KIND_OF(CC, &child); 
            52 
            53         printf("type_of Done!\n");
            54     }
            55 }//namespace _typeof_
            56 
            57 //////////////////////////////////////////////////////////////////////////
            58 // container of test
            59 //////////////////////////////////////////////////////////////////////////
            60 namespace _container_of_
            61 {
            62     struct SA 
            63     {
            64         int m_iA;
            65     };
            66 
            67     struct SB 
            68     {
            69         int m_iB;
            70         SA  m_sa;
            71     };
            72 
            73     void Test()
            74     {
            75         SB val;
            76 
            77         /**error C2039: 'm_iA' : is not a member of '_container_of_::SB'*/
            78         /**not support nesting*/
            79         //SB* pSB = CONTAINER_OF(&val.m_sa.m_iA, SB, m_iA);
            80         /**end*/
            81 
            82         SB* pSB = CONTAINER_OF(&val.m_iB, SB, m_iB);
            83 
            84         IS_TYPE_OF(SB*, pSB);
            85 
            86         printf("container_of Done!\n");
            87     }
            88 }//_container_of_
            89 
            90 int _tmain(int argc, _TCHAR* argv[])
            91 {
            92     _typeof_::Test();
            93     _container_of_::Test();
            94 
            95     return 0;
            96 }
            97 

            posted on 2010-12-28 23:51 iKusamba 閱讀(1555) 評論(0)  編輯 收藏 引用 所屬分類: C++技術

            公告

            導航

            隨筆分類

            最新隨筆

            最新評論

            閱讀排行榜

            亚洲欧美日韩精品久久亚洲区 | 伊人久久大香线蕉亚洲| 伊人久久大香线蕉综合5g| 久久99九九国产免费看小说| 人妻精品久久久久中文字幕一冢本| 狠狠色丁香婷婷综合久久来| 狠狠色丁香婷婷久久综合不卡| 青青青国产成人久久111网站| 狠狠人妻久久久久久综合蜜桃| 亚洲国产天堂久久综合| 久久久一本精品99久久精品66| 久久国产精品99久久久久久老狼| 久久夜色精品国产www| 无码人妻精品一区二区三区久久久| 秋霞久久国产精品电影院| 综合久久国产九一剧情麻豆| 久久免费视频一区| 国产精品久久99| 久久精品亚洲一区二区三区浴池| 久久精品亚洲福利| 四虎国产永久免费久久| 无码国内精品久久人妻| 伊色综合久久之综合久久| 99久久亚洲综合精品网站| 久久婷婷五月综合97色一本一本| 九九热久久免费视频| 好久久免费视频高清| 久久人人爽人人爽人人片AV不 | 国产精品欧美亚洲韩国日本久久| 亚洲午夜久久久久久久久电影网 | 国内精品久久久久久麻豆| 久久人妻少妇嫩草AV无码专区| 日日狠狠久久偷偷色综合免费| 品成人欧美大片久久国产欧美...| 国产婷婷成人久久Av免费高清| 久久久久久久97| 国产精品久久久久9999| 国产精品美女久久久久| 97精品国产91久久久久久| 久久国产欧美日韩精品 | 国产激情久久久久影院|