]> sigrok.org Git - pulseview.git/blame - pv/prop/binding/decoderoptions.cpp
Added auto-commit support to properties
[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
21#include "decoderoptions.h"
22
67fe5e9c
JH
23#include <boost/foreach.hpp>
24#include <boost/none_t.hpp>
25
26#include <pv/prop/int.h>
27#include <pv/prop/string.h>
28
29using namespace boost;
30using namespace std;
31
21ad818f
JH
32namespace pv {
33namespace prop {
34namespace binding {
35
67fe5e9c
JH
36DecoderOptions::DecoderOptions(const srd_decoder *decoder,
37 GHashTable *options) :
38 _decoder(decoder),
39 _options(options)
40{
41 assert(decoder);
42
43
44 for (GSList *l = decoder->options; l; l = l->next)
45 {
46 const srd_decoder_option *const opt =
47 (srd_decoder_option*)l->data;
48
49 const QString name(opt->desc);
50
51 const Property::Getter getter = bind(
52 &DecoderOptions::getter, this, opt->id);
53 const Property::Setter setter = bind(
54 &DecoderOptions::setter, this, opt->id, _1);
55
56 shared_ptr<Property> prop;
57
58 if (g_variant_is_of_type(opt->def, G_VARIANT_TYPE("x")))
59 prop = shared_ptr<Property>(
60 new Int(name, "", none, getter, setter));
61 else if (g_variant_is_of_type(opt->def, G_VARIANT_TYPE("s")))
62 prop = shared_ptr<Property>(
63 new String(name, getter, setter));
64 else
65 continue;
66
67 _properties.push_back(prop);
68 }
69}
70
71GVariant* DecoderOptions::getter(const char *id)
72{
73 // Get the value from the hash table if it is already present
74 GVariant *val = (GVariant*)g_hash_table_lookup(_options, id);
75
76 if (!val)
77 {
78 // Get the default value if not
79 for (GSList *l = _decoder->options; l; l = l->next)
80 {
81 const srd_decoder_option *const opt =
82 (srd_decoder_option*)l->data;
83 if (strcmp(opt->id, id) == 0)
84 val = opt->def;
85 }
86 }
87
88 if (val)
89 g_variant_ref(val);
90
91 return val;
92}
93
94void DecoderOptions::setter(const char *id, GVariant *value)
21ad818f 95{
67fe5e9c
JH
96 g_variant_ref(value);
97 g_hash_table_insert(_options, (void*)id, value);
21ad818f
JH
98}
99
100} // binding
101} // prop
102} // pv