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