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