目录结构
Muduo 的目录结构如下。
基础库
muduo/base 目录是一些基础库,都是用户可见的类,内容包括
Timestamp类封装
class Timestamp : public muduo::copyable,
public boost::less_than_comparable<Timestamp>
类图如下:
1、muduo::copyable 空基类,标识类,值类型
值语义:可以拷贝,拷贝之后,与原对象脱离关系
对象语义:要么是不能拷贝;要么可以拷贝,拷贝之后与原对象仍然存在一定的关系,比如共享底层资源
2、less_than_comparable<Timestamp> 模板类
Timestamp 要求实现<, =,可自动实现>,<=,>=
3、成员 microSecondsSinceEpoch_ 是现在这个时刻距离1970-01-01 00:00:00 (UTC 时间) 的微秒数
4、BOOST_STATIC_ASSERT 编译时断言
assert 运行时断言
5、使用PRld64
int64_t 用来表示64位整数,在32位系统中是long long int,在64位系统中是long int,所以打印int64_t的格式化方法是:
printf(“%ld”, value); // 64bit OS printf("%lld", value); // 32bit OS
跨平台的做法:
#define __STDC_FORMAT_MACROS #include <inttypes.h> #undef __STDC_FORMAT_MACROS printf("%" PRId64 "\n", value);
Timestamp.h
#ifndef MUDUO_BASE_TIMESTAMP_H
#define MUDUO_BASE_TIMESTAMP_H
#include <muduo/base/copyable.h>
#include <muduo/base/Types.h>
#include <boost/operators.hpp>
namespace muduo
{
///
/// Time stamp in UTC, in microseconds resolution.
///
/// This class is immutable.
/// It's recommended to pass it by value, since it's passed in register on x64.
///
class Timestamp : public muduo::copyable,
public boost::less_than_comparable<Timestamp>
{
public:
///
/// Constucts an invalid Timestamp.
///
Timestamp()
: microSecondsSinceEpoch_(0)
{
}
///
/// Constucts a Timestamp at specific time
///
/// @param microSecondsSinceEpoch
explicit Timestamp(int64_t microSecondsSinceEpoch);
void swap(Timestamp& that)
{
std::swap(microSecondsSinceEpoch_, that.microSecondsSinceEpoch_);
}
// default copy/assignment/dtor are Okay
string toString() const;
string toFormattedString() const;
bool valid() const { return microSecondsSinceEpoch_ > 0; }
// for internal usage.
int64_t microSecondsSinceEpoch() const { return microSecondsSinceEpoch_; }
time_t secondsSinceEpoch() const
{ return static_cast<time_t>(microSecondsSinceEpoch_ / kMicroSecondsPerSecond); }
///
/// Get time of now.
///
static Timestamp now();
static Timestamp invalid();
static const int kMicroSecondsPerSecond = 1000 * 1000;
private:
int64_t microSecondsSinceEpoch_;
};
inline bool operator<(Timestamp lhs, Timestamp rhs)
{
return lhs.microSecondsSinceEpoch() < rhs.microSecondsSinceEpoch();
}
inline bool operator==(Timestamp lhs, Timestamp rhs)
{
return lhs.microSecondsSinceEpoch() == rhs.microSecondsSinceEpoch();
}
///
/// Gets time difference of two timestamps, result in seconds.
///
/// @param high, low
/// @return (high-low) in seconds
/// @c double has 52-bit precision, enough for one-microseciond
/// resolution for next 100 years.
inline double timeDifference(Timestamp high, Timestamp low)
{
int64_t diff = high.microSecondsSinceEpoch() - low.microSecondsSinceEpoch();
return static_cast<double>(diff) / Timestamp::kMicroSecondsPerSecond;
}
///
/// Add @c seconds to given timestamp.
///
/// @return timestamp+seconds as Timestamp
///
inline Timestamp addTime(Timestamp timestamp, double seconds)
{
int64_t delta = static_cast<int64_t>(seconds * Timestamp::kMicroSecondsPerSecond);
return Timestamp(timestamp.microSecondsSinceEpoch() + delta);
}
}
#endif // MUDUO_BASE_TIMESTAMP_H
Timestamp.cc
#include <muduo/base/Timestamp.h>
#include <sys/time.h>
#include <stdio.h>
#define __STDC_FORMAT_MACROS
#include <inttypes.h>
#undef __STDC_FORMAT_MACROS
#include <boost/static_assert.hpp>
using namespace muduo;
BOOST_STATIC_ASSERT(sizeof(Timestamp) == sizeof(int64_t));
Timestamp::Timestamp(int64_t microseconds)
: microSecondsSinceEpoch_(microseconds)
{
}
string Timestamp::toString() const
{
char buf[32] = {0};
int64_t seconds = microSecondsSinceEpoch_ / kMicroSecondsPerSecond;
int64_t microseconds = microSecondsSinceEpoch_ % kMicroSecondsPerSecond;
snprintf(buf, sizeof(buf)-1, "%" PRId64 ".%06" PRId64 "", seconds, microseconds);
return buf;
}
string Timestamp::toFormattedString() const
{
char buf[32] = {0};
time_t seconds = static_cast<time_t>(microSecondsSinceEpoch_ / kMicroSecondsPerSecond);
int microseconds = static_cast<int>(microSecondsSinceEpoch_ % kMicroSecondsPerSecond);
struct tm tm_time;
gmtime_r(&seconds, &tm_time);
snprintf(buf, sizeof(buf), "%4d%02d%02d %02d:%02d:%02d.%06d",
tm_time.tm_year + 1900, tm_time.tm_mon + 1, tm_time.tm_mday,
tm_time.tm_hour, tm_time.tm_min, tm_time.tm_sec,
microseconds);
return buf;
}
Timestamp Timestamp::now()
{
struct timeval tv;
gettimeofday(&tv, NULL);
int64_t seconds = tv.tv_sec;
return Timestamp(seconds * kMicroSecondsPerSecond + tv.tv_usec);
}
Timestamp Timestamp::invalid()
{
return Timestamp();
}
Timestamp_unittest.cc
#include <muduo/base/Timestamp.h>
#include <vector>
#include <stdio.h>
using muduo::Timestamp;
void passByConstReference(const Timestamp& x)
{
printf("%s\n", x.toString().c_str());
}
void passByValue(Timestamp x)
{
printf("%s\n", x.toString().c_str());
}
void benchmark()
{
const int kNumber = 1000*1000;
std::vector<Timestamp> stamps;
stamps.reserve(kNumber);
for (int i = 0; i < kNumber; ++i)
{
stamps.push_back(Timestamp::now());
}
printf("%s\n", stamps.front().toString().c_str());
printf("%s\n", stamps.back().toString().c_str());
printf("%f\n", timeDifference(stamps.back(), stamps.front()));
int increments[100] = { 0 };
int64_t start = stamps.front().microSecondsSinceEpoch();
for (int i = 1; i < kNumber; ++i)
{
int64_t next = stamps[i].microSecondsSinceEpoch();
int64_t inc = next - start;
start = next;
if (inc < 0)
{
printf("reverse!\n");
}
else if (inc < 100)
{
++increments[inc];
}
else
{
printf("big gap %d\n", static_cast<int>(inc));
}
}
for (int i = 0; i < 100; ++i)
{
printf("%2d: %d\n", i, increments[i]);
}
}
int main()
{
Timestamp now(Timestamp::now());
printf("%s\n", now.toString().c_str());
passByValue(now);
passByConstReference(now);
benchmark();
}