USRP Hardware Driver and Device Manual Version: 4.11.0.0-0-unknown
UHD and USRP Manual
Loading...
Searching...
No Matches
property.hpp
Go to the documentation of this file.
1//
2// Copyright 2019 Ettus Research, a National Instruments Brand
3//
4// SPDX-License-Identifier: GPL-3.0-or-later
5//
6
7#pragma once
8
9#include <uhd/exception.hpp>
11#include <uhd/utils/cast.hpp>
13#include <memory>
14#include <string>
15
16
17namespace uhd { namespace rfnoc {
18
19// Forward declaration, separates includes
20class prop_accessor_t;
21
26{
27public:
28 enum access_t {
30 RO = 0x1,
31 RW = 0x3,
32 RWLOCKED = 0x5
34 };
35
36 property_base_t(const std::string& id, const res_source_info& source_info)
37 : _id(id), _source_info(source_info)
38 {
39 if (_id.find(':') != std::string::npos) {
40 throw uhd::value_error(
41 "Property ID `" + _id + "' contains invalid character!");
42 }
43 }
44
46 {
47 // nop
48 }
49
51 const std::string& get_id() const
52 {
53 return _id;
54 }
55
58 {
59 return _source_info;
60 }
61
67 virtual bool is_dirty() const = 0;
68
74 virtual bool is_valid() const = 0;
75
78 {
79 return static_cast<uint8_t>(_access_mode) & 0x1;
80 }
81
84 {
85 return static_cast<uint8_t>(_access_mode) & 0x2;
86 }
87
90 {
91 return _access_mode;
92 }
93
95 virtual bool equal(property_base_t* rhs) const = 0;
96
106 virtual std::unique_ptr<property_base_t> clone(res_source_info)
107 {
108 throw uhd::not_implemented_error("Cloning is not available for this property.");
109 }
110
111 virtual void force_dirty() = 0;
112
121 virtual void set_from_str(const std::string& new_val_str) = 0;
122
123private:
124 friend class prop_accessor_t;
125
127 virtual void mark_clean() = 0;
128
130 virtual void forward(property_base_t* next_prop) = 0;
131
136 virtual bool is_type_equal(property_base_t* other_prop) const = 0;
137
138 /*** Attributes **********************************************************/
140 const std::string _id;
141
143 const res_source_info _source_info;
144
148 access_t _access_mode = RO;
149};
150
154template <typename data_t>
156{
157public:
159 using value_type = data_t;
160
161 property_t(const std::string& id, data_t&& value, const res_source_info& source_info);
162
164 const std::string& id, const data_t& value, const res_source_info& source_info);
165
166 property_t(const std::string& id, const res_source_info& source_info);
167
168 property_t(const property_t<data_t>& prop) = default;
169
175 bool is_dirty() const override
176 {
177 return _data.is_dirty();
178 }
179
185 bool is_valid() const override
186 {
187 return _valid;
188 }
189
190 bool equal(property_base_t* rhs) const override
191 {
192 if (!is_type_equal(rhs)) {
193 return false;
194 }
195 return get() == dynamic_cast<property_t<data_t>*>(rhs)->get();
196 }
197
198 std::unique_ptr<property_base_t> clone(res_source_info new_src_info) override
199 {
200 return std::unique_ptr<property_base_t>(
201 new property_t<data_t>(get_id(), get(), new_src_info));
202 }
203
204 void set_from_str(const std::string& new_val_str) override
205 {
206 try {
207 set(uhd::cast::from_str<data_t>(new_val_str));
208 } catch (uhd::runtime_error& ex) {
209 throw uhd::runtime_error(
210 std::string("Property ") + get_id() + ":" + ex.what());
211 }
212 }
213
220 void set(const data_t& value)
221 {
222 if (write_access_granted()) {
223 _data = value;
224 _valid = true;
225 } else if (get_access_mode() == RWLOCKED) {
226 if (_data.get() != value) {
227 throw uhd::resolve_error(std::string("Attempting to overwrite property `")
228 + get_id() + "@" + get_src_info().to_string()
229 + "' with a new value after it was locked!");
230 }
231 } else {
232 throw uhd::access_error(std::string("Attempting to write to property `")
233 + get_id() + "' without access privileges!");
234 }
235 }
236
237 void force_dirty() override
238 {
239 if (write_access_granted()) {
240 _data.force_dirty();
241 } else if (get_access_mode() == RWLOCKED) {
242 if (!_data.is_dirty()) {
243 throw uhd::resolve_error(std::string("Attempting to overwrite property `")
244 + get_id()
245 + "' with dirty flag after it was locked!");
246 }
247 } else {
248 throw uhd::access_error(std::string("Attempting to flag dirty property `")
249 + get_id() + "' without access privileges!");
250 }
251 }
252
258 const data_t& get() const
259 {
260 if (!is_valid()) {
261 throw uhd::access_error(std::string("Attempting to read property `")
262 + get_id() + "@" + get_src_info().to_string()
263 + "' before it was initialized!");
264 }
265 if (read_access_granted()) {
266 return _data;
267 }
268 throw uhd::access_error(std::string("Attempting to read property `") + get_id()
269 + "' without access privileges!");
270 }
271
272 operator const data_t&() const
273 {
274 return get();
275 }
276
277 bool operator==(const data_t& rhs)
278 {
279 return get() == rhs;
280 }
281
282 property_t<data_t>& operator=(const data_t& value)
283 {
284 set(value);
285 return *this;
286 }
287
288private:
289 void mark_clean() override
290 {
291 _data.mark_clean();
292 }
293
294 void forward(property_base_t* next_prop) override
295 {
296 if (not _valid) {
297 throw uhd::resolve_error(
298 std::string("Unable to forward invalid property ") + get_id());
299 }
300 property_t<data_t>* prop_ptr = dynamic_cast<property_t<data_t>*>(next_prop);
301 if (prop_ptr == nullptr) {
302 throw uhd::type_error(std::string("Unable to cast property ")
303 + next_prop->get_id() + " to the same type as property "
304 + get_id());
305 }
306
307 prop_ptr->set(get());
308 }
309
310 bool is_type_equal(property_base_t* other_prop) const override
311 {
312 return dynamic_cast<property_t<data_t>*>(other_prop) != nullptr;
313 }
314
315 dirty_tracked<data_t> _data;
316 bool _valid;
317}; // class property_t
318
319}} /* namespace uhd::rfnoc */
320
321#include <uhd/rfnoc/property.ipp>
const std::string & get_id() const
Gets the ID (name) of this property.
Definition property.hpp:51
virtual void force_dirty()=0
const res_source_info & get_src_info() const
Return the source info for this property.
Definition property.hpp:57
virtual bool equal(property_base_t *rhs) const =0
Return true if rhs has the same type and value.
access_t
Definition property.hpp:28
@ RO
Read-Only.
Definition property.hpp:30
@ RW
Read-Write.
Definition property.hpp:31
@ NONE
Neither reading nor writing to this property is permitted.
Definition property.hpp:29
@ RWLOCKED
Definition property.hpp:32
property_base_t(const std::string &id, const res_source_info &source_info)
Definition property.hpp:36
virtual void set_from_str(const std::string &new_val_str)=0
friend class prop_accessor_t
Definition property.hpp:124
virtual bool is_valid() const =0
Query this property's valid flag.
virtual std::unique_ptr< property_base_t > clone(res_source_info)
Create a copy of this property.
Definition property.hpp:106
access_t get_access_mode() const
Return the current access mode.
Definition property.hpp:89
virtual ~property_base_t()
Definition property.hpp:45
bool write_access_granted() const
Returns true if this property can be written to.
Definition property.hpp:83
bool read_access_granted() const
Returns true if this property can be read.
Definition property.hpp:77
virtual bool is_dirty() const =0
Query this property's dirty flag.
const data_t & get() const
Get the value of this property.
Definition property.hpp:258
void set(const data_t &value)
Set the value of this property.
Definition property.hpp:220
bool is_dirty() const override
Returns the dirty state of this property.
Definition property.hpp:175
property_t(const std::string &id, data_t &&value, const res_source_info &source_info)
Definition property.ipp:10
property_t< data_t > & operator=(const data_t &value)
Definition property.hpp:282
bool is_valid() const override
Query this property's valid flag.
Definition property.hpp:185
void set_from_str(const std::string &new_val_str) override
Definition property.hpp:204
data_t value_type
We want to be good C++ citizens.
Definition property.hpp:159
bool operator==(const data_t &rhs)
Definition property.hpp:277
bool equal(property_base_t *rhs) const override
Return true if rhs has the same type and value.
Definition property.hpp:190
void force_dirty() override
Definition property.hpp:237
std::unique_ptr< property_base_t > clone(res_source_info new_src_info) override
Create a copy of this property.
Definition property.hpp:198
property_t(const property_t< data_t > &prop)=default
#define UHD_API_HEADER
Definition config.h:88
#define UHD_API
Definition config.h:87
data_t from_str(const std::string &val)
Generic cast-from-string function.
Definition cast.hpp:70
Definition actions.hpp:25
Definition build_info.hpp:12
Definition exception.hpp:168
Definition exception.hpp:157
Definition exception.hpp:281
Definition res_source_info.hpp:18
Definition exception.hpp:132
Definition exception.hpp:96