muduo源码分析(1):时间类

在muduo库中建立两个基类:

  • copyable和noncopyable来标识派生类是否是可拷贝或者不可拷贝的。muduo库中大部分类为不可拷贝的。
class noncopyable
{
 protected:
  noncopyable(){}
  // 封装了拷贝构造函数和复制操作符,使得派生类不可进行拷贝和赋值操作
 private:
  noncopyable(const noncopyable&);
  noncopyable& operator =(const noncopyable&);
};
class copyable
{
};
  • 时间类:Timestamp
class Timestamp : public muduo::copyable
{
public:
    ...
private:
    int64_t microSecondsSinceEpoch_;// 表示时间(微妙为单位)
}

成员函数实现没有什么难点,需要注意以下方面:

1。// 编译期进行断言,传统assert为运行期断言
static_assert(sizeof(Timestamp) == sizeof(int64_t), "sizeof(Timestamp) is not 8B");

2。// 
snprintf(buf, sizeof(buf)-1, "%" PRId64 "", seconds);
PRId64解析:
int64_t 表示64位整数在32位系统中为long long int型,在64位系统中为long int型
C语言中:   
        printf("%ld",value);// 64 os
        printf("%ld",value);// 32 os
跨平台应用时:
    #ifndef __STDC_FORMAT_MACROS
    #define __STDC_FORMAT_MACROS
    #endif
    #include <inttypes.h>
    printf("%" PRId64 "\n",value);

    此时,PRId64等价于"ld"->64 os,"lld"->32 os
为什么需要上面三行,在inttype.h中
#if !define _cplusplus || define _STDC_FORMAT_MACROS
...
#if __WORDSIZE ==64
#define _PRI64_PREFIX "l"
...
#else
#define _PRI64_PREFIX "ll"
...
#endif
#define PRID64 _PRI64_PREFIX "d"
...
#endif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值