summaryrefslogtreecommitdiff
path: root/gnuradio-runtime/include/gr_hier_block2.h
blob: c39a98f6d735f7226c2854294806aae6d5b34e02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/* -*- c++ -*- */
/*
 * Copyright 2006,2007,2008,2009 Free Software Foundation, Inc.
 *
 * This file is part of GNU Radio
 *
 * GNU Radio is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 *
 * GNU Radio is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GNU Radio; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street,
 * Boston, MA 02110-1301, USA.
 */
#ifndef INCLUDED_GR_HIER_BLOCK2_H
#define INCLUDED_GR_HIER_BLOCK2_H

#include <gr_runtime_api.h>
#include <gr_basic_block.h>

/*!
 * \brief public constructor for gr_hier_block2

 */
GR_RUNTIME_API gr_hier_block2_sptr gr_make_hier_block2(const std::string &name,
                                        gr_io_signature_sptr input_signature,
                                        gr_io_signature_sptr output_signature);

class gr_hier_block2_detail;

/*!
 * \brief Hierarchical container class for gr_block's and gr_hier_block2's
 * \ingroup container_blk
 * \ingroup base_blk
 *
 */
class GR_RUNTIME_API gr_hier_block2 : public gr_basic_block
{
private:
  friend class gr_hier_block2_detail;
  friend GR_RUNTIME_API gr_hier_block2_sptr gr_make_hier_block2(const std::string &name,
						 gr_io_signature_sptr input_signature,
						 gr_io_signature_sptr output_signature);

  /*!
   * \brief Private implementation details of gr_hier_block2
   */
  gr_hier_block2_detail *d_detail;

protected:
  gr_hier_block2 (void){} //allows pure virtual interface sub-classes
  gr_hier_block2(const std::string &name,
		 gr_io_signature_sptr input_signature,
		 gr_io_signature_sptr output_signature);

public:
  virtual ~gr_hier_block2();

  /*!
   * \brief typedef for object returned from self().
   *
   * This type is only guaranteed to be passable to connect and disconnect.
   * No other assumptions should be made about it.
   */
  typedef gr_basic_block_sptr	opaque_self;

  /*!
   * \brief Return an object, representing the current block, which can be passed to connect.
   *
   * The returned object may only be used as an argument to connect or disconnect.
   * Any other use of self() results in unspecified (erroneous) behavior.
   */
  opaque_self self();

  /*!
   * \brief Add a stand-alone (possibly hierarchical) block to internal graph
   *
   * This adds a gr-block or hierarchical block to the internal graph
   * without wiring it to anything else.
   */
  void connect(gr_basic_block_sptr block);

  /*!
   * \brief Add gr-blocks or hierarchical blocks to internal graph and wire together
   *
   * This adds (if not done earlier by another connect) a pair of gr-blocks or
   * hierarchical blocks to the internal flowgraph, and wires the specified output
   * port to the specified input port.
   */
  void connect(gr_basic_block_sptr src, int src_port,
	       gr_basic_block_sptr dst, int dst_port);

  /*!
   * \brief Add gr-blocks or hierarchical blocks to internal graph and wire together
   *
   * This adds (if not done earlier by another connect) a pair of gr-blocks or
   * hierarchical blocks to the internal message port subscription
   */
  void msg_connect(gr_basic_block_sptr src, pmt::pmt_t srcport,
	       gr_basic_block_sptr dst, pmt::pmt_t dstport);
  void msg_connect(gr_basic_block_sptr src, std::string srcport,
	       gr_basic_block_sptr dst, std::string dstport);
  void msg_disconnect(gr_basic_block_sptr src, pmt::pmt_t srcport,
	       gr_basic_block_sptr dst, pmt::pmt_t dstport);
  void msg_disconnect(gr_basic_block_sptr src, std::string srcport,
	       gr_basic_block_sptr dst, std::string dstport);

  /*!
   * \brief Remove a gr-block or hierarchical block from the internal flowgraph.
   *
   * This removes a gr-block or hierarchical block from the internal flowgraph,
   * disconnecting it from other blocks as needed.
   *
   */
  void disconnect(gr_basic_block_sptr block);

  /*!
   * \brief Disconnect a pair of gr-blocks or hierarchical blocks in internal
   *        flowgraph.
   *
   * This disconnects the specified input port from the specified output port
   * of a pair of gr-blocks or hierarchical blocks.
   */
  void disconnect(gr_basic_block_sptr src, int src_port,
		  gr_basic_block_sptr dst, int dst_port);

  /*!
   * \brief Disconnect all connections in the internal flowgraph.
   *
   * This call removes all output port to input port connections in the internal
   * flowgraph.
   */
  void disconnect_all();

  /*!
   * Lock a flowgraph in preparation for reconfiguration.  When an equal
   * number of calls to lock() and unlock() have occurred, the flowgraph
   * will be reconfigured.
   *
   * N.B. lock() and unlock() may not be called from a flowgraph thread
   * (E.g., gr_block::work method) or deadlock will occur when
   * reconfiguration happens.
   */
  virtual void lock();

  /*!
   * Unlock a flowgraph in preparation for reconfiguration.  When an equal
   * number of calls to lock() and unlock() have occurred, the flowgraph
   * will be reconfigured.
   *
   * N.B. lock() and unlock() may not be called from a flowgraph thread
   * (E.g., gr_block::work method) or deadlock will occur when
   * reconfiguration happens.
   */
  virtual void unlock();

  // This is a public method for ease of code organization, but should be
  // ignored by the user.
  gr_flat_flowgraph_sptr flatten() const;

  gr_hier_block2_sptr to_hier_block2(); // Needed for Python type coercion

  bool has_msg_port(pmt::pmt_t which_port){
    return message_port_is_hier(which_port) || gr_basic_block::has_msg_port(which_port);
    }   
  
  bool message_port_is_hier(pmt::pmt_t port_id){
    return message_port_is_hier_in(port_id) || message_port_is_hier_out(port_id);
    }
  bool message_port_is_hier_in(pmt::pmt_t port_id){
    return pmt::list_has(hier_message_ports_in, port_id);
    }
  bool message_port_is_hier_out(pmt::pmt_t port_id){
    return pmt::list_has(hier_message_ports_out, port_id);
    }

  pmt::pmt_t hier_message_ports_in;
  pmt::pmt_t hier_message_ports_out;

  void message_port_register_hier_in(pmt::pmt_t port_id){
    if(pmt::list_has(hier_message_ports_in, port_id))
        throw std::invalid_argument("hier msg in port by this name already registered");
    if(msg_queue.find(port_id) != msg_queue.end())
        throw std::invalid_argument("block already has a primitive input port by this name");
    hier_message_ports_in = pmt::list_add(hier_message_ports_in, port_id);
    }
  void message_port_register_hier_out(pmt::pmt_t port_id){
    if(pmt::list_has(hier_message_ports_out, port_id))
        throw std::invalid_argument("hier msg out port by this name already registered");
    if(pmt::dict_has_key(message_subscribers, port_id))
        throw std::invalid_argument("block already has a primitive output port by this name");
    hier_message_ports_out = pmt::list_add(hier_message_ports_out, port_id);
    }

};

inline gr_hier_block2_sptr cast_to_hier_block2_sptr(gr_basic_block_sptr block) {
  return boost::dynamic_pointer_cast<gr_hier_block2, gr_basic_block>(block);
}

#endif /* INCLUDED_GR_HIER_BLOCK2_H */