libdrmconf 0.15.0
A library to program DMR radios.
Loading...
Searching...
No Matches
level.hh
1#ifndef LEVEL_HH
2#define LEVEL_HH
3
4#include <QString>
5#include <QMetaType>
6#include <limits>
7#include <yaml-cpp/yaml.h>
8#include "codeplug.hh"
9
10
11
14class Level
15{
16protected:
18 inline constexpr Level(unsigned int value) : _level(value) {}
19
20public:
22 Level();
23
24 inline constexpr Level(const Level &other)
25 : _level(other._level)
26 {
27 // pass...
28 }
29
30 inline Level &operator =(const Level &other) = default;
31
32 inline bool isNull() const { return 0 == _level; }
34 inline bool isInvalid() const {
35 return std::numeric_limits<unsigned int>::max() == _level;
36 }
37
38 inline bool isFinite() const { return (!isNull()) && (!isInvalid()); }
39
40 inline bool operator !=(const Level &other) const {
41 return _level != other._level;
42 }
43 inline bool operator ==(const Level &other) const {
44 return _level == other._level;
45 }
46 inline bool operator< (const Level &other) const {
47 return _level < other._level;
48 }
49 inline bool operator<= (const Level &other) const {
50 return _level <= other._level;
51 }
52 inline bool operator> (const Level &other) const {
53 return _level > other._level;
54 }
55 inline bool operator>= (const Level &other) const {
56 return _level >= other._level;
57 }
58
60 inline unsigned int value() const { return _level; }
61 inline unsigned int mapTo(const Codeplug::Element::Limit::Range<unsigned int> &range) const {
62 if (isNull() || isInvalid())
63 return 0;
65 }
66
68 QString format() const;
70 bool parse(const QString &value);
71
72public:
74 inline static constexpr Level null() { return Level(0); }
76 inline static constexpr Level invalid() { return Level(std::numeric_limits<unsigned int>::max()); }
78 inline static constexpr Level fromValue(unsigned int value, const Codeplug::Element::Limit::Range<unsigned int> range={1,10}) {
79 // If 0 is not in normal range -> always may 0 -> 0 (e.g. means off).
80 if ((0 == value) && (0 != range.min))
81 return Level::null();
82 return Level(range.mapTo({1,10},value));
83 }
84
85protected:
87 unsigned int _level;
88};
89
90
91Q_DECLARE_METATYPE(Level)
92
93
94namespace YAML
95{
97template<>
98struct convert<Level>
99{
101 static Node encode(const Level& rhs) {
102 return Node(rhs.format().toStdString());
103 }
104
106 static bool decode(const Node& node, Level& rhs) {
107 // Usually means default level
108 if (node.IsNull()) {
109 rhs = Level::invalid();
110 return true;
111 }
112 // Fails on non-scalars
113 if (!node.IsScalar())
114 return false;
115 // Otherwise, parse string.
116 return rhs.parse(QString::fromStdString(node.as<std::string>()));
117 }
118};
119}
120
121
122#endif // LEVEL_HH
Some simple class implementing a [1-10] level setting.
Definition level.hh:15
bool isNull() const
Test for 0.
Definition level.hh:32
bool parse(const QString &value)
Parses a frequency.
Definition level.cc:22
bool operator!=(const Level &other) const
Definition level.hh:40
bool operator>(const Level &other) const
Definition level.hh:52
unsigned int value() const
Returns the value of the level.
Definition level.hh:60
QString format() const
Format the frequency.
Definition level.cc:12
bool isInvalid() const
Test for invalid level.
Definition level.hh:34
Level()
Default constructor.
Definition level.cc:4
static constexpr Level null()
Constructs null level.
Definition level.hh:74
constexpr Level(unsigned int value)
Constructor from value.
Definition level.hh:18
static constexpr Level fromValue(unsigned int value, const Codeplug::Element::Limit::Range< unsigned int > range={1, 10})
Constructs a proper level.
Definition level.hh:78
static constexpr Level invalid()
Constructs an invalid level.
Definition level.hh:76
bool isFinite() const
Test for finite values.
Definition level.hh:38
bool operator>=(const Level &other) const
Definition level.hh:55
bool operator<(const Level &other) const
Definition level.hh:46
unsigned int _level
The actual level value.
Definition level.hh:87
bool operator==(const Level &other) const
Definition level.hh:43
bool operator<=(const Level &other) const
Definition level.hh:49
Holds a range of values [min, max].
Definition codeplug.hh:94
T mapTo(const Range< T > &other, const T &value) const
Maps a value from this range to the given range.
Definition codeplug.hh:108
const T min
Lower bound.
Definition codeplug.hh:96
static Node encode(const Level &rhs)
Serializes the interval.
Definition level.hh:101
static bool decode(const Node &node, Level &rhs)
Parses the interval.
Definition level.hh:106