Path: blob/main/src/utils/common/ParametrisedWrappingCommand.h
169678 views
/****************************************************************************/1// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo2// Copyright (C) 2001-2025 German Aerospace Center (DLR) and others.3// This program and the accompanying materials are made available under the4// terms of the Eclipse Public License 2.0 which is available at5// https://www.eclipse.org/legal/epl-2.0/6// This Source Code may also be made available under the following Secondary7// Licenses when the conditions for such availability set forth in the Eclipse8// Public License 2.0 are satisfied: GNU General Public License, version 29// or later which is available at10// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html11// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later12/****************************************************************************/13/// @file ParametrisedWrappingCommand.h14/// @author Leonhard Luecken15/// @date Apr 201916///17// A wrapper for a Command function with parameter18/****************************************************************************/1920#pragma once21#include <config.h>2223#include "Command.h"242526// ===========================================================================27// class definition28// ===========================================================================29/**30* @class ParametrisedWrappingCommand31* @brief A wrapper for a Command function with parameter32* @see WrappingCommand.h33*/34template< class T, class S >35class ParametrisedWrappingCommand : public Command {36public:3738/// @brief Type of the function to execute. (with parameter)39typedef SUMOTime(T::* Operation)(SUMOTime, S);4041public:4243/**44* @brief Constructor.45*46* @param[in] receiver Pointer to object of type T that will receive a call to one of its methods.47* @param[in] parameter The methods parameter (must be copy-constructable)48* @param[in] operation The objects' method that will be called on execute()49*/50ParametrisedWrappingCommand(T* receiver, const S& param, Operation operation)51: myReceiver(receiver), myParameter(param), myOperation(operation),52myAmDescheduledByParent(false) {}535455/// @brief Destructor56~ParametrisedWrappingCommand() {}575859/** @brief Marks this Command as being descheduled60*61* A simple boolean marker ("myAmDescheduledByParent") is set which62* prevents this command from being executed.63*/64void deschedule() {65myAmDescheduledByParent = true;66}6768/// @brief whether this command has been descheduled69bool isDescheduled() {70return myAmDescheduledByParent;71}727374/// @name Derived from Command75/// @{7677/** @brief Executes the command.78*79* If the command is not descheduled, the stored method of the stored instance80* is called.81*82* @param[in] currentTime The current simulation time83* @return The time after which the command shall be executed again, 0 if this command shall be descheduled.84* @exception ProcessError Derived actions may throw this exception85*/86SUMOTime execute(SUMOTime currentTime) {87// do not execute if the command was descheduled88if (myAmDescheduledByParent) {89return 0;90}91// execute if stil valid92return (myReceiver->*myOperation)(currentTime, myParameter);93}94/// @}959697private:98/// @brief The object the action is directed to.99T* myReceiver;100101/// @brief The parameter102S myParameter;103104/// @brief The object's operation to perform.105Operation myOperation;106107/// @brief Whether this command was descheduled (is invalid) and shall not be executed108bool myAmDescheduledByParent;109110};111112113