[Bf-blender-cvs] [5ed3a5d0237] master: Cleanup: Add an empty() method to RNA's CollectionRef class

Jesse Yurkovich noreply at git.blender.org
Thu Nov 18 23:34:43 CET 2021


Commit: 5ed3a5d0237949411fe403c9a1cbb5a53a685f69
Author: Jesse Yurkovich
Date:   Thu Nov 18 14:32:56 2021 -0800
Branches: master
https://developer.blender.org/rB5ed3a5d0237949411fe403c9a1cbb5a53a685f69

Cleanup: Add an empty() method to RNA's CollectionRef class

The existing RNA CollectionRef class only offers a length() operation
which is sometimes used for checking if the collection is empty. This is
inefficient for certain collection types which do not have a native
length member; the entire list is iterated to find the count.

This patch creates an explicit empty() method to be used in such cases
for better semantics. Additionally, many collection types will behave
more efficiently when using the new method instead of checking length.

Making use of the new method will follow separately.

Differential Revision: https://developer.blender.org/D12314

===================================================================

M	source/blender/makesrna/intern/makesrna.c

===================================================================

diff --git a/source/blender/makesrna/intern/makesrna.c b/source/blender/makesrna/intern/makesrna.c
index f2e87b29c1f..a6732ca1760 100644
--- a/source/blender/makesrna/intern/makesrna.c
+++ b/source/blender/makesrna/intern/makesrna.c
@@ -4698,6 +4698,19 @@ static const char *cpp_classes =
     "    inline static int sname##_##identifier##_length_wrap(PointerRNA *ptr) \\\n"
     "    { return sname##_##identifier##_length(ptr); } \n"
     "\n"
+    "#define COLLECTION_PROPERTY_EMPTY_false(sname, identifier) \\\n"
+    "    inline static bool sname##_##identifier##_empty_wrap(PointerRNA *ptr) \\\n"
+    "    { \\\n"
+    "        CollectionPropertyIterator iter; \\\n"
+    "        sname##_##identifier##_begin(&iter, ptr); \\\n"
+    "        bool empty = !iter.valid; \\\n"
+    "        sname##_##identifier##_end(&iter); \\\n"
+    "        return empty; \\\n"
+    "    } \n"
+    "#define COLLECTION_PROPERTY_EMPTY_true(sname, identifier) \\\n"
+    "    inline static bool sname##_##identifier##_empty_wrap(PointerRNA *ptr) \\\n"
+    "    { return sname##_##identifier##_length(ptr) == 0; } \n"
+    "\n"
     "#define COLLECTION_PROPERTY_LOOKUP_INT_false(sname, identifier) \\\n"
     "    inline static int sname##_##identifier##_lookup_int_wrap(PointerRNA *ptr, int key, "
     "PointerRNA *r_ptr) \\\n"
@@ -4774,11 +4787,13 @@ static const char *cpp_classes =
     "    typedef CollectionIterator<type, sname##_##identifier##_begin, \\\n"
     "        sname##_##identifier##_next, sname##_##identifier##_end> identifier##_iterator; \\\n"
     "    COLLECTION_PROPERTY_LENGTH_##has_length(sname, identifier) \\\n"
+    "    COLLECTION_PROPERTY_EMPTY_##has_length(sname, identifier) \\\n"
     "    COLLECTION_PROPERTY_LOOKUP_INT_##has_lookup_int(sname, identifier) \\\n"
     "    COLLECTION_PROPERTY_LOOKUP_STRING_##has_lookup_string(sname, identifier) \\\n"
     "    CollectionRef<sname, type, sname##_##identifier##_begin, \\\n"
     "        sname##_##identifier##_next, sname##_##identifier##_end, \\\n"
     "        sname##_##identifier##_length_wrap, \\\n"
+    "        sname##_##identifier##_empty_wrap, \\\n"
     "        sname##_##identifier##_lookup_int_wrap, sname##_##identifier##_lookup_string_wrap, "
     "collection_funcs> identifier;\n"
     "\n"
@@ -4844,6 +4859,7 @@ static const char *cpp_classes =
     "typedef void (*TNextFunc)(CollectionPropertyIterator *iter);\n"
     "typedef void (*TEndFunc)(CollectionPropertyIterator *iter);\n"
     "typedef int (*TLengthFunc)(PointerRNA *ptr);\n"
+    "typedef bool (*TEmptyFunc)(PointerRNA *ptr);\n"
     "typedef int (*TLookupIntFunc)(PointerRNA *ptr, int key, PointerRNA *r_ptr);\n"
     "typedef int (*TLookupStringFunc)(PointerRNA *ptr, const char *key, PointerRNA *r_ptr);\n"
     "\n"
@@ -4882,8 +4898,8 @@ static const char *cpp_classes =
     "};\n"
     "\n"
     "template<typename Tp, typename T, TBeginFunc Tbegin, TNextFunc Tnext, TEndFunc Tend,\n"
-    "         TLengthFunc Tlength, TLookupIntFunc Tlookup_int, TLookupStringFunc Tlookup_string,\n"
-    "         typename Tcollection_funcs>\n"
+    "         TLengthFunc Tlength, TEmptyFunc Tempty, TLookupIntFunc Tlookup_int,\n"
+    "         TLookupStringFunc Tlookup_string, typename Tcollection_funcs>\n"
     "class CollectionRef : public Tcollection_funcs {\n"
     "public:\n"
     "    CollectionRef(const PointerRNA &p) : Tcollection_funcs(p), ptr(p) {}\n"
@@ -4897,6 +4913,8 @@ static const char *cpp_classes =
     ""
     "    int length()\n"
     "    { return Tlength(&ptr); }\n"
+    "    bool empty()\n"
+    "    { return Tempty(&ptr); }\n"
     "    T operator[](int key)\n"
     "    { PointerRNA r_ptr; Tlookup_int(&ptr, key, &r_ptr); return T(r_ptr); }\n"
     "    T operator[](const std::string &key)\n"



More information about the Bf-blender-cvs mailing list