From: Daniel Elstner Date: Thu, 15 Oct 2015 17:21:28 +0000 (+0200) Subject: C++: Replace custom deleters with std::default_delete X-Git-Tag: libsigrok-0.4.0~167 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=a98729a742ebbb9df9dbb9ce8aae1cc4c3f1e00d;p=libsigrok.git C++: Replace custom deleters with std::default_delete Replace custom Deleter classes with std::default_delete<>, declared as friend so it can invoke the private destructor. Inexplicably, std::shared_ptr<> does not use default_delete<> by default, so it is still necessary to explicitly specify the deleter when creating shared_ptr instances. With this, unique_ptr and shared_ptr instances now use the same default delete mechanism. --- diff --git a/bindings/cxx/classes.cpp b/bindings/cxx/classes.cpp index 5a977a86..c5ce6386 100644 --- a/bindings/cxx/classes.cpp +++ b/bindings/cxx/classes.cpp @@ -116,7 +116,7 @@ SR_PRIV ssize_t ResourceReader::read_callback(const struct sr_resource *res, shared_ptr Context::create() { - return shared_ptr(new Context(), Context::Deleter()); + return shared_ptr{new Context{}, default_delete{}}; } Context::Context() : @@ -252,15 +252,16 @@ void Context::set_resource_reader(ResourceReader *reader) shared_ptr Context::create_session() { - return shared_ptr( - new Session(shared_from_this()), Session::Deleter()); + return shared_ptr{new Session{shared_from_this()}, + default_delete{}}; } shared_ptr Context::create_user_device( string vendor, string model, string version) { - return shared_ptr( - new UserDevice(vendor, model, version), UserDevice::Deleter()); + return shared_ptr{ + new UserDevice{move(vendor), move(model), move(version)}, + default_delete{}}; } shared_ptr Context::create_header_packet(Glib::TimeVal start_time) @@ -272,7 +273,8 @@ shared_ptr Context::create_header_packet(Glib::TimeVal start_time) auto packet = g_new(struct sr_datafeed_packet, 1); packet->type = SR_DF_HEADER; packet->payload = header; - return shared_ptr(new Packet(nullptr, packet), Packet::Deleter()); + return shared_ptr{new Packet{nullptr, packet}, + default_delete{}}; } shared_ptr Context::create_meta_packet( @@ -291,7 +293,8 @@ shared_ptr Context::create_meta_packet( auto packet = g_new(struct sr_datafeed_packet, 1); packet->type = SR_DF_META; packet->payload = meta; - return shared_ptr(new Packet(nullptr, packet), Packet::Deleter()); + return shared_ptr{new Packet{nullptr, packet}, + default_delete{}}; } shared_ptr Context::create_logic_packet( @@ -304,7 +307,7 @@ shared_ptr Context::create_logic_packet( auto packet = g_new(struct sr_datafeed_packet, 1); packet->type = SR_DF_LOGIC; packet->payload = logic; - return shared_ptr(new Packet(nullptr, packet), Packet::Deleter()); + return shared_ptr{new Packet{nullptr, packet}, default_delete{}}; } shared_ptr Context::create_analog_packet( @@ -327,19 +330,21 @@ shared_ptr Context::create_analog_packet( auto packet = g_new(struct sr_datafeed_packet, 1); packet->type = SR_DF_ANALOG; packet->payload = analog; - return shared_ptr(new Packet(nullptr, packet), Packet::Deleter()); + return shared_ptr{new Packet{nullptr, packet}, default_delete{}}; } shared_ptr Context::load_session(string filename) { - return shared_ptr( - new Session(shared_from_this(), move(filename)), Session::Deleter()); + return shared_ptr{ + new Session{shared_from_this(), move(filename)}, + default_delete{}}; } shared_ptr Context::create_trigger(string name) { - return shared_ptr( - new Trigger(shared_from_this(), move(name)), Trigger::Deleter()); + return shared_ptr{ + new Trigger{shared_from_this(), move(name)}, + default_delete{}}; } shared_ptr Context::open_file(string filename) @@ -347,8 +352,9 @@ shared_ptr Context::open_file(string filename) const struct sr_input *input; check(sr_input_scan_file(filename.c_str(), &input)); - return shared_ptr( - new Input(shared_from_this(), input), Input::Deleter()); + return shared_ptr{ + new Input{shared_from_this(), input}, + default_delete{}}; } shared_ptr Context::open_stream(string header) @@ -359,8 +365,9 @@ shared_ptr Context::open_stream(string header) auto ret = sr_input_scan_buffer(gstr, &input); g_string_free(gstr, true); check(ret); - return shared_ptr( - new Input(shared_from_this(), input), Input::Deleter()); + return shared_ptr{ + new Input{shared_from_this(), input}, + default_delete{}}; } map Context::serials(shared_ptr driver) const @@ -433,9 +440,10 @@ vector> Driver::scan( for (GSList *device = device_list; device; device = device->next) { auto *const sdi = static_cast(device->data); - result.push_back(shared_ptr( - new HardwareDevice(shared_from_this(), sdi), - HardwareDevice::Deleter())); + shared_ptr hwdev { + new HardwareDevice{shared_from_this(), sdi}, + default_delete{}}; + result.push_back(move(hwdev)); } /* Free GSList returned from scan. */ @@ -858,7 +866,7 @@ void DatafeedCallbackData::run(const struct sr_dev_inst *sdi, const struct sr_datafeed_packet *pkt) { auto device = _session->get_device(sdi); - auto packet = shared_ptr(new Packet(device, pkt), Packet::Deleter()); + shared_ptr packet {new Packet{device, pkt}, default_delete{}}; _callback(move(device), move(packet)); } @@ -1249,15 +1257,18 @@ vector InputFormat::extensions() const map> InputFormat::options() { - const struct sr_option **options = sr_input_options_get(_structure); map> result; - if (options) + + if (const struct sr_option **options = sr_input_options_get(_structure)) { - auto option_array = shared_ptr( - options, sr_input_options_free); - for (int i = 0; options[i]; i++) - result[options[i]->id] = shared_ptr