CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
Exception.hpp
Go to the documentation of this file.
1//
2// CeresEngine - A game development framework
3//
4// Created by Rogiel Sulzbach.
5// Copyright (c) 2018-2022 Rogiel Sulzbach. All rights reserved.
6//
7
8#pragma once
9
11
14
15#include <stdexcept>
16
17#if __has_include(<source_location>)
18#include <source_location>
19#endif
20
21namespace CeresEngine {
22
23#if defined(__cpp_lib_source_location)
24 using SourceLocation = std::source_location;
25#else
27 const char* filename = "";
28 const char* function = "";
29
30 static SourceLocation current(const char* const filename = __FILE__) {
31 return SourceLocation{
33 .function = "",
34 };
35 }
36 [[nodiscard]] constexpr std::uint_least32_t line() const noexcept { return 0; }
37 [[nodiscard]] constexpr std::uint_least32_t column() const noexcept { return 0; }
38 [[nodiscard]] constexpr const char* file_name() const noexcept { return filename; }
39 [[nodiscard]] constexpr const char* function_name() const noexcept { return function; }
40 };
41#endif
42
43 class Exception : public std::exception {
44 private:
49
50 public:
53
54 template<typename S, typename... Args>
55 explicit Exception(const char* typeName, const S& formatString, Args&&... args)
57
58 Exception(const Exception& other) = default;
59 Exception& operator=(const Exception& other) = default;
60
61 Exception(Exception&& other) noexcept = default;
62 Exception& operator=(Exception&& other) noexcept = default;
63
65
70 if(mFullDescription.empty()) {
71 StringStream desc;
72
73 desc << "EXCEPTION(" << mTypeName << "): " << mDescription << " in " << getFile();
74 if(getLine() > 0) {
75 desc << " at " << getFile() << " (line " << getLine() << ")";
76 }
77
78 mFullDescription = desc.str();
79 }
80
81 return mFullDescription;
82 }
83
86
89
92
95
97 [[nodiscard]] const char* what() const noexcept override { return getFullDescription().c_str(); }
98 };
99
100#define CE_EXCEPTION_DECL2(Name, Parent) \
101 class Name : public Parent { \
102 public: \
103 template<typename S, typename... Args> \
104 explicit Name(const S& formatString, Args&&... args) : Parent(#Name, formatString, std::forward<Args>(args)...) {} \
105 }
106#define CE_EXCEPTION_DECL(Name) CE_EXCEPTION_DECL2(Name, Exception)
107
116
117} // namespace CeresEngine
#define CE_EXCEPTION_DECL(Name)
Definition Exception.hpp:106
Definition Exception.hpp:43
Exception(Exception &&other) noexcept=default
Exception(const Exception &other)=default
String mDescription
Definition Exception.hpp:46
virtual StringView getSource() const noexcept
Gets the source function that threw the exception.
Definition Exception.hpp:85
virtual UInt32 getLine() const noexcept
Gets line number on which the exception was thrown.
Definition Exception.hpp:91
SourceLocation mSourceLocation
Definition Exception.hpp:47
virtual StringView getDescription() const noexcept
Gets a short description about the exception.
Definition Exception.hpp:94
StringView mTypeName
Definition Exception.hpp:45
virtual StringView getFile() const noexcept
Gets the source file name in which the exception was thrown.
Definition Exception.hpp:88
virtual const String & getFullDescription() const
Returns a string with the full description of the exception.
Definition Exception.hpp:69
Exception(const char *typeName, const String &description="", const SourceLocation &sourceLocation=SourceLocation::current())
Definition Exception.hpp:51
String mFullDescription
Definition Exception.hpp:48
Exception(const char *typeName, const S &formatString, Args &&... args)
Definition Exception.hpp:55
const char * what() const noexcept override
Overriden std::exception::what. Returns the same value as getFullDescription().
Definition Exception.hpp:97
Exception & operator=(Exception &&other) noexcept=default
Exception & operator=(const Exception &other)=default
~Exception() noexcept override=default
Definition Exception.hpp:109
Definition Exception.hpp:110
Definition Exception.hpp:113
Definition Exception.hpp:114
Definition Exception.hpp:112
Definition Exception.hpp:111
Definition Exception.hpp:108
Definition Exception.hpp:115
Definition Application.hpp:19
BasicStringStream< char > StringStream
Wide string stream used for primarily for constructing narrow strings.
Definition StringStream.hpp:32
std::uint32_t UInt32
Definition DataTypes.hpp:23
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25
Definition Span.hpp:668
Definition Exception.hpp:26
static SourceLocation current(const char *const filename=__FILE__)
Definition Exception.hpp:30
constexpr std::uint_least32_t column() const noexcept
Definition Exception.hpp:37
constexpr std::uint_least32_t line() const noexcept
Definition Exception.hpp:36
constexpr const char * file_name() const noexcept
Definition Exception.hpp:38
const char * filename
Definition Exception.hpp:27
const char * function
Definition Exception.hpp:28
constexpr const char * function_name() const noexcept
Definition Exception.hpp:39