X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=bindings%2Fcxx%2Finclude%2Flibsigrokcxx%2Flibsigrokcxx.hpp;h=a83600aa99753d9b4a5baf83ce95a4c4b7de5eaa;hb=21d1bec60e6d69f6f2ef6f3dc3e07219153285c0;hp=2441c2c37b9933f3f602498bf422d79f0938645c;hpb=f91cf612dfde284c8a146417644fbfba05cca1a2;p=libsigrok.git diff --git a/bindings/cxx/include/libsigrokcxx/libsigrokcxx.hpp b/bindings/cxx/include/libsigrokcxx/libsigrokcxx.hpp index 2441c2c3..a83600aa 100644 --- a/bindings/cxx/include/libsigrokcxx/libsigrokcxx.hpp +++ b/bindings/cxx/include/libsigrokcxx/libsigrokcxx.hpp @@ -91,7 +91,6 @@ class SR_API Driver; class SR_API Device; class SR_API HardwareDevice; class SR_API Channel; -class SR_API EventSource; class SR_API Session; class SR_API ConfigKey; class SR_API InputFormat; @@ -121,16 +120,27 @@ class SR_API UserDevice; class SR_API Error: public exception { public: - Error(int result); - ~Error() throw(); + explicit Error(int result); + ~Error() noexcept; const int result; - const char *what() const throw(); + const char *what() const noexcept; }; /* Base template for classes whose resources are owned by a parent object. */ template class SR_API ParentOwned { +private: + /* Weak pointer for shared_from_this() implementation. */ + weak_ptr _weak_this; + + static void reset_parent(Class *object) + { + if (!object->_parent) + throw Error(SR_ERR_BUG); + object->_parent.reset(); + } + protected: /* Parent object which owns this child object's underlying structure. @@ -147,8 +157,12 @@ protected: references to both the parent and all its children are gone. */ shared_ptr _parent; - /* Weak pointer for shared_from_this() implementation. */ - weak_ptr _weak_this; + Struct *_structure; + + explicit ParentOwned(Struct *structure) : + _structure(structure) + { + } public: /* Get parent object that owns this object. */ @@ -164,7 +178,7 @@ public: if (!(shared = _weak_this.lock())) { - shared = shared_ptr((Class *) this, reset_parent); + shared = shared_ptr(static_cast(this), reset_parent); _weak_this = shared; } @@ -185,20 +199,6 @@ public: throw Error(SR_ERR_BUG); return get_shared_pointer(parent->shared_from_this()); } -protected: - static void reset_parent(Class *object) - { - if (!object->_parent) - throw Error(SR_ERR_BUG); - object->_parent.reset(); - } - - Struct *_structure; - - ParentOwned(Struct *structure) : - _structure(structure) - { - } }; /* Base template for classes whose resources are owned by the user. */ @@ -216,7 +216,7 @@ public: protected: Struct *_structure; - UserOwned(Struct *structure) : + explicit UserOwned(Struct *structure) : _structure(structure) { } @@ -247,11 +247,11 @@ private: virtual size_t read(const struct sr_resource *res, void *buf, size_t count) = 0; static SR_PRIV int open_callback(struct sr_resource *res, - const char *name, void *cb_data); + const char *name, void *cb_data) noexcept; static SR_PRIV int close_callback(struct sr_resource *res, - void *cb_data); + void *cb_data) noexcept; static SR_PRIV ssize_t read_callback(const struct sr_resource *res, - void *buf, size_t count, void *cb_data); + void *buf, size_t count, void *cb_data) noexcept; friend class Context; }; @@ -262,9 +262,9 @@ public: /** Create new context */ static shared_ptr create(); /** libsigrok package version. */ - string package_version(); + static string package_version(); /** libsigrok library version. */ - string lib_version(); + static string lib_version(); /** Available hardware drivers, indexed by name. */ map > drivers(); /** Available input formats, indexed by name. */ @@ -272,7 +272,7 @@ public: /** Available output formats, indexed by name. */ map > output_formats(); /** Current log level. */ - const LogLevel *log_level(); + const LogLevel *log_level() const; /** Set the log level. * @param level LogLevel to use. */ void set_log_level(const LogLevel *level); @@ -293,15 +293,15 @@ public: shared_ptr create_header_packet(Glib::TimeVal start_time); /** Create a meta packet. */ shared_ptr create_meta_packet( - map config); + const map &config); /** Create a logic packet. */ shared_ptr create_logic_packet( void *data_pointer, size_t data_length, unsigned int unit_size); /** Create an analog packet. */ shared_ptr create_analog_packet( - vector > channels, + const vector > &channels, float *data_pointer, unsigned int num_samples, const Quantity *mq, - const Unit *unit, vector mqflags); + const Unit *unit, const vector &mqflags); /** Load a saved session. * @param filename File name string. */ shared_ptr load_session(string filename); @@ -314,8 +314,8 @@ public: /** Open an input stream based on header data. * @param header Initial data from stream. */ shared_ptr open_stream(string header); - map serials(shared_ptr driver); -protected: + map serials(shared_ptr driver) const; +private: map _drivers; map _input_formats; map _output_formats; @@ -340,18 +340,18 @@ class SR_API Configurable public: /** Read configuration for the given key. * @param key ConfigKey to read. */ - Glib::VariantBase config_get(const ConfigKey *key); + Glib::VariantBase config_get(const ConfigKey *key) const; /** Set configuration for the given key to a specified value. * @param key ConfigKey to set. * @param value Value to set. */ - void config_set(const ConfigKey *key, Glib::VariantBase value); + void config_set(const ConfigKey *key, const Glib::VariantBase &value); /** Enumerate available values for the given configuration key. * @param key ConfigKey to enumerate values for. */ - Glib::VariantContainerBase config_list(const ConfigKey *key); + Glib::VariantContainerBase config_list(const ConfigKey *key) const; /** Enumerate available keys, according to a given index key. */ map > config_keys(const ConfigKey *key); /** Check for a key in the list from a given index key. */ - bool config_check(const ConfigKey *key, const ConfigKey *index_key); + bool config_check(const ConfigKey *key, const ConfigKey *index_key) const; protected: Configurable( struct sr_dev_driver *driver, @@ -370,18 +370,17 @@ class SR_API Driver : { public: /** Name of this driver. */ - string name(); + string name() const; /** Long name for this driver. */ - string long_name(); + string long_name() const; /** Scan for devices and return a list of devices found. * @param options Mapping of (ConfigKey, value) pairs. */ vector > scan( - map options = - map()); -protected: + const map &options = {}); +private: bool _initialized; vector _devices; - Driver(struct sr_dev_driver *structure); + explicit Driver(struct sr_dev_driver *structure); ~Driver(); friend class Context; friend class HardwareDevice; @@ -393,15 +392,15 @@ class SR_API Device : public Configurable { public: /** Vendor name for this device. */ - string vendor(); + string vendor() const; /** Model name for this device. */ - string model(); + string model() const; /** Version string for this device. */ - string version(); + string version() const; /** Serial number for this device. */ - string serial_number(); + string serial_number() const; /** Connection ID for this device. */ - string connection_id(); + string connection_id() const; /** List of the channels available on this device. */ vector > channels(); /** Channel groups available on this device, indexed by name. */ @@ -411,12 +410,14 @@ public: /** Close device. */ void close(); protected: - Device(struct sr_dev_inst *structure); + explicit Device(struct sr_dev_inst *structure); ~Device(); virtual shared_ptr get_shared_from_this() = 0; shared_ptr get_channel(struct sr_channel *ptr); + struct sr_dev_inst *_structure; map _channels; +private: map _channel_groups; /** Deleter needed to allow shared_ptr use with protected destructor. */ class Deleter @@ -440,7 +441,7 @@ class SR_API HardwareDevice : public: /** Driver providing this device. */ shared_ptr driver(); -protected: +private: HardwareDevice(shared_ptr driver, struct sr_dev_inst *structure); ~HardwareDevice(); shared_ptr get_shared_from_this(); @@ -464,7 +465,7 @@ class SR_API UserDevice : public: /** Add a new channel to this device. */ shared_ptr add_channel(unsigned int index, const ChannelType *type, string name); -protected: +private: UserDevice(string vendor, string model, string version); ~UserDevice(); shared_ptr get_shared_from_this(); @@ -484,21 +485,21 @@ class SR_API Channel : { public: /** Current name of this channel. */ - string name(); + string name() const; /** Set the name of this channel. * * @param name Name string to set. */ void set_name(string name); /** Type of this channel. */ - const ChannelType *type(); + const ChannelType *type() const; /** Enabled status of this channel. */ - bool enabled(); + bool enabled() const; /** Set the enabled status of this channel. * @param value Boolean value to set. */ void set_enabled(bool value); /** Get the index number of this channel. */ - unsigned int index(); -protected: - Channel(struct sr_channel *structure); + unsigned int index() const; +private: + explicit Channel(struct sr_channel *structure); ~Channel(); const ChannelType * const _type; friend class Device; @@ -516,10 +517,10 @@ class SR_API ChannelGroup : { public: /** Name of this channel group. */ - string name(); + string name() const; /** List of the channels in this group. */ vector > channels(); -protected: +private: ChannelGroup(Device *device, struct sr_channel_group *structure); ~ChannelGroup(); vector _channels; @@ -531,12 +532,12 @@ class SR_API Trigger : public UserOwned { public: /** Name of this trigger configuration. */ - string name(); + string name() const; /** List of the stages in this trigger. */ vector > stages(); /** Add a new stage to this trigger. */ shared_ptr add_stage(); -protected: +private: Trigger(shared_ptr context, string name); ~Trigger(); shared_ptr _context; @@ -552,7 +553,7 @@ class SR_API TriggerStage : { public: /** Index number of this stage. */ - int number(); + int number() const; /** List of match conditions on this stage. */ vector > matches(); /** Add a new match condition to this stage. @@ -564,9 +565,9 @@ public: * @param type TriggerMatchType to apply. * @param value Threshold value. */ void add_match(shared_ptr channel, const TriggerMatchType *type, float value); -protected: +private: vector _matches; - TriggerStage(struct sr_trigger_stage *structure); + explicit TriggerStage(struct sr_trigger_stage *structure); ~TriggerStage(); friend class Trigger; }; @@ -579,10 +580,10 @@ public: /** Channel this condition matches on. */ shared_ptr channel(); /** Type of match. */ - const TriggerMatchType *type(); + const TriggerMatchType *type() const; /** Threshold value. */ - float value(); -protected: + float value() const; +private: TriggerMatch(struct sr_trigger_match *structure, shared_ptr channel); ~TriggerMatch(); shared_ptr _channel; @@ -602,7 +603,7 @@ class SR_PRIV DatafeedCallbackData public: void run(const struct sr_dev_inst *sdi, const struct sr_datafeed_packet *pkt); -protected: +private: DatafeedCallbackFunction _callback; DatafeedCallbackData(Session *session, DatafeedCallbackFunction callback); @@ -610,78 +611,13 @@ protected: friend class Session; }; -/** Type of source callback */ -typedef function - SourceCallbackFunction; - -/* Data required for C callback function to call a C++ source callback */ -class SR_PRIV SourceCallbackData -{ -public: - bool run(int revents); -protected: - SourceCallbackData(shared_ptr source); - shared_ptr _source; - friend class Session; -}; - -/** An I/O event source */ -class SR_API EventSource -{ -public: - /** Create an event source from a file descriptor. - * @param fd File descriptor. - * @param events GLib IOCondition event mask. - * @param timeout Timeout in milliseconds. - * @param callback Callback of the form callback(events) */ - static shared_ptr create(int fd, Glib::IOCondition events, - int timeout, SourceCallbackFunction callback); - /** Create an event source from a GLib PollFD - * @param pollfd GLib PollFD - * @param timeout Timeout in milliseconds. - * @param callback Callback of the form callback(events) */ - static shared_ptr create(Glib::PollFD pollfd, int timeout, - SourceCallbackFunction callback); - /** Create an event source from a GLib IOChannel - * @param channel GLib IOChannel. - * @param events GLib IOCondition event mask. - * @param timeout Timeout in milliseconds. - * @param callback Callback of the form callback(events) */ - static shared_ptr create( - Glib::RefPtr channel, Glib::IOCondition events, - int timeout, SourceCallbackFunction callback); -protected: - EventSource(int timeout, SourceCallbackFunction callback); - ~EventSource(); - enum source_type { - SOURCE_FD, - SOURCE_POLLFD, - SOURCE_IOCHANNEL - } _type; - int _fd; - Glib::PollFD _pollfd; - Glib::RefPtr _channel; - Glib::IOCondition _events; - int _timeout; - SourceCallbackFunction _callback; - /** Deleter needed to allow shared_ptr use with protected destructor. */ - class Deleter - { - public: - void operator()(EventSource *source) { delete source; } - }; - friend class Deleter; - friend class Session; - friend class SourceCallbackData; -}; - /** A virtual device associated with a stored session */ class SR_API SessionDevice : public ParentOwned, public Device { -protected: - SessionDevice(struct sr_dev_inst *sdi); +private: + explicit SessionDevice(struct sr_dev_inst *sdi); ~SessionDevice(); shared_ptr get_shared_from_this(); /** Deleter needed to allow shared_ptr use with protected destructor. */ @@ -710,12 +646,6 @@ public: void add_datafeed_callback(DatafeedCallbackFunction callback); /** Remove all datafeed callbacks from this session. */ void remove_datafeed_callbacks(); - /** Add an I/O event source. - * @param source EventSource to add. */ - void add_source(shared_ptr source); - /** Remove an event source. - * @param source EventSource to remove. */ - void remove_source(shared_ptr source); /** Start the session. */ void start(); /** Run the session event loop. */ @@ -734,9 +664,9 @@ public: * @param trigger Trigger object to use. */ void set_trigger(shared_ptr trigger); /** Get filename this session was loaded from. */ - string filename(); -protected: - Session(shared_ptr context); + string filename() const; +private: + explicit Session(shared_ptr context); Session(shared_ptr context, string filename); ~Session(); shared_ptr get_device(const struct sr_dev_inst *sdi); @@ -745,7 +675,6 @@ protected: map > _other_devices; vector _datafeed_callbacks; SessionStoppedCallback _stopped_callback; - map, SourceCallbackData *> _source_callbacks; string _filename; shared_ptr _trigger; friend class Deleter; @@ -759,10 +688,10 @@ class SR_API Packet : public UserOwned { public: /** Type of this packet. */ - const PacketType *type(); + const PacketType *type() const; /** Payload of this packet. */ shared_ptr payload(); -protected: +private: Packet(shared_ptr device, const struct sr_datafeed_packet *structure); ~Packet(); @@ -786,12 +715,7 @@ protected: PacketPayload(); virtual ~PacketPayload() = 0; virtual shared_ptr get_shared_pointer(Packet *parent) = 0; - /** Deleter needed to allow shared_ptr use with protected destructor. */ - class Deleter - { - public: - void operator()(PacketPayload *payload) { delete payload; } - }; + friend class Deleter; friend class Packet; friend class Output; @@ -804,11 +728,11 @@ class SR_API Header : { public: /* Feed version number. */ - int feed_version(); + int feed_version() const; /* Start time of this session. */ - Glib::TimeVal start_time(); -protected: - Header(const struct sr_datafeed_header *structure); + Glib::TimeVal start_time() const; +private: + explicit Header(const struct sr_datafeed_header *structure); ~Header(); shared_ptr get_shared_pointer(Packet *parent); friend class Packet; @@ -821,9 +745,9 @@ class SR_API Meta : { public: /* Mapping of (ConfigKey, value) pairs. */ - map config(); -protected: - Meta(const struct sr_datafeed_meta *structure); + map config() const; +private: + explicit Meta(const struct sr_datafeed_meta *structure); ~Meta(); shared_ptr get_shared_pointer(Packet *parent); map _config; @@ -839,11 +763,11 @@ public: /* Pointer to data. */ void *data_pointer(); /* Data length in bytes. */ - size_t data_length(); + size_t data_length() const; /* Size of each sample in bytes. */ - unsigned int unit_size(); -protected: - Logic(const struct sr_datafeed_logic *structure); + unsigned int unit_size() const; +private: + explicit Logic(const struct sr_datafeed_logic *structure); ~Logic(); shared_ptr get_shared_pointer(Packet *parent); friend class Packet; @@ -856,19 +780,19 @@ class SR_API Analog : { public: /** Pointer to data. */ - float *data_pointer(); + void *data_pointer(); /** Number of samples in this packet. */ - unsigned int num_samples(); + unsigned int num_samples() const; /** Channels for which this packet contains data. */ vector > channels(); /** Measured quantity of the samples in this packet. */ - const Quantity *mq(); + const Quantity *mq() const; /** Unit of the samples in this packet. */ - const Unit *unit(); + const Unit *unit() const; /** Measurement flags associated with the samples in this packet. */ - vector mq_flags(); -protected: - Analog(const struct sr_datafeed_analog *structure); + vector mq_flags() const; +private: + explicit Analog(const struct sr_datafeed_analog *structure); ~Analog(); shared_ptr get_shared_pointer(Packet *parent); friend class Packet; @@ -880,20 +804,19 @@ class SR_API InputFormat : { public: /** Name of this input format. */ - string name(); + string name() const; /** Description of this input format. */ - string description(); + string description() const; /** A list of preferred file name extensions for this file format. * @note This list is a recommendation only. */ - vector extensions(); + vector extensions() const; /** Options supported by this input format. */ map > options(); /** Create an input using this input format. * @param options Mapping of (option name, value) pairs. */ - shared_ptr create_input(map options = - map()); -protected: - InputFormat(const struct sr_input_module *structure); + shared_ptr create_input(const map &options = {}); +private: + explicit InputFormat(const struct sr_input_module *structure); ~InputFormat(); friend class Context; friend class InputDevice; @@ -911,7 +834,7 @@ public: void send(void *data, size_t length); /** Signal end of input data. */ void end(); -protected: +private: Input(shared_ptr context, const struct sr_input *structure); ~Input(); shared_ptr _context; @@ -926,7 +849,7 @@ class SR_API InputDevice : public ParentOwned, public Device { -protected: +private: InputDevice(shared_ptr input, struct sr_dev_inst *sdi); ~InputDevice(); shared_ptr get_shared_from_this(); @@ -939,16 +862,16 @@ class SR_API Option : public UserOwned { public: /** Short name of this option suitable for command line usage. */ - string id(); + string id() const; /** Short name of this option suitable for GUI usage. */ - string name(); + string name() const; /** Description of this option in a sentence. */ - string description(); + string description() const; /** Default value for this option. */ - Glib::VariantBase default_value(); + Glib::VariantBase default_value() const; /** Possible values for this option, if a limited set. */ - vector values(); -protected: + vector values() const; +private: Option(const struct sr_option *structure, shared_ptr structure_array); ~Option(); @@ -964,12 +887,12 @@ class SR_API OutputFormat : { public: /** Name of this output format. */ - string name(); + string name() const; /** Description of this output format. */ - string description(); + string description() const; /** A list of preferred file name extensions for this file format. * @note This list is a recommendation only. */ - vector extensions(); + vector extensions() const; /** Options supported by this output format. */ map > options(); /** Create an output using this format. @@ -977,25 +900,23 @@ public: * @param options Mapping of (option name, value) pairs. */ shared_ptr create_output( shared_ptr device, - map options = - map()); + const map &options = {}); /** Create an output using this format. * @param filename Name of destination file. * @param device Device to output for. * @param options Mapping of (option name, value) pairs. */ shared_ptr create_output(string filename, shared_ptr device, - map options = - map()); + const map &options = {}); /** * Checks whether a given flag is set. * @param flag Flag to check * @return true if flag is set for this module * @see sr_output_flags */ - bool test_flag(const OutputFlag *flag); -protected: - OutputFormat(const struct sr_output_module *structure); + bool test_flag(const OutputFlag *flag) const; +private: + explicit OutputFormat(const struct sr_output_module *structure); ~OutputFormat(); friend class Context; friend class Output; @@ -1008,12 +929,12 @@ public: /** Update output with data from the given packet. * @param packet Packet to handle. */ string receive(shared_ptr packet); -protected: +private: Output(shared_ptr format, shared_ptr device); Output(shared_ptr format, - shared_ptr device, map options); + shared_ptr device, const map &options); Output(string filename, shared_ptr format, - shared_ptr device, map options); + shared_ptr device, const map &options); ~Output(); const shared_ptr _format; const shared_ptr _device; @@ -1039,10 +960,10 @@ public: /** Get value associated with a given integer constant. */ static const Class *get(int id) { - auto key = static_cast(id); - if (_values.find(key) == _values.end()) + const auto pos = _values.find(static_cast(id)); + if (pos == _values.end()) throw Error(SR_ERR_ARG); - return _values.at(key); + return pos->second; } /** Get possible values. */ static std::vector values() @@ -1059,6 +980,7 @@ protected: ~EnumValue() { } +private: static const std::map _values; const Enum _id; const string _name;