]> sigrok.org Git - libsigrok.git/commitdiff
C++: Replace custom deleters with std::default_delete
authorDaniel Elstner <redacted>
Thu, 15 Oct 2015 17:21:28 +0000 (19:21 +0200)
committerDaniel Elstner <redacted>
Mon, 26 Oct 2015 05:45:56 +0000 (06:45 +0100)
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.

bindings/cxx/classes.cpp
bindings/cxx/include/libsigrokcxx/libsigrokcxx.hpp

index 5a977a86bf0fcf0d0856e615422f0e72c755c2af..c5ce63864359eeb08fe6a956debbc001dd2f3a21 100644 (file)
@@ -116,7 +116,7 @@ SR_PRIV ssize_t ResourceReader::read_callback(const struct sr_resource *res,
 
 shared_ptr<Context> Context::create()
 {
-       return shared_ptr<Context>(new Context(), Context::Deleter());
+       return shared_ptr<Context>{new Context{}, default_delete<Context>{}};
 }
 
 Context::Context() :
@@ -252,15 +252,16 @@ void Context::set_resource_reader(ResourceReader *reader)
 
 shared_ptr<Session> Context::create_session()
 {
-       return shared_ptr<Session>(
-               new Session(shared_from_this()), Session::Deleter());
+       return shared_ptr<Session>{new Session{shared_from_this()},
+               default_delete<Session>{}};
 }
 
 shared_ptr<UserDevice> Context::create_user_device(
                string vendor, string model, string version)
 {
-       return shared_ptr<UserDevice>(
-               new UserDevice(vendor, model, version), UserDevice::Deleter());
+       return shared_ptr<UserDevice>{
+               new UserDevice{move(vendor), move(model), move(version)},
+               default_delete<UserDevice>{}};
 }
 
 shared_ptr<Packet> Context::create_header_packet(Glib::TimeVal start_time)
@@ -272,7 +273,8 @@ shared_ptr<Packet> 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<Packet>(new Packet(nullptr, packet), Packet::Deleter());
+       return shared_ptr<Packet>{new Packet{nullptr, packet},
+               default_delete<Packet>{}};
 }
 
 shared_ptr<Packet> Context::create_meta_packet(
@@ -291,7 +293,8 @@ shared_ptr<Packet> Context::create_meta_packet(
        auto packet = g_new(struct sr_datafeed_packet, 1);
        packet->type = SR_DF_META;
        packet->payload = meta;
-       return shared_ptr<Packet>(new Packet(nullptr, packet), Packet::Deleter());
+       return shared_ptr<Packet>{new Packet{nullptr, packet},
+               default_delete<Packet>{}};
 }
 
 shared_ptr<Packet> Context::create_logic_packet(
@@ -304,7 +307,7 @@ shared_ptr<Packet> Context::create_logic_packet(
        auto packet = g_new(struct sr_datafeed_packet, 1);
        packet->type = SR_DF_LOGIC;
        packet->payload = logic;
-       return shared_ptr<Packet>(new Packet(nullptr, packet), Packet::Deleter());
+       return shared_ptr<Packet>{new Packet{nullptr, packet}, default_delete<Packet>{}};
 }
 
 shared_ptr<Packet> Context::create_analog_packet(
@@ -327,19 +330,21 @@ shared_ptr<Packet> Context::create_analog_packet(
        auto packet = g_new(struct sr_datafeed_packet, 1);
        packet->type = SR_DF_ANALOG;
        packet->payload = analog;
-       return shared_ptr<Packet>(new Packet(nullptr, packet), Packet::Deleter());
+       return shared_ptr<Packet>{new Packet{nullptr, packet}, default_delete<Packet>{}};
 }
 
 shared_ptr<Session> Context::load_session(string filename)
 {
-       return shared_ptr<Session>(
-               new Session(shared_from_this(), move(filename)), Session::Deleter());
+       return shared_ptr<Session>{
+               new Session{shared_from_this(), move(filename)},
+               default_delete<Session>{}};
 }
 
 shared_ptr<Trigger> Context::create_trigger(string name)
 {
-       return shared_ptr<Trigger>(
-               new Trigger(shared_from_this(), move(name)), Trigger::Deleter());
+       return shared_ptr<Trigger>{
+               new Trigger{shared_from_this(), move(name)},
+               default_delete<Trigger>{}};
 }
 
 shared_ptr<Input> Context::open_file(string filename)
@@ -347,8 +352,9 @@ shared_ptr<Input> Context::open_file(string filename)
        const struct sr_input *input;
 
        check(sr_input_scan_file(filename.c_str(), &input));
-       return shared_ptr<Input>(
-               new Input(shared_from_this(), input), Input::Deleter());
+       return shared_ptr<Input>{
+               new Input{shared_from_this(), input},
+               default_delete<Input>{}};
 }
 
 shared_ptr<Input> Context::open_stream(string header)
@@ -359,8 +365,9 @@ shared_ptr<Input> Context::open_stream(string header)
        auto ret = sr_input_scan_buffer(gstr, &input);
        g_string_free(gstr, true);
        check(ret);
-       return shared_ptr<Input>(
-               new Input(shared_from_this(), input), Input::Deleter());
+       return shared_ptr<Input>{
+               new Input{shared_from_this(), input},
+               default_delete<Input>{}};
 }
 
 map<string, string> Context::serials(shared_ptr<Driver> driver) const
@@ -433,9 +440,10 @@ vector<shared_ptr<HardwareDevice>> Driver::scan(
        for (GSList *device = device_list; device; device = device->next)
        {
                auto *const sdi = static_cast<struct sr_dev_inst *>(device->data);
-               result.push_back(shared_ptr<HardwareDevice>(
-                       new HardwareDevice(shared_from_this(), sdi),
-                       HardwareDevice::Deleter()));
+               shared_ptr<HardwareDevice> hwdev {
+                       new HardwareDevice{shared_from_this(), sdi},
+                       default_delete<HardwareDevice>{}};
+               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<Packet>(new Packet(device, pkt), Packet::Deleter());
+       shared_ptr<Packet> packet {new Packet{device, pkt}, default_delete<Packet>{}};
        _callback(move(device), move(packet));
 }
 
@@ -1249,15 +1257,18 @@ vector<string> InputFormat::extensions() const
 
 map<string, shared_ptr<Option>> InputFormat::options()
 {
-       const struct sr_option **options = sr_input_options_get(_structure);
        map<string, shared_ptr<Option>> result;
-       if (options)
+
+       if (const struct sr_option **options = sr_input_options_get(_structure))
        {
-               auto option_array = shared_ptr<const struct sr_option *>(
-                       options, sr_input_options_free);
-               for (int i = 0; options[i]; i++)
-                       result[options[i]->id] = shared_ptr<Option>(
-                               new Option(options[i], option_array), Option::Deleter());
+               shared_ptr<const struct sr_option *> option_array
+                       {options, &sr_input_options_free};
+               for (int i = 0; options[i]; i++) {
+                       shared_ptr<Option> opt {
+                               new Option{options[i], option_array},
+                               default_delete<Option>{}};
+                       result.emplace(opt->id(), move(opt));
+               }
        }
        return result;
 }
@@ -1268,7 +1279,7 @@ shared_ptr<Input> InputFormat::create_input(
        auto input = sr_input_new(_structure, map_to_hash_variant(options));
        if (!input)
                throw Error(SR_ERR_ARG);
-       return shared_ptr<Input>(new Input(_parent, input), Input::Deleter());
+       return shared_ptr<Input>{new Input{_parent, input}, default_delete<Input>{}};
 }
 
 Input::Input(shared_ptr<Context> context, const struct sr_input *structure) :
@@ -1395,15 +1406,18 @@ vector<string> OutputFormat::extensions() const
 
 map<string, shared_ptr<Option>> OutputFormat::options()
 {
-       const struct sr_option **options = sr_output_options_get(_structure);
        map<string, shared_ptr<Option>> result;
-       if (options)
+
+       if (const struct sr_option **options = sr_output_options_get(_structure))
        {
-               auto option_array = shared_ptr<const struct sr_option *>(
-                       options, sr_output_options_free);
-               for (int i = 0; options[i]; i++)
-                       result[options[i]->id] = shared_ptr<Option>(
-                               new Option(options[i], option_array), Option::Deleter());
+               shared_ptr<const struct sr_option *> option_array
+                       {options, &sr_output_options_free};
+               for (int i = 0; options[i]; i++) {
+                       shared_ptr<Option> opt {
+                               new Option{options[i], option_array},
+                               default_delete<Option>{}};
+                       result.emplace(opt->id(), move(opt));
+               }
        }
        return result;
 }
@@ -1411,17 +1425,17 @@ map<string, shared_ptr<Option>> OutputFormat::options()
 shared_ptr<Output> OutputFormat::create_output(
        shared_ptr<Device> device, const map<string, Glib::VariantBase> &options)
 {
-       return shared_ptr<Output>(
-               new Output(shared_from_this(), move(device), options),
-               Output::Deleter());
+       return shared_ptr<Output>{
+               new Output{shared_from_this(), move(device), options},
+               default_delete<Output>{}};
 }
 
 shared_ptr<Output> OutputFormat::create_output(string filename,
        shared_ptr<Device> device, const map<string, Glib::VariantBase> &options)
 {
-       return shared_ptr<Output>(
-               new Output(move(filename), shared_from_this(), move(device), options),
-               Output::Deleter());
+       return shared_ptr<Output>{
+               new Output{move(filename), shared_from_this(), move(device), options},
+               default_delete<Output>{}};
 }
 
 bool OutputFormat::test_flag(const OutputFlag *flag) const
index 5e101fc8209b1ccef9b02b48960d7d312aa1cc1e..a74bbfcfa5002ba665e747c17edd4f3235125710 100644 (file)
@@ -203,13 +203,6 @@ protected:
                        throw Error(SR_ERR_BUG);
                return shared;
        }
-
-       /* Deleter needed to allow shared_ptr use with protected destructor. */
-       class Deleter
-       {
-       public:
-               void operator()(Class *object) { delete object; }
-       };
 };
 
 /** Type of log callback */
@@ -307,9 +300,9 @@ private:
        LogCallbackFunction _log_callback;
        Context();
        ~Context();
-       friend class Deleter;
        friend class Session;
        friend class Driver;
+       friend class std::default_delete<Context>;
 };
 
 enum Capability {
@@ -405,18 +398,13 @@ protected:
        map<struct sr_channel *, unique_ptr<Channel> > _channels;
 private:
        map<string, unique_ptr<ChannelGroup> > _channel_groups;
-       /** Deleter needed to allow shared_ptr use with protected destructor. */
-       class Deleter
-       {
-       public:
-               void operator()(Device *device) { delete device; }
-       };
-       friend class Deleter;
+
        friend class Session;
        friend class Channel;
        friend class ChannelGroup;
        friend class Output;
        friend class Analog;
+       friend class std::default_delete<Device>;
 };
 
 /** A real hardware device, connected via a driver */
@@ -432,15 +420,10 @@ private:
        ~HardwareDevice();
        shared_ptr<Device> get_shared_from_this();
        shared_ptr<Driver> _driver;
-       /** Deleter needed to allow shared_ptr use with protected destructor. */
-       class Deleter
-       {
-       public:
-               void operator()(HardwareDevice *device) { delete device; }
-       };
-       friend class Deleter;
+
        friend class Driver;
        friend class ChannelGroup;
+       friend class std::default_delete<HardwareDevice>;
 };
 
 /** A virtual device, created by the user */
@@ -455,14 +438,9 @@ private:
        UserDevice(string vendor, string model, string version);
        ~UserDevice();
        shared_ptr<Device> get_shared_from_this();
-       /** Deleter needed to allow shared_ptr use with protected destructor. */
-       class Deleter
-       {
-       public:
-               void operator()(UserDevice *device) { delete device; }
-       };
+
        friend class Context;
-       friend class Deleter;
+       friend class std::default_delete<UserDevice>;
 };
 
 /** A channel on a device */
@@ -532,9 +510,9 @@ private:
        struct sr_trigger *_structure;
        shared_ptr<Context> _context;
        vector<unique_ptr<TriggerStage> > _stages;
-       friend class Deleter;
        friend class Context;
        friend class Session;
+       friend class std::default_delete<Trigger>;
 };
 
 /** A stage in a trigger configuration */
@@ -614,13 +592,7 @@ private:
        explicit SessionDevice(struct sr_dev_inst *sdi);
        ~SessionDevice();
        shared_ptr<Device> get_shared_from_this();
-       /** Deleter needed to allow shared_ptr use with protected destructor. */
-       class Deleter
-       {
-       public:
-               void operator()(SessionDevice *device) { delete device; }
-       };
-       friend class Deleter;
+
        friend class Session;
        friend class std::default_delete<SessionDevice>;
 };
@@ -673,10 +645,11 @@ private:
        SessionStoppedCallback _stopped_callback;
        string _filename;
        shared_ptr<Trigger> _trigger;
-       friend class Deleter;
+
        friend class Context;
        friend class DatafeedCallbackData;
        friend class SessionDevice;
+       friend class std::default_delete<Session>;
 };
 
 /** A packet on the session datafeed */
@@ -694,7 +667,7 @@ private:
        const struct sr_datafeed_packet *_structure;
        shared_ptr<Device> _device;
        unique_ptr<PacketPayload> _payload;
-       friend class Deleter;
+
        friend class Session;
        friend class Output;
        friend class DatafeedCallbackData;
@@ -703,6 +676,7 @@ private:
        friend class Logic;
        friend class Analog;
        friend class Context;
+       friend class std::default_delete<Packet>;
 };
 
 /** Abstract base class for datafeed packet payloads */
@@ -714,13 +688,6 @@ protected:
 private:
        virtual shared_ptr<PacketPayload> share_owned_by(shared_ptr<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;
        friend class std::default_delete<PacketPayload>;
@@ -861,9 +828,10 @@ private:
        const struct sr_input *_structure;
        shared_ptr<Context> _context;
        unique_ptr<InputDevice> _device;
-       friend class Deleter;
+
        friend class Context;
        friend class InputFormat;
+       friend class std::default_delete<Input>;
 };
 
 /** A virtual device associated with an input */
@@ -900,9 +868,10 @@ private:
        ~Option();
        const struct sr_option *_structure;
        shared_ptr<const struct sr_option *> _structure_array;
-       friend class Deleter;
+
        friend class InputFormat;
        friend class OutputFormat;
+       friend class std::default_delete<Option>;
 };
 
 /** An output format supported by the library */
@@ -970,8 +939,8 @@ private:
        const shared_ptr<Device> _device;
        const map<string, Glib::VariantBase> _options;
 
-       friend class Deleter;
        friend class OutputFormat;
+       friend class std::default_delete<Output>;
 };
 
 /** Base class for objects which wrap an enumeration value from libsigrok */