Skip to content

Commit

Permalink
Build system switched to cmake (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
vvish committed Sep 6, 2023
1 parent 186684c commit 5441c31
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 42 deletions.
25 changes: 0 additions & 25 deletions .github/workflows/c-cpp.yml

This file was deleted.

45 changes: 45 additions & 0 deletions .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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 --gcov-tool gcov-12 --base-directory . --directory ${{github.workspace}}/build -c -o coverage.info
lcov --gcov-tool gcov-12 --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

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
bin/

# Coverage dir
build/
coverage/

# Coverage/profiling artifacts
Expand Down
12 changes: 12 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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()
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 6 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
add_executable(
example
example.cpp
)

set_target_properties(example PROPERTIES CXX_STANDARD 14)
4 changes: 4 additions & 0 deletions examples/example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
38 changes: 22 additions & 16 deletions include/respp/result.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,15 @@ constexpr uint8_t sum_widths()
template <typename Cat, typename C, typename... Cs>
struct count_bits_before {
static constexpr int value
= std::is_same_v<Cat, C>
? 0
: (count_bits_before<Cat, Cs...>::value >= 0)
? C::bit_width + count_bits_before<Cat, Cs...>::value
: -1;
= std::is_same<Cat, C>::value ? 0
: (count_bits_before<Cat, Cs...>::value >= 0)
? C::bit_width + count_bits_before<Cat, Cs...>::value
: -1;
};

template <typename Cat, typename C>
struct count_bits_before<Cat, C> {
static constexpr int value = std::is_same_v<Cat, C> ? 0 : -1;
static constexpr int value = std::is_same<Cat, C>::value ? 0 : -1;
};

template <typename Ut, uint8_t Offset, typename C>
Expand Down Expand Up @@ -144,18 +143,18 @@ struct result_t {
bits_occupied_by_categories <= detail::sizeof_in_bits_v<Ut>,
"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<underlaying_type, 0>({}, categories...);

constexpr auto bits_remaining_for_code
= detail::sizeof_in_bits_v<
underlaying_type> - bits_occupied_by_categories;
= detail::sizeof_in_bits_v<underlaying_type>
- bits_occupied_by_categories;
constexpr auto mask
= ~detail::mask<underlaying_type, 0, bits_remaining_for_code>;
return result_t{static_cast<underlaying_type>(r | (code & mask))};
Expand All @@ -167,6 +166,9 @@ struct result_t {
}
};

template <typename Ut, typename... Cs>
constexpr result_t<Ut, Cs...> result_t<Ut, Cs...>::success;

template <typename Result>
class error_iterator_t;

Expand All @@ -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<result_underlaying_type>
* shift_value;
auto const slot_value = static_cast<result_underlaying_type>(
container >> shift_in_bits);
if (!slot_value) {
Expand All @@ -203,17 +205,17 @@ 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<result_underlaying_type>
* shift_value;
auto const slot_value = static_cast<result_underlaying_type>(
container >> shift_in_bits);
if (!slot_value) {
break;
}
}
auto const shift_in_bits
= detail::sizeof_in_bits_v<
typename Result::underlaying_type> * shift_value;
= detail::sizeof_in_bits_v<typename Result::underlaying_type>
* shift_value;

container &= detail::generate_mask<Ut>(
shift_in_bits, detail::sizeof_in_bits_v<result_underlaying_type>);
Expand Down Expand Up @@ -349,6 +351,10 @@ struct aggregate_result_t {
}
};

template <typename Ut, typename Result, typename PlacementStrategy>
constexpr aggregate_result_t<Ut, Result, PlacementStrategy>
aggregate_result_t<Ut, Result, PlacementStrategy>::success;

template <typename CatToFind, typename Ut, typename... Cs>
constexpr CatToFind get_category(result_t<Ut, Cs...> result)
{
Expand Down
26 changes: 26 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 5441c31

Please sign in to comment.