]> sigrok.org Git - libsigrok.git/blame - bindings/cxx/include/libsigrok/libsigrok.hpp
bindings: Revise Session::append() API.
[libsigrok.git] / bindings / cxx / include / libsigrok / libsigrok.hpp
CommitLineData
c23c8659
ML
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2013-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
07443fd2
ML
20/**
21
22@mainpage API Reference
23
24Introduction
25------------
26
27The sigrok++ API provides an object-oriented C++ interface to the functionality
28in libsigrok, including automatic memory and resource management.
29
30It is built on top of the public libsigrok C API, and is designed to be used as
31a standalone alternative API. Programs should not mix usage of the C and C++
32APIs; the C++ interface code needs to have full control of all C API calls for
33resources to be managed correctly.
34
35Memory management
36-----------------
37
38All runtime objects created through the C++ API are passed and accessed via
39shared pointers, using the C++11 std::shared_ptr implementation. This means
40that a reference count is kept for each object.
41
42Shared pointers can be copied and assigned in a user's program, automatically
43updating their reference count and deleting objects when they are no longer in
44use. The C++ interface code also keeps track of internal dependencies between
45libsigrok resources, and ensures that objects are not prematurely deleted when
46their resources are in use by other objects.
47
48This means that management of sigrok++ objects and their underlying libsigrok
49resources can be treated as fully automatic. As long as all shared pointers to
50objects are deleted or reassigned when no longer in use, all underlying
51resources will be released at the right time.
52
53Getting started
54---------------
55
56Usage of the C++ API needs to begin with a call to sigrok::Context::create().
57This will create the global libsigrok context and returns a shared pointer to
58the sigrok::Context object. Methods on this object provide access to the
59hardware drivers, input and output formats supported by the library, as well
60as means of creating other objects such as sessions and triggers.
61
62Error handling
63--------------
64
65When any libsigrok C API call returns an error, a sigrok::Error exception is
66raised, which provides access to the error code and description.
67
68*/
69
c23c8659
ML
70#ifndef LIBSIGROK_HPP
71#define LIBSIGROK_HPP
72
73#include "libsigrok/libsigrok.h"
74#include <glibmm-2.4/glibmm.h>
75
76#include <stdexcept>
77#include <memory>
78#include <vector>
79#include <map>
80
81namespace sigrok
82{
83
84using namespace std;
85
86/* Forward declarations */
87class SR_API Error;
88class SR_API Context;
89class SR_API Driver;
90class SR_API Device;
91class SR_API HardwareDevice;
92class SR_API Channel;
93class SR_API EventSource;
94class SR_API Session;
95class SR_API ConfigKey;
96class SR_API InputFormat;
97class SR_API OutputFormat;
98class SR_API LogLevel;
99class SR_API ChannelGroup;
100class SR_API Trigger;
101class SR_API TriggerStage;
102class SR_API TriggerMatch;
103class SR_API TriggerMatchType;
104class SR_API ChannelType;
105class SR_API Packet;
106class SR_API PacketPayload;
107class SR_API PacketType;
108class SR_API Quantity;
109class SR_API Unit;
110class SR_API QuantityFlag;
111class SR_API InputFileDevice;
112class SR_API Output;
113class SR_API DataType;
114
115/** Exception thrown when an error code is returned by any libsigrok call. */
116class SR_API Error: public exception
117{
118public:
119 Error(int result);
120 ~Error() throw();
121 const int result;
122 const char *what() const throw();
123};
124
07443fd2 125/* Base template for most classes which wrap a struct type from libsigrok. */
c23c8659
ML
126template <class Parent, typename Struct> class SR_API StructureWrapper :
127 public enable_shared_from_this<StructureWrapper<Parent, Struct> >
128{
7649683c 129protected:
07443fd2 130 /* Parent object which owns this child object's underlying structure.
c23c8659
ML
131
132 This shared pointer will be null when this child is unused, but
133 will be assigned to point to the parent before any shared pointer
134 to this child is handed out to the user.
135
136 When the reference count of this child falls to zero, this shared
137 pointer to its parent is reset by a custom deleter on the child's
138 shared pointer.
139
140 This strategy ensures that the destructors for both the child and
141 the parent are called at the correct time, i.e. only when all
142 references to both the parent and all its children are gone. */
143 shared_ptr<Parent> parent;
7649683c
ML
144
145public:
146 shared_ptr<StructureWrapper<Parent, Struct> >
147 get_shared_pointer(Parent *parent)
148 {
149 this->parent = static_pointer_cast<Parent>(parent->shared_from_this());
150 return shared_ptr<StructureWrapper<Parent, Struct> >(
151 this, reset_parent);
152 }
153 shared_ptr<StructureWrapper<Parent, Struct> >
154 get_shared_pointer(shared_ptr<Parent> parent)
155 {
156 this->parent = parent;
157 return shared_ptr<StructureWrapper<Parent, Struct> >(
158 this, reset_parent);
159 }
c23c8659 160protected:
7649683c
ML
161 static void reset_parent(StructureWrapper<Parent, Struct> *object)
162 {
163 object->parent.reset();
164 }
165
c23c8659
ML
166 Struct *structure;
167
168 StructureWrapper<Parent, Struct>(Struct *structure) :
169 structure(structure)
170 {
171 }
172};
173
174/** Type of log callback */
175typedef function<void(const LogLevel *, string message)> LogCallbackFunction;
176
07443fd2 177/** The global libsigrok context */
c23c8659
ML
178class SR_API Context : public enable_shared_from_this<Context>
179{
180public:
181 /** Create new context */
182 static shared_ptr<Context> create();
183 /** libsigrok package version. */
184 string get_package_version();
185 /** libsigrok library version. */
186 string get_lib_version();
187 /** Available hardware drivers, indexed by name. */
188 map<string, shared_ptr<Driver> > get_drivers();
189 /** Available input formats, indexed by name. */
190 map<string, shared_ptr<InputFormat> > get_input_formats();
191 /** Available output formats, indexed by name. */
192 map<string, shared_ptr<OutputFormat> > get_output_formats();
193 /** Current log level. */
194 const LogLevel *get_log_level();
195 /** Set the log level. */
196 void set_log_level(const LogLevel *level);
197 /** Current log domain. */
198 string get_log_domain();
199 /** Set the log domain. */
200 void set_log_domain(string value);
201 /** Set the log callback. */
202 void set_log_callback(LogCallbackFunction callback);
203 /** Set the log callback to the default handler. */
204 void set_log_callback_default();
205 /** Create a new session. */
206 shared_ptr<Session> create_session();
207 /** Load a saved session. */
208 shared_ptr<Session> load_session(string filename);
209 /** Create a new trigger. */
210 shared_ptr<Trigger> create_trigger(string name);
211protected:
212 struct sr_context *structure;
213 map<string, Driver *> drivers;
214 map<string, InputFormat *> input_formats;
215 map<string, OutputFormat *> output_formats;
216 Session *session;
217 LogCallbackFunction log_callback;
218 Context();
219 ~Context();
220 /** Deleter needed to allow shared_ptr use with protected destructor. */
221 class Deleter
222 {
223 public:
224 void operator()(Context *context) { delete context; }
225 };
226 friend class Deleter;
227 friend class Session;
228 friend class Driver;
229};
230
07443fd2 231/** A hardware driver provided by the library */
c23c8659
ML
232class SR_API Driver : public StructureWrapper<Context, struct sr_dev_driver>
233{
234public:
235 /** Name of this driver. */
236 string get_name();
237 /** Long name for this driver. */
238 string get_long_name();
239 /** Scan for devices and return a list of devices found. */
240 vector<shared_ptr<HardwareDevice> > scan(
241 map<const ConfigKey *, Glib::VariantBase> options = {});
242protected:
243 bool initialized;
244 vector<HardwareDevice *> devices;
245 Driver(struct sr_dev_driver *structure);
246 ~Driver();
247 friend class Context;
248 friend class HardwareDevice;
249 friend class ChannelGroup;
250};
251
252/** An object that can be configured. */
253class SR_API Configurable
254{
255public:
256 /** Read configuration for the given key. */
257 Glib::VariantBase config_get(const ConfigKey *key);
258 /** Set configuration for the given key to a specified value. */
259 void config_set(const ConfigKey *key, Glib::VariantBase value);
260 /** Enumerate available values for the given configuration key. */
261 Glib::VariantBase config_list(const ConfigKey *key);
262protected:
263 Configurable(
264 struct sr_dev_driver *driver,
265 struct sr_dev_inst *sdi,
266 struct sr_channel_group *channel_group);
267 virtual ~Configurable();
268 struct sr_dev_driver *config_driver;
269 struct sr_dev_inst *config_sdi;
270 struct sr_channel_group *config_channel_group;
271};
272
07443fd2 273/** A generic device, either hardware or virtual */
c23c8659
ML
274class SR_API Device :
275 public Configurable,
276 public StructureWrapper<Context, struct sr_dev_inst>
277{
278public:
279 /** Vendor name for this device. */
280 string get_vendor();
281 /** Model name for this device. */
282 string get_model();
283 /** Version string for this device. */
284 string get_version();
285 /** List of the channels available on this device. */
286 vector<shared_ptr<Channel> > get_channels();
6be7a7f2
ML
287 /** Channel groups available on this device, indexed by name. */
288 map<string, shared_ptr<ChannelGroup> > get_channel_groups();
c23c8659
ML
289 /** Open device. */
290 void open();
291 /** Close device. */
292 void close();
293protected:
294 Device(struct sr_dev_inst *structure);
295 ~Device();
4178d971
ML
296 shared_ptr<Channel> get_channel(struct sr_channel *ptr);
297 map<struct sr_channel *, Channel *> channels;
6be7a7f2 298 map<string, ChannelGroup *> channel_groups;
c23c8659
ML
299 /** Deleter needed to allow shared_ptr use with protected destructor. */
300 class Deleter
301 {
302 public:
303 void operator()(Device *device) { delete device; }
304 };
305 friend class Deleter;
306 friend class Session;
307 friend class Channel;
308 friend class ChannelGroup;
309 friend class Output;
2928f47d 310 friend class Analog;
c23c8659
ML
311};
312
07443fd2 313/** A real hardware device, connected via a driver */
c23c8659
ML
314class SR_API HardwareDevice : public Device
315{
316public:
317 /** Driver providing this device. */
318 shared_ptr<Driver> get_driver();
c23c8659
ML
319protected:
320 HardwareDevice(Driver *driver, struct sr_dev_inst *structure);
321 ~HardwareDevice();
322 Driver *driver;
c23c8659
ML
323 friend class Driver;
324 friend class ChannelGroup;
325};
326
07443fd2 327/** A channel on a device */
c23c8659
ML
328class SR_API Channel : public StructureWrapper<Device, struct sr_channel>
329{
330public:
331 /** Current name of this channel. */
332 string get_name();
333 /** Set the name of this channel. */
334 void set_name(string name);
335 /** Type of this channel. */
336 const ChannelType *get_type();
337 /** Enabled status of this channel. */
338 bool get_enabled();
339 /** Set the enabled status of this channel. */
340 void set_enabled(bool value);
341protected:
342 Channel(struct sr_channel *structure);
343 ~Channel();
344 const ChannelType * const type;
345 friend class Device;
346 friend class ChannelGroup;
347 friend class Session;
348 friend class TriggerStage;
349};
350
07443fd2 351/** A group of channels on a device, which share some configuration */
c23c8659 352class SR_API ChannelGroup :
6be7a7f2 353 public StructureWrapper<Device, struct sr_channel_group>,
c23c8659
ML
354 public Configurable
355{
356public:
357 /** Name of this channel group. */
358 string get_name();
359 /** List of the channels in this group. */
360 vector<shared_ptr<Channel> > get_channels();
361protected:
6be7a7f2 362 ChannelGroup(Device *device, struct sr_channel_group *structure);
c23c8659
ML
363 ~ChannelGroup();
364 vector<Channel *> channels;
6be7a7f2 365 friend class Device;
c23c8659
ML
366};
367
07443fd2 368/** A trigger configuration */
c23c8659
ML
369class SR_API Trigger : public enable_shared_from_this<Trigger>
370{
371public:
372 string get_name();
373 vector<shared_ptr<TriggerStage> > get_stages();
374 shared_ptr<TriggerStage> add_stage();
375protected:
376 Trigger(shared_ptr<Context> context, string name);
377 ~Trigger();
378 struct sr_trigger *structure;
379 shared_ptr<Context> context;
380 vector<TriggerStage *> stages;
381 /** Deleter needed to allow shared_ptr use with protected destructor. */
382 class Deleter
383 {
384 public:
385 void operator()(Trigger *trigger) { delete trigger; }
386 };
387 friend class Context;
6fa0eb86 388 friend class Session;
c23c8659
ML
389};
390
07443fd2 391/** A stage in a trigger configuration */
c23c8659
ML
392class SR_API TriggerStage : public StructureWrapper<Trigger, struct sr_trigger_stage>
393{
394public:
395 int get_number();
396 vector<shared_ptr<TriggerMatch> > get_matches();
397 void add_match(shared_ptr<Channel> channel, const TriggerMatchType *type);
398 void add_match(shared_ptr<Channel> channel, const TriggerMatchType *type, float value);
399protected:
400 vector<TriggerMatch *> matches;
401 TriggerStage(struct sr_trigger_stage *structure);
402 ~TriggerStage();
403 friend class Trigger;
404};
405
07443fd2 406/** A match condition in a trigger configuration */
c23c8659
ML
407class SR_API TriggerMatch : public StructureWrapper<TriggerStage, struct sr_trigger_match>
408{
409public:
410 shared_ptr<Channel> get_channel();
411 const TriggerMatchType *get_type();
412 float get_value();
413protected:
414 TriggerMatch(struct sr_trigger_match *structure, shared_ptr<Channel> channel);
415 ~TriggerMatch();
416 shared_ptr<Channel> channel;
417 friend class TriggerStage;
418};
419
420/** Type of datafeed callback */
421typedef function<void(shared_ptr<Device>, shared_ptr<Packet>)>
422 DatafeedCallbackFunction;
423
07443fd2 424/* Data required for C callback function to call a C++ datafeed callback */
c23c8659
ML
425class SR_PRIV DatafeedCallbackData
426{
427public:
428 void run(const struct sr_dev_inst *sdi,
429 const struct sr_datafeed_packet *pkt);
430protected:
431 DatafeedCallbackFunction callback;
432 DatafeedCallbackData(Session *session,
433 DatafeedCallbackFunction callback);
434 Session *session;
435 friend class Session;
436};
437
438/** Type of source callback */
439typedef function<bool(Glib::IOCondition)>
440 SourceCallbackFunction;
441
07443fd2 442/* Data required for C callback function to call a C++ source callback */
c23c8659
ML
443class SR_PRIV SourceCallbackData
444{
445public:
446 bool run(int revents);
447protected:
448 SourceCallbackData(shared_ptr<EventSource> source);
449 shared_ptr<EventSource> source;
450 friend class Session;
451};
452
07443fd2 453/** An I/O event source */
c23c8659
ML
454class SR_API EventSource
455{
456public:
457 /** Create an event source from a file descriptor. */
458 static shared_ptr<EventSource> create(int fd, Glib::IOCondition events,
459 int timeout, SourceCallbackFunction callback);
460 /** Create an event source from a Glib::PollFD */
461 static shared_ptr<EventSource> create(Glib::PollFD pollfd, int timeout,
462 SourceCallbackFunction callback);
463 /** Create an event source from a Glib::IOChannel */
464 static shared_ptr<EventSource> create(
465 Glib::RefPtr<Glib::IOChannel> channel, Glib::IOCondition events,
466 int timeout, SourceCallbackFunction callback);
467protected:
468 EventSource(int timeout, SourceCallbackFunction callback);
469 ~EventSource();
470 enum source_type {
471 SOURCE_FD,
472 SOURCE_POLLFD,
473 SOURCE_IOCHANNEL
474 } type;
475 int fd;
476 Glib::PollFD pollfd;
477 Glib::RefPtr<Glib::IOChannel> channel;
478 Glib::IOCondition events;
479 int timeout;
480 SourceCallbackFunction callback;
481 /** Deleter needed to allow shared_ptr use with protected destructor. */
482 class Deleter
483 {
484 public:
485 void operator()(EventSource *source) { delete source; }
486 };
487 friend class Deleter;
488 friend class Session;
489 friend class SourceCallbackData;
490};
491
07443fd2 492/** A sigrok session */
c23c8659
ML
493class SR_API Session
494{
495public:
496 /** Add a device to this session. */
497 void add_device(shared_ptr<Device> device);
498 /** List devices attached to this session. */
499 vector<shared_ptr<Device> > get_devices();
500 /** Remove all devices from this session. */
501 void remove_devices();
502 /** Add a datafeed callback to this session. */
503 void add_datafeed_callback(DatafeedCallbackFunction callback);
504 /** Remove all datafeed callbacks from this session. */
505 void remove_datafeed_callbacks();
506 /** Add an event source. */
507 void add_source(shared_ptr<EventSource> source);
508 /** Remove an event source. */
509 void remove_source(shared_ptr<EventSource> source);
510 /** Start the session. */
511 void start();
512 /** Run the session event loop. */
513 void run();
514 /** Stop the session. */
515 void stop();
516 /** Begin saving session to a file. */
517 void begin_save(string filename);
518 /** Append a packet to the session file being saved. */
1d67cfb4
ML
519 void append(shared_ptr<Packet> packet);
520 /** Append raw logic data to the session file being saved. */
521 void append(void *data, size_t length, unsigned int unit_size);
6fa0eb86
ML
522 /** Get current trigger setting. */
523 shared_ptr<Trigger> get_trigger();
524 /** Set trigger setting. */
525 void set_trigger(shared_ptr<Trigger> trigger);
c23c8659
ML
526protected:
527 Session(shared_ptr<Context> context);
528 Session(shared_ptr<Context> context, string filename);
529 ~Session();
530 struct sr_session *structure;
531 const shared_ptr<Context> context;
532 map<const struct sr_dev_inst *, shared_ptr<Device> > devices;
533 vector<DatafeedCallbackData *> datafeed_callbacks;
534 map<shared_ptr<EventSource>, SourceCallbackData *> source_callbacks;
535 bool saving;
536 bool save_initialized;
537 string save_filename;
538 uint64_t save_samplerate;
6fa0eb86 539 shared_ptr<Trigger> trigger;
c23c8659
ML
540 /** Deleter needed to allow shared_ptr use with protected destructor. */
541 class Deleter
542 {
543 public:
544 void operator()(Session *session) { delete session; }
545 };
546 friend class Deleter;
547 friend class Context;
548 friend class DatafeedCallbackData;
549};
550
07443fd2 551/** A packet on the session datafeed */
2928f47d 552class SR_API Packet : public enable_shared_from_this<Packet>
c23c8659
ML
553{
554public:
90ba83f2
ML
555 /** Type of this packet. */
556 const PacketType *get_type();
c23c8659 557 /** Payload of this packet. */
2928f47d 558 shared_ptr<PacketPayload> get_payload();
c23c8659 559protected:
2928f47d
ML
560 Packet(shared_ptr<Device> device,
561 const struct sr_datafeed_packet *structure);
c23c8659
ML
562 ~Packet();
563 const struct sr_datafeed_packet *structure;
2928f47d 564 shared_ptr<Device> device;
c23c8659
ML
565 PacketPayload *payload;
566 /** Deleter needed to allow shared_ptr use with protected destructor. */
567 class Deleter
568 {
569 public:
570 void operator()(Packet *packet) { delete packet; }
571 };
572 friend class Deleter;
573 friend class Session;
574 friend class Output;
575 friend class DatafeedCallbackData;
2928f47d
ML
576 friend class Header;
577 friend class Meta;
578 friend class Logic;
579 friend class Analog;
c23c8659
ML
580};
581
07443fd2 582/** Abstract base class for datafeed packet payloads */
c23c8659
ML
583class SR_API PacketPayload
584{
585protected:
586 PacketPayload();
587 virtual ~PacketPayload() = 0;
2928f47d
ML
588 shared_ptr<PacketPayload> get_shared_pointer(Packet *parent) {
589 return static_pointer_cast<PacketPayload>(get_shared_pointer(parent));
590 }
591 /** Deleter needed to allow shared_ptr use with protected destructor. */
592 class Deleter
593 {
594 public:
595 void operator()(PacketPayload *payload) { delete payload; }
596 };
597 friend class Deleter;
c23c8659
ML
598 friend class Packet;
599 friend class Output;
600};
601
2928f47d
ML
602/** Payload of a datafeed header packet */
603class SR_API Header : public PacketPayload,
604 public StructureWrapper<Packet, const struct sr_datafeed_header>
605{
606public:
607 int get_feed_version();
608 Glib::TimeVal get_start_time();
609protected:
610 Header(const struct sr_datafeed_header *structure);
611 ~Header();
612 const struct sr_datafeed_header *structure;
613 friend class Packet;
614};
615
616/** Payload of a datafeed metadata packet */
617class SR_API Meta : public PacketPayload,
618 public StructureWrapper<Packet, const struct sr_datafeed_meta>
619{
620public:
621 map<const ConfigKey *, Glib::VariantBase> get_config();
622protected:
623 Meta(const struct sr_datafeed_meta *structure);
624 ~Meta();
625 const struct sr_datafeed_meta *structure;
626 map<const ConfigKey *, Glib::VariantBase> config;
627 friend class Packet;
628};
629
07443fd2 630/** Payload of a datafeed packet with logic data */
2928f47d
ML
631class SR_API Logic : public PacketPayload,
632 public StructureWrapper<Packet, const struct sr_datafeed_logic>
c23c8659 633{
2928f47d
ML
634public:
635 /* Pointer to data. */
636 void *get_data_pointer();
637 /* Data length in bytes. */
638 size_t get_data_length();
639 /* Size of each sample in bytes. */
640 unsigned int get_unit_size();
c23c8659
ML
641protected:
642 Logic(const struct sr_datafeed_logic *structure);
643 ~Logic();
644 const struct sr_datafeed_logic *structure;
c23c8659
ML
645 friend class Packet;
646};
647
07443fd2 648/** Payload of a datafeed packet with analog data */
2928f47d
ML
649class SR_API Analog : public PacketPayload,
650 public StructureWrapper<Packet, const struct sr_datafeed_analog>
c23c8659
ML
651{
652public:
2928f47d
ML
653 /** Pointer to data. */
654 float *get_data_pointer();
c23c8659
ML
655 /** Number of samples in this packet. */
656 unsigned int get_num_samples();
2928f47d
ML
657 /** Channels for which this packet contains data. */
658 vector<shared_ptr<Channel> > get_channels();
c23c8659
ML
659 /** Measured quantity of the samples in this packet. */
660 const Quantity *get_mq();
661 /** Unit of the samples in this packet. */
662 const Unit *get_unit();
663 /** Measurement flags associated with the samples in this packet. */
664 vector<const QuantityFlag *> get_mq_flags();
665protected:
666 Analog(const struct sr_datafeed_analog *structure);
667 ~Analog();
668 const struct sr_datafeed_analog *structure;
c23c8659
ML
669 friend class Packet;
670};
671
07443fd2 672/** An input format supported by the library */
c23c8659
ML
673class SR_API InputFormat :
674 public StructureWrapper<Context, struct sr_input_format>
675{
676public:
677 /** Name of this input format. */
678 string get_name();
679 /** Description of this input format. */
680 string get_description();
681 /** Check whether a given file matches this input format. */
682 bool format_match(string filename);
683 /** Open a file using this input format. */
684 shared_ptr<InputFileDevice> open_file(string filename,
685 map<string, string> options = {});
686protected:
687 InputFormat(struct sr_input_format *structure);
688 ~InputFormat();
689 friend class Context;
690 friend class InputFileDevice;
691};
692
07443fd2 693/** A virtual device associated with an input file */
c23c8659
ML
694class SR_API InputFileDevice : public Device
695{
696public:
697 /** Load data from file. */
698 void load();
699protected:
700 InputFileDevice(shared_ptr<InputFormat> format,
701 struct sr_input *input, string filename);
702 ~InputFileDevice();
703 struct sr_input *input;
704 shared_ptr<InputFormat> format;
705 string filename;
706 /** Deleter needed to allow shared_ptr use with protected destructor. */
707 class Deleter
708 {
709 public:
710 void operator()(InputFileDevice *device) { delete device; }
711 };
712 friend class Deleter;
713 friend class InputFormat;
714};
715
07443fd2 716/** An output format supported by the library */
c23c8659
ML
717class SR_API OutputFormat :
718 public StructureWrapper<Context, struct sr_output_format>
719{
720public:
721 /** Name of this output format. */
722 string get_name();
723 /** Description of this output format. */
724 string get_description();
725 /** Create an output using this format. */
726 shared_ptr<Output> create_output(shared_ptr<Device> device, map<string, string> options = {});
727protected:
728 OutputFormat(struct sr_output_format *structure);
729 ~OutputFormat();
730 friend class Context;
731 friend class Output;
732};
733
07443fd2 734/** An output instance (an output format applied to a device) */
c23c8659
ML
735class SR_API Output
736{
737public:
738 /** Update output with data from the given packet. */
739 string receive(shared_ptr<Packet> packet);
740protected:
741 Output(shared_ptr<OutputFormat> format, shared_ptr<Device> device);
742 Output(shared_ptr<OutputFormat> format,
743 shared_ptr<Device> device, map<string, string> options);
744 ~Output();
745 struct sr_output *structure;
746 const shared_ptr<OutputFormat> format;
747 const shared_ptr<Device> device;
748 const map<string, string> options;
749 /** Deleter needed to allow shared_ptr use with protected destructor. */
750 class Deleter
751 {
752 public:
753 void operator()(Output *output) { delete output; }
754 };
755 friend class Deleter;
756 friend class OutputFormat;
757};
758
759/** Base class for objects which wrap an enumeration value from libsigrok */
760template <typename T> class SR_API EnumValue
761{
762public:
763 /** The enum constant associated with this value. */
764 T get_id() const { return id; }
765 /** The name associated with this value. */
766 string get_name() const { return name; }
767protected:
768 EnumValue(T id, const char name[]) : id(id), name(name) {}
769 ~EnumValue() {}
770 const T id;
771 const string name;
772};
773
774#include "enums.hpp"
775
776}
777
778#endif // LIBSIGROK_HPP