00001
00021
00022
00023
00024
00025 #ifndef __SYNFIG_TIME_H
00026 #define __SYNFIG_TIME_H
00027
00028
00029
00030 #include "string_decl.h"
00031
00032
00033
00034
00035
00036
00037
00038 namespace synfig {
00039
00044 class Time
00045 {
00046 public:
00047 typedef double value_type;
00048
00052 enum Format
00053 {
00054 FORMAT_NORMAL=0,
00055 FORMAT_NOSPACES=(1<<0),
00056 FORMAT_FULL=(1<<1),
00057 FORMAT_VIDEO=(1<<2),
00058
00059 FORMAT_END=(1<<4)
00060 };
00061
00062 private:
00063 value_type value_;
00064
00065 static const value_type epsilon_() { return static_cast<value_type>(0.0005); }
00066
00067 public:
00068 Time() { }
00069
00070 Time(const value_type &x):value_(x) { }
00071
00072 Time(int x):value_(x) { }
00073
00074 Time(int hour, int minute, float second):value_(static_cast<value_type>(second+hour*3600+minute*60)) { }
00075
00077
00083 Time(const String &string, float fps=0);
00084
00086 static const Time begin() { return static_cast<synfig::Time>(-32767.0f*512.0f); }
00087
00089 static const Time end() { return static_cast<synfig::Time>(32767.0f*512.0f); }
00090
00092 static const Time zero() { return static_cast<synfig::Time>(0); }
00093
00095 static const Time epsilon() { return static_cast<synfig::Time>(epsilon_()); }
00096
00098
00099 String get_string(float fps=0, Time::Format format=FORMAT_NORMAL)const;
00100
00102 bool is_valid()const;
00103
00105 Time round(float fps)const;
00106
00107 bool is_equal(const Time& rhs)const { return (value_>rhs.value_)?value_-rhs.value_<=epsilon_():rhs.value_-value_<=epsilon_(); }
00108 bool is_less_than(const Time& rhs)const { return rhs.value_-value_ > epsilon_(); }
00109 bool is_more_than(const Time& rhs)const { return value_-rhs.value_ > epsilon_(); }
00110
00111 operator double()const { return value_; }
00112
00113 template<typename U> bool operator<(const U& rhs)const { return value_<rhs; }
00114 template<typename U> bool operator>(const U& rhs)const { return value_>rhs; }
00115 template<typename U> bool operator<=(const U& rhs)const { return value_<=rhs; }
00116 template<typename U> bool operator>=(const U& rhs)const { return value_>=rhs; }
00117 template<typename U> bool operator==(const U& rhs)const { return value_==rhs; }
00118 template<typename U> bool operator!=(const U& rhs)const { return value_!=rhs; }
00119
00120 #if 0
00121 bool operator<(const Time& rhs)const { return value_<rhs.value_; }
00122 bool operator>(const Time& rhs)const { return value_>rhs.value_; }
00123 bool operator<=(const Time& rhs)const { return value_<=rhs.value_; }
00124 bool operator>=(const Time& rhs)const { return value_>=rhs.value_; }
00125 bool operator==(const Time& rhs)const { return value_==rhs.value_; }
00126 bool operator!=(const Time& rhs)const { return value_!=rhs.value_; }
00127 #else
00128 bool operator<(const Time& rhs)const { return is_less_than(rhs); }
00129 bool operator>(const Time& rhs)const { return is_more_than(rhs); }
00130 bool operator<=(const Time& rhs)const { return is_less_than(rhs)||is_equal(rhs); }
00131 bool operator>=(const Time& rhs)const { return is_more_than(rhs)||is_equal(rhs); }
00132 bool operator==(const Time& rhs)const { return is_equal(rhs); }
00133 bool operator!=(const Time& rhs)const { return !is_equal(rhs); }
00134 #endif
00135
00136 template<typename U> const Time& operator+=(const U &rhs) { value_+=static_cast<value_type>(rhs); return *this; }
00137 template<typename U> const Time& operator-=(const U &rhs) { value_-=static_cast<value_type>(rhs); return *this; }
00138 template<typename U> const Time& operator*=(const U &rhs) { value_*=static_cast<value_type>(rhs); return *this; }
00139 template<typename U> const Time& operator/=(const U &rhs) { value_/=static_cast<value_type>(rhs); return *this; }
00140
00141 template<typename U> Time operator+(const U &rhs)const { return value_+static_cast<value_type>(rhs); }
00142 template<typename U> Time operator-(const U &rhs)const { return value_-static_cast<value_type>(rhs); }
00143 template<typename U> Time operator*(const U &rhs)const { return value_*static_cast<value_type>(rhs); }
00144 template<typename U> Time operator/(const U &rhs)const { return value_/static_cast<value_type>(rhs); }
00145
00146 Time operator-()const { return -value_; }
00147 };
00148
00150
00151 inline Time::Format operator|(Time::Format lhs, Time::Format rhs)
00152 { return static_cast<Time::Format>((int)lhs|(int)rhs); }
00153
00155
00158 inline bool operator<=(Time::Format lhs, Time::Format rhs)
00159 { return (static_cast<int>(lhs) & static_cast<int>(rhs))==static_cast<int>(rhs); }
00160
00161 };
00162
00163
00164
00165 #endif