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 ParametrisedWrappingCommand.h 15 : /// @author Leonhard Luecken 16 : /// @date Apr 2019 17 : /// 18 : // A wrapper for a Command function with parameter 19 : /****************************************************************************/ 20 : 21 : #pragma once 22 : #include <config.h> 23 : 24 : #include "Command.h" 25 : 26 : 27 : // =========================================================================== 28 : // class definition 29 : // =========================================================================== 30 : /** 31 : * @class ParametrisedWrappingCommand 32 : * @brief A wrapper for a Command function with parameter 33 : * @see WrappingCommand.h 34 : */ 35 : template< class T, class S > 36 : class ParametrisedWrappingCommand : public Command { 37 : public: 38 : 39 : /// @brief Type of the function to execute. (with parameter) 40 : typedef SUMOTime(T::* Operation)(SUMOTime, S); 41 : 42 : public: 43 : 44 : /** 45 : * @brief Constructor. 46 : * 47 : * @param[in] receiver Pointer to object of type T that will receive a call to one of its methods. 48 : * @param[in] parameter The methods parameter (must be copy-constructable) 49 : * @param[in] operation The objects' method that will be called on execute() 50 : */ 51 196 : ParametrisedWrappingCommand(T* receiver, const S& param, Operation operation) 52 196 : : myReceiver(receiver), myParameter(param), myOperation(operation), 53 : myAmDescheduledByParent(false) {} 54 : 55 : 56 : /// @brief Destructor 57 196 : ~ParametrisedWrappingCommand() {} 58 : 59 : 60 : /** @brief Marks this Command as being descheduled 61 : * 62 : * A simple boolean marker ("myAmDescheduledByParent") is set which 63 : * prevents this command from being executed. 64 : */ 65 : void deschedule() { 66 302 : myAmDescheduledByParent = true; 67 : } 68 : 69 : /// @brief whether this command has been descheduled 70 : bool isDescheduled() { 71 : return myAmDescheduledByParent; 72 : } 73 : 74 : 75 : /// @name Derived from Command 76 : /// @{ 77 : 78 : /** @brief Executes the command. 79 : * 80 : * If the command is not descheduled, the stored method of the stored instance 81 : * is called. 82 : * 83 : * @param[in] currentTime The current simulation time 84 : * @return The time after which the command shall be executed again, 0 if this command shall be descheduled. 85 : * @exception ProcessError Derived actions may throw this exception 86 : */ 87 4470 : SUMOTime execute(SUMOTime currentTime) { 88 : // do not execute if the command was descheduled 89 4470 : if (myAmDescheduledByParent) { 90 : return 0; 91 : } 92 : // execute if stil valid 93 4386 : return (myReceiver->*myOperation)(currentTime, myParameter); 94 : } 95 : /// @} 96 : 97 : 98 : private: 99 : /// @brief The object the action is directed to. 100 : T* myReceiver; 101 : 102 : /// @brief The parameter 103 : S myParameter; 104 : 105 : /// @brief The object's operation to perform. 106 : Operation myOperation; 107 : 108 : /// @brief Whether this command was descheduled (is invalid) and shall not be executed 109 : bool myAmDescheduledByParent; 110 : 111 : };