

BOOST_STATIC_ASSERT
#include <boost/static_assert.hpp>
#include <cassert>
class Timestamp
{
private:
int64_t microSecondsSinceEpoch_;
};
BOOST_STATIC_ASSERT(sizeof(Timestamp) == sizeof(int64_t));//编译时断言
//BOOST_STATIC_ASSERT(sizeof(int) == sizeof(short));
int main(void)
{
assert(sizeof(Timestamp) == sizeof(int64_t));//运行时断言
system("pause");
return 0;
}
boost::less_than_comparable、boost::equality_comparable
定义一个类型的操作符时,保证所有操作符都有定义是一件乏味并容易出错的工作。例如,你提供了operator< ,通常都要同时提供operator<=, operator>, 和 operator>= 。Operators可以根据给定类型的最小的用户自定义操作符集合,自动声明并定义其它所有的相关操作符。
头文件: “boost/operators.hpp”
less_than_comparable
less_than_comparable 要求类型T具有以下语义:
bool operator<(const T&,const T&);
bool operator>(const T&,const T&);
bool operator<=(const T&,const T&);
bool operator>=(const T&,const T&);
要派生自 boost::less_than_comparable, 派生类T必须提供:
bool operator<(const T&, const T&);
equality_comparable
equality_comparable 要求类型T具有以下语义:
bool operator==(const T&,const T&);
bool operator!=(const T&,const T&);
要派生自 boost::equality_comparable, 派生类T必须提供:
bool operator==(const T&,const T&);
自己简单写了个例子:
#include <boost/operators.hpp>
#include<iostream>
using namespace std;
class Test :boost::equality_comparable<Test>,
boost::less_than_comparable<Test>
{
int value_;
public:
Test(int value) :value_(value) {}
//自定义公共接口
bool less_than(const Test& other) const {
return this->value_ < other.value_;
}
friend bool operator==(const Test& lhs, const Test& rhs) {
return lhs.value_ == rhs.value_;
//把运算符重载为类的友元函数(类的友元函数不是类的成员函数)
}
};
bool operator<(const Test& lhs, const Test& rhs) {
return lhs.less_than(rhs);//常对象只能调用常成员函数
//注意,这里把运算符重载为普通函数,但是value_是私有成员,所以不能直接写成return lhs.value_<rhs.value_;
//所以要借助自定义的公共接口less_than
}
int main(void)
{
Test t1(11);
Test t2(12);
if (t1 != t2) {
cout << "t1!=t2\n";
if (t1 > t2)
cout << "t1>t2\n";
else
cout << "t1<t2\n";
}
else
cout << "t1==t2\n";
system("pause");
return 0;
}
Timestamp类源码
copyable.h
很好懂
#ifndef MUDUO_BASE_COPYABLE_H
#define MUDUO_BASE_COPYABLE_H
namespace muduo
{
/// A tag class emphasises the objects are copyable.
/// The empty base class optimization applies.
/// Any derived class of copyable should be a value type.
class copyable
{
protected:
copyable() = default;
~copyable() = default;
};
} // namespace muduo
#endif // MUDUO_BASE_COPYABLE_H
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::equality_comparable<Timestamp>,
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 microSecondsSinceEpochArg)
: microSecondsSinceEpoch_(microSecondsSinceEpochArg)
{
}
void swap(Timestamp& that)
{
std::swap(microSecondsSinceEpoch_, that.microSecondsSinceEpoch_);
}
// default copy/assignment/dtor are Okay
string toString() const;
string toFormattedString(bool showMicroseconds = true) 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()
{
return Timestamp();
}
static Timestamp fromUnixTime(time_t t)
{
return fromUnixTime(t, 0);
}
static Timestamp fromUnixTime(time_t t, int microseconds)
{
return Timestamp(static_cast<int64_t>(t) * kMicroSecondsPerSecond + microseconds);
}
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-microsecond
/// 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);
}
} // namespace muduo
#endif // MUDUO_BASE_TIMESTAMP_H
Timestamp.cc
#include "muduo/base/Timestamp.h"
#include <sys/time.h>
#include <stdio.h>
#ifndef __STDC_FORMAT_MACROS
#define __STDC_FORMAT_MACROS
#endif
#include <inttypes.h>
using namespace muduo;
static_assert(sizeof(Timestamp) == sizeof(int64_t),
"Timestamp is same size as int64_t");
string Timestamp::toString() const
{
char buf[32] = {0};
int64_t seconds = microSecondsSinceEpoch_ / kMicroSecondsPerSecond;
int64_t microseconds = microSecondsSinceEpoch_ % kMicroSecondsPerSecond;
snprintf(buf, sizeof(buf), "%" PRId64 ".%06" PRId64 "", seconds, microseconds);
return buf;
}
string Timestamp::toFormattedString(bool showMicroseconds) const
{
char buf[64] = {0};
time_t seconds = static_cast<time_t>(microSecondsSinceEpoch_ / kMicroSecondsPerSecond);
struct tm tm_time;
gmtime_r(&seconds, &tm_time);
if (showMicroseconds)
{
int microseconds = static_cast<int>(microSecondsSinceEpoch_ % kMicroSecondsPerSecond);
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);
}
else
{
snprintf(buf, sizeof(buf), "%4d%02d%02d %02d:%02d:%02d",
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);
}
return buf;
}
Timestamp Timestamp::now()
{
struct timeval tv;
gettimeofday(&tv, NULL);
int64_t seconds = tv.tv_sec;
return Timestamp(seconds * kMicroSecondsPerSecond + tv.tv_usec);
}
编译时注意,源码中#include "muduo/base/Timestamp.h"使用的是相对路径!

本文详细介绍了Boost库中的静态断言STATIC_ASSERT和操作符自动生成特性,展示了如何在Timestamp类中使用它们确保类型大小和操作符一致性。通过实例演示了如何实现equality_comparable和less_than_comparable,以及在实际项目中的应用。
198

被折叠的 条评论
为什么被折叠?



