LCOV - code coverage report
Current view: top level - src/microsim - MSEventControl.cpp (source / functions) Coverage Total Hit
Test: lcov.info Lines: 91.9 % 37 34
Test Date: 2026-08-23 15:49:09 Functions: 87.5 % 8 7

            Line data    Source code
       1              : /****************************************************************************/
       2              : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
       3              : // Copyright (C) 2001-2026 German Aerospace Center (DLR) and others.
       4              : // This program and the accompanying materials are made available under the
       5              : // terms of the Eclipse Public License 2.0 which is available at
       6              : // https://www.eclipse.org/legal/epl-2.0/
       7              : // This Source Code may also be made available under the following Secondary
       8              : // Licenses when the conditions for such availability set forth in the Eclipse
       9              : // Public License 2.0 are satisfied: GNU General Public License, version 2
      10              : // or later which is available at
      11              : // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
      12              : // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
      13              : /****************************************************************************/
      14              : /// @file    MSEventControl.cpp
      15              : /// @author  Christian Roessel
      16              : /// @author  Daniel Krajzewicz
      17              : /// @author  Jakob Erdmann
      18              : /// @author  Michael Behrisch
      19              : /// @author  Matthias Heppner
      20              : /// @date    Mon, 12 Mar 2001
      21              : ///
      22              : // Stores time-dependant events and executes them at the proper time
      23              : /****************************************************************************/
      24              : #include <config.h>
      25              : 
      26              : #include <cassert>
      27              : #include "MSEventControl.h"
      28              : #include <utils/common/MsgHandler.h>
      29              : #include <utils/common/Command.h>
      30              : #include "MSNet.h"
      31              : 
      32              : 
      33              : // ===========================================================================
      34              : // member definitions
      35              : // ===========================================================================
      36       128614 : MSEventControl::MSEventControl() :
      37       128614 :     myEvents() {}
      38              : 
      39              : 
      40       226771 : MSEventControl::~MSEventControl() {
      41              :     // delete the events
      42       337341 :     for (const Event& e : myEvents) {
      43       210587 :         delete e.first;
      44              :     }
      45       226771 : }
      46              : 
      47              : 
      48              : void
      49    102655282 : MSEventControl::addEvent(Command* operation, SUMOTime execTimeStep) {
      50    102655282 :     myEvents.emplace_back(Event(operation, execTimeStep));
      51    102655282 :     std::push_heap(myEvents.begin(), myEvents.end(), MSEventControl::eventCompare);
      52    102655282 : }
      53              : 
      54              : 
      55              : void
      56    195491142 : MSEventControl::execute(SUMOTime execTime) {
      57              :     // Execute all events that are scheduled for execTime.
      58    297934840 :     while (!myEvents.empty()) {
      59    167654741 :         Event currEvent = myEvents.front();
      60    167654741 :         if (currEvent.second < 0) {
      61              :             currEvent.second = execTime;
      62              :         }
      63    167654741 :         if (currEvent.second < execTime + DELTA_T) {
      64              :             Command* command = currEvent.first;
      65              :             std::pop_heap(myEvents.begin(), myEvents.end(), eventCompare);
      66              :             myEvents.pop_back();
      67              :             SUMOTime time = 0;
      68              :             try {
      69    102443810 :                 time = command->execute(execTime);
      70          112 :             } catch (...) {
      71          112 :                 delete command;
      72          112 :                 throw;
      73          112 :             }
      74              : 
      75              :             // Delete nonrecurring events, reinsert recurring ones
      76              :             // with new execution time = execTime + returned offset.
      77    102443698 :             if (time <= 0) {
      78      1577286 :                 if (time < 0) {
      79            0 :                     WRITE_WARNING("Command returned negative repeat number; will be deleted.");
      80              :                 }
      81      1577286 :                 delete currEvent.first;
      82              :             } else {
      83    100866412 :                 addEvent(currEvent.first, currEvent.second + time);
      84              :             }
      85              :         } else {
      86              :             break;
      87              :         }
      88              :     }
      89    195491030 : }
      90              : 
      91              : 
      92              : bool
      93            0 : MSEventControl::isEmpty() {
      94            0 :     return myEvents.empty();
      95              : }
      96              : 
      97              : bool
      98    684420956 : MSEventControl::eventCompare(const Event& e1, const Event& e2) {
      99    684420956 :     return e1.second == e2.second ? e1.first->priority < e2.first->priority : e1.second > e2.second;
     100              : }
     101              : 
     102              : void
     103          492 : MSEventControl::clearState(SUMOTime currentTime, SUMOTime newTime) {
     104         1589 :     for (auto eventIt = myEvents.begin(); eventIt != myEvents.end();) {
     105         1097 :         eventIt->second = eventIt->first->shiftTime(currentTime, eventIt->second, newTime);
     106         1097 :         if (eventIt->second >= 0) {
     107              :             ++eventIt;
     108              :         } else {
     109          103 :             delete eventIt->first;
     110              :             eventIt = myEvents.erase(eventIt);
     111              :         }
     112              :     }
     113          492 :     std::make_heap(myEvents.begin(), myEvents.end(), eventCompare);
     114          492 : }
     115              : 
     116              : 
     117              : /****************************************************************************/
        

Generated by: LCOV version 2.0-1