Преглед на файлове

Add fignum_exists() function

Alex Dewar преди 6 години
родител
ревизия
073dd98f62
променени са 1 файла, в които са добавени 21 реда и са изтрити 0 реда
  1. 21 0
      matplotlibcpp.h

+ 21 - 0
matplotlibcpp.h

@@ -36,6 +36,7 @@ struct _interpreter {
     PyObject *s_python_function_pause;
     PyObject *s_python_function_save;
     PyObject *s_python_function_figure;
+    PyObject *s_python_function_fignum_exists;
     PyObject *s_python_function_plot;
     PyObject *s_python_function_quiver;
     PyObject *s_python_function_semilogx;
@@ -142,6 +143,7 @@ private:
         s_python_function_draw = PyObject_GetAttrString(pymod, "draw");
         s_python_function_pause = PyObject_GetAttrString(pymod, "pause");
         s_python_function_figure = PyObject_GetAttrString(pymod, "figure");
+        s_python_function_fignum_exists = PyObject_GetAttrString(pymod, "fignum_exists");
         s_python_function_plot = PyObject_GetAttrString(pymod, "plot");
         s_python_function_quiver = PyObject_GetAttrString(pymod, "quiver");
         s_python_function_semilogx = PyObject_GetAttrString(pymod, "semilogx");
@@ -177,6 +179,7 @@ private:
             || !s_python_function_draw
             || !s_python_function_pause
             || !s_python_function_figure
+            || !s_python_function_fignum_exists
             || !s_python_function_plot
             || !s_python_function_quiver
             || !s_python_function_semilogx
@@ -211,6 +214,7 @@ private:
             || !PyFunction_Check(s_python_function_draw)
             || !PyFunction_Check(s_python_function_pause)
             || !PyFunction_Check(s_python_function_figure)
+            || !PyFunction_Check(s_python_function_fignum_exists)
             || !PyFunction_Check(s_python_function_plot)
             || !PyFunction_Check(s_python_function_quiver)
             || !PyFunction_Check(s_python_function_semilogx)
@@ -845,6 +849,23 @@ inline long figure(long number = -1)
     return figureNumber;
 }
 
+inline bool fignum_exists(long number)
+{
+    // Make sure interpreter is initialised
+    detail::_interpreter::get();
+
+    PyObject *args = PyTuple_New(1);
+    PyTuple_SetItem(args, 0, PyLong_FromLong(number));
+    PyObject *res = PyObject_CallObject(detail::_interpreter::get().s_python_function_fignum_exists, args);
+    if(!res) throw std::runtime_error("Call to fignum_exists() failed.");
+
+    bool ret = PyObject_IsTrue(res);
+    Py_DECREF(res);
+    Py_DECREF(args);
+
+    return ret;
+}
+
 inline void figure_size(size_t w, size_t h)
 {
     const size_t dpi = 100;