From: Daniel Elstner Date: Sun, 11 Oct 2015 01:12:02 +0000 (+0200) Subject: C++: Use move() and avoid passing containers by value X-Git-Tag: libsigrok-0.4.0~182 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=d370545d6071690e7a8cc747db1134ced091e0de;p=libsigrok.git C++: Use move() and avoid passing containers by value Make use of std::move() to transfer arguments passed in by value. Take complex container arguments by const reference, as passing those by value is rather unorthodox even for C++11 style code. --- diff --git a/bindings/cxx/classes.cpp b/bindings/cxx/classes.cpp index d7ba7917..a00f2967 100644 --- a/bindings/cxx/classes.cpp +++ b/bindings/cxx/classes.cpp @@ -48,7 +48,7 @@ static const char *valid_string(const char *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); @@ -56,7 +56,7 @@ static GHashTable *map_to_hash_variant(map 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) @@ -238,7 +238,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 +287,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,9 +320,9 @@ 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); @@ -345,13 +345,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) @@ -411,7 +411,7 @@ string Driver::long_name() } vector> Driver::scan( - map options) + const map &options) { /* Initialise the driver if not yet done. */ if (!_initialized) @@ -478,11 +478,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) @@ -646,7 +646,7 @@ HardwareDevice::HardwareDevice(shared_ptr driver, struct sr_dev_inst *structure) : UserOwned(structure), Device(structure), - _driver(driver) + _driver(move(driver)) { } @@ -760,7 +760,7 @@ 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( @@ -826,19 +826,19 @@ void TriggerStage::add_match(shared_ptr channel, channel->_structure, type->id(), value)); _matches.push_back(new TriggerMatch( (struct sr_trigger_match *) g_slist_last( - _structure->matches)->data, channel)); + _structure->matches)->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)) { } @@ -863,7 +863,7 @@ float TriggerMatch::value() DatafeedCallbackData::DatafeedCallbackData(Session *session, DatafeedCallbackFunction callback) : - _callback(callback), + _callback(move(callback)), _session(session) { } @@ -873,7 +873,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) : @@ -893,18 +893,18 @@ 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) @@ -939,8 +939,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() @@ -1011,13 +1012,13 @@ static void datafeed_callback(const struct sr_dev_inst *sdi, 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) @@ -1037,7 +1038,7 @@ void Session::set_trigger(shared_ptr trigger) check(sr_session_trigger_set(_structure, NULL)); else check(sr_session_trigger_set(_structure, trigger->_structure)); - _trigger = trigger; + _trigger = move(trigger); } string Session::filename() @@ -1053,7 +1054,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) { @@ -1290,7 +1291,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) @@ -1301,7 +1302,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) { } @@ -1343,7 +1344,7 @@ InputDevice::InputDevice(shared_ptr input, struct sr_dev_inst *structure) : ParentOwned(structure), Device(structure), - _input(input) + _input(move(input)) { } @@ -1359,7 +1360,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)) { } @@ -1439,18 +1440,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()); } @@ -1460,21 +1461,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), + _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) { } diff --git a/bindings/cxx/include/libsigrokcxx/libsigrokcxx.hpp b/bindings/cxx/include/libsigrokcxx/libsigrokcxx.hpp index d76f9b87..cc0ad667 100644 --- a/bindings/cxx/include/libsigrokcxx/libsigrokcxx.hpp +++ b/bindings/cxx/include/libsigrokcxx/libsigrokcxx.hpp @@ -292,15 +292,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); @@ -343,7 +343,7 @@ public: /** 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); @@ -375,8 +375,7 @@ public: /** Scan for devices and return a list of devices found. * @param options Mapping of (ConfigKey, value) pairs. */ vector > scan( - map options = - map()); + const map &options = {}); protected: bool _initialized; vector _devices; @@ -817,8 +816,7 @@ public: map > options(); /** Create an input using this input format. * @param options Mapping of (option name, value) pairs. */ - shared_ptr create_input(map options = - map()); + shared_ptr create_input(const map &options = {}); protected: InputFormat(const struct sr_input_module *structure); ~InputFormat(); @@ -904,16 +902,14 @@ 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 @@ -938,9 +934,9 @@ public: protected: 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;