From 5ed3a5d0237949411fe403c9a1cbb5a53a685f69 Mon Sep 17 00:00:00 2001 From: Jesse Yurkovich Date: Thu, 18 Nov 2021 14:32:56 -0800 Subject: 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 --- source/blender/makesrna/intern/makesrna.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) (limited to '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 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 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\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" -- cgit v1.2.3