SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MsgHandler.h
Go to the documentation of this file.
1 /****************************************************************************/
9 // Retrieves messages about the process and gives them further to output
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
12 // Copyright (C) 2003-2014 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 #ifndef MsgHandler_h
23 #define MsgHandler_h
24 
25 
26 // ===========================================================================
27 // included modules
28 // ===========================================================================
29 #ifdef _MSC_VER
30 #include <windows_config.h>
31 #else
32 #include <config.h>
33 #endif
34 
35 #include <string>
36 #include <vector>
37 #include <iostream>
38 
39 
40 // ===========================================================================
41 // class declarations
42 // ===========================================================================
43 class AbstractMutex;
44 class OutputDevice;
45 
46 
47 // ===========================================================================
48 // class definitions
49 // ===========================================================================
53 class MsgHandler {
54 public:
60  enum MsgType {
67  };
68 
71 
74 
76  static MsgHandler* getErrorInstance();
77 
78  static void initOutputOptions();
79 
81  static void cleanupOnEnd();
82 
84  void inform(std::string msg, bool addType = true);
85 
93  void beginProcessMsg(std::string msg, bool addType = true);
94 
96  void endProcessMsg(std::string msg);
97 
99  void clear();
100 
102  void addRetriever(OutputDevice* retriever);
103 
105  void removeRetriever(OutputDevice* retriever);
106 
108  bool isRetriever(OutputDevice* retriever) const;
109 
111  bool wasInformed() const;
112 
115  static void assignLock(AbstractMutex* lock);
116 
120  template <class T>
121  MsgHandler& operator<<(const T& t) {
122  // inform all other receivers
123  for (RetrieverVector::iterator i = myRetrievers.begin(); i != myRetrievers.end(); i++) {
124  (*(*i)) << t;
125  }
126  return *this;
127  }
128 
129 protected:
131  inline std::string build(const std::string& msg, bool addType) {
132  if (addType) {
133  switch (myType) {
134  case MT_MESSAGE:
135  break;
136  case MT_WARNING:
137  return "Warning: " + msg;
138  break;
139  case MT_ERROR:
140  return "Error: " + msg;
141  break;
142  default:
143  break;
144  }
145  }
146  return msg;
147  }
148 
149 
150 private:
152  MsgHandler(MsgType type);
153 
155  ~MsgHandler();
156 
157 private:
160 
163 
166 
169 
173 
174 private:
177 
180 
182  typedef std::vector<OutputDevice*> RetrieverVector;
183 
186 
187 private:
189  MsgHandler(const MsgHandler& s);
190 
192  MsgHandler& operator=(const MsgHandler& s);
193 
194 };
195 
196 
197 // ===========================================================================
198 // global definitions
199 // ===========================================================================
200 #define WRITE_WARNING(msg) MsgHandler::getWarningInstance()->inform(msg);
201 #define WRITE_MESSAGE(msg) MsgHandler::getMessageInstance()->inform(msg);
202 #define PROGRESS_BEGIN_MESSAGE(msg) MsgHandler::getMessageInstance()->beginProcessMsg((msg) + std::string("..."));
203 #define PROGRESS_DONE_MESSAGE() MsgHandler::getMessageInstance()->endProcessMsg("done.");
204 #define PROGRESS_FAILED_MESSAGE() MsgHandler::getMessageInstance()->endProcessMsg("failed.");
205 #define WRITE_ERROR(msg) MsgHandler::getErrorInstance()->inform(msg);
206 
207 #endif
208 
209 /****************************************************************************/
210 
bool isRetriever(OutputDevice *retriever) const
Returns whether the given output device retrieves messages from the handler.
Definition: MsgHandler.cpp:191
MsgHandler & operator<<(const T &t)
Generic output operator.
Definition: MsgHandler.h:121
static MsgHandler * getWarningInstance()
Returns the instance to add warnings to.
Definition: MsgHandler.cpp:71
std::vector< OutputDevice * > RetrieverVector
Definition of the list of retrievers to inform.
Definition: MsgHandler.h:182
The message is only something to show.
Definition: MsgHandler.h:62
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:80
MsgType myType
The type of the instance.
Definition: MsgHandler.h:176
MsgHandler(MsgType type)
standard constructor
Definition: MsgHandler.cpp:258
MsgHandler & operator=(const MsgHandler &s)
static MsgHandler * myWarningInstance
The instance to handle warnings.
Definition: MsgHandler.h:162
An abstract class for encapsulating mutex implementations.
Definition: AbstractMutex.h:49
void addRetriever(OutputDevice *retriever)
Adds a further retriever to the instance responsible for a certain msg type.
Definition: MsgHandler.cpp:161
static void assignLock(AbstractMutex *lock)
Sets the lock to use The lock will not be deleted.
Definition: MsgHandler.cpp:279
RetrieverVector myRetrievers
The list of retrievers that shall be informed about new messages or errors.
Definition: MsgHandler.h:185
static void cleanupOnEnd()
Removes pending handler.
Definition: MsgHandler.cpp:242
~MsgHandler()
destructor
Definition: MsgHandler.cpp:268
static AbstractMutex * myLock
The lock if any has to be used The lock will not be deleted.
Definition: MsgHandler.h:172
void removeRetriever(OutputDevice *retriever)
Removes the retriever from the handler.
Definition: MsgHandler.cpp:175
static MsgHandler * getMessageInstance()
Returns the instance to add normal messages to.
Definition: MsgHandler.cpp:62
bool wasInformed() const
Returns the information whether any messages were added.
Definition: MsgHandler.cpp:273
void beginProcessMsg(std::string msg, bool addType=true)
Begins a process information.
Definition: MsgHandler.cpp:112
The message is a warning.
Definition: MsgHandler.h:64
static MsgHandler * myMessageInstance
The instance to handle normal messages.
Definition: MsgHandler.h:165
static bool myAmProcessingProcess
Information whether a process information is printed to cout.
Definition: MsgHandler.h:168
void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:89
std::string build(const std::string &msg, bool addType)
Builds the string which includes the mml-message type.
Definition: MsgHandler.h:131
bool myWasInformed
information wehther an error occured at all
Definition: MsgHandler.h:179
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
static MsgHandler * myErrorInstance
The instance to handle errors.
Definition: MsgHandler.h:159
void clear()
Clears information whether an error occured previously.
Definition: MsgHandler.cpp:149
static void initOutputOptions()
Definition: MsgHandler.cpp:197
The message is an error.
Definition: MsgHandler.h:66
void endProcessMsg(std::string msg)
Ends a process information.
Definition: MsgHandler.cpp:131