]> sigrok.org Git - pulseview.git/blob - pv/data/decode/decoder.cpp
Replaced Decoder internal GHashTable storage of options with a std::map
[pulseview.git] / pv / data / decode / decoder.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 "decoder.h"
24
25 #include <pv/view/logicsignal.h>
26
27 using namespace boost;
28 using namespace std;
29
30 namespace pv {
31 namespace data {
32 namespace decode {
33
34 Decoder::Decoder(const srd_decoder *const dec) :
35         _decoder(dec)
36 {
37 }
38
39 Decoder::~Decoder()
40 {
41         for (map<string, GVariant*>::const_iterator i = _options.begin();
42                 i != _options.end(); i++)
43                 g_variant_unref((*i).second);
44 }
45
46 const srd_decoder* Decoder::decoder() const
47 {
48         return _decoder;
49 }
50
51 const map<const srd_probe*, shared_ptr<view::LogicSignal> >&
52 Decoder::probes() const
53 {
54         return _probes;
55 }
56
57 void Decoder::set_probes(std::map<const srd_probe*,
58         boost::shared_ptr<view::LogicSignal> > probes)
59 {
60         _probes = probes;
61 }
62
63 const std::map<std::string, GVariant*>& Decoder::options() const
64 {
65         return _options;
66 }
67
68 void Decoder::set_option(const char *id, GVariant *value)
69 {
70         assert(value);
71         g_variant_ref(value);
72         _options[id] = value;
73 }
74
75 srd_decoder_inst* Decoder::create_decoder_inst(
76         srd_session *const session) const
77 {
78         GHashTable *const opt_hash = g_hash_table_new_full(g_str_hash,
79                 g_str_equal, g_free, (GDestroyNotify)g_variant_unref);
80
81         for (map<string, GVariant*>::const_iterator i = _options.begin();
82                 i != _options.end(); i++)
83         {
84                 GVariant *const value = (*i).second;
85                 g_variant_ref(value);
86                 g_hash_table_replace(opt_hash, (void*)g_strdup(
87                         (*i).first.c_str()), value);
88         }
89
90         srd_decoder_inst *const decoder_inst = srd_inst_new(
91                 session, _decoder->id, opt_hash);
92         g_hash_table_destroy(opt_hash);
93
94         if(!decoder_inst)
95                 return NULL;
96
97         // Setup the probes
98         GHashTable *const probes = g_hash_table_new_full(g_str_hash,
99                 g_str_equal, g_free, (GDestroyNotify)g_variant_unref);
100
101         for(map<const srd_probe*, shared_ptr<view::LogicSignal> >::
102                 const_iterator i = _probes.begin();
103                 i != _probes.end(); i++)
104         {
105                 shared_ptr<view::LogicSignal> signal((*i).second);
106                 GVariant *const gvar = g_variant_new_int32(
107                         signal->probe()->index);
108                 g_variant_ref_sink(gvar);
109                 g_hash_table_insert(probes, (*i).first->id, gvar);
110         }
111
112         srd_inst_probe_set_all(decoder_inst, probes);
113
114         return decoder_inst;
115 }
116
117 } // decode
118 } // data
119 } // pv