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 }
60
64 void formatMessage(const char* message) noexcept
65 {
66#if IS_WIN
67 ::strncpy_s(_msg.get(), BUF_SIZE, message, strlen(message));
68#else
69 ::strncpy(_msg.get(), message, BUF_SIZE);
70#endif
71 }
72
79 template<typename... Args>
80 T Function(const char* mangled_name, const char* func, const char* format, Args&&... args)
81 {
82 formatMessage(demangle(mangled_name).append("::").append(func).append("() ").append(format).c_str(), args...);
83 return *dynamic_cast<T*>(this);
84 }
85
89 T Function(const char* mangled_name, const char* func, const char* message)
90 {
91 formatMessage(demangle(mangled_name).append("::").append(func).append("() ").append(message).c_str());
92 return *dynamic_cast<T*>(this);
93 }
94
95 protected:
99 std::unique_ptr<char> _msg{new char[BUF_SIZE]};
100
104 enum
105 {
106 BUF_SIZE = 1024
107 };
108};
109
113class _MISC_CLASS Exception : public ExceptionBase<Exception>
114{
115 public:
119 Exception() noexcept;
120
124 Exception(const Exception& ex) noexcept = default;
125
129 explicit Exception(const char* message) noexcept;
130
134 template<typename... Args>
135 explicit Exception(const char* format, Args&&... args) noexcept
136 : ExceptionBase()
137 {
138 formatMessage(format, args...);
139 }
140};
141
149{
150 public:
154 ExceptionSystemCall(const char* syscall, int error, const char* mangled_name, const char* func);
155};
156
157}// 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:89
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:80
ExceptionBase() noexcept
Default Constructor.
Definition Exception.h:23
@ BUF_SIZE
Definition Exception.h:106
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:64
std::unique_ptr< char > _msg
Pointer to message string.
Definition Exception.h:99
Exception implementation for system calls failing within a wrapper class.
Definition Exception.h:149
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:114
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.