2 * This file is part of the libsigrok project.
4 * Copyright (C) 2013-2014 Martin Ling <martin-sigrok@earth.li>
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.
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.
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/>.
22 @mainpage API Reference
27 The sigrok++ API provides an object-oriented C++ interface to the functionality
28 in libsigrok, including automatic memory and resource management.
30 It is built on top of the public libsigrok C API, and is designed to be used as
31 a standalone alternative API. Programs should not mix usage of the C and C++
32 APIs; the C++ interface code needs to have full control of all C API calls for
33 resources to be managed correctly.
38 All runtime objects created through the C++ API are passed and accessed via
39 shared pointers, using the C++11 std::shared_ptr implementation. This means
40 that a reference count is kept for each object.
42 Shared pointers can be copied and assigned in a user's program, automatically
43 updating their reference count and deleting objects when they are no longer in
44 use. The C++ interface code also keeps track of internal dependencies between
45 libsigrok resources, and ensures that objects are not prematurely deleted when
46 their resources are in use by other objects.
48 This means that management of sigrok++ objects and their underlying libsigrok
49 resources can be treated as fully automatic. As long as all shared pointers to
50 objects are deleted or reassigned when no longer in use, all underlying
51 resources will be released at the right time.
56 Usage of the C++ API needs to begin with a call to sigrok::Context::create().
57 This will create the global libsigrok context and returns a shared pointer to
58 the sigrok::Context object. Methods on this object provide access to the
59 hardware drivers, input and output formats supported by the library, as well
60 as means of creating other objects such as sessions and triggers.
65 When any libsigrok C API call returns an error, a sigrok::Error exception is
66 raised, which provides access to the error code and description.
73 #include "libsigrok/libsigrok.h"
74 #include <glibmm-2.4/glibmm.h>
87 /* Forward declarations */
92 class SR_API HardwareDevice;
94 class SR_API EventSource;
96 class SR_API ConfigKey;
97 class SR_API InputFormat;
98 class SR_API OutputFormat;
99 class SR_API LogLevel;
100 class SR_API ChannelGroup;
101 class SR_API Trigger;
102 class SR_API TriggerStage;
103 class SR_API TriggerMatch;
104 class SR_API TriggerMatchType;
105 class SR_API ChannelType;
107 class SR_API PacketPayload;
108 class SR_API PacketType;
109 class SR_API Quantity;
111 class SR_API QuantityFlag;
113 class SR_API InputDevice;
115 class SR_API DataType;
117 class SR_API UserDevice;
119 /** Exception thrown when an error code is returned by any libsigrok call. */
120 class SR_API Error: public exception
126 const char *what() const throw();
129 /* Base template for classes whose resources are owned by a parent object. */
130 template <class Class, class Parent, typename Struct>
131 class SR_API ParentOwned
134 /* Parent object which owns this child object's underlying structure.
136 This shared pointer will be null when this child is unused, but
137 will be assigned to point to the parent before any shared pointer
138 to this child is handed out to the user.
140 When the reference count of this child falls to zero, this shared
141 pointer to its parent is reset by a custom deleter on the child's
144 This strategy ensures that the destructors for both the child and
145 the parent are called at the correct time, i.e. only when all
146 references to both the parent and all its children are gone. */
147 shared_ptr<Parent> _parent;
149 /* Weak pointer for shared_from_this() implementation. */
150 weak_ptr<Class> _weak_this;
153 /* Get parent object that owns this object. */
154 shared_ptr<Parent> parent()
159 /* Note, this implementation will create a new smart_ptr if none exists. */
160 shared_ptr<Class> shared_from_this()
162 shared_ptr<Class> shared;
164 if (!(shared = _weak_this.lock()))
166 shared = shared_ptr<Class>((Class *) this, reset_parent);
173 shared_ptr<Class> get_shared_pointer(shared_ptr<Parent> parent)
176 throw Error(SR_ERR_BUG);
177 this->_parent = parent;
178 return shared_from_this();
181 shared_ptr<Class> get_shared_pointer(Parent *parent)
184 throw Error(SR_ERR_BUG);
185 return get_shared_pointer(parent->shared_from_this());
188 static void reset_parent(Class *object)
190 if (!object->_parent)
191 throw Error(SR_ERR_BUG);
192 object->_parent.reset();
197 ParentOwned<Class, Parent, Struct>(Struct *structure) :
198 _structure(structure)
203 /* Base template for classes whose resources are owned by the user. */
204 template <class Class, typename Struct>
205 class SR_API UserOwned : public enable_shared_from_this<Class>
208 shared_ptr<Class> shared_from_this()
210 auto shared = enable_shared_from_this<Class>::shared_from_this();
212 throw Error(SR_ERR_BUG);
218 UserOwned<Class, Struct>(Struct *structure) :
219 _structure(structure)
223 /* Deleter needed to allow shared_ptr use with protected destructor. */
227 void operator()(Class *object) { delete object; }
231 /** Type of log callback */
232 typedef function<void(const LogLevel *, string message)> LogCallbackFunction;
234 /** The global libsigrok context */
235 class SR_API Context : public UserOwned<Context, struct sr_context>
238 /** Create new context */
239 static shared_ptr<Context> create();
240 /** libsigrok package version. */
241 string package_version();
242 /** libsigrok library version. */
243 string lib_version();
244 /** Available hardware drivers, indexed by name. */
245 map<string, shared_ptr<Driver> > drivers();
246 /** Available input formats, indexed by name. */
247 map<string, shared_ptr<InputFormat> > input_formats();
248 /** Available output formats, indexed by name. */
249 map<string, shared_ptr<OutputFormat> > output_formats();
250 /** Current log level. */
251 const LogLevel *log_level();
252 /** Set the log level.
253 * @param level LogLevel to use. */
254 void set_log_level(const LogLevel *level);
255 /** Current log domain. */
257 /** Set the log domain.
258 * @param value Log domain prefix string. */
259 void set_log_domain(string value);
260 /** Set the log callback.
261 * @param callback Callback of the form callback(LogLevel, string). */
262 void set_log_callback(LogCallbackFunction callback);
263 /** Set the log callback to the default handler. */
264 void set_log_callback_default();
265 /** Create a new session. */
266 shared_ptr<Session> create_session();
267 /** Create a new user device. */
268 shared_ptr<UserDevice> create_user_device(
269 string vendor, string model, string version);
270 /** Load a saved session.
271 * @param filename File name string. */
272 shared_ptr<Session> load_session(string filename);
273 /** Create a new trigger.
274 * @param name Name string for new trigger. */
275 shared_ptr<Trigger> create_trigger(string name);
276 /** Open an input file.
277 * @param filename File name string. */
278 shared_ptr<Input> open_file(string filename);
279 /** Open an input stream based on header data.
280 * @param header Initial data from stream. */
281 shared_ptr<Input> open_stream(string header);
283 map<string, Driver *> _drivers;
284 map<string, InputFormat *> _input_formats;
285 map<string, OutputFormat *> _output_formats;
287 LogCallbackFunction _log_callback;
290 friend class Deleter;
291 friend class Session;
301 /** An object that can be configured. */
302 class SR_API Configurable
305 /** Read configuration for the given key.
306 * @param key ConfigKey to read. */
307 Glib::VariantBase config_get(const ConfigKey *key);
308 /** Set configuration for the given key to a specified value.
309 * @param key ConfigKey to set.
310 * @param value Value to set. */
311 void config_set(const ConfigKey *key, Glib::VariantBase value);
312 /** Enumerate available values for the given configuration key.
313 * @param key ConfigKey to enumerate values for. */
314 Glib::VariantContainerBase config_list(const ConfigKey *key);
315 /** Enumerate available keys, according to a given index key. */
316 map<const ConfigKey *, set<Capability> > config_keys(const ConfigKey *key);
317 /** Check for a key in the list from a given index key. */
318 bool config_check(const ConfigKey *key, const ConfigKey *index_key);
321 struct sr_dev_driver *driver,
322 struct sr_dev_inst *sdi,
323 struct sr_channel_group *channel_group);
324 virtual ~Configurable();
325 struct sr_dev_driver *config_driver;
326 struct sr_dev_inst *config_sdi;
327 struct sr_channel_group *config_channel_group;
330 /** A hardware driver provided by the library */
331 class SR_API Driver :
332 public ParentOwned<Driver, Context, struct sr_dev_driver>,
336 /** Name of this driver. */
338 /** Long name for this driver. */
340 /** Scan for devices and return a list of devices found.
341 * @param options Mapping of (ConfigKey, value) pairs. */
342 vector<shared_ptr<HardwareDevice> > scan(
343 map<const ConfigKey *, Glib::VariantBase> options =
344 map<const ConfigKey *, Glib::VariantBase>());
347 vector<HardwareDevice *> _devices;
348 Driver(struct sr_dev_driver *structure);
350 friend class Context;
351 friend class HardwareDevice;
352 friend class ChannelGroup;
355 /** A generic device, either hardware or virtual */
356 class SR_API Device : public Configurable
359 /** Vendor name for this device. */
361 /** Model name for this device. */
363 /** Version string for this device. */
365 /** Serial number for this device. */
366 string serial_number();
367 /** Connection ID for this device. */
368 string connection_id();
369 /** List of the channels available on this device. */
370 vector<shared_ptr<Channel> > channels();
371 /** Channel groups available on this device, indexed by name. */
372 map<string, shared_ptr<ChannelGroup> > channel_groups();
378 Device(struct sr_dev_inst *structure);
380 virtual shared_ptr<Device> get_shared_from_this() = 0;
381 shared_ptr<Channel> get_channel(struct sr_channel *ptr);
382 struct sr_dev_inst *_structure;
383 map<struct sr_channel *, Channel *> _channels;
384 map<string, ChannelGroup *> _channel_groups;
385 /** Deleter needed to allow shared_ptr use with protected destructor. */
389 void operator()(Device *device) { delete device; }
391 friend class Deleter;
392 friend class Session;
393 friend class Channel;
394 friend class ChannelGroup;
399 /** A real hardware device, connected via a driver */
400 class SR_API HardwareDevice :
401 public UserOwned<HardwareDevice, struct sr_dev_inst>,
405 /** Driver providing this device. */
406 shared_ptr<Driver> driver();
408 HardwareDevice(shared_ptr<Driver> driver, struct sr_dev_inst *structure);
410 shared_ptr<Device> get_shared_from_this();
411 shared_ptr<Driver> _driver;
412 /** Deleter needed to allow shared_ptr use with protected destructor. */
416 void operator()(HardwareDevice *device) { delete device; }
418 friend class Deleter;
420 friend class ChannelGroup;
423 /** A virtual device, created by the user */
424 class SR_API UserDevice :
425 public UserOwned<UserDevice, struct sr_dev_inst>,
429 /** Add a new channel to this device. */
430 shared_ptr<Channel> add_channel(unsigned int index, const ChannelType *type, string name);
432 UserDevice(string vendor, string model, string version);
434 shared_ptr<Device> get_shared_from_this();
435 /** Deleter needed to allow shared_ptr use with protected destructor. */
439 void operator()(UserDevice *device) { delete device; }
441 friend class Context;
442 friend class Deleter;
445 /** A channel on a device */
446 class SR_API Channel :
447 public ParentOwned<Channel, Device, struct sr_channel>
450 /** Current name of this channel. */
452 /** Set the name of this channel. *
453 * @param name Name string to set. */
454 void set_name(string name);
455 /** Type of this channel. */
456 const ChannelType *type();
457 /** Enabled status of this channel. */
459 /** Set the enabled status of this channel.
460 * @param value Boolean value to set. */
461 void set_enabled(bool value);
462 /** Get the index number of this channel. */
463 unsigned int index();
465 Channel(struct sr_channel *structure);
467 const ChannelType * const _type;
469 friend class UserDevice;
470 friend class ChannelGroup;
471 friend class Session;
472 friend class TriggerStage;
475 /** A group of channels on a device, which share some configuration */
476 class SR_API ChannelGroup :
477 public ParentOwned<ChannelGroup, Device, struct sr_channel_group>,
481 /** Name of this channel group. */
483 /** List of the channels in this group. */
484 vector<shared_ptr<Channel> > channels();
486 ChannelGroup(Device *device, struct sr_channel_group *structure);
488 vector<Channel *> _channels;
492 /** A trigger configuration */
493 class SR_API Trigger : public UserOwned<Trigger, struct sr_trigger>
496 /** Name of this trigger configuration. */
498 /** List of the stages in this trigger. */
499 vector<shared_ptr<TriggerStage> > stages();
500 /** Add a new stage to this trigger. */
501 shared_ptr<TriggerStage> add_stage();
503 Trigger(shared_ptr<Context> context, string name);
505 shared_ptr<Context> _context;
506 vector<TriggerStage *> _stages;
507 friend class Deleter;
508 friend class Context;
509 friend class Session;
512 /** A stage in a trigger configuration */
513 class SR_API TriggerStage :
514 public ParentOwned<TriggerStage, Trigger, struct sr_trigger_stage>
517 /** Index number of this stage. */
519 /** List of match conditions on this stage. */
520 vector<shared_ptr<TriggerMatch> > matches();
521 /** Add a new match condition to this stage.
522 * @param channel Channel to match on.
523 * @param type TriggerMatchType to apply. */
524 void add_match(shared_ptr<Channel> channel, const TriggerMatchType *type);
525 /** Add a new match condition to this stage.
526 * @param channel Channel to match on.
527 * @param type TriggerMatchType to apply.
528 * @param value Threshold value. */
529 void add_match(shared_ptr<Channel> channel, const TriggerMatchType *type, float value);
531 vector<TriggerMatch *> _matches;
532 TriggerStage(struct sr_trigger_stage *structure);
534 friend class Trigger;
537 /** A match condition in a trigger configuration */
538 class SR_API TriggerMatch :
539 public ParentOwned<TriggerMatch, TriggerStage, struct sr_trigger_match>
542 /** Channel this condition matches on. */
543 shared_ptr<Channel> channel();
544 /** Type of match. */
545 const TriggerMatchType *type();
546 /** Threshold value. */
549 TriggerMatch(struct sr_trigger_match *structure, shared_ptr<Channel> channel);
551 shared_ptr<Channel> _channel;
552 friend class TriggerStage;
555 /** Type of datafeed callback */
556 typedef function<void(shared_ptr<Device>, shared_ptr<Packet>)>
557 DatafeedCallbackFunction;
559 /* Data required for C callback function to call a C++ datafeed callback */
560 class SR_PRIV DatafeedCallbackData
563 void run(const struct sr_dev_inst *sdi,
564 const struct sr_datafeed_packet *pkt);
566 DatafeedCallbackFunction _callback;
567 DatafeedCallbackData(Session *session,
568 DatafeedCallbackFunction callback);
570 friend class Session;
573 /** Type of source callback */
574 typedef function<bool(Glib::IOCondition)>
575 SourceCallbackFunction;
577 /* Data required for C callback function to call a C++ source callback */
578 class SR_PRIV SourceCallbackData
581 bool run(int revents);
583 SourceCallbackData(shared_ptr<EventSource> source);
584 shared_ptr<EventSource> _source;
585 friend class Session;
588 /** An I/O event source */
589 class SR_API EventSource
592 /** Create an event source from a file descriptor.
593 * @param fd File descriptor.
594 * @param events GLib IOCondition event mask.
595 * @param timeout Timeout in milliseconds.
596 * @param callback Callback of the form callback(events) */
597 static shared_ptr<EventSource> create(int fd, Glib::IOCondition events,
598 int timeout, SourceCallbackFunction callback);
599 /** Create an event source from a GLib PollFD
600 * @param pollfd GLib PollFD
601 * @param timeout Timeout in milliseconds.
602 * @param callback Callback of the form callback(events) */
603 static shared_ptr<EventSource> create(Glib::PollFD pollfd, int timeout,
604 SourceCallbackFunction callback);
605 /** Create an event source from a GLib IOChannel
606 * @param channel GLib IOChannel.
607 * @param events GLib IOCondition event mask.
608 * @param timeout Timeout in milliseconds.
609 * @param callback Callback of the form callback(events) */
610 static shared_ptr<EventSource> create(
611 Glib::RefPtr<Glib::IOChannel> channel, Glib::IOCondition events,
612 int timeout, SourceCallbackFunction callback);
614 EventSource(int timeout, SourceCallbackFunction callback);
622 Glib::PollFD _pollfd;
623 Glib::RefPtr<Glib::IOChannel> _channel;
624 Glib::IOCondition _events;
626 SourceCallbackFunction _callback;
627 /** Deleter needed to allow shared_ptr use with protected destructor. */
631 void operator()(EventSource *source) { delete source; }
633 friend class Deleter;
634 friend class Session;
635 friend class SourceCallbackData;
638 /** A virtual device associated with a stored session */
639 class SR_API SessionDevice :
640 public ParentOwned<SessionDevice, Session, struct sr_dev_inst>,
644 SessionDevice(struct sr_dev_inst *sdi);
646 shared_ptr<Device> get_shared_from_this();
647 /** Deleter needed to allow shared_ptr use with protected destructor. */
651 void operator()(SessionDevice *device) { delete device; }
653 friend class Deleter;
654 friend class Session;
657 /** A sigrok session */
658 class SR_API Session : public UserOwned<Session, struct sr_session>
661 /** Add a device to this session.
662 * @param device Device to add. */
663 void add_device(shared_ptr<Device> device);
664 /** List devices attached to this session. */
665 vector<shared_ptr<Device> > devices();
666 /** Remove all devices from this session. */
667 void remove_devices();
668 /** Add a datafeed callback to this session.
669 * @param callback Callback of the form callback(Device, Packet). */
670 void add_datafeed_callback(DatafeedCallbackFunction callback);
671 /** Remove all datafeed callbacks from this session. */
672 void remove_datafeed_callbacks();
673 /** Add an I/O event source.
674 * @param source EventSource to add. */
675 void add_source(shared_ptr<EventSource> source);
676 /** Remove an event source.
677 * @param source EventSource to remove. */
678 void remove_source(shared_ptr<EventSource> source);
679 /** Start the session. */
681 /** Run the session event loop. */
683 /** Stop the session. */
685 /** Begin saving session to a file.
686 * @param filename File name string. */
687 void begin_save(string filename);
688 /** Append a packet to the session file being saved.
689 * @param packet Packet to append. */
690 void append(shared_ptr<Packet> packet);
691 /** Append raw logic data to the session file being saved. */
692 void append(void *data, size_t length, unsigned int unit_size);
693 /** Get current trigger setting. */
694 shared_ptr<Trigger> trigger();
695 /** Set trigger setting.
696 * @param trigger Trigger object to use. */
697 void set_trigger(shared_ptr<Trigger> trigger);
698 /** Get filename this session was loaded from. */
701 Session(shared_ptr<Context> context);
702 Session(shared_ptr<Context> context, string filename);
704 shared_ptr<Device> get_device(const struct sr_dev_inst *sdi);
705 const shared_ptr<Context> _context;
706 map<const struct sr_dev_inst *, SessionDevice *> _owned_devices;
707 map<const struct sr_dev_inst *, shared_ptr<Device> > _other_devices;
708 vector<DatafeedCallbackData *> _datafeed_callbacks;
709 map<shared_ptr<EventSource>, SourceCallbackData *> _source_callbacks;
712 bool _save_initialized;
713 string _save_filename;
714 uint64_t _save_samplerate;
715 shared_ptr<Trigger> _trigger;
716 friend class Deleter;
717 friend class Context;
718 friend class DatafeedCallbackData;
719 friend class SessionDevice;
722 /** A packet on the session datafeed */
723 class SR_API Packet : public UserOwned<Packet, const struct sr_datafeed_packet>
726 /** Type of this packet. */
727 const PacketType *type();
728 /** Payload of this packet. */
729 shared_ptr<PacketPayload> payload();
731 Packet(shared_ptr<Device> device,
732 const struct sr_datafeed_packet *structure);
734 shared_ptr<Device> _device;
735 PacketPayload *_payload;
736 friend class Deleter;
737 friend class Session;
739 friend class DatafeedCallbackData;
746 /** Abstract base class for datafeed packet payloads */
747 class SR_API PacketPayload
751 virtual ~PacketPayload() = 0;
752 virtual shared_ptr<PacketPayload> get_shared_pointer(Packet *parent) = 0;
753 /** Deleter needed to allow shared_ptr use with protected destructor. */
757 void operator()(PacketPayload *payload) { delete payload; }
759 friend class Deleter;
764 /** Payload of a datafeed header packet */
765 class SR_API Header :
766 public ParentOwned<Header, Packet, const struct sr_datafeed_header>,
770 /* Feed version number. */
772 /* Start time of this session. */
773 Glib::TimeVal start_time();
775 Header(const struct sr_datafeed_header *structure);
777 shared_ptr<PacketPayload> get_shared_pointer(Packet *parent);
781 /** Payload of a datafeed metadata packet */
783 public ParentOwned<Meta, Packet, const struct sr_datafeed_meta>,
787 /* Mapping of (ConfigKey, value) pairs. */
788 map<const ConfigKey *, Glib::VariantBase> config();
790 Meta(const struct sr_datafeed_meta *structure);
792 shared_ptr<PacketPayload> get_shared_pointer(Packet *parent);
793 map<const ConfigKey *, Glib::VariantBase> _config;
797 /** Payload of a datafeed packet with logic data */
799 public ParentOwned<Logic, Packet, const struct sr_datafeed_logic>,
803 /* Pointer to data. */
804 void *data_pointer();
805 /* Data length in bytes. */
806 size_t data_length();
807 /* Size of each sample in bytes. */
808 unsigned int unit_size();
810 Logic(const struct sr_datafeed_logic *structure);
812 shared_ptr<PacketPayload> get_shared_pointer(Packet *parent);
816 /** Payload of a datafeed packet with analog data */
817 class SR_API Analog :
818 public ParentOwned<Analog, Packet, const struct sr_datafeed_analog>,
822 /** Pointer to data. */
823 float *data_pointer();
824 /** Number of samples in this packet. */
825 unsigned int num_samples();
826 /** Channels for which this packet contains data. */
827 vector<shared_ptr<Channel> > channels();
828 /** Measured quantity of the samples in this packet. */
829 const Quantity *mq();
830 /** Unit of the samples in this packet. */
832 /** Measurement flags associated with the samples in this packet. */
833 vector<const QuantityFlag *> mq_flags();
835 Analog(const struct sr_datafeed_analog *structure);
837 shared_ptr<PacketPayload> get_shared_pointer(Packet *parent);
841 /** An input format supported by the library */
842 class SR_API InputFormat :
843 public ParentOwned<InputFormat, Context, const struct sr_input_module>
846 /** Name of this input format. */
848 /** Description of this input format. */
849 string description();
850 /** Options supported by this input format. */
851 map<string, shared_ptr<Option> > options();
852 /** Create an input using this input format.
853 * @param options Mapping of (option name, value) pairs. */
854 shared_ptr<Input> create_input(map<string, Glib::VariantBase> options =
855 map<string, Glib::VariantBase>());
857 InputFormat(const struct sr_input_module *structure);
859 friend class Context;
860 friend class InputDevice;
863 /** An input instance (an input format applied to a file or stream) */
864 class SR_API Input : public UserOwned<Input, const struct sr_input>
867 /** Virtual device associated with this input. */
868 shared_ptr<InputDevice> device();
869 /** Send next stream data.
870 * @param data Next stream data. */
871 void send(string data);
872 /** Signal end of input data. */
875 Input(shared_ptr<Context> context, const struct sr_input *structure);
877 shared_ptr<Context> _context;
878 InputDevice *_device;
879 friend class Deleter;
880 friend class Context;
881 friend class InputFormat;
884 /** A virtual device associated with an input */
885 class SR_API InputDevice :
886 public ParentOwned<InputDevice, Input, struct sr_dev_inst>,
890 InputDevice(shared_ptr<Input> input, struct sr_dev_inst *sdi);
892 shared_ptr<Device> get_shared_from_this();
893 shared_ptr<Input> _input;
897 /** An option used by an output format */
898 class SR_API Option : public UserOwned<Option, const struct sr_option>
901 /** Short name of this option suitable for command line usage. */
903 /** Short name of this option suitable for GUI usage. */
905 /** Description of this option in a sentence. */
906 string description();
907 /** Default value for this option. */
908 Glib::VariantBase default_value();
909 /** Possible values for this option, if a limited set. */
910 vector<Glib::VariantBase> values();
912 Option(const struct sr_option *structure,
913 shared_ptr<const struct sr_option *> structure_array);
915 shared_ptr<const struct sr_option *> _structure_array;
916 friend class Deleter;
917 friend class InputFormat;
918 friend class OutputFormat;
921 /** An output format supported by the library */
922 class SR_API OutputFormat :
923 public ParentOwned<OutputFormat, Context, const struct sr_output_module>
926 /** Name of this output format. */
928 /** Description of this output format. */
929 string description();
930 /** Options supported by this output format. */
931 map<string, shared_ptr<Option> > options();
932 /** Create an output using this format.
933 * @param device Device to output for.
934 * @param options Mapping of (option name, value) pairs. */
935 shared_ptr<Output> create_output(shared_ptr<Device> device,
936 map<string, Glib::VariantBase> options =
937 map<string, Glib::VariantBase>());
939 OutputFormat(const struct sr_output_module *structure);
941 friend class Context;
945 /** An output instance (an output format applied to a device) */
946 class SR_API Output : public UserOwned<Output, const struct sr_output>
949 /** Update output with data from the given packet.
950 * @param packet Packet to handle. */
951 string receive(shared_ptr<Packet> packet);
953 Output(shared_ptr<OutputFormat> format, shared_ptr<Device> device);
954 Output(shared_ptr<OutputFormat> format,
955 shared_ptr<Device> device, map<string, Glib::VariantBase> options);
957 const shared_ptr<OutputFormat> _format;
958 const shared_ptr<Device> _device;
959 const map<string, Glib::VariantBase> _options;
960 friend class Deleter;
961 friend class OutputFormat;
964 /** Base class for objects which wrap an enumeration value from libsigrok */
965 template <class Class, typename Enum> class SR_API EnumValue
968 /** The integer constant associated with this value. */
971 return static_cast<int>(_id);
973 /** The name associated with this value. */
978 /** Get value associated with a given integer constant. */
979 static const Class *get(int id)
981 auto key = static_cast<Enum>(id);
982 if (_values.find(key) == _values.end())
983 throw Error(SR_ERR_ARG);
984 return _values.at(key);
986 /** Get possible values. */
987 static std::vector<const Class *> values()
989 std::vector<const Class *> result;
990 for (auto entry : _values)
991 result.push_back(entry.second);
995 EnumValue(Enum id, const char name[]) : _id(id), _name(name)
1001 static const std::map<const Enum, const Class * const> _values;
1006 #include "enums.hpp"
1010 #endif // LIBSIGROK_HPP