From 3405208295108a92583b8c1d7d422f2b65f3c603 Mon Sep 17 00:00:00 2001 From: Fries Date: Wed, 21 Jun 2023 17:01:33 -0700 Subject: [PATCH] add an exception program this program messes with c++ exceptions. --- .vscode/launch.json | 16 +++ .vscode/settings.json | 4 + src/CMakeLists.txt | 3 +- src/{functions.cc => FunctionArguments.cc} | 0 src/exceptions.cc | 122 +++++++++++++++++++++ src/namespaces.cc | 1 - 6 files changed, 144 insertions(+), 2 deletions(-) create mode 100644 .vscode/launch.json rename src/{functions.cc => FunctionArguments.cc} (100%) create mode 100644 src/exceptions.cc diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..042ac30 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,16 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "lldb", + "request": "launch", + "name": "Debug", + "program": "${command:cmake.launchTargetPath}", + "args": [], + "cwd": "${workspaceFolder}", + } + ] +} diff --git a/.vscode/settings.json b/.vscode/settings.json index 71b9576..067de27 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,4 +1,8 @@ { "editor.detectIndentation": false, "editor.insertSpaces": false, + "cmake.configureEnvironment": { + "CC": "clang", + "CXX": "clang++" + }, } diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b7e10d6..6eff54a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -6,4 +6,5 @@ target_link_libraries(sqrt PRIVATE Math) add_executable(inheritance inheritance.cc) add_executable(enumerations enumerations.cc) add_executable(namespaces namespaces.cc) -add_executable(functions functions.cc) +add_executable(FunctionArguments FunctionArguments.cc) +add_executable(exceptions exceptions.cc) diff --git a/src/functions.cc b/src/FunctionArguments.cc similarity index 100% rename from src/functions.cc rename to src/FunctionArguments.cc diff --git a/src/exceptions.cc b/src/exceptions.cc new file mode 100644 index 0000000..bc8e25f --- /dev/null +++ b/src/exceptions.cc @@ -0,0 +1,122 @@ +#include +#include +#include + +namespace Crewmate { +enum class Type { + Imposter, + Crewmate +}; +class NotImposterException : public std::exception { +private: + const char* message; + +public: + NotImposterException(const char* msg) + : message(msg) + { + } + const char* what() const noexcept override + { + return message; + } +}; +class NotCrewmateException : public std::exception { +private: + const char* message; + +public: + NotCrewmateException(const char* msg) + : message(msg) + { + } + + const char* what() const noexcept override + { + return message; + } +}; +class Crewmate { +public: + Crewmate(Type type) + { + this->type = type; + } + + void is_imposter() + { + switch (this->type) { + case Type::Imposter: + std::cout << "this crewmate is an imposter. sussy." << std::endl; + break; + case Type::Crewmate: + throw NotImposterException("this crewmate is not an imposter. not sussy."); + break; + } + } + void is_crewmate() + { + switch (this->type) { + case Type::Imposter: + throw NotCrewmateException("this crewmate is not a crewmate. sussy."); + break; + case Type::Crewmate: + std::cout << "this crewmate is a crewmate. not sussy." << std::endl; + break; + } + } + void invariant_testing() + { + switch (this->type) { + + case Type::Imposter: + throw NotCrewmateException("this crewmate is not a crewmate. sussy."); + case Type::Crewmate: + throw NotImposterException("this crewmate is not an imposter. not sussy."); + break; + } + } + +private: + Type type; +}; +} + +void exception_handler(const std::exception& exception, const std::string prefix = "Exception: ") +{ + std::cerr << prefix << exception.what() << std::endl; +} + +void invariant_test(Crewmate::Crewmate& crewmate) +{ + try { + crewmate.invariant_testing(); + } catch (const Crewmate::NotImposterException& err) { + exception_handler(err, "Imposter Exception: "); + } catch (const Crewmate::NotCrewmateException& err) { + exception_handler(err, "Crewmate Exception: "); + } +} + +int main() +{ + Crewmate::Crewmate imposter(Crewmate::Type::Imposter); + Crewmate::Crewmate crewmate(Crewmate::Type::Crewmate); + + try { + imposter.is_imposter(); + imposter.is_crewmate(); + } catch (const Crewmate::NotCrewmateException& err) { + exception_handler(err); + } + + try { + crewmate.is_crewmate(); + crewmate.is_imposter(); + } catch (const Crewmate::NotImposterException& err) { + exception_handler(err); + } + + invariant_test(imposter); + invariant_test(crewmate); +} diff --git a/src/namespaces.cc b/src/namespaces.cc index 891864f..ee7f36c 100644 --- a/src/namespaces.cc +++ b/src/namespaces.cc @@ -11,5 +11,4 @@ namespace Imposter { int main() { Imposter::Namespace::sussy_function(); - return 0; }