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

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_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
{
};

};

#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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值