use fold expressiong for ensureNodes
This commit is contained in:
parent
4acd03c21b
commit
bfc5dab5b1
|
@ -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 );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 );
|
return static_cast< Root* >( root );
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue