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
This commit is contained in:
Jesse Yurkovich 2021-11-18 14:32:56 -08:00
parent 3f288e9414
commit 5ed3a5d023
1 changed files with 20 additions and 2 deletions

View File

@ -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"