From b48daed65bff5da5fddc6db11377b8730220865f Mon Sep 17 00:00:00 2001 From: Joel Holdsworth Date: Sat, 18 Apr 2015 00:09:03 +0100 Subject: [PATCH] Device: Replaced Session::read_sample_rate with read_config --- pv/devices/device.cpp | 37 +++++++++++++++++++++++++++++++++++++ pv/devices/device.hpp | 4 ++++ pv/session.cpp | 22 ++-------------------- pv/session.hpp | 2 -- 4 files changed, 43 insertions(+), 22 deletions(-) diff --git a/pv/devices/device.cpp b/pv/devices/device.cpp index 004749c9..6a36ff38 100644 --- a/pv/devices/device.cpp +++ b/pv/devices/device.cpp @@ -24,6 +24,15 @@ #include "device.hpp" +using std::map; +using std::set; + +using sigrok::ConfigKey; +using sigrok::Error; + +using Glib::VariantBase; +using Glib::Variant; + namespace pv { namespace devices { @@ -43,6 +52,34 @@ std::shared_ptr Device::device() const { return device_; } +template +uint64_t Device::read_config(const sigrok::ConfigKey*, + const uint64_t); + +template +T Device::read_config(const ConfigKey *key, const T default_value) +{ + assert(key); + map< const ConfigKey*, set > keys; + + if (!device_) + return default_value; + + try { + keys = device_->config_keys(ConfigKey::DEVICE_OPTIONS); + } catch (const Error) { + return default_value; + } + + const auto iter = keys.find(key); + if (iter == keys.end() || + (*iter).second.find(sigrok::GET) != (*iter).second.end()) + return default_value; + + return VariantBase::cast_dynamic>( + device_->config_get(ConfigKey::SAMPLERATE)).get(); +} + void Device::run() { assert(device_); assert(session_); diff --git a/pv/devices/device.hpp b/pv/devices/device.hpp index 7e0b58ad..612d7509 100644 --- a/pv/devices/device.hpp +++ b/pv/devices/device.hpp @@ -25,6 +25,7 @@ #include namespace sigrok { +class ConfigKey; class Device; class Session; } // namespace sigrok @@ -47,6 +48,9 @@ public: std::shared_ptr device() const; + template + T read_config(const sigrok::ConfigKey *key, const T default_value = 0); + /** * Builds the full name. It only contains all the fields. */ diff --git a/pv/session.cpp b/pv/session.cpp index e187a4d3..ecbb4722 100644 --- a/pv/session.cpp +++ b/pv/session.cpp @@ -405,31 +405,13 @@ shared_ptr Session::signal_from_channel( return shared_ptr(); } -void Session::read_sample_rate(shared_ptr device) -{ - assert(device); - map< const ConfigKey*, set > keys; - - try { - keys = device->config_keys(ConfigKey::DEVICE_OPTIONS); - } catch (const Error) {} - - const auto iter = keys.find(ConfigKey::SAMPLERATE); - cur_samplerate_ = (iter != keys.end() && - (*iter).second.find(sigrok::GET) != (*iter).second.end()) ? - VariantBase::cast_dynamic>( - device->config_get(ConfigKey::SAMPLERATE)).get() : 0; -} - void Session::sample_thread_proc(shared_ptr device, function error_handler) { assert(device); assert(error_handler); - const std::shared_ptr sr_dev = device->device(); - assert(sr_dev); - read_sample_rate(sr_dev); + cur_samplerate_ = device_->read_config(ConfigKey::SAMPLERATE); try { device_->session()->start(); @@ -454,7 +436,7 @@ void Session::sample_thread_proc(shared_ptr device, void Session::feed_in_header() { - read_sample_rate(device_->device()); + cur_samplerate_ = device_->read_config(ConfigKey::SAMPLERATE); } void Session::feed_in_meta(shared_ptr meta) diff --git a/pv/session.hpp b/pv/session.hpp index 90dd8237..59060a59 100644 --- a/pv/session.hpp +++ b/pv/session.hpp @@ -136,8 +136,6 @@ private: std::shared_ptr signal_from_channel( std::shared_ptr channel) const; - void read_sample_rate(std::shared_ptr device); - private: void sample_thread_proc(std::shared_ptr device, std::function error_handler); -- 2.30.2