1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
#ifndef INTERVAL_HH
#define INTERVAL_HH
#include <QString>
#include <QMetaType>
#include <yaml-cpp/yaml.h>
/** Represents a time interval.
* @ingroup utils */
class Interval
{
public:
/** Possible formats. */
enum class Format {
Automatic, Milliseconds, Seconds, Minutes
};
protected:
/** Constructor from milliseconds. */
constexpr explicit Interval(unsigned long long ms)
: _duration(ms)
{
// pass...
}
public:
/** Default constructor. */
Interval()
: _duration(0)
{
// pass...
}
/** Copy constructor. */
constexpr Interval(const Interval &other)
: _duration(other._duration)
{
// pass...
}
inline Interval &operator =(const Interval &other) { ///< Assignment.
_duration = other._duration; return *this;
}
inline bool isNull() const { return 0 == _duration; } ///< Test for 0.
/** Test for infinite durations. */
inline bool isInfinite() const {
return std::numeric_limits<unsigned long long>::max() == _duration;
}
/** Test for finite values. */
inline bool isFinite() const { return (!isNull()) && (!isInfinite()); }
inline bool operator ==(const Interval &other) const { ///< Comparison.
return _duration == other._duration;
}
inline bool operator< (const Interval &other) const {///< Comparison.
return _duration < other._duration;
}
inline bool operator<= (const Interval &other) const {///< Comparison.
return _duration <= other._duration;
}
inline bool operator> (const Interval &other) const {///< Comparison.
return _duration > other._duration;
}
inline bool operator>= (const Interval &other) const {///< Comparison.
return _duration >= other._duration;
}
inline unsigned long long milliseconds() const { return _duration; } ///< Unit conversion.
inline unsigned long long seconds() const { return _duration/1000ULL; } ///< Unit conversion.
inline unsigned long long minutes() const { return _duration/60000ULL; } ///< Unit conversion.
static inline constexpr Interval fromMilliseconds(unsigned long long ms) { ///< Unit conversion.
return Interval(ms);
}
static inline constexpr Interval fromSeconds(unsigned long long s) { ///< Unit conversion.
return Interval(s*1000ULL);
}
static inline constexpr Interval fromMinutes(unsigned long long min) { ///< Unit conversion.
return Interval(min*60000ULL);
}
/** Format the frequency. */
QString format(Format f=Format::Automatic) const;
/** Parses a frequency. */
bool parse(const QString &value);
public:
/** Constructs a null interval. */
static inline Interval null() { return Interval(0); }
/** Constructs an infinite interval. */
static inline Interval infinity() {
return Interval(std::numeric_limits<unsigned long long>::max());
}
private:
/** An interval duration in ms. */
unsigned long long _duration;
};
Q_DECLARE_METATYPE(Interval)
namespace YAML
{
/** Implements the conversion to and from YAML::Node. */
template<>
struct convert<Interval>
{
/** Serializes the interval. */
static Node encode(const Interval& rhs) {
return Node(rhs.format().toStdString());
}
/** Parses the interval. */
static bool decode(const Node& node, Interval& rhs) {
if (!node.IsScalar())
return false;
return rhs.parse(QString::fromStdString(node.as<std::string>()));
}
};
}
#endif // INTERVAL_HH
|