📄 wytimespec.h
字号:
/* Copyright is licensed under GNU LGPL. by I.J.Wang 2003*/#ifndef WYTIMESPEC_H__#define WYTIMESPEC_H__#define WYTIMESPEC_VERSION 31#include "wycseg.h"#include "wystr.h"#include <ctime> // for struct timespec#include <limits>typedef std::time_t Wy_Second; // type of timespec::tv_sectypedef signed long int Wy_Nano; // type of timespec::tv_nsec// WyTimeSpec is the class for time interval.//class WyTimeSpec { public: static const Wy_Nano _Giga=WY_CONST_GIGA; // 1x10^9 static const int _GigaDigit10=9; static const char class_name[]; static const WyTimeSpec min(void) WY__TSPC() { return WyTimeSpec( std::numeric_limits<Wy_Second>::min(), Wy_Nano(-_Giga+1)); }; static const WyTimeSpec max(void) WY__TSPC() { return WyTimeSpec( std::numeric_limits<Wy_Second>::max(), Wy_Nano(_Giga-1)); }; WY_THROW_REPLY; WyTimeSpec() WY__NOTHROW__ : _tspc() {}; WyTimeSpec(const WyTimeSpec& c) WY__NOTHROW__ : _tspc(c._tspc) {}; // self-copy ok WyTimeSpec(Wy_Second sec, Wy_Nano nano) WY__TSPC(Reply); explicit // no implicit conversion WyTimeSpec(const WyCSeg& cseg) WY__TSPC(Reply); WyTimeSpec(WyTimeSpec& c, Wy::ByMove_t) WY__NOTHROW__ : _tspc(c._tspc) {}; // self-copy ok // note: This member is not documented explicit WyTimeSpec(double fvalue) WY__TSPC(Reply); bool is_default(void) const WY__NOTHROW__ { return (_tspc.tv_sec==Wy_Second())&&(_tspc.tv_nsec==Wy_Nano()); }; Wy_Second second(void) const WY__NOTHROW__ { return _tspc.tv_sec; }; // Note: Let tm be an object of WyTimeSpec. The following equation holds. // tm == WyTimeSpec(tm.secs(),tm.nano()) // Wy_Nano nano(void) const WY__NOTHROW__ { return _tspc.tv_nsec; }; void reset(void) WY__NOTHROW__ { _tspc.tv_sec=Wy_Second(); _tspc.tv_nsec=Wy_Nano(); }; void reset(const WyTimeSpec &t) WY__NOTHROW__ { _tspc=t._tspc; }; WyRet reset(Wy_Second sec, Wy_Nano nano) WY__TSPC(); WyRet reset(const WyCSeg& cseg) WY__TSPC(); // not defined for now: ambiguity //void reset(double) WY__TSPC(); //void reset(float) WY__TSPC(); void swap(WyTimeSpec &tm) WY__NOTHROW__ { Wy__Base::vswap(_tspc,tm._tspc); }; WyTimeSpec& operator =(const WyTimeSpec &rhs) WY__NOTHROW__ { _tspc=rhs._tspc; return(*this); }; WyTimeSpec& operator =(const WyCSeg& cseg) WY__TSPC(Reply); const WyTimeSpec operator -() const WY__TSPC(Reply); WyTimeSpec& operator ++() WY__TSPC(Reply) { if(_tspc.tv_sec>=std::numeric_limits<Wy_Second>::max()) { WY_THROW( Reply(Wym_ERANGE) ); } ++_tspc.tv_sec; return(*this); }; WyTimeSpec& operator --() WY__TSPC(Reply) { if(_tspc.tv_sec<=std::numeric_limits<Wy_Second>::min()) { WY_THROW( Reply(Wym_ERANGE) ); } --_tspc.tv_sec; return(*this); }; const WyTimeSpec operator ++(int) WY__TSPC(Reply) { if(_tspc.tv_sec>=std::numeric_limits<Wy_Second>::max()) { WY_THROW( Reply(Wym_ERANGE) ); } WyTimeSpec tm(*this); _tspc.tv_sec++; return(tm); }; const WyTimeSpec operator --(int) WY__TSPC(Reply) { if(_tspc.tv_sec<=std::numeric_limits<Wy_Second>::min()) { WY_THROW( Reply(Wym_ERANGE) ); } WyTimeSpec tm(*this); _tspc.tv_sec--; return(tm); }; WyTimeSpec& operator +=(const WyTimeSpec &rhs) WY__TSPC(Reply); WyTimeSpec& operator -=(const WyTimeSpec &rhs) WY__TSPC(Reply); const WyTimeSpec operator +(const WyTimeSpec &rhs) const WY__TSPC(Reply); const WyTimeSpec operator -(const WyTimeSpec &rhs) const WY__TSPC(Reply); // [Syn] Comparisons // bool operator ==(const WyTimeSpec &rhs) const WY__TSPC() { return (_tspc.tv_nsec==rhs._tspc.tv_nsec)&& (_tspc.tv_sec==rhs._tspc.tv_sec); }; bool operator !=(const WyTimeSpec &rhs) const WY__TSPC() { return (_tspc.tv_nsec!=rhs._tspc.tv_nsec)|| (_tspc.tv_sec!=rhs._tspc.tv_sec); }; bool operator >(const WyTimeSpec &rhs) const WY__TSPC(); bool operator >=(const WyTimeSpec &rhs) const WY__TSPC(); bool operator <(const WyTimeSpec &rhs) const WY__TSPC(); bool operator <=(const WyTimeSpec &rhs) const WY__TSPC(); const WyTimeSpec abs(void) const WY__TSPC(Reply); WyRet add(Wy_Second sec, Wy_Nano nsec) WY__TSPC(); WyRet add(const WyTimeSpec& tm) WY__TSPC() { return this->add(tm._tspc.tv_sec,tm._tspc.tv_nsec); }; // Note: nsec is allowed to the maximum value of long int. // WyRet sub(Wy_Second sec, Wy_Nano nsec) WY__TSPC(); WyRet sub(const WyTimeSpec& tm) WY__TSPC() { return this->sub(tm._tspc.tv_sec,tm._tspc.tv_nsec); }; // [Syn] Multiply *this by n // // [Ret] Ok // Wym_ERANGE // WyRet mul(long int n) WY__TSPC(); // note: This member is yet not documented WyRet mul(const WyTimeSpec& n) WY__TSPC(); // [Syn] Divide *this by n // // [Ret] Ok // Wym_EDIVZERO // Wym_ERANGE // WyRet div(long int n) WY__TSPC(); // note: not figure out how this member should be implemented // Contribution Welcom! // //WyRet div(const WyTimeSpec& n) WY__TSPC(); // Convert WyTimeSpec implicitly to double/float // //operator double() const WY__TSPC() // { return (double)_secs + ((double)_nano/WY_CONST_GIGA) ; }; //operator float() const WY__TSPC() // { return (double)_secs + ((double)_nano/WY_CONST_GIGA) ; }; // const ::timespec& wy_timespec(void) const { return _tspc; }; ::timespec& wy_timespec(void) { return _tspc; }; private: class _Assertion; // [Syn] Normalize sec/nsec // sec/nsec is normalized iff // // 1. (sec>0) &&(nsec>=0)&&(nsec<Giga) // 2. (sec<0) &&(nsec<=0)&&(nsec>-Giga) // 3. (sec==0)&&(nsec>-Giga)&&(nsec<Giga) // // [Ret] Ok // Wym_ERANGE sec/nsec normalization failure // // [Property] // nsec>0 --> sec>=0 // nsec<0 --> sec<=0 // T= sec + nsec/Giga // static WyRet _normalize(long& sec, long& nsec) WY__TSPC(); static WyRet _normalize(long long int& sec, long long int& nsec) WY__TSPC(); inline WyRet _init(const char* buf,size_t blen) WY__TSPC(); struct ::timespec _tspc; // not supported yet WyRet mul(long long n) WY__TSPC(); // disabled WyTimeSpec& operator =(int); // precision not enough question WyTimeSpec(float fvalue) WY__TSPC(Reply);};namespace std {template<>class numeric_limits<WyTimeSpec> { public: static const bool is_specialized=false; static const bool is_signed=true; static const bool is_integer=false; static const int digits= std::numeric_limits<Wy_Second>::digits+29; static const bool is_exact=true; static const int radix= std::numeric_limits<Wy_Second>::radix; static WyTimeSpec min() WY__NOTHROW__ { return WyTimeSpec::min(); }; static WyTimeSpec max() WY__NOTHROW__ { return WyTimeSpec::max(); };};}; // End of stdnamespace Wy { WyRet sleep(WyTimeSpec tsp); WyRet sleep(WyTimeSpec req, WyTimeSpec& rem); WyRet sleep_till(WyTimeSpec wtm); // Note: Platform may limit the interval (2038.1.18) // WyTimeSpec now(void); WyRet set_systime(const WyTimeSpec& t); WyRet _mkstr(WyStr &str,const WyTimeSpec& num,int radix,size_t frdig); WyRet _strnum(WyTimeSpec& res, const char** endptr, WyCSeg cseg); WyRet _strnum(WyTimeSpec& res, const char** endptr, WyCSeg cseg, const int& radix); WyRet _strnum(WyTimeSpec& res, const char** endptr, WyCSeg cseg, int& radix); // Note: These two functions are not documented // For postcondition is not reliable!!! // WyRet _scanum(WyTimeSpec&, size_t&, const WyCSeg&, const int&); WyRet _scanum(WyTimeSpec&, size_t&, const WyCSeg&); WyStr wrd(const WyTimeSpec& tm); WyStr wrd(const WyTimeSpec&,int,size_t);}; // end namespace Wy/*// Wy_Array specilization to support struct timespec,timeval//#include "wy_array.h"#include <sys/time.h> // for timevaltemplate<>struct Wy_Array< ::timeval > : Wy__ArrayValue< ::timeval > { Wy_Array() : Wy__ArrayValue< ::timeval >() {}; Wy_Array(size_t s,const ::timeval& elem) : Wy__ArrayValue< ::timeval >(s,elem) {}; Wy_Array(const Wy_Array& s) : Wy__ArrayValue< ::timeval >(s) {}; Wy_Array(Wy_Array& p, Wy::ByMove_t t) : Wy__ArrayValue< ::timeval >(p,t) {};};template<>struct Wy_Array< ::timespec > : Wy__ArrayValue< ::timespec > { Wy_Array() : Wy__ArrayValue< ::timespec >() {}; Wy_Array(size_t s,const ::timespec& elem) : Wy__ArrayValue< ::timespec >(s,elem) {}; Wy_Array(const Wy_Array& s) : Wy__ArrayValue< ::timespec >(s) {}; Wy_Array(Wy_Array& p, Wy::ByMove_t t) : Wy__ArrayValue< ::timespec >(p,t) {};};*/#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -