]> sigrok.org Git - pulseview.git/commitdiff
Device: Replaced Session::read_sample_rate with read_config
authorJoel Holdsworth <redacted>
Fri, 17 Apr 2015 23:09:03 +0000 (00:09 +0100)
committerUwe Hermann <redacted>
Thu, 11 Jun 2015 18:38:47 +0000 (20:38 +0200)
pv/devices/device.cpp
pv/devices/device.hpp
pv/session.cpp
pv/session.hpp

index 004749c9462f5bee93bd983901151737d5ea3337..6a36ff384a990b5e9245c8b9d757e903aa56cc5f 100644 (file)
 
 #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<sigrok::Device> Device::device() const {
        return device_;
 }
 
+template
+uint64_t Device::read_config(const sigrok::ConfigKey*,
+       const uint64_t);
+
+template<typename T>
+T Device::read_config(const ConfigKey *key, const T default_value)
+{
+       assert(key);
+       map< const ConfigKey*, set<sigrok::Capability> > 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<Variant<T>>(
+               device_->config_get(ConfigKey::SAMPLERATE)).get();
+}
+
 void Device::run() {
        assert(device_);
        assert(session_);
index 7e0b58ad065600bdc993751f343ff8eb348d4de1..612d750940612e70e24e9eb27291590585f709d3 100644 (file)
@@ -25,6 +25,7 @@
 #include <string>
 
 namespace sigrok {
+class ConfigKey;
 class Device;
 class Session;
 } // namespace sigrok
@@ -47,6 +48,9 @@ public:
 
        std::shared_ptr<sigrok::Device> device() const;
 
+       template<typename T>
+       T read_config(const sigrok::ConfigKey *key, const T default_value = 0);
+
        /**
         * Builds the full name. It only contains all the fields.
         */
index e187a4d35245aecefe25b344433346595664c1f4..ecbb47229693c73faf7773354c58e242b888ab49 100644 (file)
@@ -405,31 +405,13 @@ shared_ptr<view::Signal> Session::signal_from_channel(
        return shared_ptr<view::Signal>();
 }
 
-void Session::read_sample_rate(shared_ptr<sigrok::Device> device)
-{
-       assert(device);
-       map< const ConfigKey*, set<sigrok::Capability> > 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<Variant<guint64>>(
-                       device->config_get(ConfigKey::SAMPLERATE)).get() : 0;
-}
-
 void Session::sample_thread_proc(shared_ptr<devices::Device> device,
        function<void (const QString)> error_handler)
 {
        assert(device);
        assert(error_handler);
 
-       const std::shared_ptr<sigrok::Device> sr_dev = device->device();
-       assert(sr_dev);
-       read_sample_rate(sr_dev);
+       cur_samplerate_ = device_->read_config<uint64_t>(ConfigKey::SAMPLERATE);
 
        try {
                device_->session()->start();
@@ -454,7 +436,7 @@ void Session::sample_thread_proc(shared_ptr<devices::Device> device,
 
 void Session::feed_in_header()
 {
-       read_sample_rate(device_->device());
+       cur_samplerate_ = device_->read_config<uint64_t>(ConfigKey::SAMPLERATE);
 }
 
 void Session::feed_in_meta(shared_ptr<Meta> meta)
index 90dd82374675d768dd1d68a3c94e2ba5cc9e276e..59060a591fdfd0d743ad83780dbcd066cdefbc01 100644 (file)
@@ -136,8 +136,6 @@ private:
        std::shared_ptr<view::Signal> signal_from_channel(
                std::shared_ptr<sigrok::Channel> channel) const;
 
-       void read_sample_rate(std::shared_ptr<sigrok::Device> device);
-
 private:
        void sample_thread_proc(std::shared_ptr<devices::Device> device,
                std::function<void (const QString)> error_handler);