Page MenuHome

ArrayBase data structure
Needs ReviewPublic

Authored by Jacques Lucke (JacquesLucke) on Jan 18 2019, 3:14 PM.
This revision needs review, but there are no reviewers specified.

Details

Reviewers
None
Summary

Goals:

  • Provide an easy to use alternative to ListBase that avoids many memory allocations when not necessary.
  • Avoid having to pass an array and its length around separately.
  • Provide some type safety (which ListBase does not provide).

Building a data structure that achieves all goal would be easy in C++.
However, most of Blender's source code is still C; so to make this structure useful, it should be usable from C.

A good compromise has to be found between:

  • Type safety (type safe structures are also usually easier to use because less type casting is necessary).
  • Code duplication.
  • "Hacking" using the preprocessor.

I think type safety and ease of use should be prioritized.
Ideally users of the data structure don't have to do anything that is not strictly necessary (e.g. type casting).
The ideal interface to a data structure is less likely to change than its implementation.

I think the ideal C interface to an array could look like so:

IntArray *array = BLI_IntArray_new();
BLI_IntArray_free(array);

ObjectArray objects;
BLI_ObjectArray_init(&objects);
BLI_ObjectArray_append(&objects, ob);
BLI_ObjectArray_clear(&objects);

There are multiple ways to write an implementation for this interface:

  • Duplicate the whole code multiple times.
  • Use the C preprocessor to simulate C++ templates.
  • Use some other (custom) preprocessor to generate the C code for all object types.
  • Implement the structure in C++ using templates and generate C wrappers for all relevant types (can also use the preprocessor to reduce code duplication).

For now I decided to use the C preprocessor to simulate C++ templates.
However, except for the first approach, I would be fine with all of them.

It would be good if autocompletion engines are able to find all functions when we use more complex preprocessor stuff.
Not sure about other editors, but VS Code does a great job autocompleting simulated templates.

Diff Detail

Repository
rB Blender
Branch
arraybase (branched from master)
Build Status
Buildable 2750
Build 2750: arc lint + arc unit