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