@@ -372,6 +372,50 @@ void EntityComponentManager::EachNoCache(typename identity<std::function<
372
372
}
373
373
}
374
374
375
+ namespace detail
376
+ {
377
+ /// \brief Helper template to call a callback function with each of the
378
+ /// components in the _data vector expanded as arguments to the callback
379
+ /// function.
380
+ /// \tparam ComponentTypeTs The actual types of each of the components.
381
+ /// \tparam FuncT The type of the callback function.
382
+ /// \tparam BaseComponentT Either "BaseComponent" or "const BaseComponent"
383
+ /// \tparam Is Index sequence that will be used to iterate through the vector
384
+ /// _data.
385
+ /// \param[in] _f The callback function
386
+ /// \param[in] _entity The entity associated with the components.
387
+ /// \param[in] _data A vector of component pointers that will be expanded to
388
+ /// become the arguments of the callback function _f.
389
+ /// \return The value of return by the function _f.
390
+ template <typename... ComponentTypeTs, typename FuncT, typename BaseComponentT,
391
+ std::size_t... Is>
392
+ constexpr bool applyFunctionImpl(const FuncT &_f, const Entity &_entity,
393
+ const std::vector<BaseComponentT *> &_data,
394
+ std::index_sequence<Is...>)
395
+ {
396
+ return _f(_entity, static_cast<ComponentTypeTs *>(_data[Is])...);
397
+ }
398
+
399
+ /// \brief Helper template to call a callback function with each of the
400
+ /// components in the _data vector expanded as arguments to the callback
401
+ /// function.
402
+ /// \tparam ComponentTypeTs The actual types of each of the components.
403
+ /// \tparam FuncT The type of the callback function.
404
+ /// \tparam BaseComponentT Either "BaseComponent" or "const BaseComponent"
405
+ /// \param[in] _f The callback function
406
+ /// \param[in] _entity The entity associated with the components.
407
+ /// \param[in] _data A vector of component pointers that will be expanded to
408
+ /// become the arguments of the callback function _f.
409
+ /// \return The value of return by the function _f.
410
+ template <typename... ComponentTypeTs, typename FuncT, typename BaseComponentT>
411
+ constexpr bool applyFunction(const FuncT &_f, const Entity &_entity,
412
+ const std::vector<BaseComponentT *> &_data)
413
+ {
414
+ return applyFunctionImpl<ComponentTypeTs...>(
415
+ _f, _entity, _data, std::index_sequence_for<ComponentTypeTs...>{});
416
+ }
417
+ } // namespace detail
418
+
375
419
//////////////////////////////////////////////////
376
420
template<typename ...ComponentTypeTs>
377
421
void EntityComponentManager::Each(typename identity<std::function<
@@ -385,7 +429,8 @@ void EntityComponentManager::Each(typename identity<std::function<
385
429
// function.
386
430
for (const Entity entity : view->Entities())
387
431
{
388
- if (!std::apply(_f, view->EntityComponentConstData(entity)))
432
+ const auto &data = view->EntityComponentData(entity);
433
+ if (!detail::applyFunction<const ComponentTypeTs...>(_f, entity, data))
389
434
{
390
435
break;
391
436
}
@@ -405,7 +450,8 @@ void EntityComponentManager::Each(typename identity<std::function<
405
450
// function.
406
451
for (const Entity entity : view->Entities())
407
452
{
408
- if (!std::apply(_f, view->EntityComponentData(entity)))
453
+ const auto &data = view->EntityComponentData(entity);
454
+ if (!detail::applyFunction<ComponentTypeTs...>(_f, entity, data))
409
455
{
410
456
break;
411
457
}
@@ -434,7 +480,8 @@ void EntityComponentManager::EachNew(typename identity<std::function<
434
480
// function.
435
481
for (const Entity entity : view->NewEntities())
436
482
{
437
- if (!std::apply(_f, view->EntityComponentData(entity)))
483
+ const auto &data = view->EntityComponentData(entity);
484
+ if (!detail::applyFunction<ComponentTypeTs...>(_f, entity, data))
438
485
{
439
486
break;
440
487
}
@@ -455,7 +502,8 @@ void EntityComponentManager::EachNew(typename identity<std::function<
455
502
// function.
456
503
for (const Entity entity : view->NewEntities())
457
504
{
458
- if (!std::apply(_f, view->EntityComponentConstData(entity)))
505
+ const auto &data = view->EntityComponentData(entity);
506
+ if (!detail::applyFunction<const ComponentTypeTs...>(_f, entity, data))
459
507
{
460
508
break;
461
509
}
@@ -476,7 +524,8 @@ void EntityComponentManager::EachRemoved(typename identity<std::function<
476
524
// function.
477
525
for (const Entity entity : view->ToRemoveEntities())
478
526
{
479
- if (!std::apply(_f, view->EntityComponentConstData(entity)))
527
+ const auto &data = view->EntityComponentData(entity);
528
+ if (!detail::applyFunction<const ComponentTypeTs...>(_f, entity, data))
480
529
{
481
530
break;
482
531
}
@@ -485,15 +534,15 @@ void EntityComponentManager::EachRemoved(typename identity<std::function<
485
534
486
535
//////////////////////////////////////////////////
487
536
template<typename ...ComponentTypeTs>
488
- detail::View<ComponentTypeTs...> *EntityComponentManager::FindView() const
537
+ detail::View *EntityComponentManager::FindView() const
489
538
{
490
539
auto viewKey = std::vector<ComponentTypeId>{ComponentTypeTs::typeId...};
491
540
492
541
auto baseViewMutexPair = this->FindView(viewKey);
493
542
auto baseViewPtr = baseViewMutexPair.first;
494
543
if (nullptr != baseViewPtr)
495
544
{
496
- auto view = static_cast<detail::View<ComponentTypeTs...> *>(baseViewPtr);
545
+ auto view = static_cast<detail::View*>(baseViewPtr);
497
546
498
547
std::unique_ptr<std::lock_guard<std::mutex>> viewLock;
499
548
if (this->LockAddingEntitiesToViews())
@@ -527,7 +576,7 @@ detail::View<ComponentTypeTs...> *EntityComponentManager::FindView() const
527
576
}
528
577
529
578
// create a new view if one wasn't found
530
- detail::View< ComponentTypeTs...> view ;
579
+ detail::View view(std::set<ComponentTypeId>{ ComponentTypeTs::typeId ...}) ;
531
580
532
581
for (const auto &vertex : this->Entities().Vertices())
533
582
{
@@ -547,8 +596,8 @@ detail::View<ComponentTypeTs...> *EntityComponentManager::FindView() const
547
596
}
548
597
549
598
baseViewPtr = this->AddView(viewKey,
550
- std::make_unique<detail::View<ComponentTypeTs...>>( view));
551
- return static_cast<detail::View<ComponentTypeTs...> *>(baseViewPtr);
599
+ std::make_unique<detail::View>(std::move( view) ));
600
+ return static_cast<detail::View *>(baseViewPtr);
552
601
}
553
602
554
603
//////////////////////////////////////////////////
0 commit comments