X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=bindings%2Fcxx%2Finclude%2Flibsigrok%2Flibsigrok.hpp;h=e25c1d5438b91a2a809bda3fcdbc791547bbcb31;hb=90e89c2a42ec085658f6c99656195ee4866e34c8;hp=0c77c9069c0e3c61cb3932b9004cfbf9bae80e45;hpb=541c855e1d05a007e996f52639698a7d3881c957;p=libsigrok.git 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;