Line data Source code
1 : /****************************************************************************/ 2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo 3 : // Copyright (C) 2001-2024 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 StaticCommand.h 15 : /// @author Daniel Krajzewicz 16 : /// @author Michael Behrisch 17 : /// @date Wed, 13.02.2008 18 : /// 19 : // A wrapper for a Command function 20 : /****************************************************************************/ 21 : #pragma once 22 : #include <config.h> 23 : #include "Command.h" 24 : 25 : 26 : // =========================================================================== 27 : // class definition 28 : // =========================================================================== 29 : /** 30 : * @class StaticCommand 31 : * @brief A wrapper for a Command function 32 : * 33 : * @see Design Patterns, Gamma et al. 34 : * @see Command 35 : * @see MSEventControl 36 : */ 37 : template< class T > 38 : class StaticCommand : public Command { 39 : public: 40 : /// @brief Type of the function to execute. 41 : typedef SUMOTime(*Operation)(SUMOTime); 42 : 43 : 44 : public: 45 : /** 46 : * @brief Constructor. 47 : * 48 : * @param[in] receiver Pointer to object of type T that will receive a call to one of its methods. 49 : * @param[in] operation The objects' method that will be called on execute() 50 : */ 51 7941 : StaticCommand(Operation operation) 52 7941 : : myOperation(operation), myAmDescheduledByParent(false) {} 53 : 54 : 55 : /// @brief Destructor 56 7923 : ~StaticCommand() {} 57 : 58 : 59 : /** @brief Marks this Command as being descheduled 60 : * 61 : * A simple boolean marker ("myAmDescheduledByParent") is set which 62 : * prevents this command from being executed. 63 : */ 64 : void deschedule() { 65 : myAmDescheduledByParent = true; 66 : } 67 : 68 : 69 : 70 : /// @name Derived from Command 71 : /// @{ 72 : 73 : /** @brief Executes the command. 74 : * 75 : * If the command is not descheduled, the stored method of the stored instance 76 : * is called. 77 : * 78 : * @param[in] currentTime The current simulation time 79 : * @return The time after which the command shall be executed again, 0 if this command shall be descheduled. 80 : * @exception ProcessError Derived actions may throw this exception 81 : */ 82 7878123 : SUMOTime execute(SUMOTime currentTime) { 83 : // do not execute if the command was descheduled 84 7878123 : if (myAmDescheduledByParent) { 85 : return 0; 86 : } 87 : // execute if stil valid 88 7878123 : return (*myOperation)(currentTime); 89 : } 90 : /// @} 91 : 92 : 93 : private: 94 : /// @brief The object's operation to perform. 95 : Operation myOperation; 96 : 97 : /// @brief Whether this command was descheduled (is invalid) and shall not be executed 98 : bool myAmDescheduledByParent; 99 : 100 : 101 : };