1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/nsprpub/pr/src/cplus/rcinrval.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,137 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +/* 1.10 +** C++ interval times (ref: prinrval.h) 1.11 +** 1.12 +** An interval is a period of time. The start of the interval (epoch) 1.13 +** must be defined by the application. The unit of time of an interval 1.14 +** is platform dependent, therefore so is the maximum interval that is 1.15 +** representable. However, that interval is never less than ~6 hours. 1.16 +*/ 1.17 +#if defined(_RCINTERVAL_H) 1.18 +#else 1.19 +#define _RCINTERVAL_H 1.20 + 1.21 +#include "rcbase.h" 1.22 +#include <prinrval.h> 1.23 + 1.24 +class PR_IMPLEMENT(RCInterval): public RCBase 1.25 +{ 1.26 +public: 1.27 + typedef enum {now, no_timeout, no_wait} RCReservedInterval; 1.28 + 1.29 + virtual ~RCInterval(); 1.30 + 1.31 + RCInterval(); 1.32 + 1.33 + RCInterval(PRIntervalTime interval); 1.34 + RCInterval(const RCInterval& copy); 1.35 + RCInterval(RCReservedInterval special); 1.36 + 1.37 + void SetToNow(); 1.38 + 1.39 + void operator=(const RCInterval&); 1.40 + void operator=(PRIntervalTime interval); 1.41 + 1.42 + PRBool operator<(const RCInterval&); 1.43 + PRBool operator>(const RCInterval&); 1.44 + PRBool operator==(const RCInterval&); 1.45 + PRBool operator>=(const RCInterval&); 1.46 + PRBool operator<=(const RCInterval&); 1.47 + 1.48 + RCInterval operator+(const RCInterval&); 1.49 + RCInterval operator-(const RCInterval&); 1.50 + RCInterval& operator+=(const RCInterval&); 1.51 + RCInterval& operator-=(const RCInterval&); 1.52 + 1.53 + RCInterval operator/(PRUint32); 1.54 + RCInterval operator*(PRUint32); 1.55 + RCInterval& operator/=(PRUint32); 1.56 + RCInterval& operator*=(PRUint32); 1.57 + 1.58 + 1.59 + PRUint32 ToSeconds() const; 1.60 + PRUint32 ToMilliseconds() const; 1.61 + PRUint32 ToMicroseconds() const; 1.62 + operator PRIntervalTime() const; 1.63 + 1.64 + static PRIntervalTime FromSeconds(PRUint32 seconds); 1.65 + static PRIntervalTime FromMilliseconds(PRUint32 milli); 1.66 + static PRIntervalTime FromMicroseconds(PRUint32 micro); 1.67 + 1.68 + friend class RCCondition; 1.69 + 1.70 +private: 1.71 + PRIntervalTime interval; 1.72 + 1.73 +}; /* RCInterval */ 1.74 + 1.75 + 1.76 +inline RCInterval::RCInterval(): RCBase() { } 1.77 + 1.78 +inline RCInterval::RCInterval(const RCInterval& his): RCBase() 1.79 + { interval = his.interval; } 1.80 + 1.81 +inline RCInterval::RCInterval(PRIntervalTime ticks): RCBase() 1.82 + { interval = ticks; } 1.83 + 1.84 +inline void RCInterval::SetToNow() { interval = PR_IntervalNow(); } 1.85 + 1.86 +inline void RCInterval::operator=(const RCInterval& his) 1.87 + { interval = his.interval; } 1.88 + 1.89 +inline void RCInterval::operator=(PRIntervalTime his) 1.90 + { interval = his; } 1.91 + 1.92 +inline PRBool RCInterval::operator==(const RCInterval& his) 1.93 + { return (interval == his.interval) ? PR_TRUE : PR_FALSE; } 1.94 +inline PRBool RCInterval::operator<(const RCInterval& his) 1.95 + { return (interval < his.interval)? PR_TRUE : PR_FALSE; } 1.96 +inline PRBool RCInterval::operator>(const RCInterval& his) 1.97 + { return (interval > his.interval) ? PR_TRUE : PR_FALSE; } 1.98 +inline PRBool RCInterval::operator<=(const RCInterval& his) 1.99 + { return (interval <= his.interval) ? PR_TRUE : PR_FALSE; } 1.100 +inline PRBool RCInterval::operator>=(const RCInterval& his) 1.101 + { return (interval <= his.interval) ? PR_TRUE : PR_FALSE; } 1.102 + 1.103 +inline RCInterval RCInterval::operator+(const RCInterval& his) 1.104 + { return RCInterval((PRIntervalTime)(interval + his.interval)); } 1.105 +inline RCInterval RCInterval::operator-(const RCInterval& his) 1.106 + { return RCInterval((PRIntervalTime)(interval - his.interval)); } 1.107 +inline RCInterval& RCInterval::operator+=(const RCInterval& his) 1.108 + { interval += his.interval; return *this; } 1.109 +inline RCInterval& RCInterval::operator-=(const RCInterval& his) 1.110 + { interval -= his.interval; return *this; } 1.111 + 1.112 +inline RCInterval RCInterval::operator/(PRUint32 him) 1.113 + { return RCInterval((PRIntervalTime)(interval / him)); } 1.114 +inline RCInterval RCInterval::operator*(PRUint32 him) 1.115 + { return RCInterval((PRIntervalTime)(interval * him)); } 1.116 + 1.117 +inline RCInterval& RCInterval::operator/=(PRUint32 him) 1.118 + { interval /= him; return *this; } 1.119 + 1.120 +inline RCInterval& RCInterval::operator*=(PRUint32 him) 1.121 + { interval *= him; return *this; } 1.122 + 1.123 +inline PRUint32 RCInterval::ToSeconds() const 1.124 + { return PR_IntervalToSeconds(interval); } 1.125 +inline PRUint32 RCInterval::ToMilliseconds() const 1.126 + { return PR_IntervalToMilliseconds(interval); } 1.127 +inline PRUint32 RCInterval::ToMicroseconds() const 1.128 + { return PR_IntervalToMicroseconds(interval); } 1.129 +inline RCInterval::operator PRIntervalTime() const { return interval; } 1.130 + 1.131 +inline PRIntervalTime RCInterval::FromSeconds(PRUint32 seconds) 1.132 + { return PR_SecondsToInterval(seconds); } 1.133 +inline PRIntervalTime RCInterval::FromMilliseconds(PRUint32 milli) 1.134 + { return PR_MillisecondsToInterval(milli); } 1.135 +inline PRIntervalTime RCInterval::FromMicroseconds(PRUint32 micro) 1.136 + { return PR_MicrosecondsToInterval(micro); } 1.137 + 1.138 +#endif /* defined(_RCINTERVAL_H) */ 1.139 + 1.140 +/* RCInterval.h */