From: Martin Ling Date: Sat, 6 Sep 2014 15:06:00 +0000 (+0100) Subject: C++: Add SessionDevice class for devices owned by loaded sessions. X-Git-Tag: libsigrok-0.4.0~1023 X-Git-Url: https://sigrok.org/gitweb/?p=libsigrok.git;a=commitdiff_plain;h=cac58676e987d06b890366ac4078a1e4fb1cbdc3 C++: Add SessionDevice class for devices owned by loaded sessions. --- diff --git a/bindings/cxx/classes.cpp b/bindings/cxx/classes.cpp index 76d3e8ec..3043ba51 100644 --- a/bindings/cxx/classes.cpp +++ b/bindings/cxx/classes.cpp @@ -745,6 +745,21 @@ EventSource::~EventSource() { } +SessionDevice::SessionDevice(struct sr_dev_inst *structure) : + ParentOwned(structure), + Device(structure) +{ +} + +SessionDevice::~SessionDevice() +{ +} + +shared_ptr SessionDevice::get_shared_from_this() +{ + return static_pointer_cast(shared_from_this()); +} + Session::Session(shared_ptr context) : UserOwned(structure), context(context), saving(false) @@ -758,6 +773,15 @@ Session::Session(shared_ptr context, string filename) : context(context), saving(false) { check(sr_session_load(filename.c_str(), &structure)); + GSList *dev_list; + check(sr_session_dev_list(structure, &dev_list)); + for (GSList *dev = dev_list; dev; dev = dev->next) + { + auto sdi = (struct sr_dev_inst *) dev->data; + auto device = new SessionDevice(sdi); + devices[sdi] = shared_ptr(device, + SessionDevice::Deleter()); + } context->session = this; } diff --git a/bindings/cxx/include/libsigrok/libsigrok.hpp b/bindings/cxx/include/libsigrok/libsigrok.hpp index 29d6c736..2a819b09 100644 --- a/bindings/cxx/include/libsigrok/libsigrok.hpp +++ b/bindings/cxx/include/libsigrok/libsigrok.hpp @@ -585,6 +585,25 @@ protected: friend class SourceCallbackData; }; +/** A virtual device associated with a stored session */ +class SR_API SessionDevice : + public ParentOwned, + public Device +{ +protected: + SessionDevice(struct sr_dev_inst *sdi); + ~SessionDevice(); + shared_ptr 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; +}; + /** A sigrok session */ class SR_API Session : public UserOwned { diff --git a/bindings/swig/classes.i b/bindings/swig/classes.i index cc4aa742..fe89952c 100644 --- a/bindings/swig/classes.i +++ b/bindings/swig/classes.i @@ -68,6 +68,7 @@ template< class T > class enable_shared_from_this; %shared_ptr(sigrok::ChannelGroup); %shared_ptr(sigrok::EventSource); %shared_ptr(sigrok::Session); +%shared_ptr(sigrok::SessionDevice); %shared_ptr(sigrok::Packet); %shared_ptr(sigrok::PacketPayload); %shared_ptr(sigrok::Header);