1 /****************************************************************************/ 2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo 3 // Copyright (C) 2001-2019 German Aerospace Center (DLR) and others. 4 // This program and the accompanying materials 5 // are made available under the terms of the Eclipse Public License v2.0 6 // which accompanies this distribution, and is available at 7 // http://www.eclipse.org/legal/epl-v20.html 8 // SPDX-License-Identifier: EPL-2.0 9 /****************************************************************************/ 10 /// @file MsgRetrievingFunction.h 11 /// @author Daniel Krajzewicz 12 /// @author Michael Behrisch 13 /// @date Mon, 24 Oct 2003 14 /// @version $Id$ 15 /// 16 // Encapsulates an object's method for using it as a message retriever 17 /****************************************************************************/ 18 #ifndef MsgRetrievingFunction_h 19 #define MsgRetrievingFunction_h 20 21 22 // =========================================================================== 23 // included modules 24 // =========================================================================== 25 26 #include <string> 27 #include <sstream> 28 #include <utils/iodevices/OutputDevice.h> 29 #include "MsgHandler.h" 30 31 32 // =========================================================================== 33 // class definitions 34 // =========================================================================== 35 /** 36 * @class MsgRetrievingFunction 37 * @brief Encapsulates an object's method for using it as a message retriever 38 * 39 * You may find an example for this class' usage in GUIRunThread. 40 */ 41 template< class T > 42 class MsgRetrievingFunction : public OutputDevice { 43 public: 44 /// @brief Type of the function to execute. 45 typedef void(T::* Operation)(const MsgHandler::MsgType, const std::string&); 46 47 48 /** @brief Constructor 49 * @param[in] object The object to call the method of 50 * @param[in] operation The method to call 51 * @param[in] type The type of the message 52 */ MsgRetrievingFunction(T * object,Operation operation,MsgHandler::MsgType type)53 MsgRetrievingFunction(T* object, Operation operation, MsgHandler::MsgType type) : 54 myObject(object), 55 myOperation(operation), 56 myMsgType(type) {} 57 58 59 /// @brief Destructor ~MsgRetrievingFunction()60 ~MsgRetrievingFunction() {} 61 62 63 protected: 64 /// @name Methods that override/implement OutputDevice-methods 65 /// @{ 66 67 /** @brief Returns the associated ostream 68 * 69 * The stream is an ostringstream, actually, into which the message 70 * is written. It is sent when postWriteHook is called. 71 * 72 * @return The used stream 73 * @see postWriteHook 74 */ getOStream()75 std::ostream& getOStream() { 76 return myMessage; 77 } 78 79 80 /** @brief Sends the data which was written to the string stream via the retrieving function. 81 */ postWriteHook()82 virtual void postWriteHook() { 83 (myObject->*myOperation)(myMsgType, myMessage.str()); 84 myMessage.str(""); 85 } 86 /// @} 87 88 89 private: 90 /// @brief The object the action is directed to. 91 T* myObject; 92 93 /// @brief The object's operation to perform. 94 Operation myOperation; 95 96 /// @brief The type of message to retrieve. 97 MsgHandler::MsgType myMsgType; 98 99 /// @brief message buffer 100 std::ostringstream myMessage; 101 102 }; 103 104 105 #endif 106 107 /****************************************************************************/ 108 109