libdrmconf  0.11.3
A library to program DMR radios.
interval.hh
1 #ifndef INTERVAL_HH
2 #define INTERVAL_HH
3 
4 #include <QString>
5 #include <QMetaType>
6 #include <yaml-cpp/yaml.h>
7 
10 class Interval
11 {
12 public:
14  enum class Format {
15  Automatic, Milliseconds, Seconds, Minutes
16  };
17 
18 protected:
20  constexpr explicit Interval(unsigned long long ms)
21  : _duration(ms)
22  {
23  // pass...
24  }
25 
26 public:
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; }
46  inline bool operator ==(const Interval &other) const {
47  return _duration == other._duration;
48  }
49  inline bool operator< (const Interval &other) const {
50  return _duration < other._duration;
51  }
52 
53  inline unsigned long long milliseconds() const { return _duration; }
54  inline unsigned long long seconds() const { return _duration/1000ULL; }
55  inline unsigned long long minutes() const { return _duration/60000ULL; }
56 
57  static inline constexpr Interval fromMilliseconds(unsigned long long ms) {
58  return Interval(ms);
59  }
60  static inline constexpr Interval fromSeconds(unsigned long long s) {
61  return Interval(s*1000ULL);
62  }
63  static inline constexpr Interval fromMinutes(unsigned long long min) {
64  return Interval(min*60000ULL);
65  }
66 
68  QString format(Format f=Format::Automatic) const;
70  bool parse(const QString &value);
71 
72 private:
74  unsigned long long _duration;
75 };
76 
77 Q_DECLARE_METATYPE(Interval)
78 
79 namespace YAML
80 {
82  template<>
83  struct convert<Interval>
84  {
86  static Node encode(const Interval& rhs) {
87  return Node(rhs.format().toStdString());
88  }
89 
91  static bool decode(const Node& node, Interval& rhs) {
92  if (!node.IsScalar())
93  return false;
94  return rhs.parse(QString::fromStdString(node.as<std::string>()));
95  }
96  };
97 }
98 
99 
100 #endif // INTERVAL_HH
Represents a time interval.
Definition: interval.hh:11
constexpr Interval(const Interval &other)
Copy constructor.
Definition: interval.hh:35
unsigned long long milliseconds() const
Unit conversion.
Definition: interval.hh:53
unsigned long long minutes() const
Unit conversion.
Definition: interval.hh:55
Format
Possible formats.
Definition: interval.hh:14
bool operator<(const Interval &other) const
Definition: interval.hh:49
QString format(Format f=Format::Automatic) const
Format the frequency.
Definition: interval.cc:5
static constexpr Interval fromMinutes(unsigned long long min)
Definition: interval.hh:63
Interval & operator=(const Interval &other)
Definition: interval.hh:41
static constexpr Interval fromSeconds(unsigned long long s)
Definition: interval.hh:60
bool operator==(const Interval &other) const
Definition: interval.hh:46
Interval()
Default constructor.
Definition: interval.hh:28
unsigned long long seconds() const
Unit conversion.
Definition: interval.hh:54
bool parse(const QString &value)
Parses a frequency.
Definition: interval.cc:26
constexpr Interval(unsigned long long ms)
Constructor from milliseconds.
Definition: interval.hh:20
static constexpr Interval fromMilliseconds(unsigned long long ms)
Definition: interval.hh:57
bool isNull() const
Test for 0.
Definition: interval.hh:45
static bool decode(const Node &node, Interval &rhs)
Parses the interval.
Definition: interval.hh:91
static Node encode(const Interval &rhs)
Serializes the interval.
Definition: interval.hh:86