]> sigrok.org Git - pulseview.git/blob - pv/binding/inputoutput.cpp
device: ensure bind_enum() checks availability of Capability::LIST.
[pulseview.git] / pv / binding / inputoutput.cpp
1 /*
2  * This file is part of the PulseView project.
3  *
4  * Copyright (C) 2015 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 <cassert>
22 #include <iostream>
23 #include <utility>
24
25 #include <boost/none_t.hpp>
26
27 #include <libsigrokcxx/libsigrokcxx.hpp>
28
29 #include <pv/prop/double.hpp>
30 #include <pv/prop/enum.hpp>
31 #include <pv/prop/int.hpp>
32 #include <pv/prop/string.hpp>
33
34 #include "inputoutput.hpp"
35
36 using boost::none;
37
38 using std::make_pair;
39 using std::map;
40 using std::pair;
41 using std::shared_ptr;
42 using std::string;
43 using std::vector;
44
45 using Glib::VariantBase;
46 using Glib::VariantType;
47
48 using sigrok::Option;
49
50 using pv::prop::Double;
51 using pv::prop::Enum;
52 using pv::prop::Int;
53 using pv::prop::Property;
54 using pv::prop::String;
55
56 namespace pv {
57 namespace binding {
58
59 InputOutput::InputOutput(
60         const map<string, shared_ptr<Option>> &options)
61 {
62         for (pair<string, shared_ptr<Option>> o : options)
63         {
64                 const shared_ptr<Option> &opt = o.second;
65                 assert(opt);
66
67                 const QString name = QString::fromStdString(opt->name());
68                 const VariantBase def_val = opt->default_value();
69                 const vector<VariantBase> values = opt->values();
70
71                 options_[opt->id()] = def_val;
72  
73                 const Property::Getter get = [&, opt]() {
74                         return options_[opt->id()]; };
75                 const Property::Setter set = [&, opt](VariantBase value) {
76                         options_[opt->id()] = value; };
77
78                 shared_ptr<Property> prop;
79
80                 if (!opt->values().empty())
81                         prop = bind_enum(name, values, get, set);
82                 else if (def_val.is_of_type(VariantType("d")))
83                         prop = shared_ptr<Property>(new Double(name, 2, "",
84                                 nullptr, nullptr, get, set));
85                 else if (def_val.is_of_type(VariantType("u")))
86                         prop = shared_ptr<Property>(
87                                 new Int(name, "", none, get, set));
88                 else if (def_val.is_of_type(VariantType("s")))
89                         prop = shared_ptr<Property>(
90                                 new String(name, get, set));
91                 else
92                         continue;
93
94                 properties_.push_back(prop);
95         }
96 }
97
98 const map<string, VariantBase>& InputOutput::options() const
99 {
100         return options_;
101 }
102
103 shared_ptr<Property> InputOutput::bind_enum(
104         const QString &name, const vector<VariantBase> &values,
105         Property::Getter getter, Property::Setter setter)
106 {
107         vector< pair<VariantBase, QString> > enum_vals;
108         for (VariantBase var : values)
109                 enum_vals.push_back(make_pair(var, print_gvariant(var)));
110         return shared_ptr<Property>(new Enum(name, enum_vals, getter, setter));
111 }
112
113 } // namespace binding
114 } // namespace pv