]> sigrok.org Git - pulseview.git/blob - pv/prop/binding/decoderoptions.cpp
Renamed pv::data::Decoder to DecoderStack
[pulseview.git] / pv / prop / binding / decoderoptions.cpp
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 <libsigrokdecode/libsigrokdecode.h>
22
23 #include "decoderoptions.h"
24
25 #include <boost/foreach.hpp>
26 #include <boost/none_t.hpp>
27
28 #include <pv/data/decoderstack.h>
29 #include <pv/prop/int.h>
30 #include <pv/prop/string.h>
31
32 using namespace boost;
33 using namespace std;
34
35 namespace pv {
36 namespace prop {
37 namespace binding {
38
39 DecoderOptions::DecoderOptions(shared_ptr<pv::data::DecoderStack> decoder) :
40         _decoder(decoder)
41 {
42         assert(_decoder);
43
44         const srd_decoder *const dec = _decoder->decoder();
45         assert(dec);
46
47         for (GSList *l = dec->options; l; l = l->next)
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
74 GVariant* DecoderOptions::getter(const char *id)
75 {
76         assert(_decoder);
77
78         // Get the value from the hash table if it is already present
79         GVariant *val = (GVariant*)g_hash_table_lookup(
80                 (GHashTable*)_decoder->options(), id);
81
82         if (!val)
83         {
84                 assert(_decoder->decoder());
85
86                 // Get the default value if not
87                 for (GSList *l = _decoder->decoder()->options; l; l = l->next)
88                 {
89                         const srd_decoder_option *const opt =
90                                 (srd_decoder_option*)l->data;
91                         if (strcmp(opt->id, id) == 0) {
92                                 val = opt->def;
93                                 break;
94                         }
95                 }
96         }
97
98         if (val)
99                 g_variant_ref(val);
100
101         return val;
102 }
103
104 void DecoderOptions::setter(const char *id, GVariant *value)
105 {
106         assert(_decoder);
107         _decoder->set_option(id, value);
108 }
109
110 } // binding
111 } // prop
112 } // pv