Program Listing for File swarm_iterator.hpp

Return to documentation for file (cosm/pal/argos/swarm_iterator.hpp)

#pragma once

/*******************************************************************************
 * Includes
 ******************************************************************************/
#include <string>

#include "cosm/pal/argos/swarm_manager_adaptor.hpp"
#include "cosm/pal/iteration_order.hpp"
#include "cosm/hal/robot.hpp"

/*******************************************************************************
 * Namespaces/Decls
 ******************************************************************************/
namespace cosm::pal::argos {

/*******************************************************************************
 * Struct Definitions
 ******************************************************************************/
struct swarm_iterator {
  template <typename TController,
            iteration_order order,
            typename TFunction,
            RCPPSW_SFINAE_DECLDEF(iteration_order::ekSTATIC == order)>
  static void controllers(const cpargos::swarm_manager_adaptor* const sm,
                          const TFunction& cb,
                          const std::string& robot_type) {
    for (auto& [name, robotp] : sm->GetSpace().GetEntitiesByType(robot_type)) {
      auto* robot = ::argos::any_cast<chal::robot*>(robotp);
      auto* controller = static_cast<TController*>(
          &robot->GetControllableEntity().GetController());
      cb(controller);
    } /* for(...) */
  }

  template <typename TController,
            iteration_order order,
            typename TFunction,
            RCPPSW_SFINAE_DECLDEF(iteration_order::ekDYNAMIC == order)>
  static void controllers(const cpargos::swarm_manager_adaptor* const sm,
                          const TFunction& cb) {
    auto wrapper = [&](auto* robot) {
      cb(static_cast<TController*>(&robot->GetController()));
    };
    sm->IterateOverControllableEntities(wrapper);
  }

  template <iteration_order order,
            typename TFunction,
            RCPPSW_SFINAE_DECLDEF(iteration_order::ekSTATIC == order)>
  static void robots(const cpargos::swarm_manager_adaptor* const sm,
                     const TFunction& cb,
                     const std::string& robot_type) {
    for (auto& [name, robotp] : sm->GetSpace().GetEntitiesByType(robot_type)) {
      auto* robot = ::argos::any_cast<chal::robot*>(robotp);
      cb(robot);
    } /* for(...) */
  }

  template <iteration_order order,
            typename TFunction,
            RCPPSW_SFINAE_DECLDEF(iteration_order::ekDYNAMIC == order)>
  static void robots(const cpargos::swarm_manager_adaptor* const sm,
                     const TFunction& cb) {
    sm->IterateOverControllableEntities(cb);
  }
};

} /* namespace cosm::pal::argos */