X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=bindings%2Fcxx%2Fclasses.cpp;h=1b53d0a795968aa79c5a64ed0f9be90b38af3f5b;hb=15914cdb0fc6c4c4e0acd410dd58177d0aa17fd2;hp=af10e8eaf7693e04b05206344c440ebb48e7809b;hpb=ee9953ef12514d833c6719777dfc285d56c0ffc1;p=libsigrok.git diff --git a/bindings/cxx/classes.cpp b/bindings/cxx/classes.cpp index af10e8ea..1b53d0a7 100644 --- a/bindings/cxx/classes.cpp +++ b/bindings/cxx/classes.cpp @@ -39,36 +39,33 @@ static void check(int result) } /** Helper function to obtain valid strings from possibly null input. */ -static const char *valid_string(const char *input) +static inline const char *valid_string(const char *input) { - if (input != NULL) - return input; - else - return ""; + return (input) ? input : ""; } /** Helper function to convert between map and GHashTable */ -static GHashTable *map_to_hash_variant(map input) +static GHashTable *map_to_hash_variant(const map &input) { - auto output = g_hash_table_new_full( - g_str_hash, g_str_equal, g_free, (GDestroyNotify) g_variant_unref); + auto output = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, + reinterpret_cast(&g_variant_unref)); for (auto entry : input) g_hash_table_insert(output, g_strdup(entry.first.c_str()), entry.second.gobj_copy()); - return output; + return output; } Error::Error(int result) : result(result) { } -const char *Error::what() const throw() +const char *Error::what() const noexcept { return sr_strerror(result); } -Error::~Error() throw() +Error::~Error() noexcept { } @@ -123,7 +120,7 @@ shared_ptr Context::create() Context::Context() : UserOwned(_structure), - _session(NULL) + _session(nullptr) { check(sr_init(&_structure)); @@ -213,20 +210,14 @@ void Context::set_log_level(const LogLevel *level) static int call_log_callback(void *cb_data, int loglevel, const char *format, va_list args) { - va_list args_copy; - va_copy(args_copy, args); - int length = vsnprintf(NULL, 0, format, args_copy); - va_end(args_copy); - char *buf = (char *) g_malloc(length + 1); - vsprintf(buf, format, args); - string message(buf, length); - g_free(buf); + const unique_ptr + message {g_strdup_vprintf(format, args), &g_free}; - LogCallbackFunction callback = *((LogCallbackFunction *) cb_data); + auto *const callback = static_cast(cb_data); try { - callback(LogLevel::get(loglevel), message); + (*callback)(LogLevel::get(loglevel), message.get()); } catch (Error e) { @@ -238,7 +229,7 @@ static int call_log_callback(void *cb_data, int loglevel, const char *format, va void Context::set_log_callback(LogCallbackFunction callback) { - _log_callback = callback; + _log_callback = move(callback); check(sr_log_callback_set(call_log_callback, &_log_callback)); } @@ -287,7 +278,7 @@ shared_ptr Context::create_header_packet(Glib::TimeVal start_time) } shared_ptr Context::create_meta_packet( - map config) + const map &config) { auto meta = g_new0(struct sr_datafeed_meta, 1); for (auto input : config) @@ -320,17 +311,21 @@ shared_ptr Context::create_logic_packet( } shared_ptr Context::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) { auto analog = g_new0(struct sr_datafeed_analog, 1); + auto meaning = g_new0(struct sr_analog_meaning, 1); + + analog->meaning = meaning; + for (auto channel : channels) - analog->channels = g_slist_append(analog->channels, channel->_structure); + meaning->channels = g_slist_append(meaning->channels, channel->_structure); analog->num_samples = num_samples; - analog->mq = mq->id(); - analog->unit = unit->id(); - analog->mqflags = QuantityFlag::mask_from_flags(mqflags); + meaning->mq = static_cast(mq->id()); + meaning->unit = static_cast(unit->id()); + meaning->mqflags = static_cast(QuantityFlag::mask_from_flags(mqflags)); analog->data = data_pointer; auto packet = g_new(struct sr_datafeed_packet, 1); packet->type = SR_DF_ANALOG; @@ -341,13 +336,13 @@ shared_ptr Context::create_analog_packet( shared_ptr Context::load_session(string filename) { return shared_ptr( - new Session(shared_from_this(), filename), Session::Deleter()); + new Session(shared_from_this(), move(filename)), Session::Deleter()); } shared_ptr Context::create_trigger(string name) { return shared_ptr( - new Trigger(shared_from_this(), name), Trigger::Deleter()); + new Trigger(shared_from_this(), move(name)), Trigger::Deleter()); } shared_ptr Context::open_file(string filename) @@ -373,21 +368,22 @@ shared_ptr Context::open_stream(string header) map Context::serials(shared_ptr driver) { - GSList *serial_list = sr_serial_list(driver ? driver->_structure : NULL); + GSList *serial_list = sr_serial_list(driver ? driver->_structure : nullptr); map serials; for (GSList *serial = serial_list; serial; serial = serial->next) { - struct sr_serial_port *port = (sr_serial_port *) serial->data; + auto *const port = static_cast(serial->data); serials[string(port->name)] = string(port->description); } - g_slist_free_full(serial_list, (GDestroyNotify)sr_serial_free); + g_slist_free_full(serial_list, + reinterpret_cast(&sr_serial_free)); return serials; } Driver::Driver(struct sr_dev_driver *structure) : ParentOwned(structure), - Configurable(structure, NULL, NULL), + Configurable(structure, nullptr, nullptr), _initialized(false) { } @@ -407,7 +403,7 @@ string Driver::long_name() } vector> Driver::scan( - map options) + const map &options) { /* Initialise the driver if not yet done. */ if (!_initialized) @@ -417,7 +413,7 @@ vector> Driver::scan( } /* Translate scan options to GSList of struct sr_config pointers. */ - GSList *option_list = NULL; + GSList *option_list = nullptr; for (auto entry : options) { auto key = entry.first; @@ -439,7 +435,7 @@ vector> Driver::scan( vector> result; for (GSList *device = device_list; device; device = device->next) { - auto sdi = (struct sr_dev_inst *) device->data; + auto *const sdi = static_cast(device->data); result.push_back(shared_ptr( new HardwareDevice(shared_from_this(), sdi), HardwareDevice::Deleter())); @@ -474,11 +470,11 @@ Glib::VariantBase Configurable::config_get(const ConfigKey *key) return Glib::VariantBase(data); } -void Configurable::config_set(const ConfigKey *key, Glib::VariantBase value) +void Configurable::config_set(const ConfigKey *key, const Glib::VariantBase &value) { check(sr_config_set( config_sdi, config_channel_group, - key->id(), value.gobj())); + key->id(), const_cast(value.gobj()))); } Glib::VariantContainerBase Configurable::config_list(const ConfigKey *key) @@ -501,8 +497,8 @@ map> Configurable::config_keys(const ConfigKe config_driver, config_sdi, config_channel_group, key->id(), &gvar_opts)); - opts = (const uint32_t *) g_variant_get_fixed_array( - gvar_opts, &num_opts, sizeof(uint32_t)); + opts = static_cast(g_variant_get_fixed_array( + gvar_opts, &num_opts, sizeof(uint32_t))); for (gsize i = 0; i < num_opts; i++) { @@ -533,12 +529,12 @@ bool Configurable::config_check(const ConfigKey *key, index_key->id(), &gvar_opts) != SR_OK) return false; - opts = (const uint32_t *) g_variant_get_fixed_array( - gvar_opts, &num_opts, sizeof(uint32_t)); + opts = static_cast(g_variant_get_fixed_array( + gvar_opts, &num_opts, sizeof(uint32_t))); for (gsize i = 0; i < num_opts; i++) { - if ((opts[i] & SR_CONF_MASK) == (uint32_t) key->id()) + if ((opts[i] & SR_CONF_MASK) == unsigned(key->id())) { g_variant_unref(gvar_opts); return true; @@ -551,18 +547,18 @@ bool Configurable::config_check(const ConfigKey *key, } Device::Device(struct sr_dev_inst *structure) : - Configurable(sr_dev_inst_driver_get(structure), structure, NULL), + Configurable(sr_dev_inst_driver_get(structure), structure, nullptr), _structure(structure) { for (GSList *entry = sr_dev_inst_channels_get(structure); entry; entry = entry->next) { - auto channel = (struct sr_channel *) entry->data; + auto *const channel = static_cast(entry->data); _channels[channel] = new Channel(channel); } for (GSList *entry = sr_dev_inst_channel_groups_get(structure); entry; entry = entry->next) { - auto group = (struct sr_channel_group *) entry->data; + auto *const group = static_cast(entry->data); _channel_groups[group->name] = new ChannelGroup(this, group); } } @@ -603,10 +599,10 @@ string Device::connection_id() vector> Device::channels() { vector> result; - for (auto channel = sr_dev_inst_channels_get(_structure); channel; channel = channel->next) - result.push_back( - _channels[(struct sr_channel *) channel->data]->get_shared_pointer( - get_shared_from_this())); + for (auto channel = sr_dev_inst_channels_get(_structure); channel; channel = channel->next) { + auto *const ch = static_cast(channel->data); + result.push_back(_channels[ch]->get_shared_pointer(get_shared_from_this())); + } return result; } @@ -642,7 +638,7 @@ HardwareDevice::HardwareDevice(shared_ptr driver, struct sr_dev_inst *structure) : UserOwned(structure), Device(structure), - _driver(driver) + _driver(move(driver)) { } @@ -681,11 +677,10 @@ shared_ptr UserDevice::add_channel(unsigned int index, { check(sr_dev_inst_channel_add(Device::_structure, index, type->id(), name.c_str())); - struct sr_channel *structure = (struct sr_channel *) - g_slist_last(sr_dev_inst_channels_get(Device::_structure))->data; - Channel *channel = new Channel(structure); - _channels[structure] = channel; - return get_channel(structure); + GSList *const last = g_slist_last(sr_dev_inst_channels_get(Device::_structure)); + auto *const ch = static_cast(last->data); + _channels[ch] = new Channel(ch); + return get_channel(ch); } Channel::Channel(struct sr_channel *structure) : @@ -733,8 +728,10 @@ ChannelGroup::ChannelGroup(Device *device, ParentOwned(structure), Configurable(sr_dev_inst_driver_get(device->_structure), device->_structure, structure) { - for (GSList *entry = structure->channels; entry; entry = entry->next) - _channels.push_back(device->_channels[(struct sr_channel *)entry->data]); + for (GSList *entry = structure->channels; entry; entry = entry->next) { + auto *const ch = static_cast(entry->data); + _channels.push_back(device->_channels[ch]); + } } ChannelGroup::~ChannelGroup() @@ -756,11 +753,11 @@ vector> ChannelGroup::channels() Trigger::Trigger(shared_ptr context, string name) : UserOwned(sr_trigger_new(name.c_str())), - _context(context) + _context(move(context)) { for (auto stage = _structure->stages; stage; stage = stage->next) _stages.push_back( - new TriggerStage((struct sr_trigger_stage *) stage->data)); + new TriggerStage(static_cast(stage->data))); } Trigger::~Trigger() @@ -820,21 +817,22 @@ void TriggerStage::add_match(shared_ptr channel, { check(sr_trigger_match_add(_structure, channel->_structure, type->id(), value)); + GSList *const last = g_slist_last(_structure->matches); _matches.push_back(new TriggerMatch( - (struct sr_trigger_match *) g_slist_last( - _structure->matches)->data, channel)); + static_cast(last->data), + move(channel))); } void TriggerStage::add_match(shared_ptr channel, const TriggerMatchType *type) { - add_match(channel, type, NAN); + add_match(move(channel), type, NAN); } TriggerMatch::TriggerMatch(struct sr_trigger_match *structure, shared_ptr channel) : ParentOwned(structure), - _channel(channel) + _channel(move(channel)) { } @@ -859,7 +857,7 @@ float TriggerMatch::value() DatafeedCallbackData::DatafeedCallbackData(Session *session, DatafeedCallbackFunction callback) : - _callback(callback), + _callback(move(callback)), _session(session) { } @@ -869,7 +867,7 @@ void DatafeedCallbackData::run(const struct sr_dev_inst *sdi, { auto device = _session->get_device(sdi); auto packet = shared_ptr(new Packet(device, pkt), Packet::Deleter()); - _callback(device, packet); + _callback(move(device), move(packet)); } SessionDevice::SessionDevice(struct sr_dev_inst *structure) : @@ -889,23 +887,22 @@ shared_ptr SessionDevice::get_shared_from_this() Session::Session(shared_ptr context) : UserOwned(_structure), - _context(context) + _context(move(context)) { - check(sr_session_new(context->_structure, &_structure)); + check(sr_session_new(_context->_structure, &_structure)); _context->_session = this; } Session::Session(shared_ptr context, string filename) : UserOwned(_structure), - _context(context), - _filename(filename) + _context(move(context)), + _filename(move(filename)) { - check(sr_session_load(context->_structure, filename.c_str(), &_structure)); + check(sr_session_load(_context->_structure, _filename.c_str(), &_structure)); GSList *dev_list; check(sr_session_dev_list(_structure, &dev_list)); - for (GSList *dev = dev_list; dev; dev = dev->next) - { - auto sdi = (struct sr_dev_inst *) dev->data; + for (GSList *dev = dev_list; dev; dev = dev->next) { + auto *const sdi = static_cast(dev->data); _owned_devices[sdi] = new SessionDevice(sdi); } _context->_session = this; @@ -935,8 +932,9 @@ shared_ptr Session::get_device(const struct sr_dev_inst *sdi) void Session::add_device(shared_ptr device) { - check(sr_session_dev_add(_structure, device->_structure)); - _other_devices[device->_structure] = device; + const auto dev_struct = device->_structure; + check(sr_session_dev_add(_structure, dev_struct)); + _other_devices[dev_struct] = move(device); } vector> Session::devices() @@ -944,9 +942,8 @@ vector> Session::devices() GSList *dev_list; check(sr_session_dev_list(_structure, &dev_list)); vector> result; - for (GSList *dev = dev_list; dev; dev = dev->next) - { - auto sdi = (struct sr_dev_inst *) dev->data; + for (GSList *dev = dev_list; dev; dev = dev->next) { + auto *const sdi = static_cast(dev->data); result.push_back(get_device(sdi)); } return result; @@ -1004,16 +1001,16 @@ static void datafeed_callback(const struct sr_dev_inst *sdi, auto callback = static_cast(cb_data); callback->run(sdi, pkt); } - + void Session::add_datafeed_callback(DatafeedCallbackFunction callback) { - auto cb_data = new DatafeedCallbackData(this, callback); + auto cb_data = new DatafeedCallbackData(this, move(callback)); check(sr_session_datafeed_callback_add(_structure, datafeed_callback, cb_data)); _datafeed_callbacks.push_back(cb_data); } -void Session::remove_datafeed_callbacks(void) +void Session::remove_datafeed_callbacks() { check(sr_session_datafeed_callback_remove_all(_structure)); for (auto callback : _datafeed_callbacks) @@ -1030,10 +1027,10 @@ void Session::set_trigger(shared_ptr trigger) { if (!trigger) // Set NULL trigger, i.e. remove any trigger from the session. - check(sr_session_trigger_set(_structure, NULL)); + check(sr_session_trigger_set(_structure, nullptr)); else check(sr_session_trigger_set(_structure, trigger->_structure)); - _trigger = trigger; + _trigger = move(trigger); } string Session::filename() @@ -1049,7 +1046,7 @@ shared_ptr Session::context() Packet::Packet(shared_ptr device, const struct sr_datafeed_packet *structure) : UserOwned(structure), - _device(device) + _device(move(device)) { switch (structure->type) { @@ -1153,9 +1150,8 @@ shared_ptr Meta::get_shared_pointer(Packet *_parent) map Meta::config() { map result; - for (auto l = _structure->config; l; l = l->next) - { - auto config = (struct sr_config *) l->data; + for (auto l = _structure->config; l; l = l->next) { + auto *const config = static_cast(l->data); result[ConfigKey::get(config->key)] = Glib::VariantBase(config->data); } return result; @@ -1208,7 +1204,7 @@ shared_ptr Analog::get_shared_pointer(Packet *_parent) ParentOwned::get_shared_pointer(_parent)); } -float *Analog::data_pointer() +void *Analog::data_pointer() { return _structure->data; } @@ -1221,25 +1217,26 @@ unsigned int Analog::num_samples() vector> Analog::channels() { vector> result; - for (auto l = _structure->channels; l; l = l->next) - result.push_back(_parent->_device->get_channel( - (struct sr_channel *)l->data)); + for (auto l = _structure->meaning->channels; l; l = l->next) { + auto *const ch = static_cast(l->data); + result.push_back(_parent->_device->get_channel(ch)); + } return result; } const Quantity *Analog::mq() { - return Quantity::get(_structure->mq); + return Quantity::get(_structure->meaning->mq); } const Unit *Analog::unit() { - return Unit::get(_structure->unit); + return Unit::get(_structure->meaning->unit); } vector Analog::mq_flags() { - return QuantityFlag::flags_from_mask(_structure->mqflags); + return QuantityFlag::flags_from_mask(_structure->meaning->mqflags); } InputFormat::InputFormat(const struct sr_input_module *structure) : @@ -1286,7 +1283,7 @@ map> InputFormat::options() } shared_ptr InputFormat::create_input( - map options) + const map &options) { auto input = sr_input_new(_structure, map_to_hash_variant(options)); if (!input) @@ -1297,7 +1294,7 @@ shared_ptr InputFormat::create_input( Input::Input(shared_ptr context, const struct sr_input *structure) : UserOwned(structure), - _context(context), + _context(move(context)), _device(nullptr) { } @@ -1317,7 +1314,7 @@ shared_ptr Input::device() void Input::send(void *data, size_t length) { - auto gstr = g_string_new_len((gchar *)data, length); + auto gstr = g_string_new_len(static_cast(data), length); auto ret = sr_input_send(_structure, gstr); g_string_free(gstr, false); check(ret); @@ -1339,7 +1336,7 @@ InputDevice::InputDevice(shared_ptr input, struct sr_dev_inst *structure) : ParentOwned(structure), Device(structure), - _input(input) + _input(move(input)) { } @@ -1355,7 +1352,7 @@ shared_ptr InputDevice::get_shared_from_this() Option::Option(const struct sr_option *structure, shared_ptr structure_array) : UserOwned(structure), - _structure_array(structure_array) + _structure_array(move(structure_array)) { } @@ -1386,8 +1383,10 @@ Glib::VariantBase Option::default_value() vector Option::values() { vector result; - for (auto l = _structure->values; l; l = l->next) - result.push_back(Glib::VariantBase((GVariant *) l->data, true)); + for (auto l = _structure->values; l; l = l->next) { + auto *const var = static_cast(l->data); + result.push_back(Glib::VariantBase(var, true)); + } return result; } @@ -1435,18 +1434,18 @@ map> OutputFormat::options() } shared_ptr OutputFormat::create_output( - shared_ptr device, map options) + shared_ptr device, const map &options) { return shared_ptr( - new Output(shared_from_this(), device, options), + new Output(shared_from_this(), move(device), options), Output::Deleter()); } shared_ptr OutputFormat::create_output(string filename, - shared_ptr device, map options) + shared_ptr device, const map &options) { return shared_ptr( - new Output(filename, shared_from_this(), device, options), + new Output(move(filename), shared_from_this(), move(device), options), Output::Deleter()); } @@ -1456,21 +1455,21 @@ bool OutputFormat::test_flag(const OutputFlag *flag) } Output::Output(shared_ptr format, - shared_ptr device, map options) : + shared_ptr device, const map &options) : UserOwned(sr_output_new(format->_structure, - map_to_hash_variant(options), device->_structure, NULL)), - _format(format), - _device(device), + map_to_hash_variant(options), device->_structure, nullptr)), + _format(move(format)), + _device(move(device)), _options(options) { } Output::Output(string filename, shared_ptr format, - shared_ptr device, map options) : + shared_ptr device, const map &options) : UserOwned(sr_output_new(format->_structure, map_to_hash_variant(options), device->_structure, filename.c_str())), - _format(format), - _device(device), + _format(move(format)), + _device(move(device)), _options(options) { }