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-2013 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