libdrmconf 0.14.1
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
57 inline unsigned int value() const { return _level; }
58 inline unsigned int mapTo(const Codeplug::Element::Limit::Range<unsigned int> &range) const {
59 if (isNull() || isInvalid())
60 return 0;
62 }
63
65 QString format() const;
67 bool parse(const QString &value);
68
69public:
71 inline static constexpr Level null() { return Level(0); }
73 inline static constexpr Level invalid() { return Level(std::numeric_limits<unsigned int>::max()); }
75 inline static constexpr Level fromValue(unsigned int value, const Codeplug::Element::Limit::Range<unsigned int> range={1,10}) {
76 // If 0 is not in normal range -> always may 0 -> 0 (e.g. means off).
77 if ((0 == value) && (0 != range.min))
78 return Level::null();
79 return Level(range.mapTo({1,10},value));
80 }
81
82protected:
84 unsigned int _level;
85};
86
87
88Q_DECLARE_METATYPE(Level)
89
90
91namespace YAML
92{
94template<>
95struct convert<Level>
96{
98 static Node encode(const Level& rhs) {
99 return Node(rhs.format().toStdString());
100 }
101
103 static bool decode(const Node& node, Level& rhs) {
104 // Usually means default level
105 if (node.IsNull()) {
106 rhs = Level::invalid();
107 return true;
108 }
109 // Fails on non-scalars
110 if (!node.IsScalar())
111 return false;
112 // Otherwise, parse string.
113 return rhs.parse(QString::fromStdString(node.as<std::string>()));
114 }
115};
116}
117
118
119#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:49
unsigned int value() const
Returns the value of the level.
Definition level.hh:57
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:71
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:75
static constexpr Level invalid()
Constructs an invalid level.
Definition level.hh:73
bool isFinite() const
Test for finite values.
Definition level.hh:38
bool operator>=(const Level &other) const
Definition level.hh:52
bool operator<(const Level &other) const
Definition level.hh:43
unsigned int _level
The actual level value.
Definition level.hh:84
bool operator==(const Level &other) const
Definition level.hh:40
bool operator<=(const Level &other) const
Definition level.hh:46
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:98
static bool decode(const Node &node, Level &rhs)
Parses the interval.
Definition level.hh:103