@@ -200,12 +200,18 @@ namespace dsm {
200200 // / @brief Update the paths of the itineraries based on the actual travel times
201201 virtual void updatePaths ();
202202
203- // / @brief Set the dynamics destination nodes auto-generating itineraries
204- // / @param destinationNodes The destination nodes
203+ // / @brief Set the destination nodes
204+ // / @param destinationNodes The destination nodes (as an initializer list)
205205 // / @param updatePaths If true, the paths are updated
206- // / @throws std::invalid_argument Ifone or more destination nodes do not exist
207- void setDestinationNodes (const std::span<Id>& destinationNodes,
206+ void setDestinationNodes (std::initializer_list<Id> destinationNodes,
208207 bool updatePaths = true );
208+ // / @brief Set the destination nodes
209+ // / @param destinationNodes A container of destination nodes ids
210+ // / @param updatePaths If true, the paths are updated
211+ // / @details The container must have a value_type convertible to Id and begin() and end() methods
212+ template <typename TContainer>
213+ requires (std::is_convertible_v<typename TContainer::value_type, Id>)
214+ void setDestinationNodes (TContainer const & destinationNodes, bool updatePaths = true );
209215
210216 // / @brief Add an agent to the simulation
211217 // / @param agent std::unique_ptr to the agent
@@ -253,6 +259,7 @@ namespace dsm {
253259 // / @brief Add an itinerary
254260 // / @param itinerary std::unique_ptr to the itinerary
255261 // / @throws std::invalid_argument If the itinerary already exists
262+ // / @throws std::invalid_argument If the itinerary's destination is not a node of the graph
256263 void addItinerary (std::unique_ptr<Itinerary> itinerary);
257264 template <typename ... Tn>
258265 requires (is_itinerary_v<Tn> && ...)
@@ -373,36 +380,28 @@ namespace dsm {
373380 m_itineraries.cbegin (), m_itineraries.cend (), [this ](auto const & pair) -> void {
374381 this ->m_updatePath (pair.second );
375382 });
376- // std::vector<std::thread> threads;
377- // threads.reserve(m_itineraries.size());
378- // std::exception_ptr pThreadException;
379- // for (const auto& [itineraryId, itinerary] : m_itineraries) {
380- // threads.emplace_back(std::thread([this, &itinerary, &pThreadException] {
381- // try {
382- // this->m_updatePath(itinerary);
383- // } catch (...) {
384- // if (!pThreadException)
385- // pThreadException = std::current_exception();
386- // }
387- // }));
388- // }
389- // for (auto& thread : threads) {
390- // thread.join();
391- // }
392- // // Throw the exception launched first
393- // if (pThreadException)
394- // std::rethrow_exception(pThreadException);
395383 }
396384
397385 template <typename agent_t >
398- void Dynamics<agent_t >::setDestinationNodes(const std::span <Id>& destinationNodes,
386+ void Dynamics<agent_t >::setDestinationNodes(std::initializer_list <Id> destinationNodes,
399387 bool updatePaths) {
400- for (const auto & nodeId : destinationNodes) {
401- if (!m_graph.nodeSet ().contains (nodeId)) {
402- Logger::error (std::format (" Node with id {} not found" , nodeId));
403- }
404- this ->addItinerary (nodeId, nodeId);
388+ std::for_each (
389+ destinationNodes.begin (),
390+ destinationNodes.end (),
391+ [this ](auto const & nodeId) -> void { this ->addItinerary (nodeId, nodeId); });
392+ if (updatePaths) {
393+ this ->updatePaths ();
405394 }
395+ }
396+ template <typename agent_t >
397+ template <typename TContainer>
398+ requires (std::is_convertible_v<typename TContainer::value_type, Id>)
399+ void Dynamics<agent_t >::setDestinationNodes(TContainer const & destinationNodes,
400+ bool updatePaths) {
401+ std::for_each (
402+ destinationNodes.begin (),
403+ destinationNodes.end (),
404+ [this ](auto const & nodeId) -> void { this ->addItinerary (nodeId, nodeId); });
406405 if (updatePaths) {
407406 this ->updatePaths ();
408407 }
@@ -488,6 +487,10 @@ namespace dsm {
488487 throw std::invalid_argument (Logger::buildExceptionMessage (
489488 std::format (" Itinerary with id {} already exists." , itinerary->id ())));
490489 }
490+ if (!m_graph.nodeSet ().contains (itinerary->destination ())) {
491+ throw std::invalid_argument (Logger::buildExceptionMessage (std::format (
492+ " Destination node with id {} not found" , itinerary->destination ())));
493+ }
491494 m_itineraries.emplace (itinerary->id (), std::move (itinerary));
492495 }
493496
0 commit comments