USRP Hardware Driver and USRP Manual  Version: 4.6.0.0
UHD and USRP Manual
block_id.hpp
Go to the documentation of this file.
1 // Copyright 2014 Ettus Research LLC
2 // Copyright 2018 Ettus Research, a National Instruments Company
3 // Copyright 2019 Ettus Research, a National Instruments Company
4 //
5 // SPDX-License-Identifier: GPL-3.0-or-later
6 //
7 
8 #pragma once
9 
10 #include <uhd/config.hpp>
11 #include <stdint.h>
12 #include <iostream>
13 #include <memory>
14 #include <string>
15 
16 namespace uhd {
17 struct fs_path;
18 
19 namespace rfnoc {
20 
40 {
41 public:
42  block_id_t();
43  block_id_t(const std::string& block_str);
47  block_id_t(const size_t device_no,
48  const std::string& block_name,
49  const size_t block_ctr = 0);
50 
52  std::string to_string() const;
53 
55  //
56  // Note: This only applies to the block *name*, not the entire block ID.
57  // Examples:
58  // * is_valid_blockname("FFT") will return true.
59  // * is_valid_blockname("FIR#Filter") will return false, because a # symbol
60  // is not allowed in a block name. Only alphanumerical characters and
61  // underscores are allowed.
62  //
63  // Internally, this matches the string with uhd::rfnoc::VALID_BLOCKNAME_REGEX.
64  static bool is_valid_blockname(const std::string& block_name);
65 
67  //
68  // Note: This does necessary require a complete complete ID. If this returns
69  // true, then it is a valid input for block_id_t::match().
70  //
71  // Examples:
72  // * is_valid_block_id("FFT") will return true.
73  // * is_valid_block_id("0/Filter#1") will return true.
74  // * is_valid_block_id("0/Filter#Foo") will return false.
75  //
76  // Internally, this matches the string with uhd::rfnoc::VALID_BLOCKID_REGEX.
77  static bool is_valid_block_id(const std::string& block_id);
78 
80  //
81  // A match is a less strict version of equality.
82  // Less specific block IDs will match more specific ones,
83  // e.g. "FFT" will match "0/FFT#1", "1/FFT#2", etc.
84  // "FFT#1" will only match the former, etc.
85  bool match(const std::string& block_str);
86 
87  // Getters
88 
90  std::string get() const
91  {
92  return to_string();
93  };
94 
96  std::string get_local() const;
97 
99  uhd::fs_path get_tree_root() const;
100 
102  size_t get_device_no() const
103  {
104  return _device_no;
105  };
106 
108  size_t get_block_count() const
109  {
110  return _block_ctr;
111  };
112 
114  std::string get_block_name() const
115  {
116  return _block_name;
117  };
118 
119  // Setters
120 
122  // Returns true if successful (i.e. if string valid)
123  bool set(const std::string& new_name);
124 
126  // and set_block_count() one after another, only if \p block_name is invalid, stops
127  // and returns false before changing anything
128  bool set(const size_t device_no,
129  const std::string& block_name,
130  const size_t block_ctr = 0);
131 
133  void set_device_no(size_t device_no)
134  {
135  _device_no = device_no;
136  };
137 
139  bool set_block_name(const std::string& block_name);
140 
142  void set_block_count(size_t count)
143  {
144  _block_ctr = count;
145  };
146 
147  // Overloaded operators
148 
150  block_id_t operator=(const std::string& new_name)
151  {
152  set(new_name);
153  return *this;
154  }
155 
156  bool operator==(const block_id_t& block_id) const
157  {
158  return (_device_no == block_id.get_device_no())
159  and (_block_name == block_id.get_block_name())
160  and (_block_ctr == block_id.get_block_count());
161  }
162 
163  bool operator!=(const block_id_t& block_id) const
164  {
165  return not(*this == block_id);
166  }
167 
168  bool operator<(const block_id_t& block_id) const
169  {
170  return (_device_no < block_id.get_device_no()
171  or (_device_no == block_id.get_device_no()
172  and _block_name < block_id.get_block_name())
173  or (_device_no == block_id.get_device_no()
174  and _block_name == block_id.get_block_name()
175  and _block_ctr < block_id.get_block_count()));
176  }
177 
178  bool operator>(const block_id_t& block_id) const
179  {
180  return (_device_no > block_id.get_device_no()
181  or (_device_no == block_id.get_device_no()
182  and _block_name > block_id.get_block_name())
183  or (_device_no == block_id.get_device_no()
184  and _block_name == block_id.get_block_name()
185  and _block_ctr > block_id.get_block_count()));
186  }
187 
189  bool operator==(const std::string& block_id_str) const
190  {
191  return get() == block_id_str;
192  }
193 
195  bool operator==(const char* block_id_str) const
196  {
197  std::string comp = std::string(block_id_str);
198  return *this == comp;
199  }
200 
202  operator std::string() const
203  {
204  return to_string();
205  }
206 
209  {
210  _block_ctr++;
211  return *this;
212  }
213 
216  {
217  _block_ctr++;
218  return *this;
219  }
220 
221 private:
222  size_t _device_no;
223  std::string _block_name;
224  size_t _block_ctr;
225 };
226 
228 inline std::ostream& operator<<(std::ostream& out, block_id_t block_id)
229 {
230  out << block_id.to_string();
231  return out;
232 }
233 
234 } // namespace rfnoc
235 } // namespace uhd
void set_block_count(size_t count)
Set the block count.
Definition: block_id.hpp:142
bool operator<(const block_id_t &block_id) const
Definition: block_id.hpp:168
size_t get_device_no() const
Return device number.
Definition: block_id.hpp:102
std::string to_string() const
Return a string like this: "0/FFT#1" (includes all components, if set)
bool operator==(const char *block_id_str) const
Check if a string matches the entire block ID (not like match())
Definition: block_id.hpp:195
Definition: property_tree.hpp:205
block_id_t operator++()
Increment the block count ("FFT#1" -> "FFT_2")
Definition: block_id.hpp:208
bool operator==(const std::string &block_id_str) const
Check if a string matches the entire block ID (not like match())
Definition: block_id.hpp:189
bool operator>(const block_id_t &block_id) const
Definition: block_id.hpp:178
bool operator==(const block_id_t &block_id) const
Definition: block_id.hpp:156
Definition: block_id.hpp:39
Definition: build_info.hpp:12
std::ostream & operator<<(std::ostream &out, block_id_t block_id)
Shortcut for << block_id.to_string()
Definition: block_id.hpp:228
bool operator!=(const block_id_t &block_id) const
Definition: block_id.hpp:163
size_t get_block_count() const
Return block count.
Definition: block_id.hpp:108
void set_device_no(size_t device_no)
Set the device number.
Definition: block_id.hpp:133
#define UHD_API
Definition: config.h:87
block_id_t operator=(const std::string &new_name)
Assignment: Works like set(std::string)
Definition: block_id.hpp:150
block_id_t operator++(int)
Increment the block count ("FFT#1" -> "FFT_2")
Definition: block_id.hpp:215
std::string get_block_name() const
Return block name.
Definition: block_id.hpp:114