]> sigrok.org Git - pulseview.git/blame - pv/prop/binding/decoderoptions.cpp
Renamed pv::data::Decoder to DecoderStack
[pulseview.git] / pv / prop / binding / decoderoptions.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
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
4e5a4405
JH
21#include <libsigrokdecode/libsigrokdecode.h>
22
21ad818f
JH
23#include "decoderoptions.h"
24
67fe5e9c
JH
25#include <boost/foreach.hpp>
26#include <boost/none_t.hpp>
27
6e89374a 28#include <pv/data/decoderstack.h>
67fe5e9c
JH
29#include <pv/prop/int.h>
30#include <pv/prop/string.h>
31
32using namespace boost;
33using namespace std;
34
21ad818f
JH
35namespace pv {
36namespace prop {
37namespace binding {
38
6e89374a 39DecoderOptions::DecoderOptions(shared_ptr<pv::data::DecoderStack> decoder) :
4e5a4405 40 _decoder(decoder)
67fe5e9c 41{
4e5a4405 42 assert(_decoder);
67fe5e9c 43
4e5a4405
JH
44 const srd_decoder *const dec = _decoder->decoder();
45 assert(dec);
67fe5e9c 46
4e5a4405 47 for (GSList *l = dec->options; l; l = l->next)
67fe5e9c
JH
48 {
49 const srd_decoder_option *const opt =
50 (srd_decoder_option*)l->data;
51
52 const QString name(opt->desc);
53
54 const Property::Getter getter = bind(
55 &DecoderOptions::getter, this, opt->id);
56 const Property::Setter setter = bind(
57 &DecoderOptions::setter, this, opt->id, _1);
58
59 shared_ptr<Property> prop;
60
61 if (g_variant_is_of_type(opt->def, G_VARIANT_TYPE("x")))
62 prop = shared_ptr<Property>(
63 new Int(name, "", none, getter, setter));
64 else if (g_variant_is_of_type(opt->def, G_VARIANT_TYPE("s")))
65 prop = shared_ptr<Property>(
66 new String(name, getter, setter));
67 else
68 continue;
69
70 _properties.push_back(prop);
71 }
72}
73
74GVariant* DecoderOptions::getter(const char *id)
75{
4e5a4405
JH
76 assert(_decoder);
77
67fe5e9c 78 // Get the value from the hash table if it is already present
4e5a4405
JH
79 GVariant *val = (GVariant*)g_hash_table_lookup(
80 (GHashTable*)_decoder->options(), id);
67fe5e9c
JH
81
82 if (!val)
83 {
4e5a4405
JH
84 assert(_decoder->decoder());
85
67fe5e9c 86 // Get the default value if not
4e5a4405 87 for (GSList *l = _decoder->decoder()->options; l; l = l->next)
67fe5e9c
JH
88 {
89 const srd_decoder_option *const opt =
90 (srd_decoder_option*)l->data;
1429b21d 91 if (strcmp(opt->id, id) == 0) {
67fe5e9c 92 val = opt->def;
1429b21d
BV
93 break;
94 }
67fe5e9c
JH
95 }
96 }
97
98 if (val)
99 g_variant_ref(val);
100
101 return val;
102}
103
104void DecoderOptions::setter(const char *id, GVariant *value)
21ad818f 105{
4e5a4405
JH
106 assert(_decoder);
107 _decoder->set_option(id, value);
21ad818f
JH
108}
109
110} // binding
111} // prop
112} // pv