再来理解Timestamp[1-2-1]

来源:互联网 发布:软件产品包装推广 编辑:程序博客网 时间:2024/06/06 00:57

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

主要理解:

muduo::copyable,
boost::less_than_comparable<Timestamp>

【1】先来看看muduo::copyable在copyable.h文件中意思是子类可以进行拷贝或者赋值操作的

#ifndef MUDUO_BASE_COPYABLE_H#define MUDUO_BASE_COPYABLE_Hnamespace 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{};};#endif  // MUDUO_BASE_COPYABLE_H

【2】再看看boost::less_than_comparable<Timestamp> 即符号重载运算符

Boost::Operators 库 之operators(1)

Operators头文件: "boost/operators.hpp"

Operators库由多个基类组成。每一个类生成与其名字概念相关的操作符。你可以用继承的方式来使用它们,如果你需要一个以上的功能,则需要使用多重继承。幸运的是,Operators中定义了一些复合的概念,在大多数情况下可以无须使用多重继承。下面将介绍最常用的一些Operator类,包括它们所表示的概念,以及它们对派生类的要求。某些情况下,使用Operators时,对真实概念的要求会不同于对该概念基类的要求。例如,概念 addable 要求有一个操作符 T operator+(const T& lhs,const T& rhs) 的定义,而Operators的基类 addable 却要求有一个成员函数,T operator+=(const T& other). 使用这个成员函数,基类 addable 为派生类自动增加了 operator+. 在以下章节中,都是首先给出概念,然后再给出对派生自该概念的类的要求。我没有重复本库中的所有概念,仅是挑选了最重要的一些;你可以在 www.boost.org 上找到完整的参考文档。

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&);

注意,返回值的类型不必是真正的 bool, 但必须可以隐式转换为 bool. C++标准中的概念 LessThanComparable 要求提供operator< ,所以从 less_than_comparable 派生的类必须符合该要求。作为回报,less_than_comparable 将依照 operator< 实现其余的三个操作符。


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&);

同样,返回值的类型不必是 bool, 但必须可以隐式转换为 bool. C++标准中的概念 EqualityComparable 要求必须提供 operator== ,因此从 equality_comparable 派生的类必须符合该要求。equality_comparable 类为 T 提供 bool operator!=(const T&,const T&).


addable

addable 概念要求类型T具有以下语义。

T operator+(const T&,const T&);
T operator+=(const T&);

要派生自 boost::addable, 派生类(T)必须提供:

T operator+=(const T&);

返回值的类型必须可以隐式转换为 T. 类 addable 为 T 实现 T operator+(const T&,const T&).


subtractable

subtractable 概念要求类型T具有以下语义。

T operator-(const T&,const T&);
T operator+=(const T&);

要派生自 boost::subtractable, 派生类(T)必须提供:

T operator-=(const T&,const T&);

返回值的类型必须可以隐式转换为 T. 类 addable 为 T 实现 T operator-(const T&,const T&).

orable

orable 概念要求类型T具有以下语义。

T operator|(const T&,const T&);
T operator|=(const T&,const T&);

要派生自 boost::orable, 派生类(T)必须提供:

T operator|=(const T&,const T&);

返回值的类型必须可以隐式转换为 T. 类 addable 为 T 实现 T operator|(const T&,const T&).

andable

andable 概念要求类型T具有以下语义。

T operator&(const T&,const T&);
T operator&=(const T&,const T&);

要派生自 boost::andable, 派生类(T)必须提供:

T operator&=(const T&,const T&);

返回值的类型必须可以隐式转换为 T. 类 addable 为 T 实现 T operator&(const T&,const T&).

incrementable

incrementable 概念要求类型T具有以下语义。

T& operator++(T&);
T operator++(T&,int);

要派生自 boost::incrementable, 派生类(T)必须提供:

T& operator++(T&);

返回值的类型必须可以隐式转换为 T. 类 addable 为 T 实现 T operator++(T&,int).

decrementable

decrementable 概念要求类型T具有以下语义。

T& operator--(T&);
T operator--(T&,int);

要派生自 boost::decrementable, 派生类(T)必须提供:

T& operator--(T&);

返回值的类型必须可以隐式转换为 T. 类 addable 为 T 实现 T operator--(T&,int).

equivalent

equivalent 概念要求类型T具有以下语义。

bool operator<(const T&,const T&);
bool operator==(const T&,const T&);

要派生自 boost::equivalent, 派生类(T)必须提供:

bool operator<(const T&,const T&);

返回值的类型必须可以隐式转换为 bool. 类 equivalent 为 T 实现 T operator==(const T&,const T&). 注意,等价(equivalence)和相等(equality)准确的说是不一样的;两个等价(equivalent)的对象并不一定是相等的(equal)。但对于这里的 equivalent 概念而言,它们是一样的。

解引用操作符

对于迭代器,有两个概念特别有用,dereferenceable 和 indexable, 分别表示了解引用的两种情况:一个是*tt是一个支持解引用的迭代器(显然所有迭代器都支持),另一个是indexing, t[x]t是一个支持下标操作符寻址的类型,而 x 通常是一个整数类型。在更高的抽象级别,它们两个通常一起使用,合称迭代器操作符,包括这两个解引用操作符和一些简单的算术操作符。

dereferenceable

dereferenceable 概念要求类型T具有以下语义,假设 T 是操作数,R 是引用类型,而 P 是指针类型(例如,T 是一个迭代器类型,R 是该迭代器的value_type的引用,而 P 则是该迭代器的value_type的指针)。

P operator->() const;
R operator*() const;

要派生自 boost::dereferenceable, 派生类(T)必须提供:

R operator*() const;

另外,R的一元 operator& 必须可以被隐式转换为 P. 这意味着 R 不必一定要是引用类型,它可以是一个代理类(proxy class)。类dereferenceable 为 T 实现 P operator->() const.

indexable

indexable 概念要求类型T具有以下语义,假设 T 是操作数,R 是引用类型,P 是指针类型,而 D 是 difference_type (例如,T 是一个迭代器类型,R 是该迭代器的value_type的引用,P 是该迭代器的value_type的指针,而 D 则是 difference_type)。

R operator[](D) const;
R operator+(const T&,D);

要派生自 boost::indexable, 派生类(T)必须提供:

R operator+(const T&,D);

类 indexable 为 T 实现 R operator[](D) const.

复合算术操作符

到目前为止我们看到的概念都只代表了最简单的功能。但是,还有一些高级的,或是复合的概念,它们由几个简单概念组合而成,或是在复合概念之上再增加简单的概念而成。例如,一个类是 totally_ordered 的,如果它同时是 less_than_comparable 的和 equality_comparable的。这些组合很有用,因为它们减少了代码的数量,同时还表明了重要且常用的概念。由于它们只是表示了已有概念的组合,所以这些概念很容易用一个表格来表示它们所包含的简单概念。例如,如果一个类派生自 totally_ordered, 它必须实现 less_than_comparable 所要求的操作符(bool operator<(const T&,const T&)) 和 equality_comparable 所要求的操作符(bool operator==(const T&,const T&))。

组合概念

由以下概念组成

totally_ordered

less_than_comparable

equality_comparable

additive

addable

subtractable

multiplicative

multipliable

dividable

integer_multiplicative

multiplicative

modable

arithmetic

additive

multiplicative

integer_arithmetic

additive

integer_multiplicative

bitwise

andable

orable

xorable

unit_steppable

incrementable

decrementable

shiftable

left_shiftable

right_shiftable

ring_operators

additive

multipliable

ordered_ring_operators

ring_operators

totally_ordered

field_operators

ring_operators

dividable

ordered_field_operators

field_operators

totally_ordered

euclidian_ring_operators

ring_operators

dividable

modable

ordered_ euclidian_ring_operators

euclidean_ring_operators

totally_ordered






0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 网络驱动被删了怎么办 新装系统网卡没驱动怎么办 核显没有dp口怎么办 苹果7屏幕太小了怎么办 苹果装系统卡住了怎么办 苹果7手机白屏黑苹果怎么办 衣服破了个洞怎么办 黑苹果自带升级怎么办 双显卡都禁用了怎么办 win10优盘识别不出来怎么办 黑苹果无限重启怎么办 水泥地面起砂怎么办?这几招很有用 跟老公没法过了怎么办 胃和肠子有点烂怎么办? 车的屏幕黑了怎么办 ios12玩游戏闪退怎么办 没网steam要登录怎么办 电脑有gta还需要安装怎么办 孤岛惊魂5卡顿怎么办 酷匠密码忘记了怎么办 战地1有时候卡死怎么办 战地一fps太低怎么办 Dnf与系统不兼容怎么办 使命召唤7显示w怎么办 战地3王者太卡怎么办 合金装备5消音器没了怎么办 红警基地没了怎么办 玩战地1帧数太低怎么办 战地1点游戏不开怎么办 ios耳机孔坏了怎么办? 吃泻药都不排便怎么办 上司离职了我该怎么办 我上司要辞职我怎么办 一方坚决不同意离婚我该怎么办 模拟农场车翻了怎么办 手机退出键坏了怎么办 dnf邮件发错了怎么办 手机提示sd卡已损坏怎么办 解压包文件数据损坏该怎么办 电脑被压缩后电脑打不开怎么办 眼睛里进了飞虫怎么办