libdrmconf  0.12.0
A library to program DMR radios.
frequency.hh
1 #ifndef FREQUENCY_HH
2 #define FREQUENCY_HH
3 
4 #include <yaml-cpp/yaml.h>
5 #include <QString>
6 #include <QMetaType>
7 
10 struct Frequency
11 {
12 public:
14  enum class Format {
15  Automatic, Hz, kHz, MHz, GHz
16  };
17 
18 protected:
20  Frequency(unsigned long long hz);
21 
22 public:
24  Frequency();
26  Frequency(const Frequency &other);
27 
29  Frequency &operator = (const Frequency &other);
30 
31  inline bool operator< (const Frequency &other) const {
32  return _frequency < other._frequency;
33  }
34  inline bool operator <=(const Frequency &other) const {
35  return _frequency <= other._frequency;
36  }
37  inline bool operator== (const Frequency &other) const {
38  return _frequency == other._frequency;
39  }
40  inline bool operator != (const Frequency &other) const {
41  return _frequency != other._frequency;
42  }
43  inline bool operator> (const Frequency &other) const {
44  return _frequency > other._frequency;
45  }
46  inline bool operator >=(const Frequency &other) const {
47  return _frequency >= other._frequency;
48  }
49 
51  QString format(Format f=Format::Automatic) const;
53  bool parse(const QString &value);
55  static Frequency fromString(const QString &freq);
56 
57  inline unsigned long long inHz() const { return _frequency; }
58  inline double inkHz() const { return double(_frequency)/1e3; }
59  inline double inMHz() const { return double(_frequency)/1e6; }
60  inline double inGHz() const { return double(_frequency)/1e9; }
61 
62  static inline Frequency fromHz(unsigned long long Hz) { return Frequency(Hz); }
63  static inline Frequency fromkHz(double kHz) { return Frequency(kHz*1e3); }
64  static inline Frequency fromMHz(double MHz) { return Frequency(MHz*1e6); }
65  static inline Frequency fromGHz(double GHz) { return Frequency(GHz*1e6); }
66 
67 protected:
69  unsigned long long _frequency;
70 };
71 
72 Q_DECLARE_METATYPE(Frequency)
73 
74 
75 namespace YAML
76 {
78  template<>
79  struct convert<Frequency>
80  {
82  static Node encode(const Frequency& rhs) {
83  return Node(rhs.format().toStdString());
84  }
85 
87  static bool decode(const Node& node, Frequency& rhs) {
88  if (! node.IsScalar())
89  return false;
90  return rhs.parse(QString::fromStdString(node.as<std::string>()));
91  }
92  };
93 }
94 #endif // FREQUENCY_HH
Helper type to encode frequencies without any rounding error.
Definition: frequency.hh:11
static Frequency fromkHz(double kHz)
Unit conversion.
Definition: frequency.hh:63
double inkHz() const
Unit conversion.
Definition: frequency.hh:58
static Frequency fromString(const QString &freq)
Pareses a frequency.
Definition: frequency.cc:104
bool operator<(const Frequency &other) const
Definition: frequency.hh:31
bool operator>=(const Frequency &other) const
Definition: frequency.hh:46
bool operator!=(const Frequency &other) const
Definition: frequency.hh:40
static Frequency fromMHz(double MHz)
Unit conversion.
Definition: frequency.hh:64
unsigned long long inHz() const
Unit conversion.
Definition: frequency.hh:57
Frequency()
Default constructor.
Definition: frequency.cc:11
bool operator>(const Frequency &other) const
Definition: frequency.hh:43
bool parse(const QString &value)
Parses a frequency.
Definition: frequency.cc:53
QString format(Format f=Format::Automatic) const
Format the frequency.
Definition: frequency.cc:30
double inMHz() const
Unit conversion.
Definition: frequency.hh:59
double inGHz() const
Unit conversion.
Definition: frequency.hh:60
unsigned long long _frequency
The actual frequency in Hz.
Definition: frequency.hh:69
Format
Possible formatting hints.
Definition: frequency.hh:14
Frequency & operator=(const Frequency &other)
Assignment.
Definition: frequency.cc:24
static Frequency fromGHz(double GHz)
Unit conversion.
Definition: frequency.hh:65
static Frequency fromHz(unsigned long long Hz)
Unit conversion.
Definition: frequency.hh:62
bool operator==(const Frequency &other) const
Definition: frequency.hh:37
bool operator<=(const Frequency &other) const
Definition: frequency.hh:34
static Node encode(const Frequency &rhs)
Serializes the frequency.
Definition: frequency.hh:82
static bool decode(const Node &node, Frequency &rhs)
Parses the frequency.
Definition: frequency.hh:87