3a663e97d45f2ba294dbf5d7eb989e1f4487507d
[synfig.git] / synfig-core / trunk / src / synfig / time.h
1 /* === S Y N F I G ========================================================= */
2 /*!     \file time.h
3 **      \brief Template Header
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **
10 **      This package is free software; you can redistribute it and/or
11 **      modify it under the terms of the GNU General Public License as
12 **      published by the Free Software Foundation; either version 2 of
13 **      the License, or (at your option) any later version.
14 **
15 **      This package is distributed in the hope that it will be useful,
16 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
17 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 **      General Public License for more details.
19 **      \endlegal
20 */
21 /* ========================================================================= */
22
23 /* === S T A R T =========================================================== */
24
25 #ifndef __SYNFIG_TIME_H
26 #define __SYNFIG_TIME_H
27
28 /* === H E A D E R S ======================================================= */
29
30 #include "string_decl.h"
31
32 /* === M A C R O S ========================================================= */
33
34 /* === T Y P E D E F S ===================================================== */
35
36 /* === C L A S S E S & S T R U C T S ======================================= */
37
38 namespace synfig {
39
40 /*!     \class Time
41 **      \todo writeme
42 **      \see TimeFormat, time_to_string(), string_to_time()
43 */
44 class Time
45 {
46 public:
47         typedef double value_type;
48
49         /*!     \enum Format
50         **      \todo writeme
51         **      \see Time, get_string() */
52         enum Format
53         {
54                 FORMAT_NORMAL=0,                //!< Represents the default method of printing the time
55                 FORMAT_NOSPACES=(1<<0), //!< Remove any whitespace
56                 FORMAT_FULL=(1<<1),             //!< Do not remove units that have "zero" value
57                 FORMAT_VIDEO=(1<<2),    //!< Use the HH:MM:SS.ff format
58
59                 FORMAT_END=(1<<4)               //!< \internal Not used
60         }; // END of enum Format
61
62 private:
63         value_type value_;
64
65         static value_type epsilon_() { return static_cast<value_type>(0.0005); }
66
67 public:
68         Time() { }
69
70         Time(const value_type &x):value_(x) { }
71
72         Time(int x):value_(x) { }
73
74         Time(int hour, int minute, float second):value_(static_cast<value_type>(second+hour*3600+minute*60)) { }
75
76         //! Constructs Time from the given string.
77         /*!     \note If the string references frames, then the
78         **      frame rate (\a fps) should be provided from the
79         **      correct source. (Which is most likely the RendDesc
80         **      of the current Canvas)
81         **      The frame count will be ignored if the
82         **      FPS is not given. */
83         Time(const String &string, float fps=0);
84
85         //! Marks the exclusive negative boundary of time
86         static const Time begin() { return static_cast<synfig::Time>(-32767.0f*512.0f); }
87
88         //! Marks the exclusive positive boundary of time
89         static const Time end() { return static_cast<synfig::Time>(32767.0f*512.0f); }
90
91         //! Marks zero time
92         static const Time zero() { return static_cast<synfig::Time>(0); }
93
94         //! The amount of allowable error in calculations
95         static const Time epsilon() { return static_cast<synfig::Time>(epsilon_()); }
96
97         //! Returns a string describing the current time value
98         /*!     \see Format */
99         String get_string(float fps=0, Time::Format format=FORMAT_NORMAL)const;
100
101 #ifdef _DEBUG
102         const char *c_str()const;
103 #endif
104
105         //! \writeme
106         bool is_valid()const;
107
108         //! Rounds time to the nearest frame for the given frame rate, \a fps
109         Time round(float fps)const;
110
111         bool is_equal(const Time& rhs)const { return (value_>rhs.value_)?value_-rhs.value_<=epsilon_():rhs.value_-value_<=epsilon_(); }
112         bool is_less_than(const Time& rhs)const { return rhs.value_-value_ > epsilon_(); }
113         bool is_more_than(const Time& rhs)const { return value_-rhs.value_ > epsilon_(); }
114
115         operator double()const { return value_; }
116
117         template<typename U> bool operator<(const U& rhs)const { return value_<rhs; }
118         template<typename U> bool operator>(const U& rhs)const { return value_>rhs; }
119         template<typename U> bool operator<=(const U& rhs)const { return value_<=rhs; }
120         template<typename U> bool operator>=(const U& rhs)const { return value_>=rhs; }
121         template<typename U> bool operator==(const U& rhs)const { return value_==rhs; }
122         template<typename U> bool operator!=(const U& rhs)const { return value_!=rhs; }
123
124 #if 0
125         bool operator<(const Time& rhs)const { return value_<rhs.value_; }
126         bool operator>(const Time& rhs)const { return value_>rhs.value_; }
127         bool operator<=(const Time& rhs)const { return value_<=rhs.value_; }
128         bool operator>=(const Time& rhs)const { return value_>=rhs.value_; }
129         bool operator==(const Time& rhs)const { return value_==rhs.value_; }
130         bool operator!=(const Time& rhs)const { return value_!=rhs.value_; }
131 #else
132         bool operator<(const Time& rhs)const { return is_less_than(rhs); }
133         bool operator>(const Time& rhs)const { return is_more_than(rhs); }
134         bool operator<=(const Time& rhs)const { return is_less_than(rhs)||is_equal(rhs); }
135         bool operator>=(const Time& rhs)const { return is_more_than(rhs)||is_equal(rhs); }
136         bool operator==(const Time& rhs)const { return is_equal(rhs); }
137         bool operator!=(const Time& rhs)const { return !is_equal(rhs); }
138 #endif
139
140         template<typename U> const Time& operator+=(const U &rhs) { value_+=static_cast<value_type>(rhs); return *this; }
141         template<typename U> const Time& operator-=(const U &rhs) { value_-=static_cast<value_type>(rhs); return *this; }
142         template<typename U> const Time& operator*=(const U &rhs) { value_*=static_cast<value_type>(rhs); return *this; }
143         template<typename U> const Time& operator/=(const U &rhs) { value_/=static_cast<value_type>(rhs); return *this; }
144
145         template<typename U> Time operator+(const U &rhs)const { return value_+static_cast<value_type>(rhs); }
146         template<typename U> Time operator-(const U &rhs)const { return value_-static_cast<value_type>(rhs); }
147         template<typename U> Time operator*(const U &rhs)const { return value_*static_cast<value_type>(rhs); }
148         template<typename U> Time operator/(const U &rhs)const { return value_/static_cast<value_type>(rhs); }
149
150         Time operator-()const { return -value_; }
151 }; // END of class Time
152
153 //! This operator allows the combining of Time::Format flags using the '|' operator
154 /*!     \see Time::Format, Time::get_string() */
155 inline Time::Format operator|(Time::Format lhs, Time::Format rhs)
156 { return static_cast<Time::Format>((int)lhs|(int)rhs); }
157
158 //! This operator is for checking Time::Format flags.
159 /*! Don't think of it as "less then or equal to", but think of it
160 **      like an arrow. Is \a rhs inside of \a lhs ?
161 **      \see Time::Format, Time::get_string() */
162 inline bool operator<=(Time::Format lhs, Time::Format rhs)
163 { return (static_cast<int>(lhs) & static_cast<int>(rhs))==static_cast<int>(rhs); }
164
165 }; // END of namespace synfig
166
167 /* === E N D =============================================================== */
168
169 #endif