复杂结构体的存取器 [C宏——智者的利刃,愚者的恶梦]

C宏——智者的利刃,愚者的恶梦!

在《C宏——智者的利刃,愚者的恶梦! 》一文中,提到了一种使用宏的方式 —— “例一、用C宏,书写代码更简洁”。
《C宏——智者的利刃,愚者的恶梦! 》: http://www.vckbase.com/document/viewdoc/?id=1454
《C宏——智者的利刃,愚者的恶梦! 》: http://blog.vckbase.com/smileonce/archive/2005/03/27/4081.html

本文章分别给出C++和C中不使用宏的实现方式。

 

首先,书写代码更简洁是否是优点?
有兴趣的读者请看看《设计Qt风格的C++API》一文中“便利陷阱” (The Convenience Trap) 一节。
中文: http://blog.csdn.net/TopLanguage/archive/2008/02/21/2111467.aspx
英文: http://doc.trolltech.com/qq/qq13-apis.html

【永远记住代码一次写就,之后需要不断的阅读并理解。】
【Keep in mind that code is written more than once but has to be understood over and over again.】

 

如果真要达到笑笑文中——【mbuf的属性,完全可以压扁到一个平面上去看】——这个目的,除了宏,也是有其他方法的。

在这里说明一下,笑笑在文中并没有给出struct mbuf的完整定义。
我没有linux,Cygwin也删掉了,安装挺麻烦的……
顺藤摸瓜的下载了一部分文件:
http://opengrok.creo.hu/dragonfly/xref/src/sys/sys/mbuf.h
http://opengrok.creo.hu/dragonfly/xref/src/sys/sys/param.h
http://opengrok.creo.hu/dragonfly/xref/src/sys/net/netisr.h
http://opengrok.creo.hu/dragonfly/xref/src/sys/net/netmsg.h
http://opengrok.creo.hu/dragonfly/xref/src/sys/sys/thread.h
http://opengrok.creo.hu/dragonfly/xref/src/sys/sys/msgport.h
企图拼出一个完整的struct mbuf定义,但实在太麻烦,这里就放弃了 ……

所以只用一个简单的例子来说明如何不使用宏来达到这一目的。
当然,也会说明如果结构体更复杂该如何扩展。

 

ExpandedBlockStart.gif
ExpandedBlockStart.gif/** structure definition */
None.gif
ExpandedBlockStart.gif
/* simple Point & Size structure */
ExpandedBlockStart.giftypedef 
struct Point_ {
InBlock.gif    
int x;
InBlock.gif    
int y;
ExpandedBlockEnd.gif}
 Point;
None.gif
ExpandedBlockStart.giftypedef 
struct Size_ {
InBlock.gif    
int width;
InBlock.gif    
int height;
ExpandedBlockEnd.gif}
 Size;
None.gif
ExpandedBlockStart.gif
/** complex Rect structrue */
ExpandedBlockStart.giftypedef 
struct Rect_ {
InBlock.gif    Point offset;
InBlock.gif    Size size;
ExpandedBlockEnd.gif}
 Rect;

 


C++方案:

 

ExpandedBlockStart.gif
ExpandedBlockStart.gifnamespace cpp {
InBlock.gif
ExpandedSubBlockStart.gif    
class RectAccessor {
InBlock.gif    
public:
InBlock.gif        RectAccessor(Rect
& r)
InBlock.gif            :x(r.offset.x)
InBlock.gif            ,y(r.offset.y)
InBlock.gif            ,width(r.size.width)
InBlock.gif            ,height(r.size.width)
ExpandedSubBlockStart.gif        
{  }
InBlock.gif    
public:
InBlock.gif        
int& x;
InBlock.gif        
int& y;
InBlock.gif        
int& width;
InBlock.gif        
int& height;
ExpandedSubBlockEnd.gif    }
;
InBlock.gif
ExpandedSubBlockStart.gif    
void test(int (&arr)[4],Rect* r) {
InBlock.gif        RectAccessor ac(
*r);
InBlock.gif        
// 同一平面
InBlock.gif
        ac.x = arr[0];
InBlock.gif        ac.y 
= arr[1];
InBlock.gif        ac.width 
= arr[2];
InBlock.gif        ac.height 
= arr[3];
InBlock.gif        printf(
"%d %d %d %d\n",ac.x,ac.y,ac.width,ac.height);
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}


const怎么办?
(对const的考虑,C++程序员总是比C程序员要多一点,不是吗?)

ExpandedBlockStart.gif
ExpandedBlockStart.gif/** const accessor */
None.gif
// 再定义一个const存取器不就完了?
ExpandedBlockStart.gif
class ConstRectAccessor /**/ };
None.gif
None.gif
//如果觉得这样名字不统一,不好看,也可以这样
None.gif
template<bool is_constant>
None.gif
class RectAccessor;
None.giftemplate
<>
ExpandedBlockStart.gif
class RectAccessor<false> /* 同上面那个RectAccessor */ };
ExpandedBlockStart.gif
class RectAccessor<true> /* 同上面那个ConstRectAccessor */ };


对更复杂的结构体,该方法的扩展是很容易的事情:在构造函数的成员初始化列表里写就是了。


C呢?是不是只能使用宏?当然不是。
C的方案:

 

ExpandedBlockStart.gif
ExpandedBlockStart.gifnamespace c {
InBlock.gif
InBlock.gif    typedef union RectAccessor_
ExpandedSubBlockStart.gif    
{
ExpandedSubBlockStart.gif        
struct S1 {
InBlock.gif            
int x;
InBlock.gif            
int y;
InBlock.gif            
int width;
InBlock.gif            
int height;
ExpandedSubBlockEnd.gif        }
;
ExpandedSubBlockStart.gif        
struct S2 {
InBlock.gif            Point offset;
InBlock.gif            Size size;
ExpandedSubBlockEnd.gif        }
;
InBlock.gif        Rect rect;
InBlock.gif
ExpandedSubBlockEnd.gif    }
 RectAccessor;
InBlock.gif
ExpandedSubBlockStart.gif    __declspec(noinline) 
void test(int (&arr)[4],Rect* r) {
InBlock.gif        RectAccessor
* ac = (RectAccessor*) r;
InBlock.gif        
// 同一平面
InBlock.gif
        ac->= arr[0];
InBlock.gif        ac
->= arr[1];
InBlock.gif        ac
->width = arr[2];
InBlock.gif        ac
->height = arr[3];
InBlock.gif        printf(
"%d %d %d %d\n",ac->x,ac->y,ac->width,ac->height);
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}


对const, 转型的时候,注意使用合适的指针类型就可以了。

想更复杂的结构体扩展:
如果对上面的方案不理解,甚至对mbuf都不理解,最好还是老老实实的使用全名。
永远记得,代码读的次数比写的次数多!

上面的方案,是利用了一个特性,叫“匿名联合”还别的什么东东。
含义大概是这样:

ExpandedBlockStart.gif
ExpandedBlockStart.gifunion U {
ExpandedSubBlockStart.gif  
struct /*anonymous */ {
InBlock.gif     t11 v11;
InBlock.gif     t12 v12;
ExpandedSubBlockStart.gif     
/* more members */
ExpandedSubBlockStart.gif  }
 /*anonymous */;
ExpandedSubBlockStart.gif  
struct /*anonymous */ {
InBlock.gif     t21 v21;
InBlock.gif     t22 v22;
ExpandedSubBlockStart.gif     
/* more members */
ExpandedSubBlockStart.gif  }
 /*anonymous */;
ExpandedSubBlockStart.gif  
/** more structures */
ExpandedBlockEnd.gif}
;
None.gif
ExpandedBlockStart.gif
/* 那么就可以*/
None.gifU u;
None.gifu.v11; u.v12; u.v21;
None.gif

 


经测试,上面两种方案,在VC8 O2优化下,生成的机器码同不使用Accessor 完全一致
GCC就没有测试了,看不懂它的汇编……

 

对宏的方案(也就是mbuf.h中提供的)的改进:
简直无法想象!居然在 头文件定义如此 普遍小写 名字!

 

ExpandedBlockStart.gif
ExpandedBlockStart.gif/** mbuf_accessor_define.h */
None.gif
#define    m_next        m_hdr.mh_next
None.gif
#define    m_len        m_hdr.mh_len
None.gif
#define    m_data        m_hdr.mh_data
None.gif
#define    m_type        m_hdr.mh_type
None.gif
#define    m_flags        m_hdr.mh_flags
None.gif
#define    m_nextpkt    m_hdr.mh_nextpkt
None.gif
#define    m_pkthdr    M_dat.MH.MH_pkthdr
None.gif
#define    m_ext        M_dat.MH.MH_dat.MH_ext
None.gif
#define    m_pktdat    M_dat.MH.MH_dat.MH_databuf
None.gif
#define    m_dat        M_dat.M_databuf
None.gif
ExpandedBlockStart.gif
/** mbuf_accessor_undef.h */
None.gif
#undef    m_next
None.gif
#undef    m_len
None.gif
#undef    m_data
None.gif
#undef    m_type
None.gif
#undef    m_flags
None.gif
#undef    m_nextpkt
None.gif
#undef    m_pkthdr
None.gif
#undef    m_ext
None.gif
#undef    m_pktdat
None.gif
#undef    m_dat
None.gif
ExpandedBlockStart.gif
/* 需要的时候 */
None.gif#include 
<mbuf_accessor_define.h>
ExpandedBlockStart.gif
/* 使用简写 */
ExpandedBlockStart.gif
/* 使用简写 */
ExpandedBlockStart.gif
/* 使用简写 */
ExpandedBlockStart.gif
/* 然后立刻取消定义 */
None.gif#include 
<mbuf_accessor_undef.h>

 


PS:C程序员总说C++的语言特性有心智包袱,难道宏就不算心智包袱?

 

物理老师从来都是这么写:              F = M*A;
没见任何一个物理老师会这么写:  F = multiply(M,A);
如果是,请立刻和同学打赌说他是程序员,而且很有可能是C程序员。

hp_int i1,i2,i3;
// ...
数学老师也总是这么写: hp_int icpp = i1 + i2 * i3;

不会有数学老师这么写:
hp_int ic;
hp_assign(&i2,&ic);
hp_multiply(&i3,&ic);
hp_plus(&i1,&ic);

或者这么写:
hp_plus(&i1,hp_multiply(&i3,hp_assgin(&i2,&ic) ) );

(hp —— 高精度,  对矩阵也是同样)


C程序员说,不知道 string s = s1 + s2 + s3;背后做了什么。
C++程序员说,由库决定。
C程序员说,我对库中那些精巧的技术不感兴趣(不熟悉,不愿意学)。
C++程序员说,就对宏技术感兴趣?
C程序员说,宏效率高。
C++程序员说, 如果 string s = s1 + s2 + s3;可以实现得比 strcat(strcat(strcat(....) 效率更高,你信不信?
C++ 程序员再说,如果可以自然的写出hp_int icpp = i1 + i2 * i3;有正确的运算优先级,效率与 hp_plus(&i1,hp_multiply(&i3,hp_assgin(&i2,&ic) ) );等同,你还愿意用后者?
C程序员说,那些实现都是心智包袱,我不喜欢。
C++程序员说,宏算不算心智包袱?你怎么就喜欢了?


总之,这只是一种不愿学习的心态,一种手拿锤子见什么都是钉子的心态。
Linus年纪也不算大……才40岁…… 哎……

转载于:https://my.oschina.net/qihh/blog/56683

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值