TreeModelColumnRecord: Allow constructing with columns
@dboles
Submitted by Daniel Boles Link to original bug (#782454)
Description
Being a big fan of const-correctness, I would like this to be possible. That way, when the set of columns is known and fixed at the time of construction, we can add them in the ctor, and have the TreeModelColumnRecord be const thereafter.
This would tie in with Bug 780134 in that it requires us to define a way to pass multiple columns (or widgets, or etc.) to some object for registration. I like to think I've found a nice solution for this; here are the comments I've written up so far:
Add an overload of add() that takes an std::initializer_list of
std::reference_wrappers to TreeModelColumnBases and calls the
single-column overload of add() for each of these columns in turn. This
spares the user having to write add() for each column manually and is in
keeping with such convenient uses of initializer_list in the C++ stdlib.
std::initializer_list is a thin iterable, const wrapper around an array
stack-allocated by the compiler at the call site. If there turns out to
be a need to use a modifiable container such as std::vector, we could
do that instead, but with the added cost of dynamic allocation overhead.
std::reference_wrapper is a pointer wrapped in a thin class that grants
reference-like semantics. This is needed because (A) initializer_list
and other containers cannot hold references, and (B) if we simply took a
list of pointers instead, then a user might pass a nullptr, and we would
have to deal with that possibility, even though it would be nonsensical.
By using reference_wrapper, we prevent nullptrs and present a nice API:
What do you think?