FifeGUI 0.3.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// Standard library includes
9#include <source_location>
10#include <stdexcept>
11#include <string>
12#include <utility>
13
14namespace fcn
15{
34 class /*FIFEGUI_API*/ Exception : public std::runtime_error
35 {
36 public:
43 explicit Exception(std::string message, std::source_location location = std::source_location::current()) :
44 std::runtime_error(message), // Pass by reference, do not move.
45 mMessage(std::move(message)), // Move only once into mMessage.
46 mFunction(location.function_name()),
47 mFilename(location.file_name()),
48 mLine(location.line())
49 {
50 }
51
57 char const * what() const noexcept override
58 {
59 return mMessage.c_str();
60 }
61
67 std::string const & getFunction() const
68 {
69 return mFunction;
70 };
71
77 std::string const & getMessage() const
78 {
79 return mMessage;
80 };
81
87 std::string const & getFilename() const
88 {
89 return mFilename;
90 };
91
97 unsigned int getLine() const
98 {
99 return mLine;
100 };
101
102 private:
106 std::string mMessage = "Message?";
107
111 std::string mFunction = "Function?";
112
116 std::string mFilename = "Filename?";
117
121 unsigned int mLine = 0;
122 };
123
130 inline void throwException(
131 std::string const & message, std::source_location location = std::source_location::current())
132 {
133 throw Exception(message, location);
134 }
135
136} // namespace fcn
137
138#endif // INCLUDE_FIFECHAN_EXCEPTION_HPP_
An exception class containing a message, a file, and a line number where the exception occurred.
Definition exception.hpp:35
char const * what() const noexcept override
Returns a pointer to a null-terminated string with a description of the exception.
Definition exception.hpp:57
std::string const & getMessage() const
Gets the message of the exception.
Definition exception.hpp:77
std::string const & getFilename() const
Gets the filename where the exception occurred.
Definition exception.hpp:87
unsigned int getLine() const
Gets the line number where the exception occurred.
Definition exception.hpp:97
std::string const & getFunction() const
Gets the function name where the exception occurred.
Definition exception.hpp:67
Exception(std::string message, std::source_location location=std::source_location::current())
Constructor that takes a message and source location.
Definition exception.hpp:43
Used replacement tokens by configure_file():
void throwException(std::string const &message, std::source_location location=std::source_location::current())
Throw an Exception capturing the current source location.