Path: blob/main/src/utils/common/MsgRetrievingFunction.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 MsgRetrievingFunction.h14/// @author Daniel Krajzewicz15/// @author Michael Behrisch16/// @date Mon, 24 Oct 200317///18// Encapsulates an object's method for using it as a message retriever19/****************************************************************************/20#pragma once21#include <config.h>22#include <string>23#include <sstream>24#include <utils/iodevices/OutputDevice.h>25#include "MsgHandler.h"262728// ===========================================================================29// class definitions30// ===========================================================================31/**32* @class MsgRetrievingFunction33* @brief Encapsulates an object's method for using it as a message retriever34*35* You may find an example for this class' usage in GUIRunThread.36*/37template< class T >38class MsgRetrievingFunction : public OutputDevice {39public:40/// @brief Type of the function to execute.41typedef void(T::* Operation)(const MsgHandler::MsgType, const std::string&);424344/** @brief Constructor45* @param[in] object The object to call the method of46* @param[in] operation The method to call47* @param[in] type The type of the message48*/49MsgRetrievingFunction(T* object, Operation operation, MsgHandler::MsgType type) :50myObject(object),51myOperation(operation),52myMsgType(type) {}535455/// @brief Destructor56~MsgRetrievingFunction() {}575859protected:60/// @name Methods that override/implement OutputDevice-methods61/// @{6263/** @brief Returns the associated ostream64*65* The stream is an ostringstream, actually, into which the message66* is written. It is sent when postWriteHook is called.67*68* @return The used stream69* @see postWriteHook70*/71std::ostream& getOStream() {72return myMessage;73}747576/** @brief Sends the data which was written to the string stream via the retrieving function.77*/78virtual void postWriteHook() {79(myObject->*myOperation)(myMsgType, myMessage.str());80myMessage.str("");81}82/// @}838485private:86/// @brief The object the action is directed to.87T* myObject;8889/// @brief The object's operation to perform.90Operation myOperation;9192/// @brief The type of message to retrieve.93MsgHandler::MsgType myMsgType;9495/// @brief message buffer96std::ostringstream myMessage;9798};99100101