use fold expressiong for ensureNodes

This commit is contained in:
Vogel, Rick 2023-07-25 12:36:13 +02:00
parent 4acd03c21b
commit bfc5dab5b1
2 changed files with 24 additions and 52 deletions

View File

@ -4,63 +4,35 @@
namespace QskSGNode namespace QskSGNode
{ {
enum AppendMode : bool { enum AppendMode
Sequential = false, {
Recursive = true Sequential,
Recursive
}; };
namespace detail
{
template< AppendMode Mode, typename... Ts >
struct append;
template<>
struct append<AppendMode::Recursive>
{
static void nodes( QSGNode* root )
{
}
};
template<>
struct append<AppendMode::Sequential>
{
static void nodes( QSGNode* root )
{
}
};
template< typename Child, typename... Children >
struct append< AppendMode::Recursive, Child, Children... >
{
static void nodes( QSGNode* root )
{
auto* const child = new Child;
root->appendChildNode( child );
append< AppendMode::Recursive, Children... >::nodes( child );
}
};
template< typename Child, typename... Children >
struct append< AppendMode::Sequential, Child, Children... >
{
static void nodes( QSGNode* root )
{
auto* const child = new Child;
root->appendChildNode( child );
append< AppendMode::Sequential, Children... >::nodes( root );
}
};
}
template< AppendMode mode, typename Root, typename... Children > template< AppendMode mode, typename Root, typename... Children >
inline Q_REQUIRED_RESULT Root* ensureNodes( QSGNode* root = nullptr ) inline Q_REQUIRED_RESULT Root* ensureNodes( QSGNode* root = nullptr )
{ {
if ( root == nullptr ) if ( root == nullptr )
{ {
root = new std::remove_const_t<Root>(); root = new Root;
detail::append< mode, Children... >::nodes( root );
} }
return static_cast<Root*>(root);
if constexpr ( mode == Recursive )
{
QSGNode* current = root;
(
[ & ]( QSGNode* const child ) {
current->appendChildNode( child );
current = child;
}( new Children ),
... );
}
else
{
( root->appendChildNode( new Children ), ... );
}
return static_cast< Root* >( root );
} }
} }