From: Martin Ling Date: Wed, 12 Nov 2014 02:23:02 +0000 (+0000) Subject: bindings: Add UserDevice wrapping. X-Git-Tag: libsigrok-0.4.0~785 X-Git-Url: https://sigrok.org/gitweb/?p=libsigrok.git;a=commitdiff_plain;h=9fa5b426ec7c0a77eb9401f3dd10ad463a8d1ac7 bindings: Add UserDevice wrapping. --- diff --git a/bindings/cxx/classes.cpp b/bindings/cxx/classes.cpp index 3e8acc0a..bed6bed8 100644 --- a/bindings/cxx/classes.cpp +++ b/bindings/cxx/classes.cpp @@ -214,6 +214,13 @@ shared_ptr Context::create_session() new Session(shared_from_this()), Session::Deleter()); } +shared_ptr Context::create_user_device( + string vendor, string model, string version) +{ + return shared_ptr( + new UserDevice(vendor, model, version), UserDevice::Deleter()); +} + shared_ptr Context::load_session(string filename) { return shared_ptr( @@ -522,6 +529,34 @@ shared_ptr HardwareDevice::driver() return _driver; } +UserDevice::UserDevice(string vendor, string model, string version) : + UserOwned(sr_dev_inst_user_new( + vendor.c_str(), model.c_str(), version.c_str())), + Device(UserOwned::_structure) +{ +} + +UserDevice::~UserDevice() +{ +} + +shared_ptr UserDevice::get_shared_from_this() +{ + return static_pointer_cast(shared_from_this()); +} + +shared_ptr UserDevice::add_channel(unsigned int index, + const ChannelType *type, string name) +{ + check(sr_dev_inst_channel_add(Device::_structure, + index, type->id(), name.c_str())); + struct sr_channel *structure = (struct sr_channel *) + g_slist_last(sr_dev_inst_channels_get(Device::_structure))->data; + Channel *channel = new Channel(structure); + _channels[structure] = channel; + return get_channel(structure); +} + Channel::Channel(struct sr_channel *structure) : ParentOwned(structure), _type(ChannelType::get(_structure->type)) diff --git a/bindings/cxx/include/libsigrok/libsigrok.hpp b/bindings/cxx/include/libsigrok/libsigrok.hpp index 3582646f..6b4fbfc1 100644 --- a/bindings/cxx/include/libsigrok/libsigrok.hpp +++ b/bindings/cxx/include/libsigrok/libsigrok.hpp @@ -114,6 +114,7 @@ class SR_API InputDevice; class SR_API Output; class SR_API DataType; class SR_API Option; +class SR_API UserDevice; /** Exception thrown when an error code is returned by any libsigrok call. */ class SR_API Error: public exception @@ -263,6 +264,9 @@ public: void set_log_callback_default(); /** Create a new session. */ shared_ptr create_session(); + /** Create a new user device. */ + shared_ptr create_user_device( + string vendor, string model, string version); /** Load a saved session. * @param filename File name string. */ shared_ptr load_session(string filename); @@ -416,6 +420,28 @@ protected: friend class ChannelGroup; }; +/** A virtual device, created by the user */ +class SR_API UserDevice : + public UserOwned, + public Device +{ +public: + /** Add a new channel to this device. */ + shared_ptr add_channel(unsigned int index, const ChannelType *type, string name); +protected: + UserDevice(string vendor, string model, string version); + ~UserDevice(); + shared_ptr 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; +}; + /** A channel on a device */ class SR_API Channel : public ParentOwned @@ -440,6 +466,7 @@ protected: ~Channel(); const ChannelType * const _type; friend class Device; + friend class UserDevice; friend class ChannelGroup; friend class Session; friend class TriggerStage; diff --git a/bindings/swig/classes.i b/bindings/swig/classes.i index 2212d9b3..92aa3871 100644 --- a/bindings/swig/classes.i +++ b/bindings/swig/classes.i @@ -84,6 +84,7 @@ template< class T > class enable_shared_from_this; %shared_ptr(sigrok::Trigger); %shared_ptr(sigrok::TriggerStage); %shared_ptr(sigrok::TriggerMatch); +%shared_ptr(sigrok::UserDevice); %template(StringMap) std::map;