Sfoglia il codice sorgente

Adds close() function.

Bobby Aldridge 7 anni fa
parent
commit
a22156399f
1 ha cambiato i file con 15 aggiunte e 0 eliminazioni
  1. 15 0
      matplotlibcpp.h

+ 15 - 0
matplotlibcpp.h

@@ -31,6 +31,7 @@ namespace matplotlibcpp {
 
 		struct _interpreter {
 			PyObject *s_python_function_show;
+			PyObject *s_python_function_close;
 			PyObject *s_python_function_draw;
 			PyObject *s_python_function_pause;
 			PyObject *s_python_function_save;
@@ -122,6 +123,7 @@ namespace matplotlibcpp {
 				if(!pylabmod) { throw std::runtime_error("Error loading module pylab!"); }
 
 				s_python_function_show = PyObject_GetAttrString(pymod, "show");
+				s_python_function_close = PyObject_GetAttrString(pymod, "close");
 				s_python_function_draw = PyObject_GetAttrString(pymod, "draw");
 				s_python_function_pause = PyObject_GetAttrString(pymod, "pause");
 				s_python_function_figure = PyObject_GetAttrString(pymod, "figure");
@@ -150,6 +152,7 @@ namespace matplotlibcpp {
 				s_python_function_xkcd = PyObject_GetAttrString(pymod, "xkcd");
 
 				if(        !s_python_function_show
+					|| !s_python_function_close
 					|| !s_python_function_draw
 					|| !s_python_function_pause
 					|| !s_python_function_figure
@@ -179,6 +182,7 @@ namespace matplotlibcpp {
 				) { throw std::runtime_error("Couldn't find required function!"); }
 
 				if (       !PyFunction_Check(s_python_function_show)
+					|| !PyFunction_Check(s_python_function_close)
 					|| !PyFunction_Check(s_python_function_draw)
 					|| !PyFunction_Check(s_python_function_pause)
 					|| !PyFunction_Check(s_python_function_figure)
@@ -913,6 +917,17 @@ namespace matplotlibcpp {
         Py_DECREF(res);
     }
 
+    inline void close()
+    {
+        PyObject* res = PyObject_CallObject(
+                detail::_interpreter::get().s_python_function_close,
+                detail::_interpreter::get().s_python_empty_tuple);
+
+        if (!res) throw std::runtime_error("Call to close() failed.");
+
+        Py_DECREF(res);
+    }
+
     inline void xkcd() {
     	PyObject* res;
     	PyObject *kwargs = PyDict_New();