|
发表于 2008-7-29 22:08:41
|
显示全部楼层
1:to_boolean 这里确实只读了1个字节
===============
to_boolean 是类ACE_InputCDR的内嵌的一个结构类型。
struct to_boolean
{
ACE_CDR::Boolean& ref;
to_boolean(ACE_CDR::Boolean& in):ref(in){};
};
cdr >> ACE_InputCDR::to_boolean( in); //右边构造了一个匿名变量,其类型为ACE_InputCDR::to_boolean,该变量的成员ref是in的引用。
就是
cdr::operator >>(ACE_InputCDR::to_boolean(in));
或者说可以更啰嗦地写为:
ACE_InputCDR::to_boolean var(in);
cdr >> var;
2:接下来读ULONG
===================
ACE_CDR::Boolean
ACE_InputCDR::read_4 (ACE_CDR::ULong *x)
{
char *buf;
if (this->adjust (ACE_CDR::LONG_SIZE, buf) == 0) //在读ULONG的4个字节前调用了adjust
{
#if !defined (ACE_DISABLE_SWAP_ON_READ)
if (!this->do_byte_swap_)
*x = *reinterpret_cast<ACE_CDR::ULong*> (buf);
else
ACE_CDR::swap_4 (buf, reinterpret_cast<char*> (x));
#else
*x = *reinterpret_cast<ACE_CDR::ULong*> (buf);
#endif /* ACE_DISABLE_SWAP_ON_READ */
return true;
}
this->good_bit_ = false;
return false;
}
CDR_BASE.h里定义了枚举 LONG_SIZE = 4
adjust做了什么?
ACE_INLINE int
ACE_InputCDR::adjust (size_t size,
size_t align,
char*& buf)
{
#if !defined (ACE_LACKS_CDR_ALIGNMENT) //如果config.h里没有定义此宏,则需要调整读指针按align=4对齐
buf = ACE_ptr_align_binary (this->rd_ptr (), align);
#else
buf = this->rd_ptr (); //如果config.h里定义了此宏,则不需要调整读指针
#endif /* ACE_LACKS_CDR_ALIGNMENT */
char *end = buf + size;
if (end <= this->wr_ptr ())
{
this->start_.rd_ptr (end);
return 0;
}
this->good_bit_ = false;
return -1;
#if defined (ACE_LACKS_CDR_ALIGNMENT)
ACE_UNUSED_ARG (align);
#endif /* ACE_LACKS_CDR_ALIGNMENT */
}
ACE_ptr_align_binary 是如何调整读指针的?
请看这里,调整后读指针的地址按alignment对齐!
OS_Memory.h:
#define ACE_align_binary(ptr, alignment) \
((ptr + ((ptrdiff_t)((alignment)-1))) & (~((ptrdiff_t)((alignment)-1))))
/// Return the next address aligned to a required boundary
#define ACE_ptr_align_binary(ptr, alignment) \
((char *) ACE_align_binary (((ptrdiff_t) (ptr)), (alignment)))
结论:
===
1:读ACE_CDR::Boolean时:确实只读了一个字节,读完后没有移动指针至ULONG的起始处。
2:读ACE_CDR::ULONG时:如没有定义宏ACE_LACKS_CDR_ALIGNMENT,读指针先向后移动至按4字节对齐,再读ULONG的4个字节。
[ 本帖最后由 bodyguard 于 2008-7-30 00:25 编辑 ] |
|