moreupdates
[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: time.h,v 1.1.1.1 2005/01/04 01:23:15 darco Exp $
6 **
7 **      \legal
8 **      Copyright (c) 2002 Robert B. Quattlebaum Jr.
9 **
10 **      This software and associated documentation
11 **      are CONFIDENTIAL and PROPRIETARY property of
12 **      the above-mentioned copyright holder.
13 **
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.
18 **      \endlegal
19 */
20 /* ========================================================================= */
21
22 /* === S T A R T =========================================================== */
23
24 #ifndef __SYNFIG_TIME_H
25 #define __SYNFIG_TIME_H
26
27 /* === H E A D E R S ======================================================= */
28
29 #include "string_decl.h"
30
31 /* === M A C R O S ========================================================= */
32
33 /* === T Y P E D E F S ===================================================== */
34
35 /* === C L A S S E S & S T R U C T S ======================================= */
36
37 namespace synfig {
38
39 /*!     \class Time
40 **      \todo writeme
41 **      \see TimeFormat, time_to_string(), string_to_time()
42 */
43 class Time
44 {
45 public:
46         typedef double value_type;
47
48         /*!     \enum Format
49         **      \todo writeme
50         **      \see Time, get_string() */
51         enum Format
52         {
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
57         
58                 FORMAT_END=(1<<4)               //!< \internal Not used
59         }; // END of enum Format
60
61 private:
62         value_type value_;
63
64         static const value_type epsilon_() { return static_cast<value_type>(0.0005); }
65
66 public:
67         Time() { }
68
69         Time(const value_type &x):value_(x) { }
70
71         Time(int x):value_(x) { }
72
73         Time(int hour, int minute, float second):value_(static_cast<value_type>(second+hour*3600+minute*60)) { }
74
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);
83
84         //! Marks the exclusive negative boundary of time
85         static const Time begin() { return static_cast<synfig::Time>(-32767.0f*512.0f); }
86         
87         //! Marks the exclusive positive boundary of time
88         static const Time end() { return static_cast<synfig::Time>(32767.0f*512.0f); }
89
90         //! Marks zero time
91         static const Time zero() { return static_cast<synfig::Time>(0); }
92         
93         //! The amount of allowable error in calculations
94         static const Time epsilon() { return static_cast<synfig::Time>(epsilon_()); }
95         
96         //! Returns a string describing the current time value
97         /*!     \see Format */
98         String get_string(float fps=0, Time::Format format=FORMAT_NORMAL)const;
99         
100         //! \writeme
101         bool is_valid()const;
102         
103         //! Rounds time to the nearest frame for the given frame rate, \a fps
104         Time round(float fps)const;
105
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_(); }
109         
110         operator double()const { return value_; }
111
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; }
118
119 #if 0
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_; }
126 #else
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); }
133 #endif
134         
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; }
139
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); }
144         
145         Time operator-()const { return -value_; }
146 }; // END of class Time
147
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); }
152
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); }
159
160 }; // END of namespace synfig
161         
162 /* === E N D =============================================================== */
163
164 #endif