Debian Codegraph @

xbmc/commons/Exception.h

raw
1 /*
2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
8
9 #pragma once
10
11 //---------------------------------------------------------
12 // This include should be moved to commons but even as it is,
13 // it wont cause a linker circular dependency since it's just
14 // a header.
15 #include "utils/StringUtils.h"
16 //---------------------------------------------------------
17 #include <stdarg.h>
18
19
20 #ifdef __GNUC__
21 // The 'this' pointer counts as a parameter on member methods.
22 #define XBMCCOMMONS_ATTRIB_EXCEPTION_FORMAT __attribute__((format(printf,2,3)))
23 #else
24 #define XBMCCOMMONS_ATTRIB_EXCEPTION_FORMAT
25 #endif
26
27 #define XBMCCOMMONS_COPYVARARGS(fmt) va_list argList; va_start(argList, fmt); Set(fmt, argList); va_end(argList)
28 #define XBMCCOMMONS_STANDARD_EXCEPTION(E) \
29 class E : public XbmcCommons::Exception \
30 { \
31 public: \
32 inline E(const char* message,...) XBMCCOMMONS_ATTRIB_EXCEPTION_FORMAT : Exception(#E) { XBMCCOMMONS_COPYVARARGS(message); } \
33 \
34 inline E(const E& other) : Exception(other) {} \
35 }
36
37 namespace XbmcCommons
38 {
39 /**
40 * This class a superclass for exceptions that want to utilize some
41 * utility functionality including autologging with the specific
42 * exception name.
43 */
44 class Exception
45 {
46 private:
47
48 std::string classname;
49 std::string message;
50
51 protected:
52
53 inline explicit Exception(const char* classname_) : classname(classname_) { }
54 inline Exception(const char* classname_, const char* message_) : classname(classname_), message(message_) { }
55 inline Exception(const Exception& other) = default;
56
57 /**
58 * This method is called from the constructor of subclasses. It
59 * will set the message from varargs as well as call log message
60 */
61 inline void Set(const char* fmt, va_list& argList)
62 {
63 message = StringUtils::FormatV(fmt, argList);
64 }
65
66 /**
67 * This message can be called from the constructor of subclasses.
68 * It will set the message and log the throwing.
69 */
70 inline void SetMessage(const char* fmt, ...) XBMCCOMMONS_ATTRIB_EXCEPTION_FORMAT
71 {
72 // calls 'set'
73 XBMCCOMMONS_COPYVARARGS(fmt);
74 }
75
76 inline void setClassname(const char* cn) { classname = cn; }
77
78 public:
79 virtual ~Exception();
80
81 virtual void LogThrowMessage(const char* prefix = NULL) const;
82
83 inline virtual const char* GetExMessage() const { return message.c_str(); }
84 };
85
86 /**
87 * This class forms the base class for unchecked exceptions. Unchecked exceptions
88 * are those that really shouldn't be handled explicitly. For example, on windows
89 * when a access violation is converted to a win32_exception, there's nothing
90 * that can be done in most code. The outer most stack frame might try to
91 * do some error logging prior to shutting down, but that's really it.
92 */
93 XBMCCOMMONS_STANDARD_EXCEPTION(UncheckedException);
94
95 /**
96 * In cases where you catch(...){} you will (may) inadvertently be
97 * catching UncheckedException's. Therefore this macro will allow
98 * you to do something equivalent to:
99 * catch (anything except UncheckedException) {}
100 *
101 * In order to avoid catching UncheckedException, use the macro as follows:
102 *
103 * try { ... }
104 * XBMCCOMMONS_HANDLE_UNCHECKED
105 * catch(...){ ... }
106 */
107 // Yes. I recognize that the name of this macro is an oxymoron.
108 #define XBMCCOMMONS_HANDLE_UNCHECKED \
109 catch (const XbmcCommons::UncheckedException& ) { throw; } \
110 catch (const XbmcCommons::UncheckedException* ) { throw; }
111
112 }
113