From 0249f7ce0cf5173b946b936c5cd66380afc8bf92 Mon Sep 17 00:00:00 2001
From: Josh Morman <mormjb@gmail.com>
Date: Thu, 23 Apr 2020 09:58:59 -0400
Subject: runtime: replace py gateway with pybind11 support, remove feval

---
 gnuradio-runtime/lib/block_gateway_impl.h | 56 +++++++++++++++++++++----------
 1 file changed, 39 insertions(+), 17 deletions(-)

(limited to 'gnuradio-runtime/lib/block_gateway_impl.h')

diff --git a/gnuradio-runtime/lib/block_gateway_impl.h b/gnuradio-runtime/lib/block_gateway_impl.h
index a87f0ec1e5..168a78650e 100644
--- a/gnuradio-runtime/lib/block_gateway_impl.h
+++ b/gnuradio-runtime/lib/block_gateway_impl.h
@@ -1,6 +1,6 @@
 /* -*- c++ -*- */
 /*
- * Copyright 2013 Free Software Foundation, Inc.
+ * Copyright 2013,2020 Free Software Foundation, Inc.
  *
  * This file is part of GNU Radio
  *
@@ -21,12 +21,10 @@ namespace gr {
 class block_gateway_impl : public block_gateway
 {
 public:
-    block_gateway_impl(feval_ll* handler,
+    block_gateway_impl(const py::handle& p,
                        const std::string& name,
                        gr::io_signature::sptr in_sig,
-                       gr::io_signature::sptr out_sig,
-                       const block_gw_work_type work_type,
-                       const unsigned factor);
+                       gr::io_signature::sptr out_sig);
 
     /*******************************************************************
      * Overloads for various scheduler-called functions
@@ -38,23 +36,47 @@ public:
                      gr_vector_const_void_star& input_items,
                      gr_vector_void_star& output_items);
 
-    int work(int noutput_items,
-             gr_vector_const_void_star& input_items,
-             gr_vector_void_star& output_items);
-
-    int fixed_rate_noutput_to_ninput(int noutput_items);
-    int fixed_rate_ninput_to_noutput(int ninput_items);
-
     bool start(void);
     bool stop(void);
+    void set_msg_handler_pybind(pmt::pmt_t which_port, std::string& handler_name)
+    {
+        if (msg_queue.find(which_port) == msg_queue.end()) {
+            throw std::runtime_error(
+                "attempt to set_msg_handler_pybind() on invalid input message port!");
+        }
+        d_msg_handlers_pybind[which_port] = handler_name;
+    }
+    
+protected:
+    // Message handlers back into python using pybind API
+    typedef std::map<pmt::pmt_t, std::string, pmt::comparator> msg_handlers_pybind_t;
+    msg_handlers_pybind_t d_msg_handlers_pybind;
+
+    bool has_msg_handler(pmt::pmt_t which_port)
+    {
+        if (d_msg_handlers_pybind.find(which_port) != d_msg_handlers_pybind.end()) {
+            return true;
+        } else {
+            return gr::basic_block::has_msg_handler(which_port);
+        }
+    }
 
-    block_gw_message_type& block_message(void);
+    void dispatch_msg(pmt::pmt_t which_port, pmt::pmt_t msg)
+    {
+        // Is there a handler?
+        if (d_msg_handlers_pybind.find(which_port) != d_msg_handlers_pybind.end()) {
+            // d_msg_handlers_pybind[which_port]->calleval(msg); // Yes, invoke it.
+            py::gil_scoped_acquire acquire;
+            // std::string handler_name(d_msg_handlers_pybind[which_port]);
+            py::object ret = _py_handle.attr(d_msg_handlers_pybind[which_port].c_str())(msg);
+        } else {
+            // Pass to generic dispatcher if not found
+            gr::basic_block::dispatch_msg(which_port, msg);
+        }
+    }
 
 private:
-    feval_ll* _handler;
-    block_gw_message_type _message;
-    const block_gw_work_type _work_type;
-    unsigned _decim, _interp;
+    py::handle _py_handle;
 };
 
 } /* namespace gr */
-- 
cgit v1.2.3