1 /* === S I N F G =========================================================== */
3 ** \brief Template Header
5 ** $Id: time.h,v 1.1.1.1 2005/01/04 01:23:15 darco Exp $
8 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
10 ** This software and associated documentation
11 ** are CONFIDENTIAL and PROPRIETARY property of
12 ** the above-mentioned copyright holder.
14 ** You may not copy, print, publish, or in any
15 ** other way distribute this software without
16 ** a prior written agreement with
17 ** the copyright holder.
20 /* ========================================================================= */
22 /* === S T A R T =========================================================== */
24 #ifndef __SINFG_TIME_H
25 #define __SINFG_TIME_H
27 /* === H E A D E R S ======================================================= */
29 #include "string_decl.h"
31 /* === M A C R O S ========================================================= */
33 /* === T Y P E D E F S ===================================================== */
35 /* === C L A S S E S & S T R U C T S ======================================= */
41 ** \see TimeFormat, time_to_string(), string_to_time()
46 typedef double value_type;
50 ** \see Time, get_string() */
53 FORMAT_NORMAL=0, //!< Represents the default method of printing the time
54 FORMAT_NOSPACES=(1<<0), //!< Remove any whitespace
55 FORMAT_FULL=(1<<1), //!< Do not remove units that have "zero" value
56 FORMAT_VIDEO=(1<<2), //!< Use the HH:MM:SS.ff format
58 FORMAT_END=(1<<4) //!< \internal Not used
59 }; // END of enum Format
64 static const value_type epsilon_() { return static_cast<value_type>(0.0005); }
69 Time(const value_type &x):value_(x) { }
71 Time(int x):value_(x) { }
73 Time(int hour, int minute, float second):value_(static_cast<value_type>(second+hour*3600+minute*60)) { }
75 //! Constructs Time from the given string.
76 /*! \note If the string references frames, then the
77 ** frame rate (\afps) should be provided from the
78 ** correct source. (Which is most likely the RendDesc
79 ** of the current Canvas)
80 ** The frame count will be ignored if the
81 ** FPS is not given. */
82 Time(const String &string, float fps=0);
84 //! Marks the exclusive negative boundary of time
85 static const Time begin() { return static_cast<sinfg::Time>(-32767.0f*512.0f); }
87 //! Marks the exclusive positive boundary of time
88 static const Time end() { return static_cast<sinfg::Time>(32767.0f*512.0f); }
91 static const Time zero() { return static_cast<sinfg::Time>(0); }
93 //! The amount of allowable error in calculations
94 static const Time epsilon() { return static_cast<sinfg::Time>(epsilon_()); }
96 //! Returns a string describing the current time value
98 String get_string(float fps=0, Time::Format format=FORMAT_NORMAL)const;
101 bool is_valid()const;
103 //! Rounds time to the nearest frame for the given frame rate, \a fps
104 Time round(float fps)const;
106 bool is_equal(const Time& rhs)const { return (value_>rhs.value_)?value_-rhs.value_<=epsilon_():rhs.value_-value_<=epsilon_(); }
107 bool is_less_than(const Time& rhs)const { return rhs.value_-value_ > epsilon_(); }
108 bool is_more_than(const Time& rhs)const { return value_-rhs.value_ > epsilon_(); }
110 operator double()const { return value_; }
112 template<typename U> bool operator<(const U& rhs)const { return value_<rhs; }
113 template<typename U> bool operator>(const U& rhs)const { return value_>rhs; }
114 template<typename U> bool operator<=(const U& rhs)const { return value_<=rhs; }
115 template<typename U> bool operator>=(const U& rhs)const { return value_>=rhs; }
116 template<typename U> bool operator==(const U& rhs)const { return value_==rhs; }
117 template<typename U> bool operator!=(const U& rhs)const { return value_!=rhs; }
120 bool operator<(const Time& rhs)const { return value_<rhs.value_; }
121 bool operator>(const Time& rhs)const { return value_>rhs.value_; }
122 bool operator<=(const Time& rhs)const { return value_<=rhs.value_; }
123 bool operator>=(const Time& rhs)const { return value_>=rhs.value_; }
124 bool operator==(const Time& rhs)const { return value_==rhs.value_; }
125 bool operator!=(const Time& rhs)const { return value_!=rhs.value_; }
127 bool operator<(const Time& rhs)const { return is_less_than(rhs); }
128 bool operator>(const Time& rhs)const { return is_more_than(rhs); }
129 bool operator<=(const Time& rhs)const { return is_less_than(rhs)||is_equal(rhs); }
130 bool operator>=(const Time& rhs)const { return is_more_than(rhs)||is_equal(rhs); }
131 bool operator==(const Time& rhs)const { return is_equal(rhs); }
132 bool operator!=(const Time& rhs)const { return !is_equal(rhs); }
135 template<typename U> const Time& operator+=(const U &rhs) { value_+=static_cast<value_type>(rhs); return *this; }
136 template<typename U> const Time& operator-=(const U &rhs) { value_-=static_cast<value_type>(rhs); return *this; }
137 template<typename U> const Time& operator*=(const U &rhs) { value_*=static_cast<value_type>(rhs); return *this; }
138 template<typename U> const Time& operator/=(const U &rhs) { value_/=static_cast<value_type>(rhs); return *this; }
140 template<typename U> Time operator+(const U &rhs)const { return value_+static_cast<value_type>(rhs); }
141 template<typename U> Time operator-(const U &rhs)const { return value_-static_cast<value_type>(rhs); }
142 template<typename U> Time operator*(const U &rhs)const { return value_*static_cast<value_type>(rhs); }
143 template<typename U> Time operator/(const U &rhs)const { return value_/static_cast<value_type>(rhs); }
145 Time operator-()const { return -value_; }
146 }; // END of class Time
148 //! This operator allows the combining of Time::Format flags using the '|' operator
149 /*! \see Time::Format, Time::get_string() */
150 inline Time::Format operator|(Time::Format lhs, Time::Format rhs)
151 { return static_cast<Time::Format>((int)lhs|(int)rhs); }
153 //! This operator is for checking Time::Format flags.
154 /*! Don't think of it as "less then or equal to", but think of it
155 ** like an arrow. Is \a rhs inside of \a lhs ?
156 ** \see Time::Format, Time::get_string() */
157 inline bool operator<=(Time::Format lhs, Time::Format rhs)
158 { return (static_cast<int>(lhs) & static_cast<int>(rhs))==static_cast<int>(rhs); }
160 }; // END of namespace sinfg
162 /* === E N D =============================================================== */