From 8748cad750b9a10a9021a72f166b5d53dfcbb75c Mon Sep 17 00:00:00 2001 From: David Rose Date: Fri, 13 Jun 2003 21:28:11 +0000 Subject: [PATCH] add get_net_tag(), etc. --- panda/src/pgraph/nodePath.I | 45 +++++++++++++++++++++++++++++++---- panda/src/pgraph/nodePath.cxx | 19 +++++++++++++++ panda/src/pgraph/nodePath.h | 3 +++ 3 files changed, 63 insertions(+), 4 deletions(-) diff --git a/panda/src/pgraph/nodePath.I b/panda/src/pgraph/nodePath.I index b737c40bf8..c04d0df77b 100644 --- a/panda/src/pgraph/nodePath.I +++ b/panda/src/pgraph/nodePath.I @@ -326,7 +326,9 @@ has_parent() const { //////////////////////////////////////////////////////////////////// INLINE NodePath NodePath:: get_parent() const { - nassertr(has_parent(), NodePath::fail()); + if (!has_parent()) { + return NodePath::fail(); + } NodePath parent; parent._head = _head->get_next(); return parent; @@ -1149,11 +1151,15 @@ set_tag(const string &key, const string &value) { // Description: Retrieves the user-defined value that was previously // set on this node for the particular key, if any. If // no value has been previously set, returns the empty -// string. +// string. See also get_net_tag(). //////////////////////////////////////////////////////////////////// INLINE string NodePath:: get_tag(const string &key) const { - nassertr_always(!is_empty(), string()); + // An empty NodePath quietly returns no tags. This makes + // get_net_tag() easier to implement. + if (is_empty()) { + return string(); + } return node()->get_tag(key); } @@ -1163,10 +1169,15 @@ get_tag(const string &key) const { // Description: Returns true if a value has been defined on this node // for the particular key (even if that value is the // empty string), or false if no value has been set. +// See also has_net_tag(). //////////////////////////////////////////////////////////////////// INLINE bool NodePath:: has_tag(const string &key) const { - nassertr_always(!is_empty(), false); + // An empty NodePath quietly has no tags. This makes has_net_tag() + // easier to implement. + if (is_empty()) { + return false; + } return node()->has_tag(key); } @@ -1183,6 +1194,32 @@ clear_tag(const string &key) { node()->clear_tag(key); } +//////////////////////////////////////////////////////////////////// +// Function: NodePath::get_net_tag +// Access: Published +// Description: Returns the tag value that has been defined on this +// node, or the nearest ancestor node, for the indicated +// key. If no value has been defined for the indicated +// key on any ancestor node, returns the empty string. +// See also get_tag(). +//////////////////////////////////////////////////////////////////// +INLINE string NodePath:: +get_net_tag(const string &key) const { + return find_net_tag(key).get_tag(key); +} + +//////////////////////////////////////////////////////////////////// +// Function: NodePath::has_net_tag +// Access: Published +// Description: Returns true if the indicated tag value has been +// defined on this node or on any ancestor node, or +// false otherwise. See also has_tag(). +//////////////////////////////////////////////////////////////////// +INLINE bool NodePath:: +has_net_tag(const string &key) const { + return find_net_tag(key).has_tag(key); +} + INLINE ostream &operator << (ostream &out, const NodePath &node_path) { node_path.output(out); diff --git a/panda/src/pgraph/nodePath.cxx b/panda/src/pgraph/nodePath.cxx index f49d716cbf..87dbc58906 100644 --- a/panda/src/pgraph/nodePath.cxx +++ b/panda/src/pgraph/nodePath.cxx @@ -2936,6 +2936,25 @@ flatten_strong() { return num_removed; } +//////////////////////////////////////////////////////////////////// +// Function: NodePath::find_net_tag +// Access: Published +// Description: Returns the lowest ancestor of this node that +// contains a tag definition with the indicated key, if +// any, or an empty NodePath if no ancestor of this node +// contains this tag definition. See set_tag(). +//////////////////////////////////////////////////////////////////// +NodePath NodePath:: +find_net_tag(const string &key) const { + if (is_empty()) { + return NodePath::not_found(); + } + if (has_tag(key)) { + return *this; + } + return get_parent().find_net_tag(key); +} + //////////////////////////////////////////////////////////////////// // Function: NodePath::write_bam_file // Access: Published diff --git a/panda/src/pgraph/nodePath.h b/panda/src/pgraph/nodePath.h index 305422a7d0..50c4044852 100644 --- a/panda/src/pgraph/nodePath.h +++ b/panda/src/pgraph/nodePath.h @@ -523,6 +523,9 @@ PUBLISHED: INLINE string get_tag(const string &key) const; INLINE bool has_tag(const string &key) const; INLINE void clear_tag(const string &key); + INLINE string get_net_tag(const string &key) const; + INLINE bool has_net_tag(const string &key) const; + NodePath find_net_tag(const string &key) const; bool write_bam_file(const string &filename) const;