Scanframe Modular Application 0.1.0
Loading...
Searching...
No Matches
Exception.h
Go to the documentation of this file.
1#pragma once
2#include <cxxabi.h>
3#include <exception>
4#include <memory>
5#include <misc/gen/string.h>
6#include <misc/global.h>
7#include <string.h>
8#include <string>
9
10namespace sf
11{
12
16template<typename T>
17class ExceptionBase : public std::exception
18{
19 public:
23 ExceptionBase() noexcept {}
24
28 ExceptionBase(const ExceptionBase& ex) noexcept
29 {
30#if IS_WIN
31 ::strncpy_s(_msg.get(), BUF_SIZE, ex._msg.get(), strlen(_msg.get()));
32#else
33 ::strncpy(_msg.get(), ex._msg.get(), BUF_SIZE);
34#endif
35 }
36
40 ExceptionBase(const ExceptionBase&& ex) noexcept
41 : _msg(std::move(ex._msg))
42 {}
43
47 [[nodiscard]] const char* what() const noexcept override
48 {
49 return _msg.get();
50 }
51
55 template<typename... Args>
56 void formatMessage(const char* format, Args... args) noexcept
57 {
58 ::snprintf(_msg.get(), BUF_SIZE, format, args...);
59 }
63 void formatMessage(const char* message) noexcept
64 {
65#if IS_WIN
66 ::strncpy_s(_msg.get(), BUF_SIZE, message, strlen(message));
67#else
68 ::strncpy(_msg.get(), message, BUF_SIZE);
69#endif
70 }
71
78 template<typename... Args>
79 T Function(const char* mangled_name, const char* func, const char* format, Args&&... args)
80 {
81 formatMessage(demangle(mangled_name).append("::").append(func).append("() ").append(format).c_str(), args...);
82 return *dynamic_cast<T*>(this);
83 }
84
88 T Function(const char* mangled_name, const char* func, const char* message)
89 {
90 formatMessage(demangle(mangled_name).append("::").append(func).append("() ").append(message).c_str());
91 return *dynamic_cast<T*>(this);
92 }
93
94 protected:
98 std::unique_ptr<char> _msg{new char[BUF_SIZE]};
102 enum
103 {
104 BUF_SIZE = 1024
105 };
106};
107
111class _MISC_CLASS Exception : public ExceptionBase<Exception>
112{
113 public:
117 Exception() noexcept;
118
122 Exception(const Exception& ex) noexcept = default;
123
127 explicit Exception(const char* message) noexcept;
128
132 template<typename... Args>
133 explicit Exception(const char* format, Args&&... args) noexcept
134 : ExceptionBase()
135 {
136 formatMessage(format, args...);
137 }
138};
139
147{
148 public:
152 ExceptionSystemCall(const char* syscall, int error, const char* mangled_name, const char* func);
153};
154
155}// namespace sf
Exception implementation inherited from std::exception.
Definition Exception.h:18
ExceptionBase(const ExceptionBase &ex) noexcept
Copy constructor.
Definition Exception.h:28
const char * what() const noexcept override
Overloaded from base class 'std::exception'.
Definition Exception.h:47
void formatMessage(const char *format, Args... args) noexcept
Formats the exception message according sprintf().
Definition Exception.h:56
T Function(const char *mangled_name, const char *func, const char *message)
Formatting function with a class type_info and passing the message without formatting.
Definition Exception.h:88
T Function(const char *mangled_name, const char *func, const char *format, Args &&... args)
Formatting function with a class type_info and formatting the message.
Definition Exception.h:79
ExceptionBase() noexcept
Default Constructor.
Definition Exception.h:23
@ BUF_SIZE
Definition Exception.h:104
ExceptionBase(const ExceptionBase &&ex) noexcept
Move constructor.
Definition Exception.h:40
void formatMessage(const char *message) noexcept
Formats the exception message when no arguments are given.
Definition Exception.h:63
std::unique_ptr< char > _msg
Pointer to message string.
Definition Exception.h:98
Exception implementation for system calls failing within a wrapper class.
Definition Exception.h:147
ExceptionSystemCall(const char *syscall, int error, const char *mangled_name, const char *func)
Formatting Constructor with a class type_info, function and error value.
Exception implementation.
Definition Exception.h:112
Exception() noexcept
Default Constructor.
#define _MISC_CLASS
Definition misc/global.h:40
Definition Application.h:10
_MISC_FUNC std::string demangle(const char *name)
Returns the unmangled function name returned by type ID.