[XAudio2] avoid leak + fix voice creation for closest match
[xbmc.git] / xbmc / commons / Exception.h
blobea2c9bc7c52e63254286a44cc8ebae825dadbf4a
1 /*
2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
9 #pragma once
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>
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
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); } \
34 inline E(const E& other) : Exception(other) {} \
37 namespace XbmcCommons
39 /**
40 * This class a superclass for exceptions that want to utilize some
41 * utility functionality including autologging with the specific
42 * exception name.
44 class Exception
46 private:
48 std::string classname;
49 std::string message;
51 protected:
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;
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
61 inline void Set(const char* fmt, va_list& argList)
63 message = StringUtils::FormatV(fmt, argList);
66 /**
67 * This message can be called from the constructor of subclasses.
68 * It will set the message and log the throwing.
70 inline void SetMessage(const char* fmt, ...) XBMCCOMMONS_ATTRIB_EXCEPTION_FORMAT
72 // calls 'set'
73 XBMCCOMMONS_COPYVARARGS(fmt);
76 inline void setClassname(const char* cn) { classname = cn; }
78 public:
79 virtual ~Exception();
81 virtual void LogThrowMessage(const char* prefix = NULL) const;
83 inline virtual const char* GetExMessage() const { return message.c_str(); }
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.
93 XBMCCOMMONS_STANDARD_EXCEPTION(UncheckedException);
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) {}
101 * In order to avoid catching UncheckedException, use the macro as follows:
103 * try { ... }
104 * XBMCCOMMONS_HANDLE_UNCHECKED
105 * catch(...){ ... }
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; }