I haven’t seen anything in the FlexBox class that is preventing it from being trivially copyable.
Right now, if you try to create a grid of items and lay them out with nested FlexBoxes, you have to keep all of your Column flexboxes alive until the Row flexboxes go out of scope:
If you do this, you won’t see anything because that inner flexbox goes out of scope:
void Editor::resized()
{
juce::FlexBox fb;
for( auto i = 0; i < channelComponents.size() / 4; ++i )
{
FlexBox column;
column.flexDirection = juce::FlexBox::Direction::column;
for( auto j = 0; j < channelComponents.size() / 4; ++j )
{
column.items.add( juce::FlexItem( *channelComponents[i*4 + j] ).withFlex(1.f) );
}
fb.items.add( juce::FlexItem(column).withFlex(1.f) );
}
fb.performLayout(getLocalBounds());
}
Yet, there’s nothing in the FlexBox class that prevents it from being copyable if you’re storing it inside a FlexItem. I haven’t seen a compelling reason to continue keeping this FlexItem member variable as a pointer.
/** If this is non-null, it represents a FlexBox whose bounds are controlled by this item. */
FlexBox* associatedFlexBox = nullptr;
All of FlexBox’s member variables are trivially copyable:
class JUCE_API FlexBox final
{
public:
//==============================================================================
Direction flexDirection = Direction::row;
Wrap flexWrap = Wrap::noWrap;
AlignContent alignContent = AlignContent::stretch;
AlignItems alignItems = AlignItems::stretch;
JustifyContent justifyContent = JustifyContent::flexStart;
Array<FlexItem> items; //trivially copyable
private:
JUCE_LEAK_DETECTOR (FlexBox)
};
This is the annoying workaround to make that first example work correctly:
void Editor::resized()
{
juce::FlexBox fb;
std::vector<juce::FlexBox> columns { static_cast<size_t>(channelComponents.size() / 4) };
for( auto i = 0; i < channelComponents.size() / 4; ++i )
{
auto& column = columns[i];
column.flexDirection = juce::FlexBox::Direction::column;
for( auto j = 0; j < channelComponents.size() / 4; ++j )
{
column.items.add( juce::FlexItem( *channelComponents[i*4 + j] ).withFlex(1.f) );
}
fb.items.add( juce::FlexItem(column).withFlex(1.f) );
}
fb.performLayout(getLocalBounds());
}
Is there a reason that FlexBox’s aren’t copyable? Making this change would help a lot of people not make the stupid mistake of letting nested FlexBoxes go out of scope.
Am I missing something obvious that explains why FlexItem::associatedFlexBox should remain as a pointer?