[cosmetics] update date in GPL header
[vuplus_xbmc] / xbmc / commons / Exception.h
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://www.xbmc.org
4  *
5  *  This Program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2, or (at your option)
8  *  any later version.
9  *
10  *  This Program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with XBMC; see the file COPYING.  If not, see
17  *  <http://www.gnu.org/licenses/>.
18  *
19  */
20
21 #pragma once
22
23 //---------------------------------------------------------
24 // This include should be moved to commons but even as it is,
25 // it wont cause a linker circular dependency since it's just
26 // a header. 
27 #include "utils/StdString.h"
28 //---------------------------------------------------------
29 #include "ilog.h"
30
31 #ifdef __GNUC__
32 // The 'this' pointer counts as a parameter on member methods.
33 #define XBMCCOMMONS_ATTRIB_EXCEPTION_FORMAT __attribute__((format(printf,2,3)))
34 #else
35 #define XBMCCOMMONS_ATTRIB_EXCEPTION_FORMAT
36 #endif
37
38 #define XBMCCOMMONS_COPYVARARGS(fmt) va_list argList; va_start(argList, fmt); Set(fmt, argList); va_end(argList)
39 #define XBMCCOMMONS_STANDARD_EXCEPTION(E) \
40   class E : public XbmcCommons::Exception \
41   { \
42   public: \
43     inline E(const char* message,...) XBMCCOMMONS_ATTRIB_EXCEPTION_FORMAT : Exception(#E) { XBMCCOMMONS_COPYVARARGS(message); } \
44     \
45     inline E(const E& other) : Exception(other) {} \
46   }
47
48 namespace XbmcCommons
49 {
50   /**
51    * This class a superclass for exceptions that want to utilize some
52    * utility functionality including autologging with the specific
53    * exception name.
54    */
55   class Exception
56   {
57   private:
58
59     std::string classname;
60     CStdString message;
61
62   protected:
63     static ILogger* logger;
64
65     inline Exception(const char* classname_) : classname(classname_) { }
66     inline Exception(const char* classname_, const char* message_) : classname(classname_), message(message_) { }
67     inline Exception(const Exception& other) : classname(other.classname), message(other.message) { }
68
69     /**
70      * This method is called from the constructor of subclasses. It
71      * will set the message from varargs as well as call log message
72      */
73     inline void Set(const char* fmt, va_list& argList)
74     {
75       message.FormatV(fmt, argList);
76     }
77
78     /**
79      * This message can be called from the constructor of subclasses.
80      * It will set the message and log the throwing.
81      */
82     inline void SetMessage(const char* fmt, ...) XBMCCOMMONS_ATTRIB_EXCEPTION_FORMAT
83     {
84       // calls 'set'
85       XBMCCOMMONS_COPYVARARGS(fmt);
86     }
87
88     inline void setClassname(const char* cn) { classname = cn; }
89
90   public:
91     virtual ~Exception();
92
93     inline virtual void LogThrowMessage(const char* prefix = NULL) const
94     {
95       if (logger)
96         logger->Log(LOGERROR,"EXCEPTION Thrown (%s) : %s", classname.c_str(), message.c_str());
97     }
98
99     inline virtual const char* GetMessage() const { return message.c_str(); }
100
101     inline static void SetLogger(ILogger* exceptionLogger) { logger = exceptionLogger; }
102   };
103
104   /**
105    * This class forms the base class for unchecked exceptions. Unchecked exceptions
106    * are those that really shouldn't be handled explicitly. For example, on windows
107    * when a access violaton is converted to a win32_exception, there's nothing
108    * that can be done in most code. The outer most stack frame might try to 
109    * do some error logging prior to shutting down, but that's really it.
110    */
111   XBMCCOMMONS_STANDARD_EXCEPTION(UncheckedException);
112
113 /**
114  * In cases where you catch(...){} you will (may) inadvertently be 
115  * catching UncheckedException's. Therefore this macro will allow 
116  * you to do something equivalent to:
117  *    catch (anything except UncheckedException) {}
118  *
119  * In order to avoid catching UncheckedException, use the macro as follows:
120  *
121  *    try { ... }
122  *    XBMCCOMMONS_HANDLE_UNCHECKED
123  *    catch(...){ ... }
124  */
125 // Yes. I recognize that the name of this macro is an oxymoron.
126 #define  XBMCCOMMONS_HANDLE_UNCHECKED \
127   catch (const XbmcCommons::UncheckedException& ) { throw; } \
128   catch (const XbmcCommons::UncheckedException* ) { throw; }
129
130 }
131