]> sigrok.org Git - pulseview.git/blame - pv/binding/decoder.cpp
Rework decoder infrastructure
[pulseview.git] / pv / binding / decoder.cpp
CommitLineData
21ad818f
JH
1/*
2 * This file is part of the PulseView project.
3 *
4 * Copyright (C) 2013 Joel Holdsworth <joel@airwebreathe.org.uk>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
efdec55a 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21ad818f
JH
18 */
19
4e5a4405
JH
20#include <libsigrokdecode/libsigrokdecode.h>
21
3cc9ad7b 22#include "decoder.hpp"
21ad818f 23
67fe5e9c
JH
24#include <boost/none_t.hpp>
25
2acdb232 26#include <pv/data/decode/decoder.hpp>
946b52e1 27#include <pv/data/decodesignal.hpp>
2acdb232
JH
28#include <pv/prop/double.hpp>
29#include <pv/prop/enum.hpp>
30#include <pv/prop/int.hpp>
31#include <pv/prop/string.hpp>
67fe5e9c 32
819f4c25 33using boost::none;
819f4c25 34using std::map;
182d2f5d 35using std::pair;
f9abf97e 36using std::shared_ptr;
819f4c25 37using std::string;
182d2f5d 38using std::vector;
67fe5e9c 39
61703a01
JH
40using pv::prop::Double;
41using pv::prop::Enum;
42using pv::prop::Int;
43using pv::prop::Property;
44using pv::prop::String;
45
21ad818f 46namespace pv {
21ad818f
JH
47namespace binding {
48
3cc9ad7b 49Decoder::Decoder(
946b52e1 50 shared_ptr<pv::data::DecodeSignal> decode_signal,
7491a29f 51 shared_ptr<data::decode::Decoder> decoder) :
946b52e1 52 decode_signal_(decode_signal),
8dbbc7f0 53 decoder_(decoder)
67fe5e9c 54{
8dbbc7f0 55 assert(decoder_);
67fe5e9c 56
6a26fc44 57 const srd_decoder *const dec = decoder_->get_srd_decoder();
4e5a4405 58 assert(dec);
67fe5e9c 59
2ad82c2e 60 for (GSList *l = dec->options; l; l = l->next) {
6a26fc44 61 const srd_decoder_option *const opt = (srd_decoder_option*)l->data;
67fe5e9c 62
636782c1 63 const QString name = QString::fromUtf8(opt->desc);
67fe5e9c 64
6db73158
JH
65 const Property::Getter get = [&, opt]() {
66 return getter(opt->id); };
e8d00928 67 const Property::Setter set = [&, opt](Glib::VariantBase value) {
6db73158 68 setter(opt->id, value); };
67fe5e9c
JH
69
70 shared_ptr<Property> prop;
71
182d2f5d 72 if (opt->values)
9a267f8d 73 prop = bind_enum(name, "", opt, get, set);
a28c3025 74 else if (g_variant_is_of_type(opt->def, G_VARIANT_TYPE("d")))
9a267f8d 75 prop = shared_ptr<Property>(new Double(name, "", 2, "",
6db73158 76 none, none, get, set));
182d2f5d 77 else if (g_variant_is_of_type(opt->def, G_VARIANT_TYPE("x")))
67fe5e9c 78 prop = shared_ptr<Property>(
9a267f8d 79 new Int(name, "", "", none, get, set));
67fe5e9c
JH
80 else if (g_variant_is_of_type(opt->def, G_VARIANT_TYPE("s")))
81 prop = shared_ptr<Property>(
9a267f8d 82 new String(name, "", get, set));
67fe5e9c
JH
83 else
84 continue;
85
8dbbc7f0 86 properties_.push_back(prop);
67fe5e9c
JH
87 }
88}
89
3cc9ad7b 90shared_ptr<Property> Decoder::bind_enum(
9a267f8d
UH
91 const QString &name, const QString &desc,
92 const srd_decoder_option *option,
182d2f5d
JH
93 Property::Getter getter, Property::Setter setter)
94{
e8d00928 95 vector< pair<Glib::VariantBase, QString> > values;
182d2f5d 96 for (GSList *l = option->values; l; l = l->next) {
e8d00928 97 Glib::VariantBase var = Glib::VariantBase((GVariant*)l->data, true);
20f59e95 98 values.emplace_back(var, print_gvariant(var));
182d2f5d
JH
99 }
100
9a267f8d 101 return shared_ptr<Property>(new Enum(name, desc, values, getter, setter));
182d2f5d
JH
102}
103
3cc9ad7b 104Glib::VariantBase Decoder::getter(const char *id)
67fe5e9c 105{
4c60462b 106 GVariant *val = nullptr;
615f6d25 107
8dbbc7f0 108 assert(decoder_);
4e5a4405 109
67fe5e9c 110 // Get the value from the hash table if it is already present
8dbbc7f0 111 const map<string, GVariant*>& options = decoder_->options();
f46e495e 112 const auto iter = options.find(id);
67fe5e9c 113
615f6d25
JH
114 if (iter != options.end())
115 val = (*iter).second;
2ad82c2e 116 else {
6a26fc44 117 assert(decoder_->get_srd_decoder());
4e5a4405 118
67fe5e9c 119 // Get the default value if not
6a26fc44
SA
120 for (GSList *l = decoder_->get_srd_decoder()->options; l; l = l->next) {
121 const srd_decoder_option *const opt = (srd_decoder_option*)l->data;
1429b21d 122 if (strcmp(opt->id, id) == 0) {
67fe5e9c 123 val = opt->def;
1429b21d
BV
124 break;
125 }
67fe5e9c
JH
126 }
127 }
128
c063290a 129 return (val) ? Glib::VariantBase(val, true) : Glib::VariantBase();
67fe5e9c
JH
130}
131
3cc9ad7b 132void Decoder::setter(const char *id, Glib::VariantBase value)
21ad818f 133{
8dbbc7f0
JH
134 assert(decoder_);
135 decoder_->set_option(id, value.gobj());
7491a29f 136
946b52e1
SA
137 assert(decode_signal_);
138 decode_signal_->begin_decode();
21ad818f
JH
139}
140
870ea3db
UH
141} // namespace binding
142} // namespace pv