Browse Source

prc: Add pickle support to ConfigVariable

The current value is not pickled.  I might change my mind on this, but my thinking is that ConfigVariable doesn't really contain a value, it's just an accessor for a value from the config page, so the current state of the variables should be pickled with the config pages.
rdb 4 years ago
parent
commit
a0c2f2ff3b

+ 2 - 0
dtool/src/prc/CMakeLists.txt

@@ -74,6 +74,8 @@ if(HAVE_OPENSSL)
 endif()
 endif()
 
 
 set(P3PRC_IGATEEXT
 set(P3PRC_IGATEEXT
+  configVariable_ext.cxx
+  configVariable_ext.h
   streamReader_ext.cxx
   streamReader_ext.cxx
   streamReader_ext.h
   streamReader_ext.h
   streamWriter_ext.cxx
   streamWriter_ext.cxx

+ 2 - 0
dtool/src/prc/configVariable.h

@@ -44,6 +44,8 @@ PUBLISHED:
 
 
   INLINE size_t get_num_words() const;
   INLINE size_t get_num_words() const;
 
 
+  EXTENSION(PyObject *__reduce__(PyObject *self) const);
+
 protected:
 protected:
   INLINE const ConfigDeclaration *get_default_value() const;
   INLINE const ConfigDeclaration *get_default_value() const;
 
 

+ 39 - 0
dtool/src/prc/configVariable_ext.cxx

@@ -0,0 +1,39 @@
+/**
+ * PANDA 3D SOFTWAREitiueuiitgyrsrtfu
+ * 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 configVariable_ext.cxx
+ * @author rdb
+ * @date 2021-01-01
+ */
+
+#include "configVariable_ext.h"
+
+#ifdef HAVE_PYTHON
+
+/**
+ * Implements pickle support.
+ */
+PyObject *Extension<ConfigVariable>::
+__reduce__(PyObject *self) const {
+  const std::string &name = _this->get_name();
+  const std::string &descr = _this->get_description();
+  int flags = _this->get_flags();
+
+  // If the subclass defines a get_default_value method, we assume it takes a
+  // default value in the constructor.
+  PyObject *get_default_value = PyObject_GetAttrString((PyObject *)Py_TYPE(self), "get_default_value");
+  if (get_default_value != nullptr) {
+    PyObject *default_value = PyObject_CallOneArg(get_default_value, self);
+    return Py_BuildValue("O(s#Ns#i)", Py_TYPE(self), name.data(), (Py_ssize_t)name.length(), default_value, descr.data(), (Py_ssize_t)descr.length(), flags);
+  }
+  else {
+    return Py_BuildValue("O(s#s#i)", Py_TYPE(self), name.data(), (Py_ssize_t)name.length(), descr.data(), (Py_ssize_t)descr.length(), flags);
+  }
+}
+
+#endif

+ 37 - 0
dtool/src/prc/configVariable_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 configVariable_ext.h
+ * @author rdb
+ * @date 2021-12-10
+ */
+
+#ifndef CONFIGVARIABLE_EXT_H
+#define CONFIGVARIABLE_EXT_H
+
+#include "dtoolbase.h"
+
+#ifdef HAVE_PYTHON
+
+#include "extension.h"
+#include "configVariable.h"
+#include "py_panda.h"
+
+/**
+ * This class defines the extension methods for ConfigVariable, which are called
+ * instead of any C++ methods with the same prototype.
+ */
+template<>
+class Extension<ConfigVariable> : public ExtensionBase<ConfigVariable> {
+public:
+  PyObject *__reduce__(PyObject *self) const;
+};
+
+#endif  // HAVE_PYTHON
+
+#endif  // CONFIGVARIABLE_EXT_H

+ 1 - 0
dtool/src/prc/p3prc_ext_composite.cxx

@@ -1,2 +1,3 @@
+#include "configVariable_ext.cxx"
 #include "streamReader_ext.cxx"
 #include "streamReader_ext.cxx"
 #include "streamWriter_ext.cxx"
 #include "streamWriter_ext.cxx"