本文以系统的struct timeval为例,使其支持+、-、+=、-=4种算术运算和<,<=,>,>=,==、!= 6种关系运算,这样一来就免去了显式调用函数的麻烦,使代码简洁优雅,实现如下
1
extern void timeval_normalize(timeval &t);
2![](/Images/OutliningIndicators/None.gif)
3
class timeval_t : public ::timeval
4![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
5
public:
6
timeval_t(long sec = 0,long usec = 0)
7![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
8
tv_sec = sec, tv_usec = usec;
9
timeval_normalize(*this);
10
}
11
12
timeval_t(const timeval& t)
13![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
14![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (this != &t)
{
15
tv_sec = t.tv_sec, tv_usec = t.tv_usec;
16
timeval_normalize(*this);
17
}
18
}
19
20
timeval_t& operator = (const timeval &t)
21![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
22![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (this != &t)
{
23
tv_sec = t.tv_sec, tv_usec = t.tv_usec;
24
timeval_normalize(*this);
25
}
26
return *this;
27
}
28
};
29![](/Images/OutliningIndicators/None.gif)
30
inline void operator += (timeval &t1,const timeval &t2)
31![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
32
t1.tv_sec += t2.tv_sec,t1.tv_usec += t2.tv_usec;
33
timeval_normalize(t1);
34
}
35![](/Images/OutliningIndicators/None.gif)
36
inline void operator -= (timeval &t1,const timeval &t2)
37![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
38
t1.tv_sec -= t2.tv_sec, t1.tv_usec -= t2.tv_usec;
39
timeval_normalize(t1);
40
}
41![](/Images/OutliningIndicators/None.gif)
42
inline timeval operator + (const timeval &t1,const timeval &t2)
43![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
44
return timeval_t(t1.tv_sec+t2.tv_sec,t1.tv_usec+t2.tv_usec);
45
}
46![](/Images/OutliningIndicators/None.gif)
47
inline timeval operator - (const timeval &t1,const timeval &t2)
48![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
49
return timeval_t(t1.tv_sec-t2.tv_sec,t1.tv_usec-t2.tv_usec);
50
}
51![](/Images/OutliningIndicators/None.gif)
52
inline bool operator == (const timeval &t1,const timeval &t2)
53![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
54
timeval_t t(t1.tv_sec-t2.tv_sec,t1.tv_usec-t2.tv_usec);
55
return 0==t.tv_sec&&0==t.tv_usec;
56
}
57![](/Images/OutliningIndicators/None.gif)
58
inline bool operator != (const timeval &t1,const timeval &t2)
59![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
60
return !(t1 == t2);
61
}
62![](/Images/OutliningIndicators/None.gif)
63
inline bool operator < (const timeval &t1,const timeval &t2)
64![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
65
timeval t = t1 - t2;
66
return t.tv_sec < 0;
67
}
68![](/Images/OutliningIndicators/None.gif)
69
inline bool operator > (const timeval &t1,const timeval &t2)
70![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
71
timeval t = t1 - t2;
72
return 0==t.tv_sec && t.tv_usec>0 || t.tv_sec>0;
73
}
74![](/Images/OutliningIndicators/None.gif)
75
inline bool operator <= (const timeval &t1,const timeval &t2)
76![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
77
return !(t1 > t2);
78
}
79![](/Images/OutliningIndicators/None.gif)
80
inline bool operator >= (const timeval &t1,const timeval &t2)
81![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
82
return !(t1 < t2);
83
}
由于timeval_t公有继承timeval,因此上述10种运算也支持timeval_t类型,而算术运算的内部实现保证了运算结果timeval对象是有效的,即其成员变量tv_usec取值在[0,1000000)区间,这是通过timeval_normalize函数实现的,代码如下
1
static const long ONE_SECOND_IN_USECS = 1000000;
2![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
3
void timeval_normalize(timeval &t)
4![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
5![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (t.tv_usec >= ONE_SECOND_IN_USECS)
{
6![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
do
{
7
++t.tv_sec;
8
t.tv_usec -= ONE_SECOND_IN_USECS;
9
}while (t.tv_usec >= ONE_SECOND_IN_USECS);
10![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
}else if (t.tv_usec <= -ONE_SECOND_IN_USECS)
{
11![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
do
{
12
--t.tv_sec;
13
t.tv_usec += ONE_SECOND_IN_USECS;
14
}while (t.tv_usec <= -ONE_SECOND_IN_USECS);
15
}
16![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
17![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (t.tv_sec >= 1 && t.tv_usec < 0)
{
18
--t.tv_sec;
19
t.tv_usec += ONE_SECOND_IN_USECS;
20
}
21
} 最后来看下使用示例
1
timeval t1,t2,t3;
2
timeval_t tv1,tv2,tv3;
3
bool b;
4![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
5
b = t1 == t2;
6
b = t1 != t2;
7
b = t1 == tv2;
8
b = t1 != tv2;
9
b = tv1 == t2;
10
b = tv1 != t2;
11
b = tv1 == tv2;
12
b = tv1 != tv2;
13![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
14
b = t1 < t2;
15
b = t1 <= t2;
16
b = t1 > t2;
17
b = t1 >= t2;
18
b = t1 < tv2;
19
b = t1 <= tv2;
20
b = t1 > tv2;
21
b = t1 >= tv2;
22![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
23
b = tv1 < t2;
24
b = tv1 <= t2;
25
b = tv1 > t2;
26
b = tv1 >= t2;
27
b = tv1 < tv2;
28
b = tv1 <= tv2;
29
b = tv1 > tv2;
30
b = tv1 >= tv2;
31![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
32
t3 = t1 + t2;
33
t3 = tv1 + tv2;
34
t3 = t1 + tv2;
35
t3 = tv1 + t2;
36
tv3 = t1 + t2;
37
tv3 = tv1 + tv2;
38
tv3 = t1 + tv2;
39
tv3 = tv1 + t2;
40![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
41
t3 = t1 - t2;
42
t3 = tv1 - tv2;
43
t3 = t1 - tv2;
44
t3 = tv1 - t2;
45
tv3 = t1 - t2;
46
tv3 = tv1 - tv2;
47
tv3 = t1 - tv2;
48
tv3 = tv1 - t2;
49![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
50
t1 += t2;
51
t1 += tv2;
52
tv1 += t2;
53
tv1 += tv2;
54
t1 -= t2;
55
t1 -= tv2;
56
tv1 -= t2;
57
tv1 -= tv2; 可以看到将运算符用到C式结构上,大大简化了代码的编写。以上所有代码在vc2005和g++4.4下编译通过。
posted on 2013-10-28 23:37
春秋十二月 阅读(1520)
评论(2) 编辑 收藏 引用 所属分类:
C/C++