GNU Radio 3.6.5.1 C++ API
gr_hier_block2.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2006,2007,2008,2009 Free Software Foundation, Inc.
4  *
5  * This file is part of GNU Radio
6  *
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  *
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with GNU Radio; see the file COPYING. If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22 #ifndef INCLUDED_GR_HIER_BLOCK2_H
23 #define INCLUDED_GR_HIER_BLOCK2_H
24 
25 #include <gr_core_api.h>
26 #include <gr_basic_block.h>
27 
28 /*!
29  * \brief public constructor for gr_hier_block2
30 
31  */
33  gr_io_signature_sptr input_signature,
34  gr_io_signature_sptr output_signature);
35 
37 
38 /*!
39  * \brief Hierarchical container class for gr_block's and gr_hier_block2's
40  * \ingroup container_blk
41  * \ingroup base_blk
42  *
43  */
45 {
46 private:
47  friend class gr_hier_block2_detail;
48  friend GR_CORE_API gr_hier_block2_sptr gr_make_hier_block2(const std::string &name,
49  gr_io_signature_sptr input_signature,
50  gr_io_signature_sptr output_signature);
51 
52  /*!
53  * \brief Private implementation details of gr_hier_block2
54  */
55  gr_hier_block2_detail *d_detail;
56 
57 protected:
58  gr_hier_block2 (void){} //allows pure virtual interface sub-classes
59  gr_hier_block2(const std::string &name,
60  gr_io_signature_sptr input_signature,
61  gr_io_signature_sptr output_signature);
62 
63 public:
64  virtual ~gr_hier_block2();
65 
66  /*!
67  * \brief typedef for object returned from self().
68  *
69  * This type is only guaranteed to be passable to connect and disconnect.
70  * No other assumptions should be made about it.
71  */
73 
74  /*!
75  * \brief Return an object, representing the current block, which can be passed to connect.
76  *
77  * The returned object may only be used as an argument to connect or disconnect.
78  * Any other use of self() results in unspecified (erroneous) behavior.
79  */
80  opaque_self self();
81 
82  /*!
83  * \brief Add a stand-alone (possibly hierarchical) block to internal graph
84  *
85  * This adds a gr-block or hierarchical block to the internal graph
86  * without wiring it to anything else.
87  */
88  void connect(gr_basic_block_sptr block);
89 
90  /*!
91  * \brief Add gr-blocks or hierarchical blocks to internal graph and wire together
92  *
93  * This adds (if not done earlier by another connect) a pair of gr-blocks or
94  * hierarchical blocks to the internal flowgraph, and wires the specified output
95  * port to the specified input port.
96  */
97  void connect(gr_basic_block_sptr src, int src_port,
98  gr_basic_block_sptr dst, int dst_port);
99 
100  /*!
101  * \brief Add gr-blocks or hierarchical blocks to internal graph and wire together
102  *
103  * This adds (if not done earlier by another connect) a pair of gr-blocks or
104  * hierarchical blocks to the internal message port subscription
105  */
106  void msg_connect(gr_basic_block_sptr src, pmt::pmt_t srcport,
107  gr_basic_block_sptr dst, pmt::pmt_t dstport);
108  void msg_connect(gr_basic_block_sptr src, std::string srcport,
109  gr_basic_block_sptr dst, std::string dstport);
111  gr_basic_block_sptr dst, pmt::pmt_t dstport);
112  void msg_disconnect(gr_basic_block_sptr src, std::string srcport,
113  gr_basic_block_sptr dst, std::string dstport);
114 
115  /*!
116  * \brief Remove a gr-block or hierarchical block from the internal flowgraph.
117  *
118  * This removes a gr-block or hierarchical block from the internal flowgraph,
119  * disconnecting it from other blocks as needed.
120  *
121  */
122  void disconnect(gr_basic_block_sptr block);
123 
124  /*!
125  * \brief Disconnect a pair of gr-blocks or hierarchical blocks in internal
126  * flowgraph.
127  *
128  * This disconnects the specified input port from the specified output port
129  * of a pair of gr-blocks or hierarchical blocks.
130  */
131  void disconnect(gr_basic_block_sptr src, int src_port,
132  gr_basic_block_sptr dst, int dst_port);
133 
134  /*!
135  * \brief Disconnect all connections in the internal flowgraph.
136  *
137  * This call removes all output port to input port connections in the internal
138  * flowgraph.
139  */
140  void disconnect_all();
141 
142  /*!
143  * Lock a flowgraph in preparation for reconfiguration. When an equal
144  * number of calls to lock() and unlock() have occurred, the flowgraph
145  * will be reconfigured.
146  *
147  * N.B. lock() and unlock() may not be called from a flowgraph thread
148  * (E.g., gr_block::work method) or deadlock will occur when
149  * reconfiguration happens.
150  */
151  virtual void lock();
152 
153  /*!
154  * Unlock a flowgraph in preparation for reconfiguration. When an equal
155  * number of calls to lock() and unlock() have occurred, the flowgraph
156  * will be reconfigured.
157  *
158  * N.B. lock() and unlock() may not be called from a flowgraph thread
159  * (E.g., gr_block::work method) or deadlock will occur when
160  * reconfiguration happens.
161  */
162  virtual void unlock();
163 
164  // This is a public method for ease of code organization, but should be
165  // ignored by the user.
166  gr_flat_flowgraph_sptr flatten() const;
167 
168  gr_hier_block2_sptr to_hier_block2(); // Needed for Python type coercion
169 
170  bool has_msg_port(pmt::pmt_t which_port){
171  return message_port_is_hier(which_port) || gr_basic_block::has_msg_port(which_port);
172  }
173 
175  return message_port_is_hier_in(port_id) || message_port_is_hier_out(port_id);
176  }
178  return pmt::pmt_list_has(hier_message_ports_in, port_id);
179  }
181  return pmt::pmt_list_has(hier_message_ports_out, port_id);
182  }
183 
186 
188  if(pmt::pmt_list_has(hier_message_ports_in, port_id))
189  throw std::invalid_argument("hier msg in port by this name already registered");
190  if(msg_queue.find(port_id) != msg_queue.end())
191  throw std::invalid_argument("block already has a primitive input port by this name");
192  hier_message_ports_in = pmt::pmt_list_add(hier_message_ports_in, port_id);
193  }
195  if(pmt::pmt_list_has(hier_message_ports_out, port_id))
196  throw std::invalid_argument("hier msg out port by this name already registered");
197  if(pmt::pmt_dict_has_key(message_subscribers, port_id))
198  throw std::invalid_argument("block already has a primitive output port by this name");
199  hier_message_ports_out = pmt::pmt_list_add(hier_message_ports_out, port_id);
200  }
201 
202 };
203 
204 inline gr_hier_block2_sptr cast_to_hier_block2_sptr(gr_basic_block_sptr block) {
205  return boost::dynamic_pointer_cast<gr_hier_block2, gr_basic_block>(block);
206 }
207 
208 #endif /* INCLUDED_GR_HIER_BLOCK2_H */