From 080094c474e94f1e106605c0ed56087bee980b17 Mon Sep 17 00:00:00 2001 From: S David <2100425+s-daveb@users.noreply.github.com> Date: Wed, 14 Sep 2022 22:38:53 -0400 Subject: [PATCH] Update clang-format configuration; reformat source files. Use std::set_terminate to a function that can handle the engine's nested exceptions and prints a stacktrace. Closes #22 - Unify source formatting with clang-format Closes #19 - Integrate exception handling and backtrace into main function --- .clang-format | 2 ++ src/engine/engine.hpp | 1 - src/game/main.cpp | 27 +++++++++++++++++++++++- src/tests/runtime.tests.cpp | 41 +------------------------------------ 4 files changed, 29 insertions(+), 42 deletions(-) diff --git a/.clang-format b/.clang-format index 8cda397..e8a8ac4 100644 --- a/.clang-format +++ b/.clang-format @@ -7,6 +7,8 @@ UseTab: AlignWithSpaces ColumnLimit: 81 AccessModifierOffset: -4 BinPackParameters: true +AlignAfterOpenBracket: BlockIndent +AlignOperands: AlignAfterOperator AlwaysBreakAfterDefinitionReturnType: TopLevel AlwaysBreakAfterReturnType: None ConstructorInitializerIndentWidth: 2 diff --git a/src/engine/engine.hpp b/src/engine/engine.hpp index 1ed26e1..a54e592 100644 --- a/src/engine/engine.hpp +++ b/src/engine/engine.hpp @@ -9,7 +9,6 @@ extern "C" { void noop(void); - } // clang-format off diff --git a/src/game/main.cpp b/src/game/main.cpp index d0883fa..f8b8ace 100644 --- a/src/game/main.cpp +++ b/src/game/main.cpp @@ -7,15 +7,40 @@ * obtain one at https://mozilla.org/MPL/2.0/. */ +#include #include +#include "engine/Exception.hpp" #include "engine/debuginfo.hpp" +void +termination_handler(void) +{ + std::cerr << "A fatal error occurred!" << std::endl; + if (auto exception_ptr = std::current_exception()) { + try { + std::rethrow_exception(exception_ptr); + } + catch (const std::exception& ex) { + std::cerr << "Uncaught exception:" << std::endl + << ex.what() << std::endl; + } + catch (...) { + std::cerr << "Unknown exception" << std::endl; + } + } else { + std::cerr << "std::terminate() was called" << std::endl; + } + std::abort(); +} int main(int argc, char* argv[]) { print_cmdline(argc, argv); - std::cout << "Hello World" << std::endl; + std::set_terminate(&termination_handler); + + THROW_EXCEPTION("Hello World"); + return 0; } // clang-format off diff --git a/src/tests/runtime.tests.cpp b/src/tests/runtime.tests.cpp index 589eb34..a9813cd 100644 --- a/src/tests/runtime.tests.cpp +++ b/src/tests/runtime.tests.cpp @@ -18,7 +18,7 @@ class SomeInterface { public: virtual int foo(int) = 0; - virtual int bar(char*)= 0; + virtual int bar(char*) = 0; }; TEST_CASE("Catch2 works with Fakeit", "[integrity-check]") @@ -40,44 +40,5 @@ TEST_CASE("Catch2 works with Fakeit", "[integrity-check]") REQUIRE(otheriface.foo(0) == -1); } -// #region seraization test setup -//struct DataRecord -//{ -// id_t id; -// std::string name; -// -// bool operator==(const DataRecord& rhs) const -// { -// return (this->id == rhs.id) && (this->name == rhs.name); -// } -// -// NLOHMANN_DEFINE_TYPE_INTRUSIVE(DataRecord, id, name) -//}; -//struct DataRecordCollection -//{ -// std::unordered_map collection; -// -// NLOHMANN_DEFINE_TYPE_INTRUSIVE(DataRecordCollection, collection) -// -//}; - -/*TEST_CASE("serizalization works as expected", "[integrity-check]") -{ - DataRecord testRecord; - DataRecord expectedResult{ .id = 52, .name = "expected" }; - - json json_object = expectedResult; - std::string json_string = json_object.dump(4); - - // print out the generated json so we can see it when the test runs - std::cout << json_string << std::endl; - - auto from_string = json::parse(json_string); - testRecord = from_string.get(); - - REQUIRE(expectedResult == testRecord); -}*/ -// #endregion - // clang-format off // vim: set foldmethod=marker foldmarker=#region,#endregion textwidth=80 ts=8 sts=0 sw=8 noexpandtab ft=cpp.doxygen :