X-Git-Url: http://sigrok.org/gitweb/?a=blobdiff_plain;f=bindings%2Fcxx%2Fclasses.cpp;h=9013e41ae9d44ad004cfe2a9b218e742a719432e;hb=90486ba835ff82c316d63b3d3b69581615d9245a;hp=66b79a93a1f31b6f29f421eebc2a99c27b0dfe43;hpb=584f76a78a9d687dcf396f0c620fe3093b38e70a;p=libsigrok.git diff --git a/bindings/cxx/classes.cpp b/bindings/cxx/classes.cpp index 66b79a93..9013e41a 100644 --- a/bindings/cxx/classes.cpp +++ b/bindings/cxx/classes.cpp @@ -725,7 +725,7 @@ DatafeedCallbackData::DatafeedCallbackData(Session *session, void DatafeedCallbackData::run(const struct sr_dev_inst *sdi, const struct sr_datafeed_packet *pkt) { - auto device = _session->_devices[sdi]; + auto device = _session->get_device(sdi); auto packet = shared_ptr(new Packet(device, pkt), Packet::Deleter()); _callback(device, packet); } @@ -789,6 +789,11 @@ SessionDevice::~SessionDevice() { } +string SessionDevice::description() +{ + return _parent->_filename; +} + shared_ptr SessionDevice::get_shared_from_this() { return static_pointer_cast(shared_from_this()); @@ -806,6 +811,7 @@ Session::Session(shared_ptr context) : Session::Session(shared_ptr context, string filename) : UserOwned(_structure), _context(context), + _filename(filename), _saving(false) { check(sr_session_load(filename.c_str(), &_structure)); @@ -814,9 +820,7 @@ Session::Session(shared_ptr context, string filename) : 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()); + _owned_devices[sdi] = new SessionDevice(sdi); } _context->_session = this; } @@ -830,12 +834,26 @@ Session::~Session() for (auto entry : _source_callbacks) delete entry.second; + + for (auto entry : _owned_devices) + delete entry.second; +} + +shared_ptr Session::get_device(const struct sr_dev_inst *sdi) +{ + if (_owned_devices.count(sdi)) + return static_pointer_cast( + _owned_devices[sdi]->get_shared_pointer(this)); + else if (_other_devices.count(sdi)) + return _other_devices[sdi]; + else + throw Error(SR_ERR_BUG); } void Session::add_device(shared_ptr device) { check(sr_session_dev_add(_structure, device->_structure)); - _devices[device->_structure] = device; + _other_devices[device->_structure] = device; } vector> Session::devices() @@ -846,14 +864,24 @@ vector> Session::devices() for (GSList *dev = dev_list; dev; dev = dev->next) { auto sdi = (struct sr_dev_inst *) dev->data; - result.push_back(_devices[sdi]); + result.push_back(get_device(sdi)); } return result; } void Session::remove_devices() { - _devices.clear(); + for (auto entry : _owned_devices) + { + // We own this device. Make sure it's not referenced. + auto device = entry.second; + auto ptr = device->get_shared_pointer(this); + if (ptr.use_count() > 1) + throw Error(SR_ERR_BUG); + delete device; + } + _owned_devices.clear(); + _other_devices.clear(); check(sr_session_dev_remove_all(_structure)); } @@ -1342,6 +1370,11 @@ InputDevice::~InputDevice() { } +string InputDevice::description() +{ + return ""; +} + shared_ptr InputDevice::get_shared_from_this() { return static_pointer_cast(shared_from_this());