Browse Source

interval: Support awaiting CInterval from coroutines

This is a partial implementation of #909 - it is somewhat inefficient (not suspending the task using a future) and does not implement cancellation.  A more complete implementation may follow in 1.11.0.
rdb 4 years ago
parent
commit
28615c6a9e

+ 3 - 0
direct/src/interval/cInterval.h

@@ -19,6 +19,7 @@
 #include "pvector.h"
 #include "config_interval.h"
 #include "pStatCollector.h"
+#include "extension.h"
 
 class CIntervalManager;
 
@@ -120,6 +121,8 @@ PUBLISHED:
   bool step_play();
 
 PUBLISHED:
+  EXTENSION(PyObject *__await__(PyObject *self));
+
   MAKE_PROPERTY(name, get_name);
   MAKE_PROPERTY(duration, get_duration);
   MAKE_PROPERTY(open_ended, get_open_ended);

+ 58 - 0
direct/src/interval/cInterval_ext.cxx

@@ -0,0 +1,58 @@
+/**
+ * PANDA 3D SOFTWARE
+ * Copyright (c) Carnegie Mellon University.  All rights reserved.
+ *
+ * All use of this software is subject to the terms of the revised BSD
+ * license.  You should have received a copy of this license along
+ * with this source code in a file named "LICENSE."
+ *
+ * @file cInterval_ext.cxx
+ * @author rdb
+ * @date 2020-10-17
+ */
+
+#include "cInterval_ext.h"
+#include "cIntervalManager.h"
+#include "asyncFuture.h"
+
+#ifdef HAVE_PYTHON
+
+#ifndef CPPPARSER
+extern struct Dtool_PyTypedObject Dtool_CInterval;
+#endif
+
+/**
+ * Yields continuously until the interval is done.
+ */
+static PyObject *gen_next(PyObject *self) {
+  const CInterval *ival;
+  if (!Dtool_Call_ExtractThisPointer(self, Dtool_CInterval, (void **)&ival)) {
+    return nullptr;
+  }
+
+  if (ival->get_state() != CInterval::S_final) {
+    // Try again next frame.
+    Py_INCREF(Py_None);
+    return Py_None;
+  }
+  else {
+    PyErr_SetNone(PyExc_StopIteration);
+    return nullptr;
+  }
+}
+
+/**
+ * Awaiting an interval starts it and yields a future until it is done.
+ */
+PyObject *Extension<CInterval>::
+__await__(PyObject *self) {
+  if (_this->get_state() != CInterval::S_initial) {
+    PyErr_SetString(PyExc_RuntimeError, "Can only await an interval that is in the initial state.");
+    return nullptr;
+  }
+
+  _this->start();
+  return Dtool_NewGenerator(self, &gen_next);
+}
+
+#endif  // HAVE_PYTHON

+ 37 - 0
direct/src/interval/cInterval_ext.h

@@ -0,0 +1,37 @@
+/**
+ * PANDA 3D SOFTWARE
+ * Copyright (c) Carnegie Mellon University.  All rights reserved.
+ *
+ * All use of this software is subject to the terms of the revised BSD
+ * license.  You should have received a copy of this license along
+ * with this source code in a file named "LICENSE."
+ *
+ * @file cInterval_ext.h
+ * @author rdb
+ * @date 2020-10-17
+ */
+
+#ifndef CINTERVAL_EXT_H
+#define CINTERVAL_EXT_H
+
+#include "dtoolbase.h"
+
+#ifdef HAVE_PYTHON
+
+#include "extension.h"
+#include "cInterval.h"
+#include "py_panda.h"
+
+/**
+ * This class defines the extension methods for CInterval, which are called
+ * instead of any C++ methods with the same prototype.
+ */
+template<>
+class Extension<CInterval> : public ExtensionBase<CInterval> {
+public:
+  PyObject *__await__(PyObject *self);
+};
+
+#endif  // HAVE_PYTHON
+
+#endif  // CINTERVAL_EXT_H

+ 2 - 0
makepanda/makepanda.py

@@ -5589,6 +5589,7 @@ if (PkgSkip("DIRECT")==0):
   IGATEFILES=GetDirectoryContents('direct/src/interval', ["*.h", "*_composite*.cxx"])
   TargetAdd('libp3interval.in', opts=OPTS, input=IGATEFILES)
   TargetAdd('libp3interval.in', opts=['IMOD:panda3d.direct', 'ILIB:libp3interval', 'SRCDIR:direct/src/interval'])
+  PyTargetAdd('p3interval_cInterval_ext.obj', opts=OPTS, input='cInterval_ext.cxx')
 
 #
 # DIRECTORY: direct/src/showbase/
@@ -5647,6 +5648,7 @@ if (PkgSkip("DIRECT")==0):
   PyTargetAdd('direct.pyd', input='libp3showbase_igate.obj')
   PyTargetAdd('direct.pyd', input='libp3deadrec_igate.obj')
   PyTargetAdd('direct.pyd', input='libp3interval_igate.obj')
+  PyTargetAdd('direct.pyd', input='p3interval_cInterval_ext.obj')
   PyTargetAdd('direct.pyd', input='libp3distributed_igate.obj')
   PyTargetAdd('direct.pyd', input='libp3motiontrail_igate.obj')