Blame synfig-core/src/synfig/time.h

Carlos Lopez a09598
/* === S Y N F I G ========================================================= */
Carlos Lopez a09598
/*!	\file time.h
Carlos Lopez a09598
**	\brief Template Header
Carlos Lopez a09598
**
Carlos Lopez a09598
**	$Id$
Carlos Lopez a09598
**
Carlos Lopez a09598
**	\legal
Carlos Lopez a09598
**	Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
Carlos Lopez a09598
**	Copyright (c) 2007 Chris Moore
Carlos Lopez a09598
**
Carlos Lopez a09598
**	This package is free software; you can redistribute it and/or
Carlos Lopez a09598
**	modify it under the terms of the GNU General Public License as
Carlos Lopez a09598
**	published by the Free Software Foundation; either version 2 of
Carlos Lopez a09598
**	the License, or (at your option) any later version.
Carlos Lopez a09598
**
Carlos Lopez a09598
**	This package is distributed in the hope that it will be useful,
Carlos Lopez a09598
**	but WITHOUT ANY WARRANTY; without even the implied warranty of
Carlos Lopez a09598
**	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Carlos Lopez a09598
**	General Public License for more details.
Carlos Lopez a09598
**	\endlegal
Carlos Lopez a09598
*/
Carlos Lopez a09598
/* ========================================================================= */
Carlos Lopez a09598
Carlos Lopez a09598
/* === S T A R T =========================================================== */
Carlos Lopez a09598
Carlos Lopez a09598
#ifndef __SYNFIG_TIME_H
Carlos Lopez a09598
#define __SYNFIG_TIME_H
Carlos Lopez a09598
Carlos Lopez a09598
/* === H E A D E R S ======================================================= */
Carlos Lopez a09598
83a7d4
#include <cmath></cmath>
83a7d4
b3a5b8
#include "string.h"
Carlos Lopez a09598
Carlos Lopez a09598
/* === M A C R O S ========================================================= */
Carlos Lopez a09598
Carlos Lopez a09598
/* === T Y P E D E F S ===================================================== */
Carlos Lopez a09598
Carlos Lopez a09598
/* === C L A S S E S & S T R U C T S ======================================= */
Carlos Lopez a09598
Carlos Lopez a09598
namespace synfig {
Carlos Lopez a09598
Carlos Lopez a09598
/*!	\class Time
Carlos Lopez a09598
**	\todo writeme
Carlos Lopez a09598
**	\see TimeFormat, time_to_string(), string_to_time()
Carlos Lopez a09598
*/
Carlos Lopez a09598
class Time
Carlos Lopez a09598
{
Carlos Lopez a09598
public:
Carlos Lopez a09598
	typedef double value_type;
518124
	typedef long long ticks_type;
Carlos Lopez a09598
Carlos Lopez a09598
	/*!	\enum Format
Carlos Lopez a09598
	**	\todo writeme
Carlos Lopez a09598
	**	\see Time, get_string() */
Carlos Lopez a09598
	enum Format
Carlos Lopez a09598
	{
Carlos Lopez a09598
		FORMAT_NORMAL=0,		//!< Represents the default method of printing the time
Carlos Lopez a09598
		FORMAT_NOSPACES=(1<<0),	//!< Remove any whitespace
Carlos Lopez a09598
		FORMAT_FULL=(1<<1),		//!< Do not remove units that have "zero" value
Carlos Lopez a09598
		FORMAT_VIDEO=(1<<2),	//!< Use the HH:MM:SS.FF format
Carlos Lopez a09598
		FORMAT_FRAMES=(1<<3),	//!< Use the FF format (frames only)
Carlos Lopez a09598
Carlos Lopez a09598
		FORMAT_END=(1<<4)		//!< \internal Not used
Carlos Lopez a09598
	}; // END of enum Format
Carlos Lopez a09598
Carlos Lopez a09598
private:
Carlos Lopez a09598
	value_type value_;
Carlos Lopez a09598
Carlos Lopez a09598
	static value_type epsilon_() { return static_cast<value_type>(0.0005); }</value_type>
Carlos Lopez a09598
Carlos Lopez a09598
public:
6320b8
	Time(): value_() { }
Carlos Lopez a09598
Carlos Lopez a09598
	Time(const value_type &x):value_(x) { }
Carlos Lopez a09598
Carlos Lopez a09598
	Time(int x):value_(x) { }
Carlos Lopez a09598
ff1868
	Time(int hour, int minute, float second):value_(static_cast<value_type>(hour*3600 + minute*60 + second)) { }</value_type>
Carlos Lopez a09598
Carlos Lopez a09598
	//! Constructs Time from the given string.
Carlos Lopez a09598
	/*!	\note If the string references frames, then the
Carlos Lopez a09598
	**	frame rate (\a fps) should be provided from the
Carlos Lopez a09598
	**	correct source. (Which is most likely the RendDesc
Carlos Lopez a09598
	**	of the current Canvas)
Carlos Lopez a09598
	**	The frame count will be ignored if the
Carlos Lopez a09598
	**	FPS is not given. */
06d09f
	explicit Time(const String &string, float fps=0);
Carlos Lopez a09598
Carlos Lopez a09598
	//! Marks the exclusive negative boundary of time
Carlos Lopez a09598
	static const Time begin() { return static_cast<synfig::time>(-32767.0f*512.0f); }</synfig::time>
Carlos Lopez a09598
Carlos Lopez a09598
	//! Marks the exclusive positive boundary of time
Carlos Lopez a09598
	static const Time end() { return static_cast<synfig::time>(32767.0f*512.0f); }</synfig::time>
Carlos Lopez a09598
Carlos Lopez a09598
	//! Marks zero time
Carlos Lopez a09598
	static const Time zero() { return static_cast<synfig::time>(0); }</synfig::time>
Carlos Lopez a09598
Carlos Lopez a09598
	//! The amount of allowable error in calculations
518124
	static Time epsilon() { return static_cast<synfig::time>(epsilon_()); }</synfig::time>
518124
518124
	//! The duration of discrete tick used for values comparison
518124
	static value_type tick() { return static_cast<value_type>(0.1*epsilon_()); }</value_type>
Carlos Lopez a09598
Carlos Lopez a09598
	//! Returns a string describing the current time value
Carlos Lopez a09598
	/*!	\see Format */
ff1868
	String get_string(float fps=0, Time::Format format=FORMAT_NORMAL) const;
ff1868
	std::string get_string(Time::Format format) const;
Carlos Lopez a09598
Carlos Lopez a09598
#ifdef _DEBUG
Carlos Lopez a09598
	const char *c_str()const;
Carlos Lopez a09598
#endif
Carlos Lopez a09598
Carlos Lopez a09598
	//! \writeme
Carlos Lopez a09598
	bool is_valid()const;
Carlos Lopez a09598
Carlos Lopez a09598
	//! Rounds time to the nearest frame for the given frame rate, \a fps
Carlos Lopez a09598
	Time round(float fps)const;
Carlos Lopez a09598
518124
	//! The discrete representation to use in std::map and std::set
518124
	ticks_type ticks() const
83a7d4
		{ return (long long)::round(value_/(tick())); }
518124
518124
	bool is_equal(const Time& rhs) const { return ticks() == rhs.ticks(); }
518124
	bool is_less_than(const Time& rhs) const { return ticks() < rhs.ticks(); }
518124
	bool is_more_than(const Time& rhs) const { return rhs.is_less_than(*this); }
Carlos Lopez a09598
518124
	operator value_type()const { return value_; }
Carlos Lopez a09598
Carlos Lopez a09598
	template<typename u=""> bool operator<(const U& rhs)const { return value_</typename>
Carlos Lopez a09598
	template<typename u=""> bool operator>(const U& rhs)const { return value_>rhs; }</typename>
Carlos Lopez a09598
	template<typename u=""> bool operator<=(const U& rhs)const { return value_<=rhs; }</typename>
Carlos Lopez a09598
	template<typename u=""> bool operator>=(const U& rhs)const { return value_>=rhs; }</typename>
Carlos Lopez a09598
	template<typename u=""> bool operator==(const U& rhs)const { return value_==rhs; }</typename>
Carlos Lopez a09598
	template<typename u=""> bool operator!=(const U& rhs)const { return value_!=rhs; }</typename>
Carlos Lopez a09598
Carlos Lopez a09598
	bool operator<(const Time& rhs)const { return is_less_than(rhs); }
Carlos Lopez a09598
	bool operator>(const Time& rhs)const { return is_more_than(rhs); }
518124
	bool operator<=(const Time& rhs)const { return !is_more_than(rhs); }
518124
	bool operator>=(const Time& rhs)const { return !is_less_than(rhs); }
Carlos Lopez a09598
	bool operator==(const Time& rhs)const { return is_equal(rhs); }
Carlos Lopez a09598
	bool operator!=(const Time& rhs)const { return !is_equal(rhs); }
Carlos Lopez a09598
Carlos Lopez a09598
	template<typename u=""> const Time& operator+=(const U &rhs) { value_+=static_cast<value_type>(rhs); return *this; }</value_type></typename>
Carlos Lopez a09598
	template<typename u=""> const Time& operator-=(const U &rhs) { value_-=static_cast<value_type>(rhs); return *this; }</value_type></typename>
Carlos Lopez a09598
	template<typename u=""> const Time& operator*=(const U &rhs) { value_*=static_cast<value_type>(rhs); return *this; }</value_type></typename>
Carlos Lopez a09598
	template<typename u=""> const Time& operator/=(const U &rhs) { value_/=static_cast<value_type>(rhs); return *this; }</value_type></typename>
Carlos Lopez a09598
Carlos Lopez a09598
	template<typename u=""> Time operator+(const U &rhs)const { return value_+static_cast<value_type>(rhs); }</value_type></typename>
Carlos Lopez a09598
	template<typename u=""> Time operator-(const U &rhs)const { return value_-static_cast<value_type>(rhs); }</value_type></typename>
Carlos Lopez a09598
	template<typename u=""> Time operator*(const U &rhs)const { return value_*static_cast<value_type>(rhs); }</value_type></typename>
Carlos Lopez a09598
	template<typename u=""> Time operator/(const U &rhs)const { return value_/static_cast<value_type>(rhs); }</value_type></typename>
Carlos Lopez a09598
Carlos Lopez a09598
	Time operator-()const { return -value_; }
Carlos Lopez a09598
}; // END of class Time
Carlos Lopez a09598
Carlos Lopez a09598
//! This operator allows the combining of Time::Format flags using the '|' operator
Carlos Lopez a09598
/*!	\see Time::Format, Time::get_string() */
Carlos Lopez a09598
inline Time::Format operator|(Time::Format lhs, Time::Format rhs)
518124
	{ return static_cast<time::format>((int)lhs|(int)rhs); }</time::format>
Carlos Lopez a09598
Carlos Lopez a09598
//! This operator is for checking Time::Format flags.
Carlos Lopez a09598
/*! Don't think of it as "less then or equal to", but think of it
Carlos Lopez a09598
**	like an arrow. Is \a rhs inside of \a lhs ?
Carlos Lopez a09598
**	\see Time::Format, Time::get_string() */
Carlos Lopez a09598
inline bool operator<=(Time::Format lhs, Time::Format rhs)
518124
	{ return (static_cast<int>(lhs) & static_cast<int>(rhs))==static_cast<int>(rhs); }</int></int></int>
Carlos Lopez a09598
Carlos Lopez a09598
}; // END of namespace synfig
Carlos Lopez a09598
Carlos Lopez a09598
/* === E N D =============================================================== */
Carlos Lopez a09598
Carlos Lopez a09598
#endif