From 681551c918e5d1422e645035be00d9675286500d Mon Sep 17 00:00:00 2001 From: David Rose Date: Sun, 15 Feb 2009 01:45:15 +0000 Subject: [PATCH] empty-node-path handling --- panda/src/pgraph/nodePath.I | 11 ----- panda/src/pgraph/nodePath.cxx | 85 +++++++++++++++++++++++++++++++++++ panda/src/pgraph/nodePath.h | 2 +- 3 files changed, 86 insertions(+), 12 deletions(-) diff --git a/panda/src/pgraph/nodePath.I b/panda/src/pgraph/nodePath.I index 40e20810d4..bf9e92c0f9 100644 --- a/panda/src/pgraph/nodePath.I +++ b/panda/src/pgraph/nodePath.I @@ -222,17 +222,6 @@ is_empty() const { 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 // Access: Published diff --git a/panda/src/pgraph/nodePath.cxx b/panda/src/pgraph/nodePath.cxx index 6ed27bdf5c..18ca7726a4 100644 --- a/panda/src/pgraph/nodePath.cxx +++ b/panda/src/pgraph/nodePath.cxx @@ -72,6 +72,91 @@ int NodePath::_max_search_depth = 7000; 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 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 // Access: Published diff --git a/panda/src/pgraph/nodePath.h b/panda/src/pgraph/nodePath.h index 4f9a929c7f..e77c93eb86 100644 --- a/panda/src/pgraph/nodePath.h +++ b/panda/src/pgraph/nodePath.h @@ -177,7 +177,7 @@ PUBLISHED: // Methods to query a NodePath's contents. INLINE bool is_empty() const; - INLINE operator bool () const; + operator bool () 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;