diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index b052d1d..753fb40 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -14,5 +14,5 @@ jobs: - name: Configure and build this project uses: threeal/cmake-action@v1.1.0 with: - source-dir: example - build-dir: example/build + source-dir: error + build-dir: error/build diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 1dc43c3..f8053a5 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -14,18 +14,18 @@ jobs: - name: Configure, build, and test this project uses: threeal/cmake-action@v1.1.0 with: - source-dir: example - build-dir: example/build + source-dir: error + build-dir: error/build args: -DBUILD_TESTING=ON run-test: true - name: Check code coverage uses: threeal/gcovr-action@main with: - root: example + root: error excludes: | - example/build/* - example/test/* + error/build/* + error/test/* fail-under-line: 100 check-warning: @@ -37,8 +37,8 @@ jobs: - name: Configure and build this project uses: threeal/cmake-action@v1.1.0 with: - source-dir: example - build-dir: example/build + source-dir: error + build-dir: error/build cxx-flags: -Werror args: -DBUILD_TESTING=ON @@ -52,9 +52,9 @@ jobs: run: pip3 install cmake-format - name: Configure CMake - run: cmake example -B example/build + run: cmake error -B error/build - name: Check code formatting run: | - cmake --build example/build --target format - cmake --build example/build --target check-format + cmake --build error/build --target format + cmake --build error/build --target check-format diff --git a/README.md b/README.md index 6b75613..7e70752 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,10 @@ A comprehensive collection of [C++](https://isocpp.org/) utility packages. +## Packages + +- [Error](./error) [WIP]: Provides utilities for error handling. + ## License This project is licensed under the terms of the [MIT License](./LICENSE). diff --git a/example/.clang-format b/error/.clang-format similarity index 100% rename from example/.clang-format rename to error/.clang-format diff --git a/example/.cmake-format b/error/.cmake-format similarity index 100% rename from example/.cmake-format rename to error/.cmake-format diff --git a/example/CMakeLists.txt b/error/CMakeLists.txt similarity index 53% rename from example/CMakeLists.txt rename to error/CMakeLists.txt index e86bf9c..ae7f826 100644 --- a/example/CMakeLists.txt +++ b/error/CMakeLists.txt @@ -1,11 +1,12 @@ cmake_minimum_required(VERSION 3.0) -project(example) +project(error) -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wshadow -Wnon-virtual-dtor -Wpedantic") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wnon-virtual-dtor -Wpedantic") +set(CMAKE_CXX_STANDARD 11) -add_library(example src/example.cpp) -target_include_directories(example PUBLIC include) +add_library(error src/error.cpp) +target_include_directories(error PUBLIC include) if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) include(cmake/CPM.cmake) @@ -19,8 +20,8 @@ if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage -fPIC -O0") - add_executable(example_test test/example_test.cpp) - target_link_libraries(example_test PRIVATE example Catch2::Catch2WithMain) - catch_discover_tests(example_test) + add_executable(error_test test/error_test.cpp) + target_link_libraries(error_test PRIVATE error Catch2::Catch2WithMain) + catch_discover_tests(error_test) endif() endif() diff --git a/error/LICENSE b/error/LICENSE new file mode 100644 index 0000000..c5ebe52 --- /dev/null +++ b/error/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2023 Alfi Maulana + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/error/README.md b/error/README.md new file mode 100644 index 0000000..a672b77 --- /dev/null +++ b/error/README.md @@ -0,0 +1,9 @@ +# Error + +A C++ package that provides utilities for error handling. + +## License + +This project is licensed under the terms of the [MIT License](./LICENSE). + +Copyright © 2023 [Alfi Maulana](https://github.com/threeal) diff --git a/example/cmake/CPM.cmake b/error/cmake/CPM.cmake similarity index 100% rename from example/cmake/CPM.cmake rename to error/cmake/CPM.cmake diff --git a/error/include/error/error.hpp b/error/include/error/error.hpp new file mode 100644 index 0000000..bd70762 --- /dev/null +++ b/error/include/error/error.hpp @@ -0,0 +1,28 @@ +#pragma once + +#include + +namespace error { + +/** + * @brief A class that represents error information. + */ +class Error : public std::exception { + private: + const char* message; /**< The error message. */ + + public: + /** + * @brief Constructs a new error with the given message. + * @param message An error message. + */ + Error(const char* message); + + /** + * @brief Returns the explanatory string. + * @return Pointer to a null-terminated string with explanatory information. + */ + const char* what() const noexcept override; +}; + +} // namespace error diff --git a/error/src/error.cpp b/error/src/error.cpp new file mode 100644 index 0000000..0c2d874 --- /dev/null +++ b/error/src/error.cpp @@ -0,0 +1,9 @@ +#include + +namespace error { + +Error::Error(const char* message) : message(message) {} + +const char* Error::what() const noexcept { return message; } + +} // namespace error diff --git a/error/test/error_test.cpp b/error/test/error_test.cpp new file mode 100644 index 0000000..3afb818 --- /dev/null +++ b/error/test/error_test.cpp @@ -0,0 +1,30 @@ +#include +#include +#include + +TEST_CASE("Error Construction") { + const error::Error err("unknown error"); + REQUIRE(std::string("unknown error") == err.what()); +} + +TEST_CASE("Error Throwing and Catching") { + SECTION("Catch as error::Error") { + try { + throw error::Error("unknown error"); + } catch (const error::Error& err) { + REQUIRE(std::string("unknown error") == err.what()); + } catch (...) { + FAIL("Expected to be caught as error::Error"); + } + } + + SECTION("Catch as std::exception") { + try { + throw error::Error("unknown error"); + } catch (const std::exception& e) { + REQUIRE(std::string("unknown error") == e.what()); + } catch (...) { + FAIL("Expected to be caught as std::exception"); + } + } +} diff --git a/example/LICENSE b/example/LICENSE deleted file mode 100644 index 68a49da..0000000 --- a/example/LICENSE +++ /dev/null @@ -1,24 +0,0 @@ -This is free and unencumbered software released into the public domain. - -Anyone is free to copy, modify, publish, use, compile, sell, or -distribute this software, either in source code form or as a compiled -binary, for any purpose, commercial or non-commercial, and by any -means. - -In jurisdictions that recognize copyright laws, the author or authors -of this software dedicate any and all copyright interest in the -software to the public domain. We make this dedication for the benefit -of the public at large and to the detriment of our heirs and -successors. We intend this dedication to be an overt act of -relinquishment in perpetuity of all present and future rights to this -software under copyright law. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR -OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, -ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -OTHER DEALINGS IN THE SOFTWARE. - -For more information, please refer to diff --git a/example/README.md b/example/README.md deleted file mode 100644 index c1f4159..0000000 --- a/example/README.md +++ /dev/null @@ -1,6 +0,0 @@ -# Example - -[![build status](https://img.shields.io/github/actions/workflow/status/threeal/cpp/build.yml?branch=main)](https://github.com/threeal/cpp/actions/workflows/build.yml) -[![test status](https://img.shields.io/github/actions/workflow/status/threeal/cpp/test.yml?label=test&branch=main)](https://github.com/threeal/cpp/actions/workflows/test.yml) - -An example C++ package. diff --git a/example/include/example/example.hpp b/example/include/example/example.hpp deleted file mode 100644 index 1d53053..0000000 --- a/example/include/example/example.hpp +++ /dev/null @@ -1,7 +0,0 @@ -#pragma once - -namespace example { - -bool is_odd(int val); - -} // namespace example diff --git a/example/src/example.cpp b/example/src/example.cpp deleted file mode 100644 index bed773d..0000000 --- a/example/src/example.cpp +++ /dev/null @@ -1,7 +0,0 @@ -#include - -namespace example { - -bool is_odd(int val) { return val % 2 == 1; } - -} // namespace example diff --git a/example/test/example_test.cpp b/example/test/example_test.cpp deleted file mode 100644 index f8a5f4a..0000000 --- a/example/test/example_test.cpp +++ /dev/null @@ -1,7 +0,0 @@ -#include -#include - -TEST_CASE("test is odd") { - CHECK(example::is_odd(3)); - CHECK_FALSE(example::is_odd(4)); -}