From: Martin Ling Date: Tue, 2 Sep 2014 20:05:42 +0000 (+0100) Subject: C++: Add UserOwned base template for objects with resources owned by user. X-Git-Tag: libsigrok-0.4.0~1046 X-Git-Url: https://sigrok.org/gitweb/?p=libsigrok.git;a=commitdiff_plain;h=90e89c2a42ec085658f6c99656195ee4866e34c8 C++: Add UserOwned base template for objects with resources owned by user. --- diff --git a/bindings/cxx/classes.cpp b/bindings/cxx/classes.cpp index dcbf8021..525e046d 100644 --- a/bindings/cxx/classes.cpp +++ b/bindings/cxx/classes.cpp @@ -71,9 +71,11 @@ shared_ptr Context::create() } Context::Context() : + UserOwned(structure), session(NULL) { check(sr_init(&structure)); + struct sr_dev_driver **driver_list = sr_driver_list(); if (driver_list) for (int i = 0; driver_list[i]; i++) @@ -533,7 +535,8 @@ vector> ChannelGroup::get_channels() } Trigger::Trigger(shared_ptr context, string name) : - structure(sr_trigger_new(name.c_str())), context(context) + UserOwned(sr_trigger_new(name.c_str())), + context(context) { for (auto stage = structure->stages; stage; stage = stage->next) stages.push_back(new TriggerStage((struct sr_trigger_stage *) stage->data)); @@ -690,6 +693,7 @@ EventSource::~EventSource() } Session::Session(shared_ptr context) : + UserOwned(structure), context(context), saving(false) { check(sr_session_new(&structure)); @@ -697,6 +701,7 @@ Session::Session(shared_ptr context) : } Session::Session(shared_ptr context, string filename) : + UserOwned(structure), context(context), saving(false) { check(sr_session_load(filename.c_str(), &structure)); @@ -936,7 +941,7 @@ void Session::set_trigger(shared_ptr trigger) Packet::Packet(shared_ptr device, const struct sr_datafeed_packet *structure) : - structure(structure), + UserOwned(structure), device(device) { switch (structure->type) @@ -1168,7 +1173,7 @@ shared_ptr InputFormat::create_input( } Input::Input(shared_ptr context, const struct sr_input *structure) : - structure(structure), + UserOwned(structure), context(context), device(nullptr) { @@ -1221,7 +1226,7 @@ shared_ptr InputDevice::get_shared_from_this() Option::Option(const struct sr_option *structure, shared_ptr structure_array) : - structure(structure), + UserOwned(structure), structure_array(structure_array) { } @@ -1299,7 +1304,7 @@ shared_ptr OutputFormat::create_output( Output::Output(shared_ptr format, shared_ptr device, map options) : - structure(sr_output_new(format->structure, + UserOwned(sr_output_new(format->structure, map_to_hash_variant(options), device->structure)), format(format), device(device), options(options) { diff --git a/bindings/cxx/include/libsigrok/libsigrok.hpp b/bindings/cxx/include/libsigrok/libsigrok.hpp index 0c77c906..e25c1d54 100644 --- a/bindings/cxx/include/libsigrok/libsigrok.hpp +++ b/bindings/cxx/include/libsigrok/libsigrok.hpp @@ -192,11 +192,32 @@ protected: } }; +/* Base template for classes whose resources are owned by the user. */ +template +class SR_API UserOwned : public enable_shared_from_this +{ +public: + shared_ptr shared_from_this() + { + auto shared = enable_shared_from_this::shared_from_this(); + if (!shared) + throw Error(SR_ERR_BUG); + return shared; + } +protected: + Struct *structure; + + UserOwned(Struct *structure) : + structure(structure) + { + } +}; + /** Type of log callback */ typedef function LogCallbackFunction; /** The global libsigrok context */ -class SR_API Context : public enable_shared_from_this +class SR_API Context : public UserOwned { public: /** Create new context */ @@ -241,7 +262,6 @@ public: * @param header Initial data from stream. */ shared_ptr open_stream(string header); protected: - struct sr_context *structure; map drivers; map input_formats; map output_formats; @@ -414,7 +434,7 @@ protected: }; /** A trigger configuration */ -class SR_API Trigger : public enable_shared_from_this +class SR_API Trigger : public UserOwned { public: /** Name of this trigger configuration. */ @@ -426,7 +446,6 @@ public: protected: Trigger(shared_ptr context, string name); ~Trigger(); - struct sr_trigger *structure; shared_ptr context; vector stages; /** Deleter needed to allow shared_ptr use with protected destructor. */ @@ -566,7 +585,7 @@ protected: }; /** A sigrok session */ -class SR_API Session +class SR_API Session : public UserOwned { public: /** Add a device to this session. @@ -610,7 +629,6 @@ protected: Session(shared_ptr context); Session(shared_ptr context, string filename); ~Session(); - struct sr_session *structure; const shared_ptr context; map > devices; vector datafeed_callbacks; @@ -632,7 +650,7 @@ protected: }; /** A packet on the session datafeed */ -class SR_API Packet : public enable_shared_from_this +class SR_API Packet : public UserOwned { public: /** Type of this packet. */ @@ -643,7 +661,6 @@ protected: Packet(shared_ptr device, const struct sr_datafeed_packet *structure); ~Packet(); - const struct sr_datafeed_packet *structure; shared_ptr device; PacketPayload *payload; /** Deleter needed to allow shared_ptr use with protected destructor. */ @@ -779,7 +796,7 @@ protected: }; /** An input instance (an input format applied to a file or stream) */ -class SR_API Input : public enable_shared_from_this +class SR_API Input : public UserOwned { public: /** Virtual device associated with this input. */ @@ -790,7 +807,6 @@ public: protected: Input(shared_ptr context, const struct sr_input *structure); ~Input(); - const struct sr_input *structure; shared_ptr context; InputDevice *device; /** Deleter needed to allow shared_ptr use with protected destructor. */ @@ -825,7 +841,7 @@ protected: }; /** An option used by an output format */ -class SR_API Option +class SR_API Option : public UserOwned { public: /** Short name of this option suitable for command line usage. */ @@ -842,7 +858,6 @@ protected: Option(const struct sr_option *structure, shared_ptr structure_array); ~Option(); - const struct sr_option *structure; shared_ptr structure_array; /** Deleter needed to allow shared_ptr use with protected destructor. */ class Deleter @@ -879,7 +894,7 @@ protected: }; /** An output instance (an output format applied to a device) */ -class SR_API Output +class SR_API Output : public UserOwned { public: /** Update output with data from the given packet. @@ -890,7 +905,6 @@ protected: Output(shared_ptr format, shared_ptr device, map options); ~Output(); - const struct sr_output *structure; const shared_ptr format; const shared_ptr device; const map options;