Browse Source

empty-node-path handling

David Rose 17 years ago
parent
commit
681551c918
3 changed files with 86 additions and 12 deletions
  1. 0 11
      panda/src/pgraph/nodePath.I
  2. 85 0
      panda/src/pgraph/nodePath.cxx
  3. 1 1
      panda/src/pgraph/nodePath.h

+ 0 - 11
panda/src/pgraph/nodePath.I

@@ -222,17 +222,6 @@ is_empty() const {
   return (_head == (NodePathComponent *)NULL);
   return (_head == (NodePathComponent *)NULL);
 }
 }
 
 
-////////////////////////////////////////////////////////////////////
-//     Function: NodePath::operator bool
-//       Access: Published
-//  Description: Returns true if the NodePath is valid (not empty),
-//               or false if it contains no nodes.
-////////////////////////////////////////////////////////////////////
-INLINE NodePath::
-operator bool () const {
-  return !is_empty();
-}
-
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: NodePath::is_singleton
 //     Function: NodePath::is_singleton
 //       Access: Published
 //       Access: Published

+ 85 - 0
panda/src/pgraph/nodePath.cxx

@@ -72,6 +72,91 @@
 int NodePath::_max_search_depth = 7000; 
 int NodePath::_max_search_depth = 7000; 
 TypeHandle NodePath::_type_handle;
 TypeHandle NodePath::_type_handle;
 
 
+
+// ***Begin temporary transition code for operator bool
+enum EmptyNodePathType {
+  ENP_future,
+  ENP_transition,
+  ENP_deprecated,
+};
+
+ostream &operator << (ostream &out, EmptyNodePathType enp) {
+  switch (enp) {
+  case ENP_future:
+    return out << "future";
+  case ENP_transition:
+    return out << "transition";
+  case ENP_deprecated:
+    return out << "deprecated";
+  }
+  return out << "**invalid EmptyNodePathType value (" << (int)enp << ")**";
+}
+
+istream &operator >> (istream &in, EmptyNodePathType &enp) {
+  string word;
+  in >> word;
+  if (word == "future") {
+    enp = ENP_future;
+  } else if (word == "transition") {
+    enp = ENP_transition;
+  } else if (word == "deprecated") {
+    enp = ENP_deprecated;
+  } else {
+    pgraph_cat.warning()
+      << "Invalid EmptyNodePathType value (\"" << word << "\")\n";
+    enp = ENP_transition;
+  }
+  return in;
+}
+
+static ConfigVariableEnum<EmptyNodePathType> empty_node_path
+("empty-node-path", ENP_transition,
+ PRC_DESC("This is a temporary transition variable to control the behavior "
+          "of a NodePath when it is used as a boolean false.  Set this to "
+          "'deprecated' to preserve the original behavior: every NodePath "
+          "evaluates true, even an empty NodePath.  Set it to 'future' to "
+          "support the new behavior: non-empty NodePaths evaluate true, "
+          "and empty NodePaths evaluate false.  Set it to 'transition' to "
+          "raise an exception if an empty NodePath is used as a boolean."));
+
+// ***End temporary transition code for operator bool
+
+
+////////////////////////////////////////////////////////////////////
+//     Function: NodePath::operator bool
+//       Access: Published
+//  Description: Returns true if the NodePath is valid (not empty),
+//               or false if it contains no nodes.
+////////////////////////////////////////////////////////////////////
+NodePath::
+operator bool () const {
+  switch (empty_node_path) {
+  case ENP_future:
+    return !is_empty();
+
+  case ENP_deprecated:
+    return true;
+
+  case ENP_transition:
+    if (!is_empty()) {
+      return true;
+    }
+
+    {
+      const char *message = "Using an empty NodePath as a boolean value.  Because the meaning of this operation is changing, you should avoid doing this to avoid ambiguity, or set the config variable empty-node-path to 'future' or 'deprecated' to specify the desired behavior.";
+      pgraph_cat.warning()
+        << message << "\n";
+#ifdef HAVE_PYTHON
+      PyErr_Warn(PyExc_FutureWarning, (char *)message);
+#endif
+    }
+    return true;
+  }
+
+  nassertr(false, true);
+  return true;
+}
+
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: NodePath::get_num_nodes
 //     Function: NodePath::get_num_nodes
 //       Access: Published
 //       Access: Published

+ 1 - 1
panda/src/pgraph/nodePath.h

@@ -177,7 +177,7 @@ PUBLISHED:
 
 
   // Methods to query a NodePath's contents.
   // Methods to query a NodePath's contents.
   INLINE bool is_empty() const;
   INLINE bool is_empty() const;
-  INLINE operator bool () const;
+  operator bool () const;
 
 
   INLINE bool is_singleton(Thread *current_thread = Thread::get_current_thread()) const;
   INLINE bool is_singleton(Thread *current_thread = Thread::get_current_thread()) const;
   int get_num_nodes(Thread *current_thread = Thread::get_current_thread()) const;
   int get_num_nodes(Thread *current_thread = Thread::get_current_thread()) const;