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 /****************************************************************************/
8 // Retrieves messages about the process and gives them further to output
9 /****************************************************************************/
10 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
11 // Copyright (C) 2001-2014 DLR (http://www.dlr.de/) and contributors
12 /****************************************************************************/
13 //
14 // This file is part of SUMO.
15 // SUMO is free software: you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation, either version 3 of the License, or
18 // (at your option) any later version.
19 //
20 /****************************************************************************/
21 #ifndef MsgHandler_h
22 #define MsgHandler_h
23 
24 
25 // ===========================================================================
26 // included modules
27 // ===========================================================================
28 #ifdef _MSC_VER
29 #include <windows_config.h>
30 #else
31 #include <config.h>
32 #endif
33 
34 #include <string>
35 #include <vector>
36 #include <iostream>
37 
38 
39 // ===========================================================================
40 // class declarations
41 // ===========================================================================
42 class AbstractMutex;
43 class OutputDevice;
44 
45 
46 // ===========================================================================
47 // class definitions
48 // ===========================================================================
52 class MsgHandler {
53 public:
59  enum MsgType {
66  };
67 
70 
73 
75  static MsgHandler* getErrorInstance();
76 
77  static void initOutputOptions();
78 
80  static void cleanupOnEnd();
81 
83  void inform(std::string msg, bool addType = true);
84 
92  void beginProcessMsg(std::string msg, bool addType = true);
93 
95  void endProcessMsg(std::string msg);
96 
98  void clear();
99 
101  void addRetriever(OutputDevice* retriever);
102 
104  void removeRetriever(OutputDevice* retriever);
105 
107  bool wasInformed() const;
108 
111  static void assignLock(AbstractMutex* lock);
112 
116  template <class T>
117  MsgHandler& operator<<(const T& t) {
118  // inform all other receivers
119  for (RetrieverVector::iterator i = myRetrievers.begin(); i != myRetrievers.end(); i++) {
120  (*(*i)) << t;
121  }
122  return *this;
123  }
124 
125 protected:
127  inline std::string build(const std::string& msg, bool addType) {
128  if (addType) {
129  switch (myType) {
130  case MT_MESSAGE:
131  break;
132  case MT_WARNING:
133  return "Warning: " + msg;
134  break;
135  case MT_ERROR:
136  return "Error: " + msg;
137  break;
138  default:
139  break;
140  }
141  }
142  return msg;
143  }
144 
145 
146 private:
148  MsgHandler(MsgType type);
149 
151  ~MsgHandler();
152 
153 private:
156 
159 
162 
165 
169 
170 private:
173 
176 
178  typedef std::vector<OutputDevice*> RetrieverVector;
179 
182 
183 private:
185  MsgHandler(const MsgHandler& s);
186 
188  MsgHandler& operator=(const MsgHandler& s);
189 
190 };
191 
192 
193 // ===========================================================================
194 // global definitions
195 // ===========================================================================
196 #define WRITE_WARNING(msg) MsgHandler::getWarningInstance()->inform(msg);
197 #define WRITE_MESSAGE(msg) MsgHandler::getMessageInstance()->inform(msg);
198 #define PROGRESS_BEGIN_MESSAGE(msg) MsgHandler::getMessageInstance()->beginProcessMsg((msg) + std::string("..."));
199 #define PROGRESS_DONE_MESSAGE() MsgHandler::getMessageInstance()->endProcessMsg("done.");
200 #define PROGRESS_FAILED_MESSAGE() MsgHandler::getMessageInstance()->endProcessMsg("failed.");
201 #define WRITE_ERROR(msg) MsgHandler::getErrorInstance()->inform(msg);
202 
203 #endif
204 
205 /****************************************************************************/
206 
MsgHandler & operator<<(const T &t)
Generic output operator.
Definition: MsgHandler.h:117
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:178
The message is only something to show.
Definition: MsgHandler.h:61
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:80
MsgType myType
The type of the instance.
Definition: MsgHandler.h:172
MsgHandler(MsgType type)
standard constructor
Definition: MsgHandler.cpp:254
MsgHandler & operator=(const MsgHandler &s)
static MsgHandler * myWarningInstance
The instance to handle warnings.
Definition: MsgHandler.h:158
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:275
RetrieverVector myRetrievers
The list of retrievers that shall be informed about new messages or errors.
Definition: MsgHandler.h:181
static void cleanupOnEnd()
Removes pending handler.
Definition: MsgHandler.cpp:238
~MsgHandler()
destructor
Definition: MsgHandler.cpp:264
static AbstractMutex * myLock
The lock if any has to be used The lock will not be deleted.
Definition: MsgHandler.h:168
void removeRetriever(OutputDevice *retriever)
Removes the retriever from the.
Definition: MsgHandler.cpp:177
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:269
void beginProcessMsg(std::string msg, bool addType=true)
Begins a process information.
Definition: MsgHandler.cpp:112
The message is a warning.
Definition: MsgHandler.h:63
static MsgHandler * myMessageInstance
The instance to handle normal messages.
Definition: MsgHandler.h:161
static bool myAmProcessingProcess
Information whether a process information is printed to cout.
Definition: MsgHandler.h:164
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:127
bool myWasInformed
information wehther an error occured at all
Definition: MsgHandler.h:175
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:70
static MsgHandler * myErrorInstance
The instance to handle errors.
Definition: MsgHandler.h:155
void clear()
Clears information whether an error occured previously.
Definition: MsgHandler.cpp:149
static void initOutputOptions()
Definition: MsgHandler.cpp:193
The message is an error.
Definition: MsgHandler.h:65
void endProcessMsg(std::string msg)
Ends a process information.
Definition: MsgHandler.cpp:131