From 8c83ab08f4df23539a7a9fd0f61b1579fdf638e0 Mon Sep 17 00:00:00 2001 From: rdb Date: Sun, 2 Dec 2012 12:21:17 +0000 Subject: [PATCH] Add get_tag_keys / get_python_tag_keys (based on patch by Josh Enes) --- panda/src/pgraph/nodePath.I | 67 ++++++++++++++++++++++++++ panda/src/pgraph/nodePath.h | 4 ++ panda/src/pgraph/pandaNode.cxx | 88 ++++++++++++++++++++++++++++++++++ panda/src/pgraph/pandaNode.h | 6 ++- 4 files changed, 164 insertions(+), 1 deletion(-) diff --git a/panda/src/pgraph/nodePath.I b/panda/src/pgraph/nodePath.I index 5dc6a6b542..d7eced9bf0 100644 --- a/panda/src/pgraph/nodePath.I +++ b/panda/src/pgraph/nodePath.I @@ -2181,6 +2181,73 @@ get_tag(const string &key) const { return node()->get_tag(key); } +//////////////////////////////////////////////////////////////////// +// Function: NodePath::get_tag_keys +// Access: Published +// Description: Fills the given vector up with the +// list of tags on this PandaNode. +// +// It is the user's responsibility to ensure that the +// keys vector is empty before making this call; +// otherwise, the new files will be appended to it. +//////////////////////////////////////////////////////////////////// +INLINE void NodePath:: +get_tag_keys(vector_string &keys) const { + nassertv_always(!is_empty()); + node()->get_tag_keys(keys); +} + +#ifdef HAVE_PYTHON +//////////////////////////////////////////////////////////////////// +// Function: NodePath::get_python_tag_keys +// Access: Published +// Description: Fills the given vector up with the +// list of Python tags on this PandaNode. +// +// It is the user's responsibility to ensure that the +// keys vector is empty before making this call; +// otherwise, the new files will be appended to it. +//////////////////////////////////////////////////////////////////// +INLINE void NodePath:: +get_python_tag_keys(vector_string &keys) const { + nassertv_always(!is_empty()); + node()->get_python_tag_keys(keys); +} + +//////////////////////////////////////////////////////////////////// +// Function: Filename::get_tag_keys +// Access: Published +// Description: This variant on get_tag_keys returns a Python list +// of strings. Returns None if the NodePath is empty. +//////////////////////////////////////////////////////////////////// +INLINE PyObject *NodePath:: +get_tag_keys() const { + // An empty NodePath returns None + if (is_empty()) { + Py_INCREF(Py_None); + return Py_None; + } + return node()->get_tag_keys(); +} + +//////////////////////////////////////////////////////////////////// +// Function: Filename::get_python_tag_keys +// Access: Published +// Description: This variant on get_python_tag_keys returns a +// Python list of strings. +// Returns None if the NodePath is empty. +//////////////////////////////////////////////////////////////////// +INLINE PyObject *NodePath:: +get_python_tag_keys() const { + // An empty NodePath returns None + if (is_empty()) { + Py_INCREF(Py_None); + return Py_None; + } + return node()->get_python_tag_keys(); +} +#endif // HAVE_PYTHON + //////////////////////////////////////////////////////////////////// // Function: NodePath::has_tag // Access: Published diff --git a/panda/src/pgraph/nodePath.h b/panda/src/pgraph/nodePath.h index 2a5bda7ad9..c12ea43ce5 100644 --- a/panda/src/pgraph/nodePath.h +++ b/panda/src/pgraph/nodePath.h @@ -891,6 +891,7 @@ PUBLISHED: INLINE void set_tag(const string &key, const string &value); INLINE string get_tag(const string &key) const; + INLINE void get_tag_keys(vector_string &keys) 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; @@ -898,8 +899,11 @@ PUBLISHED: NodePath find_net_tag(const string &key) const; #ifdef HAVE_PYTHON + INLINE PyObject *get_tag_keys() const; INLINE void set_python_tag(const string &key, PyObject *value); INLINE PyObject *get_python_tag(const string &key) const; + INLINE void get_python_tag_keys(vector_string &keys) const; + INLINE PyObject *get_python_tag_keys() const; INLINE bool has_python_tag(const string &key) const; INLINE void clear_python_tag(const string &key); INLINE PyObject *get_net_python_tag(const string &key) const; diff --git a/panda/src/pgraph/pandaNode.cxx b/panda/src/pgraph/pandaNode.cxx index 4b781e8367..5e516ee5b0 100644 --- a/panda/src/pgraph/pandaNode.cxx +++ b/panda/src/pgraph/pandaNode.cxx @@ -1721,6 +1721,94 @@ list_tags(ostream &out, const string &separator) const { #endif // HAVE_PYTHON } +//////////////////////////////////////////////////////////////////// +// Function: Filename::get_tag_keys +// Access: Published +// Description: Fills the given vector up with the +// list of tags on this PandaNode. +// +// It is the user's responsibility to ensure that the +// keys vector is empty before making this call; +// otherwise, the new keys will be appended to it. +//////////////////////////////////////////////////////////////////// +void PandaNode:: +get_tag_keys(vector_string &keys) const { + CDReader cdata(_cycler); + if (!cdata->_tag_data.empty()) { + TagData::const_iterator ti = cdata->_tag_data.begin(); + while (ti != cdata->_tag_data.end()) { + keys.push_back((*ti).first); + ++ti; + } + } +} + +#ifdef HAVE_PYTHON +//////////////////////////////////////////////////////////////////// +// Function: Filename::get_python_tag_keys +// Access: Published +// Description: Fills the given vector up with the +// list of Python tags on this PandaNode. +// +// It is the user's responsibility to ensure that the +// keys vector is empty before making this call; +// otherwise, the new files will be appended to it. +//////////////////////////////////////////////////////////////////// +void PandaNode:: +get_python_tag_keys(vector_string &keys) const { + CDReader cdata(_cycler); + if (!cdata->_python_tag_data.empty()) { + PythonTagData::const_iterator ti = cdata->_python_tag_data.begin(); + while (ti != cdata->_python_tag_data.end()) { + keys.push_back((*ti).first); + ++ti; + } + } +} + +//////////////////////////////////////////////////////////////////// +// Function: Filename::get_tag_keys +// Access: Published +// Description: This variant on get_tag_keys returns +// a Python list of strings. +//////////////////////////////////////////////////////////////////// +PyObject *PandaNode:: +get_tag_keys() const { + vector_string keys; + get_tag_keys(keys); + + PyObject *result = PyList_New(keys.size()); + for (size_t i = 0; i < keys.size(); ++i) { + const string &tag_name = keys[i]; + PyObject *str = PyString_FromStringAndSize(tag_name.data(), tag_name.size()); + PyList_SET_ITEM(result, i, str); + } + + return result; +} + +//////////////////////////////////////////////////////////////////// +// Function: Filename::get_python_tag_keys +// Access: Published +// Description: This variant on get_python_tag_keys returns +// a Python list of strings. +//////////////////////////////////////////////////////////////////// +PyObject *PandaNode:: +get_python_tag_keys() const { + vector_string keys; + get_python_tag_keys(keys); + + PyObject *result = PyList_New(keys.size()); + for (size_t i = 0; i < keys.size(); ++i) { + const string &tag_name = keys[i]; + PyObject *str = PyString_FromStringAndSize(tag_name.data(), tag_name.size()); + PyList_SET_ITEM(result, i, str); + } + + return result; +} +#endif // HAVE_PYTHON + //////////////////////////////////////////////////////////////////// // Function: PandaNode::compare_tags // Access: Published diff --git a/panda/src/pgraph/pandaNode.h b/panda/src/pgraph/pandaNode.h index c3461d9f32..5970d086dd 100644 --- a/panda/src/pgraph/pandaNode.h +++ b/panda/src/pgraph/pandaNode.h @@ -201,12 +201,16 @@ PUBLISHED: Thread *current_thread = Thread::get_current_thread()) const; void clear_tag(const string &key, Thread *current_thread = Thread::get_current_thread()); - + void get_tag_keys(vector_string &keys) const; #ifdef HAVE_PYTHON + PyObject *get_tag_keys() const; + void set_python_tag(const string &key, PyObject *value); PyObject *get_python_tag(const string &key) const; bool has_python_tag(const string &key) const; void clear_python_tag(const string &key); + void get_python_tag_keys(vector_string &keys) const; + PyObject *get_python_tag_keys() const; #endif // HAVE_PYTHON INLINE bool has_tags() const;