kodi
Exception.h
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 {
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 
61  inline void Set(const char* fmt, va_list& argList)
62  {
63  message = StringUtils::FormatV(fmt, argList);
64  }
65 
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 
93  XBMCCOMMONS_STANDARD_EXCEPTION(UncheckedException);
94 
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 
Definition: Buffer.h:15
void Set(const char *fmt, va_list &argList)
This method is called from the constructor of subclasses.
Definition: Exception.h:61
void SetMessage(const char *fmt,...) XBMCCOMMONS_ATTRIB_EXCEPTION_FORMAT
This message can be called from the constructor of subclasses.
Definition: Exception.h:70
This class a superclass for exceptions that want to utilize some utility functionality including auto...
Definition: Exception.h:44