File: pycallback_object.h

package info (click to toggle)
gnuradio 3.7.13.4-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 45,992 kB
  • sloc: cpp: 155,723; python: 88,934; xml: 42,165; ansic: 41,036; fortran: 927; asm: 803; sh: 224; lisp: 31; makefile: 17
file content (199 lines) | stat: -rw-r--r-- 5,593 bytes parent folder | download
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
/* -*- c++ -*- */
/*
 * Copyright 2012 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.
 */

#include <iostream>
#include <gnuradio/rpcregisterhelpers.h>
#include <pythread.h>
#include <boost/format.hpp>

enum pyport_t {
  PYPORT_STRING,
  PYPORT_FLOAT
};

int pycallback_object_count = 500;

// a simple to-PMT converter template class-function
template <class myType> class pmt_assist
{
public:
  static pmt::pmt_t make(myType _val)
  {
    return pmt::mp(_val);
  }
};

/* template specializations for vectors that can't use pmt::mp() */
template<>
pmt::pmt_t pmt_assist<std::vector<float> >::make(std::vector<float> _val)
{
  return pmt::init_f32vector(_val.size(), _val);
}

template<>
pmt::pmt_t pmt_assist<std::vector<gr_complex> >::make(std::vector<gr_complex> _val)
{
  return pmt::init_c32vector(_val.size(), _val);
}

template <class myType> class pycallback_object
{
public:
  pycallback_object(std::string name, std::string functionbase,
            std::string units, std::string desc,
            myType min, myType max, myType deflt,
            DisplayType dtype) :
    d_callback(NULL),
    d_functionbase(functionbase), d_units(units), d_desc(desc),
    d_min(min), d_max(max), d_deflt(deflt), d_dtype(dtype),
    d_name(name), d_id(pycallback_object_count++)
  {
    d_callback = NULL;
    setup_rpc();
  }

  void add_rpc_variable(rpcbasic_sptr s)
  {
    d_rpc_vars.push_back(s);
  }

  myType get() {
    myType rVal = d_deflt;
    if(d_callback == NULL) {
      printf("WARNING: pycallback_object get() called without py callback set!\n");
      return rVal;
    }
    else {
      // obtain  PyGIL
      PyGILState_STATE state = PyGILState_Ensure();

      PyObject *func;
      //PyObject *arglist;
      PyObject *result;

      func = (PyObject *) d_callback;               // Get Python function
      //arglist = Py_BuildValue("");             // Build argument list
      result = PyEval_CallObject(func,NULL);     // Call Python
      //result = PyEval_CallObject(func,arglist);     // Call Python
      //Py_DECREF(arglist);                           // Trash arglist
      if(result) {                                 // If no errors, return double
        rVal = pyCast(result);
      }
      Py_XDECREF(result);

      // release  PyGIL
      PyGILState_Release(state);
      return rVal;
    }
  }

  void set_callback(PyObject *cb)
  {
    d_callback = cb;
  }

  void setup_rpc()
  {
#ifdef GR_CTRLPORT
    add_rpc_variable(
      rpcbasic_sptr(new rpcbasic_register_get<pycallback_object, myType>(
        (boost::format("%s%d") % d_name % d_id).str() , d_functionbase.c_str(),
        this, &pycallback_object::get, pmt_assist<myType>::make(d_min),
        pmt_assist<myType>::make(d_max), pmt_assist<myType>::make(d_deflt),
        d_units.c_str(), d_desc.c_str(), RPC_PRIVLVL_MIN, d_dtype)));
#endif /* GR_CTRLPORT */
  }

private:
  PyObject* d_callback;
  std::string d_functionbase, d_units, d_desc;
  myType d_min, d_max, d_deflt;
  DisplayType d_dtype;

  /* This is a fall-through converter in case someone tries to call pyCast on an
   * object type for which there isn't a template specialization (located below
   * this class) function. This function should never get called, and it is
   * unknown if changing the return type from myType to 'void' will break
   * something. */
  myType pyCast(PyObject* obj) {
    printf("TYPE NOT IMPLEMENTED!\n");
    assert(0);
    // the following is to make compilers happy only.
    myType dummy;
    return(dummy);
  };

  std::vector<boost::any> d_rpc_vars; // container for all RPC variables
  std::string d_name;
  int d_id;
};


// template specialization conversion functions
// get data out of the PyObject and into the real world
template<>
std::string pycallback_object<std::string>::pyCast(PyObject* obj)
{
  return std::string(PyString_AsString(obj));
}

template<>
double pycallback_object<double>::pyCast(PyObject* obj)
{
  return PyFloat_AsDouble(obj);
}

template<>
float pycallback_object<float>::pyCast(PyObject* obj)
{
  return (float)PyFloat_AsDouble(obj);
}

template<>
int pycallback_object<int>::pyCast(PyObject* obj)
{
  return PyInt_AsLong(obj);
}

template<>
std::vector<float> pycallback_object<std::vector<float> >::pyCast(PyObject* obj)
{
  int size = PyObject_Size(obj);
  std::vector<float> rval(size);
  for(int i=0; i<size; i++) {
    rval[i] = (float)PyFloat_AsDouble(PyList_GetItem(obj, i));
  }
  return rval;
}

template<>
std::vector<gr_complex> pycallback_object<std::vector<gr_complex> >::pyCast(PyObject* obj)
{
  int size = PyObject_Size(obj);
  std::vector<gr_complex> rval(size);
  for(int i=0; i<size; i++){ rval[i] = \
      gr_complex((float)PyComplex_RealAsDouble(PyList_GetItem(obj, i)),
		 (float)PyComplex_ImagAsDouble(PyList_GetItem(obj, i)));
  }
  return rval;
}
// TODO: add more template specializations as needed!