SUMO - Simulation of Urban MObility
MsgHandler.h
Go to the documentation of this file.
00001 /****************************************************************************/
00008 // Retrieves messages about the process and gives them further to output
00009 /****************************************************************************/
00010 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00011 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
00012 /****************************************************************************/
00013 //
00014 //   This file is part of SUMO.
00015 //   SUMO is free software: you can redistribute it and/or modify
00016 //   it under the terms of the GNU General Public License as published by
00017 //   the Free Software Foundation, either version 3 of the License, or
00018 //   (at your option) any later version.
00019 //
00020 /****************************************************************************/
00021 #ifndef MsgHandler_h
00022 #define MsgHandler_h
00023 
00024 
00025 // ===========================================================================
00026 // included modules
00027 // ===========================================================================
00028 #ifdef _MSC_VER
00029 #include <windows_config.h>
00030 #else
00031 #include <config.h>
00032 #endif
00033 
00034 #include <string>
00035 #include <vector>
00036 #include <iostream>
00037 
00038 
00039 // ===========================================================================
00040 // class declarations
00041 // ===========================================================================
00042 class AbstractMutex;
00043 class OutputDevice;
00044 
00045 
00046 // ===========================================================================
00047 // class definitions
00048 // ===========================================================================
00052 class MsgHandler {
00053 public:
00059     enum MsgType {
00061         MT_MESSAGE,
00063         MT_WARNING,
00065         MT_ERROR
00066     };
00067 
00069     static MsgHandler* getMessageInstance();
00070 
00072     static MsgHandler* getWarningInstance();
00073 
00075     static MsgHandler* getErrorInstance();
00076 
00077     static void initOutputOptions();
00078 
00080     static void cleanupOnEnd();
00081 
00083     void inform(std::string msg, bool addType = true);
00084 
00092     void beginProcessMsg(std::string msg, bool addType = true);
00093 
00095     void endProcessMsg(std::string msg);
00096 
00098     void clear();
00099 
00101     void addRetriever(OutputDevice* retriever);
00102 
00104     void removeRetriever(OutputDevice* retriever);
00105 
00107     bool wasInformed() const;
00108 
00111     static void assignLock(AbstractMutex* lock);
00112 
00116     template <class T>
00117     MsgHandler& operator<<(const T& t) {
00118         // inform all other receivers
00119         for (RetrieverVector::iterator i = myRetrievers.begin(); i != myRetrievers.end(); i++) {
00120             (*(*i)) << t;
00121         }
00122         return *this;
00123     }
00124 
00125 protected:
00127     inline std::string build(const std::string& msg, bool addType) {
00128         if (addType) {
00129             switch (myType) {
00130                 case MT_MESSAGE:
00131                     break;
00132                 case MT_WARNING:
00133                     return "Warning: " + msg;
00134                     break;
00135                 case MT_ERROR:
00136                     return "Error: " + msg;
00137                     break;
00138                 default:
00139                     break;
00140             }
00141         }
00142         return msg;
00143     }
00144 
00145 
00146 private:
00148     MsgHandler(MsgType type);
00149 
00151     ~MsgHandler();
00152 
00153 private:
00155     static MsgHandler* myErrorInstance;
00156 
00158     static MsgHandler* myWarningInstance;
00159 
00161     static MsgHandler* myMessageInstance;
00162 
00164     static bool myAmProcessingProcess;
00165 
00168     static AbstractMutex* myLock;
00169 
00170 private:
00172     MsgType myType;
00173 
00175     bool myWasInformed;
00176 
00178     typedef std::vector<OutputDevice*> RetrieverVector;
00179 
00181     RetrieverVector myRetrievers;
00182 
00183 private:
00185     MsgHandler(const MsgHandler& s);
00186 
00188     MsgHandler& operator=(const MsgHandler& s);
00189 
00190 };
00191 
00192 
00193 // ===========================================================================
00194 // global definitions
00195 // ===========================================================================
00196 #define WRITE_WARNING(msg) MsgHandler::getWarningInstance()->inform(msg);
00197 #define WRITE_MESSAGE(msg) MsgHandler::getMessageInstance()->inform(msg);
00198 #define PROGRESS_BEGIN_MESSAGE(msg) MsgHandler::getMessageInstance()->beginProcessMsg((msg) + std::string("..."));
00199 #define PROGRESS_DONE_MESSAGE() MsgHandler::getMessageInstance()->endProcessMsg("done.");
00200 #define PROGRESS_FAILED_MESSAGE() MsgHandler::getMessageInstance()->endProcessMsg("failed.");
00201 #define WRITE_ERROR(msg)   MsgHandler::getErrorInstance()->inform(msg);
00202 
00203 #endif
00204 
00205 /****************************************************************************/
00206 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines