00001 /******************************************************************************* 00002 00003 Copyright (C) 2007 by Brendon Costa 00004 00005 This library is free software; you can redistribute it and/or modify 00006 it under the terms of the "LGPL Like" License defined in the file COPYING 00007 that should have been distributed along with this source. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 00012 00013 You should have received a copy of the "LGPL Like" License 00014 along with this library; see the file COPYING. if not, it can be 00015 obtained from the EDoc++ website: 00016 http://edoc.sourceforge.net/license.html 00017 00018 *******************************************************************************/ 00019 #ifndef EDOC_NOTIFICATIONIFACE_H 00020 #define EDOC_NOTIFICATIONIFACE_H 00021 00022 #include "EDoc/stdint.h" 00023 #include "EDoc/exceptions.h" 00024 #include "EDoc/utils.h" 00025 00026 #include <string> 00027 #include <map> 00028 #include <list> 00029 00030 namespace EDoc 00031 { 00032 class Function; 00033 class File; 00034 class Type; 00035 class PropogatingException; 00036 class FunctionType; 00037 00038 //=========================================================================== 00039 /** \brief Enumerates the event types that exist. 00040 */ 00041 enum EventType 00042 { 00043 EVENT_ERROR, 00044 EVENT_WARNING 00045 }; 00046 00047 typedef uint32_t EventCode; 00048 00049 //=========================================================================== 00050 /** \brief Notifies user of errors/warnings. 00051 * 00052 * This provides functionality for notifying users of errors and warnings. It 00053 * also provides a facility to filter out some warnings/errors by registering 00054 * a callback function pointer that returns false if the message to be 00055 * suppressed. 00056 */ 00057 class NotificationIFace 00058 { 00059 public: 00060 00061 //------------------------------------------------------------------------ 00062 /** \brief Constructor. 00063 * 00064 * By default filter allows ALL events to be shown. 00065 */ 00066 NotificationIFace() 00067 { 00068 filter = &NotificationIFace::AllowEverythingFilter; 00069 filter_data = NULL; 00070 } 00071 00072 //------------------------------------------------------------------------ 00073 /** \brief Destructor. 00074 */ 00075 virtual ~NotificationIFace() {} 00076 00077 //------------------------------------------------------------------------ 00078 /** \brief Is called upon an event to be displayed to the user. 00079 * 00080 * NOTE Should use the macro EDOC_NOTIFICATION() NOT call this function 00081 * directly. 00082 * 00083 * The additional parameters are used in filtering. They are not used to 00084 * generate any message text. Any of these parameters can be NULL, but it 00085 * is preferrable that if data is associated with the event then it should 00086 * be passed along so that the filtering functions can better identify 00087 * individual events for filtering purposes. 00088 * 00089 * As an example while running EDoc it may produce anumber of WUNIMPL 00090 * warning events for a series of functions that are used but not 00091 * implemented. You may wish to suppress all these WUNIMPL messages for a 00092 * certain list of function names, in which case the filter function would 00093 * check the function parameter, or it could choose to filter out all 00094 * WUNIMPL messages where the function was declared in the <builtin> 00095 * file. This would allow those WUNIMPL's to be suppressed but all others 00096 * would still be emitted. 00097 */ 00098 virtual void Event(EventCode code, 00099 const char* source_file, 00100 unsigned int source_line, 00101 std::string message, 00102 const Function* function, 00103 const File* file, 00104 uint32_t line, 00105 const File* rfile, 00106 uint32_t rline, 00107 const Type* type, 00108 const PropogatingException* prop_exc, 00109 const FunctionType* function_type, 00110 const char* extra) = 0; 00111 00112 //------------------------------------------------------------------------ 00113 /** \brief Function pointer type for the filter callback function. 00114 * 00115 * \see RegisterFilter() 00116 */ 00117 typedef bool (*AllowEventFP)(void* filter_data, 00118 EventCode code, 00119 std::string ev_name, 00120 EventType ev_type, 00121 const Function* function, 00122 const File* file, 00123 uint32_t line, 00124 const File* rfile, 00125 uint32_t rline, 00126 const Type* type, 00127 const PropogatingException* prop_exc, 00128 const FunctionType* function_type, 00129 const char* extra); 00130 00131 //------------------------------------------------------------------------ 00132 /** \brief The default filter that filters out no events at all. 00133 * 00134 * \see RegisterFilter() 00135 */ 00136 static bool AllowEverythingFilter(void* EDOC_UNUSED(filter_data), 00137 EventCode EDOC_UNUSED(code), 00138 std::string EDOC_UNUSED(ev_name), 00139 EventType EDOC_UNUSED(ev_type), 00140 const Function* EDOC_UNUSED(function), 00141 const File* EDOC_UNUSED(file), 00142 uint32_t EDOC_UNUSED(line), 00143 const File* EDOC_UNUSED(rfile), 00144 uint32_t EDOC_UNUSED(rline), 00145 const Type* EDOC_UNUSED(type), 00146 const PropogatingException* EDOC_UNUSED(prop_exc), 00147 const FunctionType* EDOC_UNUSED(function_type), 00148 const char* EDOC_UNUSED(extra)) 00149 { 00150 return true; 00151 } 00152 00153 //------------------------------------------------------------------------ 00154 /** \brief Set the filter callback function. 00155 * 00156 * The callback function should return true if the event is to be 00157 * processed and false if the event is to be suppressed. 00158 * 00159 * \param fp The function pointer that is to be called whenever an event 00160 * is being generated by the Event() function being called. 00161 * 00162 * \param data This is some optional additional data that is passed to the 00163 * function pointer when it is called. This can store anything. It was 00164 * primarily added because additional data is required for the callback 00165 * that executes a python function in the suppressions file. 00166 */ 00167 virtual void RegisterFilter(AllowEventFP fp, void* data) 00168 { 00169 filter = fp; 00170 filter_data = data; 00171 } 00172 00173 //------------------------------------------------------------------------ 00174 00175 protected: 00176 00177 //------------------------------------------------------------------------ 00178 /** \brief The currently registered function pointer. 00179 */ 00180 AllowEventFP filter; 00181 00182 //------------------------------------------------------------------------ 00183 /** \brief Data associated function pointer. 00184 */ 00185 void* filter_data; 00186 00187 //------------------------------------------------------------------------ 00188 }; 00189 00190 //=========================================================================== 00191 /** \brief Returns a reference to the global notifier. 00192 */ 00193 NotificationIFace* GetGlobalNotifier(); 00194 00195 //=========================================================================== 00196 /** \brief Returns a reference to the global notifier or throws a 00197 * BugException if it is not set. 00198 */ 00199 NotificationIFace* GetGlobalNotifierAssert(); 00200 00201 //=========================================================================== 00202 /** \brief Sets the global notifier. 00203 * 00204 * Should be called once upon startup of the application. 00205 */ 00206 void SetGlobalNotifier(NotificationIFace* gn); 00207 00208 //=========================================================================== 00209 /** \brief Define the standard set of errors and warnings used by EDoc. 00210 */ 00211 enum Events 00212 { 00213 #define EDOC_EVENT_ITEM(Type, Name, Description) Name, 00214 #include "EDoc/events.inc" 00215 #undef EDOC_EVENT_ITEM 00216 EV_UNDEFINED 00217 }; 00218 00219 } 00220 00221 //============================================================================== 00222 /** \brief Macro used to generate notification events. 00223 * 00224 * This should be used instead of calling NotificationIFace::Event() directly. 00225 */ 00226 #define EDOC_NOTIFICATION(Code, Message, IdFunction, IDFile, IdLine, \ 00227 IDRFile, IdRLine, IdType, IdPropogatingException, IdFunctionType, \ 00228 Extra) \ 00229 { \ 00230 std::ostringstream stream_kjhgbvk791; \ 00231 stream_kjhgbvk791 << Message; \ 00232 ::EDoc::GetGlobalNotifierAssert()->Event((::EDoc::EventCode)Code, \ 00233 __FILE__, __LINE__, \ 00234 stream_kjhgbvk791.str(), IdFunction, IDFile, IdLine, IDRFile, IdRLine, \ 00235 IdType, IdPropogatingException, IdFunctionType, Extra); \ 00236 } 00237 00238 #endif // EDOC_NOTIFICATIONIFACE_H