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 MsgRetrievingFunction.h
15 : /// @author Daniel Krajzewicz
16 : /// @author Michael Behrisch
17 : /// @date Mon, 24 Oct 2003
18 : ///
19 : // Encapsulates an object's method for using it as a message retriever
20 : /****************************************************************************/
21 : #pragma once
22 : #include <config.h>
23 : #include <string>
24 : #include <sstream>
25 : #include <utils/iodevices/OutputDevice.h>
26 : #include "MsgHandler.h"
27 :
28 :
29 : // ===========================================================================
30 : // class definitions
31 : // ===========================================================================
32 : /**
33 : * @class MsgRetrievingFunction
34 : * @brief Encapsulates an object's method for using it as a message retriever
35 : *
36 : * You may find an example for this class' usage in GUIRunThread.
37 : */
38 : template< class T >
39 : class MsgRetrievingFunction : public OutputDevice {
40 : public:
41 : /// @brief Type of the function to execute.
42 : typedef void(T::* Operation)(const MsgHandler::MsgType, const std::string&);
43 :
44 :
45 : /** @brief Constructor
46 : * @param[in] object The object to call the method of
47 : * @param[in] operation The method to call
48 : * @param[in] type The type of the message
49 : */
50 45306 : MsgRetrievingFunction(T* object, Operation operation, MsgHandler::MsgType type) :
51 45306 : myObject(object),
52 45306 : myOperation(operation),
53 90612 : myMsgType(type) {}
54 :
55 :
56 : /// @brief Destructor
57 45228 : ~MsgRetrievingFunction() {}
58 :
59 :
60 : protected:
61 : /// @name Methods that override/implement OutputDevice-methods
62 : /// @{
63 :
64 : /** @brief Returns the associated ostream
65 : *
66 : * The stream is an ostringstream, actually, into which the message
67 : * is written. It is sent when postWriteHook is called.
68 : *
69 : * @return The used stream
70 : * @see postWriteHook
71 : */
72 61896 : std::ostream& getOStream() {
73 61896 : return myMessage;
74 : }
75 :
76 :
77 : /** @brief Sends the data which was written to the string stream via the retrieving function.
78 : */
79 61896 : virtual void postWriteHook() {
80 123792 : (myObject->*myOperation)(myMsgType, myMessage.str());
81 61896 : myMessage.str("");
82 61896 : }
83 : /// @}
84 :
85 :
86 : private:
87 : /// @brief The object the action is directed to.
88 : T* myObject;
89 :
90 : /// @brief The object's operation to perform.
91 : Operation myOperation;
92 :
93 : /// @brief The type of message to retrieve.
94 : MsgHandler::MsgType myMsgType;
95 :
96 : /// @brief message buffer
97 : std::ostringstream myMessage;
98 :
99 : };
|