]> sigrok.org Git - pulseview.git/blob - pv/devices/device.cpp
Fix random clazy warnings
[pulseview.git] / pv / devices / device.cpp
1 /*
2  * This file is part of the PulseView project.
3  *
4  * Copyright (C) 2015 Joel Holdsworth <joel@airwebreathe.org.uk>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <cassert>
21 #include <type_traits>
22
23 #include <QApplication>
24 #include <QDebug>
25 #include <QString>
26
27 #include <libsigrokcxx/libsigrokcxx.hpp>
28
29 #include "device.hpp"
30
31 using std::is_same;
32 using std::shared_ptr;
33
34 using sigrok::ConfigKey;
35 using sigrok::Capability;
36
37 using Glib::VariantBase;
38 using Glib::Variant;
39
40 namespace pv {
41 namespace devices {
42
43 Device::~Device()
44 {
45         if (session_)
46                 session_->remove_datafeed_callbacks();
47 }
48
49 shared_ptr<sigrok::Session> Device::session() const
50 {
51         return session_;
52 }
53
54 shared_ptr<sigrok::Device> Device::device() const
55 {
56         return device_;
57 }
58
59 template uint64_t Device::read_config(const sigrok::ConfigKey*, const uint64_t);
60
61 template<typename T>
62 T Device::read_config(const ConfigKey *key, const T default_value)
63 {
64         assert(key);
65
66         if (!device_)
67                 return default_value;
68
69         if (!device_->config_check(key, Capability::GET)) {
70                 qWarning() << QApplication::tr("Querying config key %1 is not allowed")
71                         .arg(QString::fromStdString(key->identifier()));
72                 return default_value;
73         }
74
75         VariantBase value;
76         try {
77                 value = device_->config_get(key);
78         } catch (const sigrok::Error &e) {
79                 qWarning() << QApplication::tr("Querying config key %1 resulted in %2")
80                         .arg(QString::fromStdString(key->identifier()), e.what());
81                 return default_value;
82         }
83
84         if (is_same<T, uint32_t>::value)
85                 return VariantBase::cast_dynamic<Glib::Variant<guint32>>(value).get();
86         if (is_same<T, int32_t>::value)
87                 return VariantBase::cast_dynamic<Glib::Variant<gint32>>(value).get();
88         if (is_same<T, uint64_t>::value)
89                 return VariantBase::cast_dynamic<Glib::Variant<guint64>>(value).get();
90         if (is_same<T, int64_t>::value)
91                 return VariantBase::cast_dynamic<Glib::Variant<gint64>>(value).get();
92
93         qWarning() << QApplication::tr("Unknown type supplied when attempting to query %1")
94                 .arg(QString::fromStdString(key->identifier()));
95         return default_value;
96 }
97
98 void Device::start()
99 {
100         assert(session_);
101         session_->start();
102 }
103
104 void Device::run()
105 {
106         assert(device_);
107         assert(session_);
108         session_->run();
109 }
110
111 void Device::stop()
112 {
113         assert(session_);
114         session_->stop();
115 }
116
117 } // namespace devices
118 } // namespace pv