]> sigrok.org Git - libsigrok.git/blobdiff - bindings/cxx/classes.cpp
C++: Use noexcept instead of throw()
[libsigrok.git] / bindings / cxx / classes.cpp
index 850225fdd467840630442442ab2159e3f852022d..1b53d0a795968aa79c5a64ed0f9be90b38af3f5b 100644 (file)
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#include "libsigrokcxx/libsigrokcxx.hpp"
+/* Needed for isascii(), as used in the GNU libstdc++ headers */
+#ifndef _XOPEN_SOURCE
+#define _XOPEN_SOURCE 600
+#endif
+
+#include <config.h>
+#include <libsigrokcxx/libsigrokcxx.hpp>
 
 #include <sstream>
 #include <cmath>
@@ -33,39 +39,80 @@ static void check(int result)
 }
 
 /** Helper function to obtain valid strings from possibly null input. */
-static const char *valid_string(const char *input)
+static inline const char *valid_string(const char *input)
 {
-       if (input != NULL)
-               return input;
-       else
-               return "";
+       return (input) ? input : "";
 }
 
 /** Helper function to convert between map<string, VariantBase> and GHashTable */
-static GHashTable *map_to_hash_variant(map<string, Glib::VariantBase> input)
+static GHashTable *map_to_hash_variant(const map<string, Glib::VariantBase> &input)
 {
-       auto output = g_hash_table_new_full(
-               g_str_hash, g_str_equal, g_free, (GDestroyNotify) g_variant_unref);
+       auto output = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
+                       reinterpret_cast<GDestroyNotify>(&g_variant_unref));
        for (auto entry : input)
                g_hash_table_insert(output,
                        g_strdup(entry.first.c_str()),
                        entry.second.gobj_copy());
-    return output;
+       return output;
 }
 
 Error::Error(int result) : result(result)
 {
 }
 
-const char *Error::what() const throw()
+const char *Error::what() const noexcept
 {
        return sr_strerror(result);
 }
 
-Error::~Error() throw()
+Error::~Error() noexcept
+{
+}
+
+ResourceReader::~ResourceReader()
 {
 }
 
+SR_PRIV int ResourceReader::open_callback(struct sr_resource *res,
+               const char *name, void *cb_data)
+{
+       try {
+               auto *const reader = static_cast<ResourceReader*>(cb_data);
+               reader->open(res, name);
+       } catch (const Error &err) {
+               return err.result;
+       } catch (...) {
+               return SR_ERR;
+       }
+       return SR_OK;
+}
+
+SR_PRIV int ResourceReader::close_callback(struct sr_resource *res, void *cb_data)
+{
+       try {
+               auto *const reader = static_cast<ResourceReader*>(cb_data);
+               reader->close(res);
+       } catch (const Error &err) {
+               return err.result;
+       } catch (...) {
+               return SR_ERR;
+       }
+       return SR_OK;
+}
+
+SR_PRIV ssize_t ResourceReader::read_callback(const struct sr_resource *res,
+               void *buf, size_t count, void *cb_data)
+{
+       try {
+               auto *const reader = static_cast<ResourceReader*>(cb_data);
+               return reader->read(res, buf, count);
+       } catch (const Error &err) {
+               return err.result;
+       } catch (...) {
+               return SR_ERR;
+       }
+}
+
 shared_ptr<Context> Context::create()
 {
        return shared_ptr<Context>(new Context(), Context::Deleter());
@@ -73,11 +120,11 @@ shared_ptr<Context> Context::create()
 
 Context::Context() :
        UserOwned(_structure),
-       _session(NULL)
+       _session(nullptr)
 {
        check(sr_init(&_structure));
 
-       struct sr_dev_driver **driver_list = sr_driver_list();
+       struct sr_dev_driver **driver_list = sr_driver_list(_structure);
        if (driver_list)
                for (int i = 0; driver_list[i]; i++)
                        _drivers[driver_list[i]->name] =
@@ -161,32 +208,16 @@ void Context::set_log_level(const LogLevel *level)
        check(sr_log_loglevel_set(level->id()));
 }
 
-string Context::log_domain()
-{
-       return valid_string(sr_log_logdomain_get());
-}
-
-void Context::set_log_domain(string value)
-{
-       check(sr_log_logdomain_set(value.c_str()));
-}
-
 static int call_log_callback(void *cb_data, int loglevel, const char *format, va_list args)
 {
-       va_list args_copy;
-       va_copy(args_copy, args);
-       int length = vsnprintf(NULL, 0, format, args_copy);
-       va_end(args_copy);
-       char *buf = (char *) g_malloc(length + 1);
-       vsprintf(buf, format, args);
-       string message(buf, length);
-       g_free(buf);
+       const unique_ptr<char, decltype(&g_free)>
+               message {g_strdup_vprintf(format, args), &g_free};
 
-       LogCallbackFunction callback = *((LogCallbackFunction *) cb_data);
+       auto *const callback = static_cast<LogCallbackFunction *>(cb_data);
 
        try
        {
-               callback(LogLevel::get(loglevel), message);
+               (*callback)(LogLevel::get(loglevel), message.get());
        }
        catch (Error e)
        {
@@ -198,15 +229,28 @@ static int call_log_callback(void *cb_data, int loglevel, const char *format, va
 
 void Context::set_log_callback(LogCallbackFunction callback)
 {
-       _log_callback = callback;
+       _log_callback = move(callback);
        check(sr_log_callback_set(call_log_callback, &_log_callback));
-} 
+}
 
 void Context::set_log_callback_default()
 {
        check(sr_log_callback_set_default());
        _log_callback = nullptr;
-} 
+}
+
+void Context::set_resource_reader(ResourceReader *reader)
+{
+       if (reader) {
+               check(sr_resource_set_hooks(_structure,
+                               &ResourceReader::open_callback,
+                               &ResourceReader::close_callback,
+                               &ResourceReader::read_callback, reader));
+       } else {
+               check(sr_resource_set_hooks(_structure,
+                               nullptr, nullptr, nullptr, nullptr));
+       }
+}
 
 shared_ptr<Session> Context::create_session()
 {
@@ -234,7 +278,7 @@ shared_ptr<Packet> Context::create_header_packet(Glib::TimeVal start_time)
 }
 
 shared_ptr<Packet> Context::create_meta_packet(
-       map<const ConfigKey *, Glib::VariantBase> config)
+       const map<const ConfigKey *, Glib::VariantBase> &config)
 {
        auto meta = g_new0(struct sr_datafeed_meta, 1);
        for (auto input : config)
@@ -267,17 +311,21 @@ shared_ptr<Packet> Context::create_logic_packet(
 }
 
 shared_ptr<Packet> Context::create_analog_packet(
-       vector<shared_ptr<Channel> > channels,
+       const vector<shared_ptr<Channel> > &channels,
        float *data_pointer, unsigned int num_samples, const Quantity *mq,
-       const Unit *unit, vector<const QuantityFlag *> mqflags)
+       const Unit *unit, const vector<const QuantityFlag *> &mqflags)
 {
        auto analog = g_new0(struct sr_datafeed_analog, 1);
+       auto meaning = g_new0(struct sr_analog_meaning, 1);
+
+       analog->meaning = meaning;
+
        for (auto channel : channels)
-               analog->channels = g_slist_append(analog->channels, channel->_structure);
+               meaning->channels = g_slist_append(meaning->channels, channel->_structure);
        analog->num_samples = num_samples;
-       analog->mq = mq->id();
-       analog->unit = unit->id();
-       analog->mqflags = QuantityFlag::mask_from_flags(mqflags);
+       meaning->mq = static_cast<sr_mq>(mq->id());
+       meaning->unit = static_cast<sr_unit>(unit->id());
+       meaning->mqflags = static_cast<sr_mqflag>(QuantityFlag::mask_from_flags(mqflags));
        analog->data = data_pointer;
        auto packet = g_new(struct sr_datafeed_packet, 1);
        packet->type = SR_DF_ANALOG;
@@ -288,13 +336,13 @@ shared_ptr<Packet> Context::create_analog_packet(
 shared_ptr<Session> Context::load_session(string filename)
 {
        return shared_ptr<Session>(
-               new Session(shared_from_this(), filename), Session::Deleter());
+               new Session(shared_from_this(), move(filename)), Session::Deleter());
 }
 
 shared_ptr<Trigger> Context::create_trigger(string name)
 {
        return shared_ptr<Trigger>(
-               new Trigger(shared_from_this(), name), Trigger::Deleter());
+               new Trigger(shared_from_this(), move(name)), Trigger::Deleter());
 }
 
 shared_ptr<Input> Context::open_file(string filename)
@@ -320,21 +368,22 @@ shared_ptr<Input> Context::open_stream(string header)
 
 map<string, string> Context::serials(shared_ptr<Driver> driver)
 {
-       GSList *serial_list = sr_serial_list(driver ? driver->_structure : NULL);
+       GSList *serial_list = sr_serial_list(driver ? driver->_structure : nullptr);
        map<string, string> serials;
 
        for (GSList *serial = serial_list; serial; serial = serial->next) {
-               struct sr_serial_port *port = (sr_serial_port *) serial->data;
+               auto *const port = static_cast<sr_serial_port *>(serial->data);
                serials[string(port->name)] = string(port->description);
        }
 
-       g_slist_free_full(serial_list, (GDestroyNotify)sr_serial_free);
+       g_slist_free_full(serial_list,
+               reinterpret_cast<GDestroyNotify>(&sr_serial_free));
        return serials;
 }
 
 Driver::Driver(struct sr_dev_driver *structure) :
        ParentOwned(structure),
-       Configurable(structure, NULL, NULL),
+       Configurable(structure, nullptr, nullptr),
        _initialized(false)
 {
 }
@@ -354,7 +403,7 @@ string Driver::long_name()
 }
 
 vector<shared_ptr<HardwareDevice>> Driver::scan(
-       map<const ConfigKey *, Glib::VariantBase> options)
+       const map<const ConfigKey *, Glib::VariantBase> &options)
 {
        /* Initialise the driver if not yet done. */
        if (!_initialized)
@@ -364,7 +413,7 @@ vector<shared_ptr<HardwareDevice>> Driver::scan(
        }
 
        /* Translate scan options to GSList of struct sr_config pointers. */
-       GSList *option_list = NULL;
+       GSList *option_list = nullptr;
        for (auto entry : options)
        {
                auto key = entry.first;
@@ -386,7 +435,7 @@ vector<shared_ptr<HardwareDevice>> Driver::scan(
        vector<shared_ptr<HardwareDevice>> result;
        for (GSList *device = device_list; device; device = device->next)
        {
-               auto sdi = (struct sr_dev_inst *) device->data;
+               auto *const sdi = static_cast<struct sr_dev_inst *>(device->data);
                result.push_back(shared_ptr<HardwareDevice>(
                        new HardwareDevice(shared_from_this(), sdi),
                        HardwareDevice::Deleter()));
@@ -421,11 +470,11 @@ Glib::VariantBase Configurable::config_get(const ConfigKey *key)
        return Glib::VariantBase(data);
 }
 
-void Configurable::config_set(const ConfigKey *key, Glib::VariantBase value)
+void Configurable::config_set(const ConfigKey *key, const Glib::VariantBase &value)
 {
        check(sr_config_set(
                config_sdi, config_channel_group,
-               key->id(), value.gobj()));
+               key->id(), const_cast<GVariant*>(value.gobj())));
 }
 
 Glib::VariantContainerBase Configurable::config_list(const ConfigKey *key)
@@ -448,8 +497,8 @@ map<const ConfigKey *, set<Capability>> Configurable::config_keys(const ConfigKe
                config_driver, config_sdi, config_channel_group,
                key->id(), &gvar_opts));
 
-       opts = (const uint32_t *) g_variant_get_fixed_array(
-               gvar_opts, &num_opts, sizeof(uint32_t));
+       opts = static_cast<const uint32_t *>(g_variant_get_fixed_array(
+               gvar_opts, &num_opts, sizeof(uint32_t)));
 
        for (gsize i = 0; i < num_opts; i++)
        {
@@ -480,12 +529,12 @@ bool Configurable::config_check(const ConfigKey *key,
                        index_key->id(), &gvar_opts) != SR_OK)
                return false;
 
-       opts = (const uint32_t *) g_variant_get_fixed_array(
-               gvar_opts, &num_opts, sizeof(uint32_t));
+       opts = static_cast<const uint32_t *>(g_variant_get_fixed_array(
+               gvar_opts, &num_opts, sizeof(uint32_t)));
 
        for (gsize i = 0; i < num_opts; i++)
        {
-               if ((opts[i] & SR_CONF_MASK) == (uint32_t) key->id())
+               if ((opts[i] & SR_CONF_MASK) == unsigned(key->id()))
                {
                        g_variant_unref(gvar_opts);
                        return true;
@@ -498,18 +547,18 @@ bool Configurable::config_check(const ConfigKey *key,
 }
 
 Device::Device(struct sr_dev_inst *structure) :
-       Configurable(sr_dev_inst_driver_get(structure), structure, NULL),
+       Configurable(sr_dev_inst_driver_get(structure), structure, nullptr),
        _structure(structure)
 {
        for (GSList *entry = sr_dev_inst_channels_get(structure); entry; entry = entry->next)
        {
-               auto channel = (struct sr_channel *) entry->data;
+               auto *const channel = static_cast<struct sr_channel *>(entry->data);
                _channels[channel] = new Channel(channel);
        }
 
        for (GSList *entry = sr_dev_inst_channel_groups_get(structure); entry; entry = entry->next)
        {
-               auto group = (struct sr_channel_group *) entry->data;
+               auto *const group = static_cast<struct sr_channel_group *>(entry->data);
                _channel_groups[group->name] = new ChannelGroup(this, group);
        }
 }
@@ -550,10 +599,10 @@ string Device::connection_id()
 vector<shared_ptr<Channel>> Device::channels()
 {
        vector<shared_ptr<Channel>> result;
-       for (auto channel = sr_dev_inst_channels_get(_structure); channel; channel = channel->next)
-               result.push_back(
-                       _channels[(struct sr_channel *) channel->data]->get_shared_pointer(
-                               get_shared_from_this()));
+       for (auto channel = sr_dev_inst_channels_get(_structure); channel; channel = channel->next) {
+               auto *const ch = static_cast<struct sr_channel *>(channel->data);
+               result.push_back(_channels[ch]->get_shared_pointer(get_shared_from_this()));
+       }
        return result;
 }
 
@@ -589,7 +638,7 @@ HardwareDevice::HardwareDevice(shared_ptr<Driver> driver,
                struct sr_dev_inst *structure) :
        UserOwned(structure),
        Device(structure),
-       _driver(driver)
+       _driver(move(driver))
 {
 }
 
@@ -628,11 +677,10 @@ shared_ptr<Channel> UserDevice::add_channel(unsigned int index,
 {
        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);
+       GSList *const last = g_slist_last(sr_dev_inst_channels_get(Device::_structure));
+       auto *const ch = static_cast<struct sr_channel *>(last->data);
+       _channels[ch] = new Channel(ch);
+       return get_channel(ch);
 }
 
 Channel::Channel(struct sr_channel *structure) :
@@ -680,8 +728,10 @@ ChannelGroup::ChannelGroup(Device *device,
        ParentOwned(structure),
        Configurable(sr_dev_inst_driver_get(device->_structure), device->_structure, structure)
 {
-       for (GSList *entry = structure->channels; entry; entry = entry->next)
-               _channels.push_back(device->_channels[(struct sr_channel *)entry->data]);
+       for (GSList *entry = structure->channels; entry; entry = entry->next) {
+               auto *const ch = static_cast<struct sr_channel *>(entry->data);
+               _channels.push_back(device->_channels[ch]);
+       }
 }
 
 ChannelGroup::~ChannelGroup()
@@ -703,11 +753,11 @@ vector<shared_ptr<Channel>> ChannelGroup::channels()
 
 Trigger::Trigger(shared_ptr<Context> context, string name) : 
        UserOwned(sr_trigger_new(name.c_str())),
-       _context(context)
+       _context(move(context))
 {
        for (auto stage = _structure->stages; stage; stage = stage->next)
                _stages.push_back(
-                       new TriggerStage((struct sr_trigger_stage *) stage->data));
+                       new TriggerStage(static_cast<struct sr_trigger_stage *>(stage->data)));
 }
 
 Trigger::~Trigger()
@@ -767,21 +817,22 @@ void TriggerStage::add_match(shared_ptr<Channel> channel,
 {
        check(sr_trigger_match_add(_structure,
                channel->_structure, type->id(), value));
+       GSList *const last = g_slist_last(_structure->matches);
        _matches.push_back(new TriggerMatch(
-               (struct sr_trigger_match *) g_slist_last(
-                       _structure->matches)->data, channel));
+               static_cast<struct sr_trigger_match *>(last->data),
+               move(channel)));
 }
 
 void TriggerStage::add_match(shared_ptr<Channel> channel,
        const TriggerMatchType *type)
 {
-       add_match(channel, type, NAN);
+       add_match(move(channel), type, NAN);
 }
 
 TriggerMatch::TriggerMatch(struct sr_trigger_match *structure,
                shared_ptr<Channel> channel) :
        ParentOwned(structure),
-       _channel(channel)
+       _channel(move(channel))
 {
 }
 
@@ -806,7 +857,7 @@ float TriggerMatch::value()
 
 DatafeedCallbackData::DatafeedCallbackData(Session *session,
                DatafeedCallbackFunction callback) :
-       _callback(callback),
+       _callback(move(callback)),
        _session(session)
 {
 }
@@ -816,56 +867,7 @@ void DatafeedCallbackData::run(const struct sr_dev_inst *sdi,
 {
        auto device = _session->get_device(sdi);
        auto packet = shared_ptr<Packet>(new Packet(device, pkt), Packet::Deleter());
-       _callback(device, packet);
-}
-
-SourceCallbackData::SourceCallbackData(shared_ptr<EventSource> source) :
-       _source(source)
-{
-}
-
-bool SourceCallbackData::run(int revents)
-{
-       return _source->_callback((Glib::IOCondition) revents);
-}
-
-shared_ptr<EventSource> EventSource::create(int fd, Glib::IOCondition events,
-       int timeout, SourceCallbackFunction callback)
-{
-       auto result = new EventSource(timeout, callback);
-       result->_type = EventSource::SOURCE_FD;
-       result->_fd = fd;
-       result->_events = events;
-       return shared_ptr<EventSource>(result, EventSource::Deleter());
-}
-
-shared_ptr<EventSource> EventSource::create(Glib::PollFD pollfd, int timeout,
-       SourceCallbackFunction callback)
-{
-       auto result = new EventSource(timeout, callback);
-       result->_type = EventSource::SOURCE_POLLFD;
-       result->_pollfd = pollfd;
-       return shared_ptr<EventSource>(result, EventSource::Deleter());
-}
-
-shared_ptr<EventSource> EventSource::create(Glib::RefPtr<Glib::IOChannel> channel,
-       Glib::IOCondition events, int timeout, SourceCallbackFunction callback)
-{
-       auto result = new EventSource(timeout, callback);
-       result->_type = EventSource::SOURCE_IOCHANNEL;
-       result->_channel = channel;
-       result->_events = events;
-       return shared_ptr<EventSource>(result, EventSource::Deleter());
-}
-
-EventSource::EventSource(int timeout, SourceCallbackFunction callback) :
-       _timeout(timeout),
-       _callback(callback)
-{
-}
-
-EventSource::~EventSource()
-{
+       _callback(move(device), move(packet));
 }
 
 SessionDevice::SessionDevice(struct sr_dev_inst *structure) :
@@ -885,25 +887,22 @@ shared_ptr<Device> SessionDevice::get_shared_from_this()
 
 Session::Session(shared_ptr<Context> context) :
        UserOwned(_structure),
-       _context(context),
-       _saving(false)
+       _context(move(context))
 {
-       check(sr_session_new(context->_structure, &_structure));
+       check(sr_session_new(_context->_structure, &_structure));
        _context->_session = this;
 }
 
 Session::Session(shared_ptr<Context> context, string filename) :
        UserOwned(_structure),
-       _context(context),
-       _filename(filename),
-       _saving(false)
+       _context(move(context)),
+       _filename(move(filename))
 {
-       check(sr_session_load(context->_structure, filename.c_str(), &_structure));
+       check(sr_session_load(_context->_structure, _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;
+       for (GSList *dev = dev_list; dev; dev = dev->next) {
+               auto *const sdi = static_cast<struct sr_dev_inst *>(dev->data);
                _owned_devices[sdi] = new SessionDevice(sdi);
        }
        _context->_session = this;
@@ -916,9 +915,6 @@ Session::~Session()
        for (auto callback : _datafeed_callbacks)
                delete callback;
 
-       for (auto entry : _source_callbacks)
-               delete entry.second;
-
        for (auto entry : _owned_devices)
                delete entry.second;
 }
@@ -936,8 +932,9 @@ shared_ptr<Device> Session::get_device(const struct sr_dev_inst *sdi)
 
 void Session::add_device(shared_ptr<Device> device)
 {
-       check(sr_session_dev_add(_structure, device->_structure));
-       _other_devices[device->_structure] = device;
+       const auto dev_struct = device->_structure;
+       check(sr_session_dev_add(_structure, dev_struct));
+       _other_devices[dev_struct] = move(device);
 }
 
 vector<shared_ptr<Device>> Session::devices()
@@ -945,9 +942,8 @@ vector<shared_ptr<Device>> Session::devices()
        GSList *dev_list;
        check(sr_session_dev_list(_structure, &dev_list));
        vector<shared_ptr<Device>> result;
-       for (GSList *dev = dev_list; dev; dev = dev->next)
-       {
-               auto sdi = (struct sr_dev_inst *) dev->data;
+       for (GSList *dev = dev_list; dev; dev = dev->next) {
+               auto *const sdi = static_cast<struct sr_dev_inst *>(dev->data);
                result.push_back(get_device(sdi));
        }
        return result;
@@ -974,91 +970,29 @@ void Session::stop()
        check(sr_session_stop(_structure));
 }
 
-void Session::begin_save(string filename)
+bool Session::is_running() const
 {
-       _saving = true;
-       _save_initialized = false;
-       _save_filename = filename;
-       _save_samplerate = 0;
+       const int ret = sr_session_is_running(_structure);
+       if (ret < 0)
+               throw Error{ret};
+       return (ret != 0);
 }
 
-void Session::append(shared_ptr<Packet> packet)
+static void session_stopped_callback(void *data)
 {
-       if (!_saving)
-               throw Error(SR_ERR);
-
-       switch (packet->_structure->type)
-       {
-               case SR_DF_META:
-               {
-                       auto meta = (const struct sr_datafeed_meta *)
-                               packet->_structure->payload;
-
-                       for (auto l = meta->config; l; l = l->next)
-                       {
-                               auto config = (struct sr_config *) l->data;
-                               if (config->key == SR_CONF_SAMPLERATE)
-                                       _save_samplerate = g_variant_get_uint64(config->data);
-                       }
-
-                       break;
-               }
-               case SR_DF_LOGIC:
-               {
-                       if (_save_samplerate == 0)
-                       {
-                               GVariant *samplerate;
-
-                               check(sr_config_get(sr_dev_inst_driver_get(packet->_device->_structure),
-                                       packet->_device->_structure, NULL, SR_CONF_SAMPLERATE,
-                                       &samplerate));
-
-                               _save_samplerate = g_variant_get_uint64(samplerate);
-
-                               g_variant_unref(samplerate);
-                       }
-
-                       if (!_save_initialized)
-                       {
-                               vector<shared_ptr<Channel>> save_channels;
-
-                               for (auto channel : packet->_device->channels())
-                                       if (channel->_structure->enabled &&
-                                                       channel->_structure->type == SR_CHANNEL_LOGIC)
-                                               save_channels.push_back(channel);
-
-                               auto channels = g_new(char *, save_channels.size());
-
-                               int i = 0;
-                               for (auto channel : save_channels)
-                                               channels[i++] = channel->_structure->name;
-                               channels[i] = NULL;
-
-                               int ret = sr_session_save_init(_structure, _save_filename.c_str(),
-                                               _save_samplerate, channels);
-
-                               g_free(channels);
-
-                               if (ret != SR_OK)
-                                       throw Error(ret);
-
-                               _save_initialized = true;
-                       }
-
-                       auto logic = (const struct sr_datafeed_logic *)
-                               packet->_structure->payload;
-
-                       check(sr_session_append(_structure, _save_filename.c_str(),
-                               (uint8_t *) logic->data, logic->unitsize,
-                               logic->length / logic->unitsize));
-               }
-       }
+       auto *const callback = static_cast<SessionStoppedCallback*>(data);
+       (*callback)();
 }
 
-void Session::append(void *data, size_t length, unsigned int unit_size)
+void Session::set_stopped_callback(SessionStoppedCallback callback)
 {
-       check(sr_session_append(_structure, _save_filename.c_str(),
-               (uint8_t *) data, unit_size, length));
+       _stopped_callback = move(callback);
+       if (_stopped_callback)
+               check(sr_session_stopped_callback_set(_structure,
+                               &session_stopped_callback, &_stopped_callback));
+       else
+               check(sr_session_stopped_callback_set(_structure,
+                               nullptr, nullptr));
 }
 
 static void datafeed_callback(const struct sr_dev_inst *sdi,
@@ -1067,16 +1001,16 @@ static void datafeed_callback(const struct sr_dev_inst *sdi,
        auto callback = static_cast<DatafeedCallbackData *>(cb_data);
        callback->run(sdi, pkt);
 }
-       
+
 void Session::add_datafeed_callback(DatafeedCallbackFunction callback)
 {
-       auto cb_data = new DatafeedCallbackData(this, callback);
+       auto cb_data = new DatafeedCallbackData(this, move(callback));
        check(sr_session_datafeed_callback_add(_structure,
                datafeed_callback, cb_data));
        _datafeed_callbacks.push_back(cb_data);
 }
 
-void Session::remove_datafeed_callbacks(void)
+void Session::remove_datafeed_callbacks()
 {
        check(sr_session_datafeed_callback_remove_all(_structure));
        for (auto callback : _datafeed_callbacks)
@@ -1084,66 +1018,6 @@ void Session::remove_datafeed_callbacks(void)
        _datafeed_callbacks.clear();
 }
 
-static int source_callback(int fd, int revents, void *cb_data)
-{
-       (void) fd;
-       auto callback = (SourceCallbackData *) cb_data;
-       return callback->run(revents);
-}
-
-void Session::add_source(shared_ptr<EventSource> source)
-{
-       if (_source_callbacks.count(source) == 1)
-               throw Error(SR_ERR_ARG);
-
-       auto cb_data = new SourceCallbackData(source);
-
-       switch (source->_type)
-       {
-               case EventSource::SOURCE_FD:
-                       check(sr_session_source_add(_structure, source->_fd, source->_events,
-                               source->_timeout, source_callback, cb_data));
-                       break;
-               case EventSource::SOURCE_POLLFD:
-                       check(sr_session_source_add_pollfd(_structure,
-                               source->_pollfd.gobj(), source->_timeout, source_callback,
-                               cb_data));
-                       break;
-               case EventSource::SOURCE_IOCHANNEL:
-                       check(sr_session_source_add_channel(_structure,
-                               source->_channel->gobj(), source->_events, source->_timeout,
-                               source_callback, cb_data));
-                       break;
-       }
-
-       _source_callbacks[source] = cb_data;
-}
-
-void Session::remove_source(shared_ptr<EventSource> source)
-{
-       if (_source_callbacks.count(source) == 0)
-               throw Error(SR_ERR_ARG);
-
-       switch (source->_type)
-       {
-               case EventSource::SOURCE_FD:
-                       check(sr_session_source_remove(_structure, source->_fd));
-                       break;
-               case EventSource::SOURCE_POLLFD:
-                       check(sr_session_source_remove_pollfd(_structure,
-                               source->_pollfd.gobj()));
-                       break;
-               case EventSource::SOURCE_IOCHANNEL:
-                       check(sr_session_source_remove_channel(_structure,
-                               source->_channel->gobj()));
-                       break;
-       }
-
-       delete _source_callbacks[source];
-
-       _source_callbacks.erase(source);
-}
-
 shared_ptr<Trigger> Session::trigger()
 {
        return _trigger;
@@ -1153,10 +1027,10 @@ void Session::set_trigger(shared_ptr<Trigger> trigger)
 {
        if (!trigger)
                // Set NULL trigger, i.e. remove any trigger from the session.
-               check(sr_session_trigger_set(_structure, NULL));
+               check(sr_session_trigger_set(_structure, nullptr));
        else
                check(sr_session_trigger_set(_structure, trigger->_structure));
-       _trigger = trigger;
+       _trigger = move(trigger);
 }
 
 string Session::filename()
@@ -1172,7 +1046,7 @@ shared_ptr<Context> Session::context()
 Packet::Packet(shared_ptr<Device> device,
        const struct sr_datafeed_packet *structure) :
        UserOwned(structure),
-       _device(device)
+       _device(move(device))
 {
        switch (structure->type)
        {
@@ -1276,9 +1150,8 @@ shared_ptr<PacketPayload> Meta::get_shared_pointer(Packet *_parent)
 map<const ConfigKey *, Glib::VariantBase> Meta::config()
 {
        map<const ConfigKey *, Glib::VariantBase> result;
-       for (auto l = _structure->config; l; l = l->next)
-       {
-               auto config = (struct sr_config *) l->data;
+       for (auto l = _structure->config; l; l = l->next) {
+               auto *const config = static_cast<struct sr_config *>(l->data);
                result[ConfigKey::get(config->key)] = Glib::VariantBase(config->data);
        }
        return result;
@@ -1331,7 +1204,7 @@ shared_ptr<PacketPayload> Analog::get_shared_pointer(Packet *_parent)
                ParentOwned::get_shared_pointer(_parent));
 }
 
-float *Analog::data_pointer()
+void *Analog::data_pointer()
 {
        return _structure->data;
 }
@@ -1344,25 +1217,26 @@ unsigned int Analog::num_samples()
 vector<shared_ptr<Channel>> Analog::channels()
 {
        vector<shared_ptr<Channel>> result;
-       for (auto l = _structure->channels; l; l = l->next)
-               result.push_back(_parent->_device->get_channel(
-                       (struct sr_channel *)l->data));
+       for (auto l = _structure->meaning->channels; l; l = l->next) {
+               auto *const ch = static_cast<struct sr_channel *>(l->data);
+               result.push_back(_parent->_device->get_channel(ch));
+       }
        return result;
 }
 
 const Quantity *Analog::mq()
 {
-       return Quantity::get(_structure->mq);
+       return Quantity::get(_structure->meaning->mq);
 }
 
 const Unit *Analog::unit()
 {
-       return Unit::get(_structure->unit);
+       return Unit::get(_structure->meaning->unit);
 }
 
 vector<const QuantityFlag *> Analog::mq_flags()
 {
-       return QuantityFlag::flags_from_mask(_structure->mqflags);
+       return QuantityFlag::flags_from_mask(_structure->meaning->mqflags);
 }
 
 InputFormat::InputFormat(const struct sr_input_module *structure) :
@@ -1409,7 +1283,7 @@ map<string, shared_ptr<Option>> InputFormat::options()
 }
 
 shared_ptr<Input> InputFormat::create_input(
-       map<string, Glib::VariantBase> options)
+       const map<string, Glib::VariantBase> &options)
 {
        auto input = sr_input_new(_structure, map_to_hash_variant(options));
        if (!input)
@@ -1420,7 +1294,7 @@ shared_ptr<Input> InputFormat::create_input(
 
 Input::Input(shared_ptr<Context> context, const struct sr_input *structure) :
        UserOwned(structure),
-       _context(context),
+       _context(move(context)),
        _device(nullptr)
 {
 }
@@ -1438,9 +1312,9 @@ shared_ptr<InputDevice> Input::device()
        return _device->get_shared_pointer(shared_from_this());
 }
 
-void Input::send(string data)
+void Input::send(void *data, size_t length)
 {
-       auto gstr = g_string_new(data.c_str());
+       auto gstr = g_string_new_len(static_cast<char *>(data), length);
        auto ret = sr_input_send(_structure, gstr);
        g_string_free(gstr, false);
        check(ret);
@@ -1462,7 +1336,7 @@ InputDevice::InputDevice(shared_ptr<Input> input,
                struct sr_dev_inst *structure) :
        ParentOwned(structure),
        Device(structure),
-       _input(input)
+       _input(move(input))
 {
 }
 
@@ -1478,7 +1352,7 @@ shared_ptr<Device> InputDevice::get_shared_from_this()
 Option::Option(const struct sr_option *structure,
                shared_ptr<const struct sr_option *> structure_array) :
        UserOwned(structure),
-       _structure_array(structure_array)
+       _structure_array(move(structure_array))
 {
 }
 
@@ -1509,8 +1383,10 @@ Glib::VariantBase Option::default_value()
 vector<Glib::VariantBase> Option::values()
 {
        vector<Glib::VariantBase> result;
-       for (auto l = _structure->values; l; l = l->next)
-               result.push_back(Glib::VariantBase((GVariant *) l->data, true));
+       for (auto l = _structure->values; l; l = l->next) {
+               auto *const var = static_cast<GVariant *>(l->data);
+               result.push_back(Glib::VariantBase(var, true));
+       }
        return result;
 }
 
@@ -1558,19 +1434,42 @@ map<string, shared_ptr<Option>> OutputFormat::options()
 }
 
 shared_ptr<Output> OutputFormat::create_output(
-       shared_ptr<Device> device, map<string, Glib::VariantBase> options)
+       shared_ptr<Device> device, const map<string, Glib::VariantBase> &options)
+{
+       return shared_ptr<Output>(
+               new Output(shared_from_this(), move(device), options),
+               Output::Deleter());
+}
+
+shared_ptr<Output> OutputFormat::create_output(string filename,
+       shared_ptr<Device> device, const map<string, Glib::VariantBase> &options)
 {
        return shared_ptr<Output>(
-               new Output(shared_from_this(), device, options),
+               new Output(move(filename), shared_from_this(), move(device), options),
                Output::Deleter());
 }
 
+bool OutputFormat::test_flag(const OutputFlag *flag)
+{
+       return sr_output_test_flag(_structure, flag->id());
+}
+
 Output::Output(shared_ptr<OutputFormat> format,
-               shared_ptr<Device> device, map<string, Glib::VariantBase> options) :
+               shared_ptr<Device> device, const map<string, Glib::VariantBase> &options) :
+       UserOwned(sr_output_new(format->_structure,
+               map_to_hash_variant(options), device->_structure, nullptr)),
+       _format(move(format)),
+       _device(move(device)),
+       _options(options)
+{
+}
+
+Output::Output(string filename, shared_ptr<OutputFormat> format,
+               shared_ptr<Device> device, const map<string, Glib::VariantBase> &options) :
        UserOwned(sr_output_new(format->_structure,
-               map_to_hash_variant(options), device->_structure)),
-       _format(format),
-       _device(device),
+               map_to_hash_variant(options), device->_structure, filename.c_str())),
+       _format(move(format)),
+       _device(move(device)),
        _options(options)
 {
 }
@@ -1596,6 +1495,6 @@ string Output::receive(shared_ptr<Packet> packet)
        }
 }
 
-#include "enums.cpp"
+#include <enums.cpp>
 
 }