From a01e642d9016980af55926b332c5e9dc43a0c091 Mon Sep 17 00:00:00 2001 From: vvish Date: Sun, 3 Sep 2023 22:44:19 +0200 Subject: [PATCH] Build system switched to cmake (#17) --- .github/workflows/c-cpp.yml | 25 -------------------- .github/workflows/cmake.yml | 46 +++++++++++++++++++++++++++++++++++++ .gitignore | 1 + CMakeLists.txt | 12 ++++++++++ README.md | 2 +- examples/CMakeLists.txt | 6 +++++ examples/example.cpp | 4 ++++ include/respp/result.hpp | 38 +++++++++++++++++------------- test/CMakeLists.txt | 26 +++++++++++++++++++++ 9 files changed, 118 insertions(+), 42 deletions(-) delete mode 100644 .github/workflows/c-cpp.yml create mode 100644 .github/workflows/cmake.yml create mode 100644 CMakeLists.txt create mode 100644 examples/CMakeLists.txt create mode 100644 test/CMakeLists.txt diff --git a/.github/workflows/c-cpp.yml b/.github/workflows/c-cpp.yml deleted file mode 100644 index df3a7ea..0000000 --- a/.github/workflows/c-cpp.yml +++ /dev/null @@ -1,25 +0,0 @@ -name: C/C++ CI - -on: - push: - branches: [ master ] - pull_request: - branches: [ master ] - -jobs: - build: - - runs-on: ubuntu-latest - - steps: - - name: Install gtest - run: sudo apt-get install libgtest-dev && cd /usr/src/gtest && sudo cmake CMakeLists.txt && sudo make && sudo cp *.a /usr/lib && sudo ln -s /usr/lib/libgtest.a /usr/local/lib/libgtest.a && sudo ln -s /usr/lib/libgtest_main.a /usr/local/lib/libgtest_main.a - - name: Install gmock - run: sudo apt-get install google-mock && cd /usr/src/gmock && sudo cmake CMakeLists.txt && sudo make && sudo cp *.a /usr/lib - - uses: actions/checkout@v2 - - name: Build - run: make - - name: Run tests and produce code coverage - run: make code-coverage - - name: Upload coverage to Codecov - uses: codecov/codecov-action@v1 diff --git a/.github/workflows/cmake.yml b/.github/workflows/cmake.yml new file mode 100644 index 0000000..3877dc0 --- /dev/null +++ b/.github/workflows/cmake.yml @@ -0,0 +1,46 @@ +name: CMake + +on: + push: + branches: [ "master" ] + pull_request: + branches: [ "master" ] + +env: + BUILD_TYPE: Debug + CXX_COMPILER: g++-12 + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + with: + submodules: recursive + + - name: Configure CMake + run: cmake -B ${{github.workspace}}/build -DCMAKE_CXX_COMPILER=${{env.CXX_COMPILER}} -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} + + - name: Build + run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}} + + - name: Test + working-directory: ${{github.workspace}}/build + run: ctest -C ${{env.BUILD_TYPE}} + + - name: Generate coverage report + working-directory: ${{github.workspace}} + run: | + sudo apt-get install lcov + lcov --base-directory . --directory ${{github.workspace}}/build -c -o coverage.info + lcov --remove coverage.info "*/test/*" -o coverage.info + + + - name: Upload coverage to Codecov + uses: codecov/codecov-action@v3 + with: + fail_ci_if_error: true + verbose: true + files: coverage.info + diff --git a/.gitignore b/.gitignore index e267a44..cae50da 100644 --- a/.gitignore +++ b/.gitignore @@ -35,6 +35,7 @@ bin/ # Coverage dir +build/ coverage/ # Coverage/profiling artifacts diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..c5d2623 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,12 @@ +cmake_policy(SET CMP0048 NEW) + +project(cpp_result VERSION 0.1) + +cmake_minimum_required(VERSION 3.9) + +include_directories(include) + +add_subdirectory(test) +add_subdirectory(examples) + +enable_testing() diff --git a/README.md b/README.md index b7ec12a..7ae0601 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ encapsulation. ## Build -The library requires C++17. +The library requires C++14. Makefile in the project root can be used to build and execute unit-tests. To build and execute tests the following make target can be used: diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt new file mode 100644 index 0000000..aebec59 --- /dev/null +++ b/examples/CMakeLists.txt @@ -0,0 +1,6 @@ +add_executable( + example + example.cpp +) + +set_target_properties(example PROPERTIES CXX_STANDARD 14) \ No newline at end of file diff --git a/examples/example.cpp b/examples/example.cpp index f3dace0..c8f4720 100644 --- a/examples/example.cpp +++ b/examples/example.cpp @@ -50,6 +50,9 @@ class BackendClient { } }; +constexpr Result BackendClient::rpcError; +constexpr Result BackendClient::wrongQuery; + // shared error codes can be used constexpr uint8_t backendAccessErrorCode = 2; constexpr auto backendAccessError @@ -89,6 +92,7 @@ AggregateResult retrieveData() auto result = backend_access::retrieveRemoteData(); if (respp::is_success(result)) { // positive case + return {}; } else { // append error from upper layer return result << dataRetrievalError; diff --git a/include/respp/result.hpp b/include/respp/result.hpp index e23eacc..fb4d8c3 100644 --- a/include/respp/result.hpp +++ b/include/respp/result.hpp @@ -77,16 +77,15 @@ constexpr uint8_t sum_widths() template struct count_bits_before { static constexpr int value - = std::is_same_v - ? 0 - : (count_bits_before::value >= 0) - ? C::bit_width + count_bits_before::value - : -1; + = std::is_same::value ? 0 + : (count_bits_before::value >= 0) + ? C::bit_width + count_bits_before::value + : -1; }; template struct count_bits_before { - static constexpr int value = std::is_same_v ? 0 : -1; + static constexpr int value = std::is_same::value ? 0 : -1; }; template @@ -144,18 +143,18 @@ struct result_t { bits_occupied_by_categories <= detail::sizeof_in_bits_v, "The underlying type is too small to contain category"); - static constexpr result_t success{0}; + static constexpr result_t success{}; underlaying_type result; - static constexpr result_t make(Cs const &... categories, Ut code) + static constexpr result_t make(Cs const &...categories, Ut code) { auto const r = detail::place_category({}, categories...); constexpr auto bits_remaining_for_code - = detail::sizeof_in_bits_v< - underlaying_type> - bits_occupied_by_categories; + = detail::sizeof_in_bits_v + - bits_occupied_by_categories; constexpr auto mask = ~detail::mask; return result_t{static_cast(r | (code & mask))}; @@ -167,6 +166,9 @@ struct result_t { } }; +template +constexpr result_t result_t::success; + template class error_iterator_t; @@ -181,8 +183,8 @@ struct place_while_space_is_available { = sizeof(Ut) / sizeof(result_underlaying_type); for (auto shift_value = 0; shift_value < capacity; ++shift_value) { auto const shift_in_bits - = detail::sizeof_in_bits_v< - result_underlaying_type> * shift_value; + = detail::sizeof_in_bits_v + * shift_value; auto const slot_value = static_cast( container >> shift_in_bits); if (!slot_value) { @@ -203,8 +205,8 @@ struct replace_topmost { auto shift_value = 0; for (; shift_value < capacity - 1; ++shift_value) { auto const shift_in_bits - = detail::sizeof_in_bits_v< - result_underlaying_type> * shift_value; + = detail::sizeof_in_bits_v + * shift_value; auto const slot_value = static_cast( container >> shift_in_bits); if (!slot_value) { @@ -212,8 +214,8 @@ struct replace_topmost { } } auto const shift_in_bits - = detail::sizeof_in_bits_v< - typename Result::underlaying_type> * shift_value; + = detail::sizeof_in_bits_v + * shift_value; container &= detail::generate_mask( shift_in_bits, detail::sizeof_in_bits_v); @@ -349,6 +351,10 @@ struct aggregate_result_t { } }; +template +constexpr aggregate_result_t + aggregate_result_t::success; + template constexpr CatToFind get_category(result_t result) { diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 0000000..cba22dd --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1,26 @@ +add_executable( + unit-tests + result_test.cpp +) + +enable_testing() + +include(FetchContent) +FetchContent_Declare( + googletest + GIT_REPOSITORY https://github.com/google/googletest.git + GIT_TAG v1.14.0 +) + +set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) + +FetchContent_MakeAvailable(googletest) + +set_target_properties(unit-tests PROPERTIES CXX_STANDARD 14) +target_link_libraries(unit-tests GTest::gtest_main) +target_compile_options(unit-tests PRIVATE -O0 --coverage -g) +target_link_options(unit-tests PRIVATE --coverage) + +include(GoogleTest) + +gtest_discover_tests(unit-tests)