FifeGUI 0.2.0
A C++ GUI library designed for games.
exception.hpp
1// SPDX-License-Identifier: LGPL-2.1-or-later OR BSD-3-Clause
2// SPDX-FileCopyrightText: 2004 - 2008 Olof Naessén and Per Larsson
3// SPDX-FileCopyrightText: 2013 - 2026 Fifengine contributors
4
5#ifndef INCLUDE_FIFECHAN_EXCEPTION_HPP_
6#define INCLUDE_FIFECHAN_EXCEPTION_HPP_
7
8#include <source_location>
9#include <stdexcept>
10#include <string>
11#include <utility>
12
13#include "fifechan/platform.hpp"
14
15namespace fcn
16{
35 class /*FIFEGUI_API*/ Exception : public std::runtime_error
36 {
37 public:
44 explicit Exception(std::string message, std::source_location location = std::source_location::current()) :
45 std::runtime_error(message), // Pass by reference, do not move.
46 mMessage(std::move(message)), // Move only once into mMessage.
47 mFunction(location.function_name()),
48 mFilename(location.file_name()),
49 mLine(location.line())
50 {
51 }
52
58 char const * what() const noexcept override
59 {
60 return mMessage.c_str();
61 }
62
68 std::string const & getFunction() const
69 {
70 return mFunction;
71 };
72
78 std::string const & getMessage() const
79 {
80 return mMessage;
81 };
82
88 std::string const & getFilename() const
89 {
90 return mFilename;
91 };
92
98 unsigned int getLine() const
99 {
100 return mLine;
101 };
102
103 private:
107 std::string mMessage = "Message?";
108
112 std::string mFunction = "Function?";
113
117 std::string mFilename = "Filename?";
118
122 unsigned int mLine = 0;
123 };
124
125 inline void throwException(
126 std::string const & message, std::source_location location = std::source_location::current())
127 {
128 throw Exception(message, location);
129 }
130
131} // namespace fcn
132
133#endif // INCLUDE_FIFECHAN_EXCEPTION_HPP_
char const * what() const noexcept override
Returns a pointer to a null-terminated string with a description of the exception.
Definition exception.hpp:58
std::string const & getMessage() const
Gets the message of the exception.
Definition exception.hpp:78
std::string const & getFilename() const
Gets the filename where the exception occurred.
Definition exception.hpp:88
unsigned int getLine() const
Gets the line number where the exception occurred.
Definition exception.hpp:98
std::string const & getFunction() const
Gets the function name where the exception occurred.
Definition exception.hpp:68
Exception(std::string message, std::source_location location=std::source_location::current())
Constructor that takes a message and source location.
Definition exception.hpp:44