GNU Radio Manual and C++ API Reference  3.10.9.1
The Free & Open Software Radio Ecosystem
hier_block2.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2006-2009,2013 Free Software Foundation, Inc.
4  *
5  * This file is part of GNU Radio
6  *
7  * SPDX-License-Identifier: GPL-3.0-or-later
8  *
9  */
10 
11 #ifndef INCLUDED_GR_RUNTIME_HIER_BLOCK2_H
12 #define INCLUDED_GR_RUNTIME_HIER_BLOCK2_H
13 
14 #include <gnuradio/api.h>
15 #include <gnuradio/basic_block.h>
16 
17 namespace gr {
18 
19 /*!
20  * \brief public constructor for hier_block2
21  */
22 GR_RUNTIME_API hier_block2_sptr make_hier_block2(const std::string& name,
23  gr::io_signature::sptr input_signature,
24  gr::io_signature::sptr output_signature);
25 
26 class hier_block2_detail;
27 
28 /*!
29  * \brief Hierarchical container class for gr::block's and gr::hier_block2's
30  * \ingroup container_blk
31  * \ingroup base_blk
32  */
34 {
35 private:
36  friend class hier_block2_detail;
37  template <typename T, typename... Args>
38  friend std::shared_ptr<T> gnuradio::make_block_sptr(Args&&... args);
39 
40  /*!
41  * \brief Private implementation details of gr::hier_block2.
42  *
43  * This is a pointer in order to not break ABI when implementation object
44  * changes.
45  */
46  std::unique_ptr<hier_block2_detail> d_detail;
47 
48 
49 protected:
50  hier_block2(); // allows pure virtual interface sub-classes
51  hier_block2(const std::string& name,
52  gr::io_signature::sptr input_signature,
53  gr::io_signature::sptr output_signature);
54 
55 public:
56  ~hier_block2() override;
57 
58  /*!
59  * \brief typedef for object returned from self().
60  *
61  * This type is only guaranteed to be passable to connect and
62  * disconnect. No other assumptions should be made about it.
63  */
64  typedef basic_block_sptr opaque_self;
65 
66  /*!
67  * \brief Return an object, representing the current block, which
68  * can be passed to connect.
69  *
70  * The returned object may only be used as an argument to connect
71  * or disconnect. Any other use of self() results in unspecified
72  * (erroneous) behavior.
73  */
74  opaque_self self();
75 
76  /*!
77  * \brief Add a stand-alone (possibly hierarchical) block to
78  * internal graph
79  *
80  * This adds a gr-block or hierarchical block to the internal
81  * graph without wiring it to anything else.
82  */
83  void connect(basic_block_sptr block);
84 
85  /*!
86  * \brief Add gr-blocks or hierarchical blocks to internal graph
87  * and wire together
88  *
89  * This adds (if not done earlier by another connect) a pair of
90  * gr-blocks or hierarchical blocks to the internal flowgraph, and
91  * wires the specified output port to the specified input port.
92  */
93  void connect(basic_block_sptr src, int src_port, basic_block_sptr dst, int dst_port);
94 
95  /*!
96  * \brief Add gr-blocks or hierarchical blocks to internal graph
97  * and wire together
98  *
99  * This adds (if not done earlier by another connect) a pair of
100  * gr-blocks or hierarchical blocks to the internal message port
101  * subscription
102  */
103  void msg_connect(basic_block_sptr src,
104  pmt::pmt_t srcport,
105  basic_block_sptr dst,
106  pmt::pmt_t dstport);
107  void msg_connect(basic_block_sptr src,
108  std::string srcport,
109  basic_block_sptr dst,
110  std::string dstport);
111  void msg_disconnect(basic_block_sptr src,
112  pmt::pmt_t srcport,
113  basic_block_sptr dst,
114  pmt::pmt_t dstport);
115  void msg_disconnect(basic_block_sptr src,
116  std::string srcport,
117  basic_block_sptr dst,
118  std::string dstport);
119 
120  /*!
121  * \brief Remove a gr-block or hierarchical block from the
122  * internal flowgraph.
123  *
124  * This removes a gr-block or hierarchical block from the internal
125  * flowgraph, disconnecting it from other blocks as needed.
126  */
127  void disconnect(basic_block_sptr block);
128 
129  /*!
130  * \brief Disconnect a pair of gr-blocks or hierarchical blocks in
131  * internal flowgraph.
132  *
133  * This disconnects the specified input port from the specified
134  * output port of a pair of gr-blocks or hierarchical blocks.
135  */
136  void
137  disconnect(basic_block_sptr src, int src_port, basic_block_sptr dst, int dst_port);
138 
139  /*!
140  * \brief Disconnect all connections in the internal flowgraph.
141  *
142  * This call removes all output port to input port connections in
143  * the internal flowgraph.
144  */
146 
147  /*!
148  * Lock a flowgraph in preparation for reconfiguration. When an
149  * equal number of calls to lock() and unlock() have occurred, the
150  * flowgraph will be reconfigured.
151  *
152  * N.B. lock() and unlock() may not be called from a flowgraph
153  * thread (E.g., gr::block::work method) or deadlock will occur
154  * when reconfiguration happens.
155  */
156  virtual void lock();
157 
158  /*!
159  * Unlock a flowgraph in preparation for reconfiguration. When an
160  * equal number of calls to lock() and unlock() have occurred, the
161  * flowgraph will be reconfigured.
162  *
163  * N.B. lock() and unlock() may not be called from a flowgraph
164  * thread (E.g., gr::block::work method) or deadlock will occur
165  * when reconfiguration happens.
166  */
167  virtual void unlock();
168 
169  /*!
170  * \brief Returns max buffer size (itemcount) on output port \p i.
171  */
172  int max_output_buffer(size_t port = 0);
173 
174  /*!
175  * \brief Sets max buffer size (itemcount) on all output ports.
176  */
177  void set_max_output_buffer(int max_output_buffer);
178 
179  /*!
180  * \brief Sets max buffer size (itemcount) on output port \p port.
181  */
182  void set_max_output_buffer(size_t port, int max_output_buffer);
183 
184  /*!
185  * \brief Returns min buffer size (itemcount) on output port \p i.
186  */
187  int min_output_buffer(size_t port = 0);
188 
189  /*!
190  * \brief Sets min buffer size (itemcount) on all output ports.
191  */
192  void set_min_output_buffer(int min_output_buffer);
193 
194  /*!
195  * \brief Sets min buffer size (itemcount) on output port \p port.
196  */
197  void set_min_output_buffer(size_t port, int min_output_buffer);
198 
199 
200  // This is a public method for ease of code organization, but should be
201  // ignored by the user.
202  flat_flowgraph_sptr flatten() const;
203 
204  hier_block2_sptr to_hier_block2(); // Needed for Python type coercion
205 
206  bool has_msg_port(pmt::pmt_t which_port) override
207  {
208  return message_port_is_hier(which_port) || basic_block::has_msg_port(which_port);
209  }
210 
211  bool message_port_is_hier(pmt::pmt_t port_id) override
212  {
213  return message_port_is_hier_in(port_id) || message_port_is_hier_out(port_id);
214  }
215 
216  bool message_port_is_hier_in(pmt::pmt_t port_id) override
217  {
218  return pmt::list_has(hier_message_ports_in, port_id);
219  }
220 
221  bool message_port_is_hier_out(pmt::pmt_t port_id) override
222  {
223  return pmt::list_has(hier_message_ports_out, port_id);
224  }
225 
228 
230  {
231  if (pmt::list_has(hier_message_ports_in, port_id))
232  throw std::invalid_argument(
233  "hier msg in port by this name already registered");
234  if (msg_queue.find(port_id) != msg_queue.end())
235  throw std::invalid_argument(
236  "block already has a primitive input port by this name");
237  hier_message_ports_in = pmt::list_add(hier_message_ports_in, port_id);
238  }
239 
241  {
242  if (pmt::list_has(hier_message_ports_out, port_id))
243  throw std::invalid_argument(
244  "hier msg out port by this name already registered");
245  if (pmt::dict_has_key(d_message_subscribers, port_id))
246  throw std::invalid_argument(
247  "block already has a primitive output port by this name");
248  hier_message_ports_out = pmt::list_add(hier_message_ports_out, port_id);
249  }
250 
251  /*!
252  * \brief Set the affinity of all blocks in hier_block2 to processor core \p n.
253  *
254  * \param mask a vector of ints of the core numbers available to this block.
255  */
256  void set_processor_affinity(const std::vector<int>& mask) override;
257 
258  /*!
259  * \brief Remove processor affinity for all blocks in hier_block2.
260  */
261  void unset_processor_affinity() override;
262 
263  /*!
264  * \brief Get the current processor affinity.
265  *
266  * \details This returns the processor affinity value for the first
267  * block in the hier_block2's list of blocks with the assumption
268  * that they have always only been set through the hier_block2's
269  * interface. If any block has been individually set, then this
270  * call could be misleading.
271  */
272  std::vector<int> processor_affinity() override;
273 
274  /*!
275  * \brief Set the logger's output level.
276  *
277  * Sets the level of the logger for all connected blocks. This takes
278  * a string that is translated to the standard levels and can be
279  * (case insensitive):
280  *
281  * \li off , notset
282  * \li debug
283  * \li info
284  * \li notice
285  * \li warn
286  * \li error
287  * \li crit
288  * \li alert
289  * \li fatal
290  * \li emerg
291  */
292  void set_log_level(const std::string& level) override;
293 
294  /*!
295  * \brief Get the logger's output level
296  */
297  std::string log_level() override;
298 
299  /*!
300  * \brief Get if all block min buffers should be set.
301  *
302  * \details this returns whether all the block min output buffers
303  * should be set or just the block ports connected to the hier ports.
304  */
306 
307  /*!
308  * \brief Get if all block max buffers should be set.
309  *
310  * \details this returns whether all the block max output buffers
311  * should be set or just the block ports connected to the hier ports.
312  */
314 };
315 
316 /*!
317  * \brief Return hierarchical block's flow graph represented in dot language
318  */
319 GR_RUNTIME_API std::string dot_graph(hier_block2_sptr hierblock2);
320 
321 inline hier_block2_sptr cast_to_hier_block2_sptr(basic_block_sptr block)
322 {
323  return std::dynamic_pointer_cast<hier_block2, basic_block>(block);
324 }
325 
326 } /* namespace gr */
327 
328 #endif /* INCLUDED_GR_RUNTIME_HIER_BLOCK2_H */
The abstract base class for all signal processing blocks.
Definition: basic_block.h:63
virtual bool has_msg_port(pmt::pmt_t which_port)
Definition: basic_block.h:306
The abstract base class for all 'terminal' processing blocks.
Definition: gnuradio-runtime/include/gnuradio/block.h:63
Hierarchical container class for gr::block's and gr::hier_block2's.
Definition: hier_block2.h:34
std::string log_level() override
Get the logger's output level.
void set_max_output_buffer(int max_output_buffer)
Sets max buffer size (itemcount) on all output ports.
void set_max_output_buffer(size_t port, int max_output_buffer)
Sets max buffer size (itemcount) on output port port.
void msg_disconnect(basic_block_sptr src, pmt::pmt_t srcport, basic_block_sptr dst, pmt::pmt_t dstport)
void disconnect_all()
Disconnect all connections in the internal flowgraph.
std::vector< int > processor_affinity() override
Get the current processor affinity.
flat_flowgraph_sptr flatten() const
void msg_disconnect(basic_block_sptr src, std::string srcport, basic_block_sptr dst, std::string dstport)
~hier_block2() override
pmt::pmt_t hier_message_ports_out
Definition: hier_block2.h:227
void disconnect(basic_block_sptr block)
Remove a gr-block or hierarchical block from the internal flowgraph.
void set_min_output_buffer(int min_output_buffer)
Sets min buffer size (itemcount) on all output ports.
hier_block2(const std::string &name, gr::io_signature::sptr input_signature, gr::io_signature::sptr output_signature)
bool message_port_is_hier_in(pmt::pmt_t port_id) override
Definition: hier_block2.h:216
int max_output_buffer(size_t port=0)
Returns max buffer size (itemcount) on output port i.
void msg_connect(basic_block_sptr src, pmt::pmt_t srcport, basic_block_sptr dst, pmt::pmt_t dstport)
Add gr-blocks or hierarchical blocks to internal graph and wire together.
void connect(basic_block_sptr src, int src_port, basic_block_sptr dst, int dst_port)
Add gr-blocks or hierarchical blocks to internal graph and wire together.
basic_block_sptr opaque_self
typedef for object returned from self().
Definition: hier_block2.h:64
void unset_processor_affinity() override
Remove processor affinity for all blocks in hier_block2.
void disconnect(basic_block_sptr src, int src_port, basic_block_sptr dst, int dst_port)
Disconnect a pair of gr-blocks or hierarchical blocks in internal flowgraph.
bool message_port_is_hier(pmt::pmt_t port_id) override
Definition: hier_block2.h:211
hier_block2_sptr to_hier_block2()
void set_processor_affinity(const std::vector< int > &mask) override
Set the affinity of all blocks in hier_block2 to processor core n.
void msg_connect(basic_block_sptr src, std::string srcport, basic_block_sptr dst, std::string dstport)
bool all_min_output_buffer_p(void)
Get if all block min buffers should be set.
virtual void lock()
void connect(basic_block_sptr block)
Add a stand-alone (possibly hierarchical) block to internal graph.
void message_port_register_hier_out(pmt::pmt_t port_id)
Definition: hier_block2.h:240
bool has_msg_port(pmt::pmt_t which_port) override
Definition: hier_block2.h:206
void set_min_output_buffer(size_t port, int min_output_buffer)
Sets min buffer size (itemcount) on output port port.
virtual void unlock()
pmt::pmt_t hier_message_ports_in
Definition: hier_block2.h:226
bool message_port_is_hier_out(pmt::pmt_t port_id) override
Definition: hier_block2.h:221
bool all_max_output_buffer_p(void)
Get if all block max buffers should be set.
int min_output_buffer(size_t port=0)
Returns min buffer size (itemcount) on output port i.
void set_log_level(const std::string &level) override
Set the logger's output level.
void message_port_register_hier_in(pmt::pmt_t port_id)
Definition: hier_block2.h:229
std::shared_ptr< io_signature > sptr
Definition: io_signature.h:52
thread-safe message queue
Definition: msg_queue.h:25
#define GR_RUNTIME_API
Definition: gnuradio-runtime/include/gnuradio/api.h:18
GNU Radio logging wrapper.
Definition: basic_block.h:29
GR_RUNTIME_API hier_block2_sptr make_hier_block2(const std::string &name, gr::io_signature::sptr input_signature, gr::io_signature::sptr output_signature)
public constructor for hier_block2
GR_RUNTIME_API std::string dot_graph(hier_block2_sptr hierblock2)
Return hierarchical block's flow graph represented in dot language.
PMT_API bool dict_has_key(const pmt_t &dict, const pmt_t &key)
Return true if key exists in dict.
PMT_API pmt_t list_add(pmt_t list, const pmt_t &item)
Return list with item added to it.
std::shared_ptr< pmt_base > pmt_t
typedef for shared pointer (transparent reference counting).
Definition: pmt.h:83
PMT_API bool list_has(pmt_t list, const pmt_t &item)
Return bool of list contains item.