本文以系统的struct timeval为例,使其支持+、-、+=、-=4种算术运算和<,<=,>,>=,==、!= 6种关系运算,这样一来就免去了显式调用函数的麻烦,使代码简洁优雅,实现如下
1extern void timeval_normalize(timeval &t);
2
3class timeval_t : public ::timeval
4{
5public:
6 timeval_t(long sec = 0,long usec = 0)
7 {
8 tv_sec = sec, tv_usec = usec;
9 timeval_normalize(*this);
10 }
11
12 timeval_t(const timeval& t)
13 {
14 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 {
22 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
30inline void operator += (timeval &t1,const timeval &t2)
31{
32 t1.tv_sec += t2.tv_sec,t1.tv_usec += t2.tv_usec;
33 timeval_normalize(t1);
34}
35
36inline void operator -= (timeval &t1,const timeval &t2)
37{
38 t1.tv_sec -= t2.tv_sec, t1.tv_usec -= t2.tv_usec;
39 timeval_normalize(t1);
40}
41
42inline timeval operator + (const timeval &t1,const timeval &t2)
43{
44 return timeval_t(t1.tv_sec+t2.tv_sec,t1.tv_usec+t2.tv_usec);
45}
46
47inline timeval operator - (const timeval &t1,const timeval &t2)
48{
49 return timeval_t(t1.tv_sec-t2.tv_sec,t1.tv_usec-t2.tv_usec);
50}
51
52inline bool operator == (const timeval &t1,const timeval &t2)
53{
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
58inline bool operator != (const timeval &t1,const timeval &t2)
59{
60 return !(t1 == t2);
61}
62
63inline bool operator < (const timeval &t1,const timeval &t2)
64{
65 timeval t = t1 - t2;
66 return t.tv_sec < 0;
67}
68
69inline bool operator > (const timeval &t1,const timeval &t2)
70{
71 timeval t = t1 - t2;
72 return 0==t.tv_sec && t.tv_usec>0 || t.tv_sec>0;
73}
74
75inline bool operator <= (const timeval &t1,const timeval &t2)
76{
77 return !(t1 > t2);
78}
79
80inline bool operator >= (const timeval &t1,const timeval &t2)
81{
82 return !(t1 < t2);
83}
由于timeval_t公有继承timeval,因此上述10种运算也支持timeval_t类型,而算术运算的内部实现保证了运算结果timeval对象是有效的,即其成员变量tv_usec取值在[0,1000000)区间,这是通过timeval_normalize函数实现的,代码如下
1static const long ONE_SECOND_IN_USECS = 1000000;
2
3void timeval_normalize(timeval &t)
4{
5 if (t.tv_usec >= ONE_SECOND_IN_USECS){
6 do {
7 ++t.tv_sec;
8 t.tv_usec -= ONE_SECOND_IN_USECS;
9 }while (t.tv_usec >= ONE_SECOND_IN_USECS);
10 }else if (t.tv_usec <= -ONE_SECOND_IN_USECS) {
11 do {
12 --t.tv_sec;
13 t.tv_usec += ONE_SECOND_IN_USECS;
14 }while (t.tv_usec <= -ONE_SECOND_IN_USECS);
15 }
16
17 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
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
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
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
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
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
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
春秋十二月 阅读(1529)
评论(2) 编辑 收藏 引用 所属分类:
C/C++