]> sigrok.org Git - libsigrok.git/blob - bindings/swig/classes.i
bindings: Add Channel::get_index() method.
[libsigrok.git] / bindings / swig / classes.i
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2014 Martin Ling <martin-sigrok@earth.li>
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 3 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, see <http://www.gnu.org/licenses/>.
18  */
19
20 #pragma SWIG nowarn=325,401
21
22 %{
23 #include "libsigrok/libsigrok.hpp"
24 using namespace std;
25 %}
26
27 %include "typemaps.i"
28 %include "std_string.i"
29 %include "std_vector.i"
30 %include "std_map.i"
31 %include "std_shared_ptr.i"
32 %include "exception.i"
33
34 %{
35
36 static int swig_exception_code(int sigrok_exception_code) {
37     switch (sigrok_exception_code) {
38         case SR_ERR_MALLOC:
39             return SWIG_MemoryError;
40         case SR_ERR_ARG:
41             return SWIG_ValueError;
42         default:
43             return SWIG_RuntimeError;
44     }
45 }
46
47 %}
48
49 %exception {
50     try {
51         $action
52     } catch (sigrok::Error &e) {
53         SWIG_exception(swig_exception_code(e.result),
54             const_cast<char*>(e.what()));
55     }
56 }
57
58 template< class T > class enable_shared_from_this;
59
60 %template(ContextShared) enable_shared_from_this<Context>;
61
62 %shared_ptr(sigrok::Context);
63 %shared_ptr(sigrok::Driver);
64 %shared_ptr(sigrok::Device);
65 %shared_ptr(sigrok::Configurable);
66 %shared_ptr(sigrok::HardwareDevice);
67 %shared_ptr(sigrok::Channel);
68 %shared_ptr(sigrok::ChannelGroup);
69 %shared_ptr(sigrok::EventSource);
70 %shared_ptr(sigrok::Session);
71 %shared_ptr(sigrok::Packet);
72 %shared_ptr(sigrok::PacketPayload);
73 %shared_ptr(sigrok::Header);
74 %shared_ptr(sigrok::Meta);
75 %shared_ptr(sigrok::Analog);
76 %shared_ptr(sigrok::Logic);
77 %shared_ptr(sigrok::InputFormat);
78 %shared_ptr(sigrok::InputFileDevice);
79 %shared_ptr(sigrok::OutputFormat);
80 %shared_ptr(sigrok::Output);
81 %shared_ptr(sigrok::Trigger);
82 %shared_ptr(sigrok::TriggerStage);
83 %shared_ptr(sigrok::TriggerMatch);
84
85 %template(StringMap) std::map<std::string, std::string>;
86
87 %template(DriverMap)
88     std::map<std::string, std::shared_ptr<sigrok::Driver> >;
89 %template(InputFormatMap)
90     std::map<std::string, std::shared_ptr<sigrok::InputFormat> >;
91 %template(OutputFormatMap)
92     std::map<std::string, std::shared_ptr<sigrok::OutputFormat> >;
93
94 %template(HardwareDeviceVector)
95     std::vector<std::shared_ptr<sigrok::HardwareDevice> >;
96
97 %template(DeviceVector)
98     std::vector<std::shared_ptr<sigrok::Device> >;
99
100 %template(ChannelVector)
101     std::vector<std::shared_ptr<sigrok::Channel> >;
102
103 %template(ChannelGroupMap)
104     std::map<std::string, std::shared_ptr<sigrok::ChannelGroup> >;
105
106 /* Workaround for SWIG bug. The vector template instantiation
107    isn't needed but somehow fixes a bug that stops the wrapper
108    for the map instantiation from compiling. */
109 %template(ConfigVector)
110     std::vector<const sigrok::ConfigKey *>;
111 %template(ConfigMap)
112     std::map<const sigrok::ConfigKey *, Glib::VariantBase>;
113
114 %template(QuantityFlagVector)
115     std::vector<const sigrok::QuantityFlag *>;
116
117 %template(TriggerStageVector)
118  std::vector<std::shared_ptr<sigrok::TriggerStage> >;
119
120 %template(TriggerMatchVector)
121  std::vector<std::shared_ptr<sigrok::TriggerMatch> >;
122
123 #define SR_API
124 #define SR_PRIV
125
126 %ignore sigrok::DatafeedCallbackData;
127 %ignore sigrok::SourceCallbackData;
128
129 %include "libsigrok/libsigrok.hpp"
130
131 namespace sigrok {
132 %include "libsigrok/enums.hpp"
133 }
134
135 #define SWIG_ATTRIBUTE_TEMPLATE
136
137 %include "attribute.i"
138
139 %inline {
140 typedef std::map<std::string, std::shared_ptr<sigrok::Driver> >
141     map_string_Driver;
142 typedef std::map<std::string, std::shared_ptr<sigrok::InputFormat> >
143     map_string_InputFormat;
144 typedef std::map<std::string, std::shared_ptr<sigrok::OutputFormat> >
145     map_string_OutputFormat;
146 typedef std::map<std::string, std::shared_ptr<sigrok::ChannelGroup> >
147     map_string_ChannelGroup;
148 typedef std::map<const sigrok::ConfigKey *, Glib::VariantBase>
149     map_ConfigKey_Variant;
150 }
151
152 %attributeval(sigrok::Context,
153     map_string_Driver, drivers, get_drivers);
154 %attributeval(sigrok::Context,
155     map_string_InputFormat, input_formats, get_input_formats);
156 %attributeval(sigrok::Context,
157     map_string_OutputFormat, output_formats, get_output_formats);
158
159 %attributestring(sigrok::Context,
160     std::string, package_version, get_package_version);
161 %attributestring(sigrok::Context,
162     std::string, lib_version, get_lib_version);
163
164 %attribute(sigrok::Context,
165     const sigrok::LogLevel *, log_level, get_log_level, set_log_level);
166
167 %attributestring(sigrok::Context,
168     std::string, log_domain, get_log_domain, set_log_domain);
169
170 %attributestring(sigrok::Driver, std::string, name, get_name);
171 %attributestring(sigrok::Driver, std::string, long_name, get_long_name);
172
173 %attributestring(sigrok::InputFormat,
174     std::string, name, get_name);
175 %attributestring(sigrok::InputFormat,
176     std::string, description, get_description);
177
178 %attributestring(sigrok::OutputFormat,
179     std::string, name, get_name);
180 %attributestring(sigrok::OutputFormat,
181     std::string, description, get_description);
182
183 %attributestring(sigrok::Device, std::string, description, get_description);
184 %attributestring(sigrok::Device, std::string, vendor, get_vendor);
185 %attributestring(sigrok::Device, std::string, model, get_model);
186 %attributestring(sigrok::Device, std::string, version, get_version);
187
188 %attributeval(sigrok::Device,
189     std::vector<std::shared_ptr<sigrok::Channel> >,
190     channels, get_channels);
191
192 %attributeval(sigrok::Device, map_string_ChannelGroup,
193     channel_groups, get_channel_groups);
194
195 /* Using %attributestring for shared_ptr attribute. See
196    http://sourceforge.net/p/swig/mailman/message/31832070/ */
197 %attributestring(sigrok::HardwareDevice,
198     std::shared_ptr<sigrok::Driver>, driver, get_driver);
199
200 %attributestring(sigrok::Channel, std::string, name, get_name, set_name);
201 %attribute(sigrok::Channel, bool, enabled, get_enabled, set_enabled);
202 %attribute(sigrok::Channel, const sigrok::ChannelType *, type, get_type);
203 %attribute(sigrok::Channel, unsigned int, index, get_index);
204
205 %attributestring(sigrok::ChannelGroup, std::string, name, get_name);
206 %attributeval(sigrok::ChannelGroup,
207     std::vector<std::shared_ptr<sigrok::Channel> >,
208     channels, get_channels);
209
210 %attributestring(sigrok::Trigger, std::string, name, get_name);
211 %attributeval(sigrok::Trigger,
212     std::vector<std::shared_ptr<sigrok::TriggerStage> >,
213     stages, get_stages);
214
215 %attribute(sigrok::TriggerStage, int, number, get_number);
216 %attributeval(sigrok::TriggerStage,
217     std::vector<std::shared_ptr<sigrok::TriggerMatch> >,
218     matches, get_matches);
219
220 %attributestring(sigrok::TriggerMatch,
221     std::shared_ptr<sigrok::Channel>, channel, get_channel);
222 %attribute(sigrok::TriggerMatch, const sigrok::TriggerMatchType *, type, get_type);
223 %attribute(sigrok::TriggerMatch, float, value, get_value);
224
225 %attributeval(sigrok::Session,
226     std::vector<std::shared_ptr<sigrok::Device> >,
227     devices, get_devices);
228
229 %attributestring(sigrok::Session,
230     std::shared_ptr<sigrok::Trigger>, trigger, get_trigger, set_trigger);
231
232 %attributestring(sigrok::Packet,
233     std::shared_ptr<sigrok::PacketPayload>, payload, get_payload);
234
235 %attributeval(sigrok::Meta, map_ConfigKey_Variant, config, get_config);
236
237 %attribute(sigrok::Analog, int, num_samples, get_num_samples);
238 %attribute(sigrok::Analog, const sigrok::Quantity *, mq, get_mq);
239 %attribute(sigrok::Analog, const sigrok::Unit *, unit, get_unit);
240 %attributeval(sigrok::Analog, std::vector<const sigrok::QuantityFlag *>, mq_flags, get_mq_flags);