]> sigrok.org Git - pulseview.git/blob - pv/prop/binding/decoderoptions.cpp
Replaced boost::shared_ptr with std::shared_ptr
[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/bind.hpp>
26 #include <boost/none_t.hpp>
27
28 #include <pv/data/decoderstack.h>
29 #include <pv/data/decode/decoder.h>
30 #include <pv/prop/double.h>
31 #include <pv/prop/enum.h>
32 #include <pv/prop/int.h>
33 #include <pv/prop/string.h>
34
35 using boost::bind;
36 using boost::none;
37 using std::make_pair;
38 using std::map;
39 using std::pair;
40 using std::shared_ptr;
41 using std::string;
42 using std::vector;
43
44 namespace pv {
45 namespace prop {
46 namespace binding {
47
48 DecoderOptions::DecoderOptions(
49         shared_ptr<pv::data::DecoderStack> decoder_stack,
50         shared_ptr<data::decode::Decoder> decoder) :
51         _decoder_stack(decoder_stack),
52         _decoder(decoder)
53 {
54         assert(_decoder);
55
56         const srd_decoder *const dec = _decoder->decoder();
57         assert(dec);
58
59         for (GSList *l = dec->options; l; l = l->next)
60         {
61                 const srd_decoder_option *const opt =
62                         (srd_decoder_option*)l->data;
63
64                 const QString name = QString::fromUtf8(opt->desc);
65
66                 const Property::Getter getter = bind(
67                         &DecoderOptions::getter, this, opt->id);
68                 const Property::Setter setter = bind(
69                         &DecoderOptions::setter, this, opt->id, _1);
70
71                 shared_ptr<Property> prop;
72
73                 if (opt->values)
74                         prop = bind_enum(name, opt, getter, setter);
75                 else if (g_variant_is_of_type(opt->def, G_VARIANT_TYPE("d")))
76                         prop = shared_ptr<Property>(new Double(name, 2, "",
77                                 none, none, getter, setter));
78                 else if (g_variant_is_of_type(opt->def, G_VARIANT_TYPE("x")))
79                         prop = shared_ptr<Property>(
80                                 new Int(name, "", none, getter, setter));
81                 else if (g_variant_is_of_type(opt->def, G_VARIANT_TYPE("s")))
82                         prop = shared_ptr<Property>(
83                                 new String(name, getter, setter));
84                 else
85                         continue;
86
87                 _properties.push_back(prop);
88         }
89 }
90
91 shared_ptr<Property> DecoderOptions::bind_enum(
92         const QString &name, const srd_decoder_option *option,
93         Property::Getter getter, Property::Setter setter)
94 {
95         vector< pair<GVariant*, QString> > values;
96         for (GSList *l = option->values; l; l = l->next) {
97                 GVariant *const var = (GVariant*)l->data;
98                 assert(var);
99                 values.push_back(make_pair(var, print_gvariant(var)));
100         }
101
102         return shared_ptr<Property>(new Enum(name, values, getter, setter));
103 }
104
105 GVariant* DecoderOptions::getter(const char *id)
106 {
107         GVariant *val = NULL;
108
109         assert(_decoder);
110
111         // Get the value from the hash table if it is already present
112         const map<string, GVariant*>& options = _decoder->options();
113         const auto iter = options.find(id);
114
115         if (iter != options.end())
116                 val = (*iter).second;
117         else
118         {
119                 assert(_decoder->decoder());
120
121                 // Get the default value if not
122                 for (GSList *l = _decoder->decoder()->options; l; l = l->next)
123                 {
124                         const srd_decoder_option *const opt =
125                                 (srd_decoder_option*)l->data;
126                         if (strcmp(opt->id, id) == 0) {
127                                 val = opt->def;
128                                 break;
129                         }
130                 }
131         }
132
133         if (val)
134                 g_variant_ref(val);
135
136         return val;
137 }
138
139 void DecoderOptions::setter(const char *id, GVariant *value)
140 {
141         assert(_decoder);
142         _decoder->set_option(id, value);
143
144         assert(_decoder_stack);
145         _decoder_stack->begin_decode();
146 }
147
148 } // binding
149 } // prop
150 } // pv