Skip to content

Commit

Permalink
Add support for C++14 (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavel-kirienko authored Jun 26, 2024
1 parent b2e10cf commit 8110aaa
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 71 deletions.
23 changes: 13 additions & 10 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,19 @@ jobs:
toolchain: ['clang', 'gcc']
include:
- toolchain: gcc
c-compiler: gcc-12
cxx-compiler: g++-12
c-compiler: gcc
cxx-compiler: g++
- toolchain: clang
c-compiler: clang
cxx-compiler: clang++
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Install Dependencies
run: sudo apt install gcc-multilib g++-multilib clang-tidy-14
run: |
sudo apt install gcc-multilib g++-multilib clang-tidy
g++ --version
clang-tidy --version
- name: Configure CMake
run: >
Expand All @@ -46,13 +49,13 @@ jobs:
build_type: [Release, MinSizeRel]
include:
- toolchain: gcc
c-compiler: gcc-12
cxx-compiler: g++-12
c-compiler: gcc
cxx-compiler: g++
- toolchain: clang
c-compiler: clang
cxx-compiler: clang++
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Install Dependencies
run: sudo apt install gcc-multilib g++-multilib
Expand All @@ -78,10 +81,10 @@ jobs:
style_check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: DoozyX/clang-format-lint-action@v0.14
- uses: actions/checkout@v4
- uses: DoozyX/clang-format-lint-action@v0.17
with:
source: '.'
exclude: './unity'
extensions: 'c,h,cpp,hpp'
clangFormatVersion: 14
clangFormatVersion: 17
2 changes: 2 additions & 0 deletions .idea/dictionaries/pavel.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 16 additions & 10 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,30 @@ set(CTEST_OUTPUT_ON_FAILURE ON)

# Use -DNO_STATIC_ANALYSIS=1 to suppress static analysis. If not suppressed, the tools used here shall be available.
if (NOT NO_STATIC_ANALYSIS)
find_program(clang_tidy NAMES clang-tidy-14 clang-tidy)
find_program(clang_tidy NAMES clang-tidy)
if (NOT clang_tidy)
message(FATAL_ERROR "Could not locate clang-tidy")
endif ()
message(STATUS "Using clang-tidy: ${clang_tidy}")
set(CMAKE_CXX_CLANG_TIDY ${clang_tidy})
endif ()

find_program(clang_format NAMES clang-format-14 clang-format)
find_program(clang_format NAMES clang-format)
if (NOT clang_format)
message(STATUS "Could not locate clang-format")
else ()
file(GLOB format_files
${CMAKE_CURRENT_SOURCE_DIR}/c/*.[ch]pp
${CMAKE_CURRENT_SOURCE_DIR}/c/*.[ch]
${CMAKE_CURRENT_SOURCE_DIR}/c++/*.[ch]pp
${CMAKE_CURRENT_SOURCE_DIR}/c/*.[ch]pp
${CMAKE_CURRENT_SOURCE_DIR}/c/*.[ch]
${CMAKE_CURRENT_SOURCE_DIR}/c++/*.[ch]pp
)
message(STATUS "Using clang-format: ${clang_format}; files: ${format_files}")
add_custom_target(format COMMAND ${clang_format} -i -fallback-style=none -style=file --verbose ${format_files})
endif ()

set(CMAKE_C_STANDARD 11)
set(CMAKE_C_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Werror -pedantic -fstrict-aliasing")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wdouble-promotion -Wswitch-enum -Wfloat-equal -Wundef")
Expand All @@ -53,10 +53,16 @@ if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
# This deficiency of the test suite is not expected to affect applications.
# This workaround is also not required for clang.
target_compile_options(test_c PRIVATE -fno-strict-aliasing)
endif()
endif ()
target_link_libraries(test_c unity)
add_test("run_test_c" "test_c")

add_executable(test_cpp ${CMAKE_CURRENT_SOURCE_DIR}/c++/test.cpp)
target_link_libraries(test_cpp unity)
add_test("run_test_cpp" "test_cpp")
add_executable(test_cpp20 ${CMAKE_CURRENT_SOURCE_DIR}/c++/test.cpp)
set_target_properties(test_cpp20 PROPERTIES CXX_STANDARD 20)
target_link_libraries(test_cpp20 unity)
add_test("run_test_cpp20" "test_cpp20")

add_executable(test_cpp14 ${CMAKE_CURRENT_SOURCE_DIR}/c++/test.cpp)
set_target_properties(test_cpp14 PROPERTIES CXX_STANDARD 14)
target_link_libraries(test_cpp14 unity)
add_test("run_test_cpp14" "test_cpp14")
34 changes: 22 additions & 12 deletions c++/cavl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ class Tree;
template <typename Derived>
class Node
{
// Polyfill for C++17's std::invoke_result_t.
template <typename F, typename... Args>
using invoke_result =
#if __cplusplus >= 201703L
std::invoke_result_t<F, Args...>;
#else
std::result_of_t<F(Args...)>;
#endif

public:
/// Helper aliases.
using TreeType = Tree<Derived>;
Expand Down Expand Up @@ -143,9 +152,9 @@ class Node
/// traversal will stop when the first true value is returned, which is propagated back to the caller; if none
/// of the calls returned true or the tree is empty, a default value is constructed and returned.
/// The tree shall not be modified while traversal is in progress, otherwise bad memory access will likely occur.
template <typename Vis, typename R = std::invoke_result_t<Vis, Derived&>>
static auto traverse(Derived* const root, const Vis& visitor, const bool reverse = false)
-> std::enable_if_t<!std::is_void_v<R>, R>
template <typename Vis, typename R = invoke_result<Vis, Derived&>>
static auto traverse(Derived* const root, const Vis& visitor, const bool reverse = false) //
-> std::enable_if_t<!std::is_void<R>::value, R>
{
if (Node* const n = root)
{
Expand All @@ -163,7 +172,7 @@ class Node
}
template <typename Vis>
static auto traverse(Derived* const root, const Vis& visitor, const bool reverse = false)
-> std::enable_if_t<std::is_void_v<std::invoke_result_t<Vis, Derived&>>>
-> std::enable_if_t<std::is_void<invoke_result<Vis, Derived&>>::value>
{
if (Node* const n = root)
{
Expand All @@ -172,9 +181,9 @@ class Node
Node::traverse<Vis>(down(n->lr[!reverse]), visitor, reverse);
}
}
template <typename Vis, typename R = std::invoke_result_t<Vis, const Derived&>>
static auto traverse(const Derived* const root, const Vis& visitor, const bool reverse = false)
-> std::enable_if_t<!std::is_void_v<R>, R>
template <typename Vis, typename R = invoke_result<Vis, const Derived&>>
static auto traverse(const Derived* const root, const Vis& visitor, const bool reverse = false) //
-> std::enable_if_t<!std::is_void<R>::value, R>
{
if (const Node* const n = root)
{
Expand All @@ -192,7 +201,7 @@ class Node
}
template <typename Vis>
static auto traverse(const Derived* const root, const Vis& visitor, const bool reverse = false)
-> std::enable_if_t<std::is_void_v<std::invoke_result_t<Vis, const Derived&>>>
-> std::enable_if_t<std::is_void<invoke_result<Vis, const Derived&>>::value>
{
if (const Node* const n = root)
{
Expand Down Expand Up @@ -565,13 +574,13 @@ class Tree final
template <typename Vis>
auto traverse(const Vis& visitor, const bool reverse = false)
{
TraversalIndicatorUpdater upd(*this);
const TraversalIndicatorUpdater upd(*this);
return NodeType::template traverse<Vis>(*this, visitor, reverse);
}
template <typename Vis>
auto traverse(const Vis& visitor, const bool reverse = false) const
{
TraversalIndicatorUpdater upd(*this);
const TraversalIndicatorUpdater upd(*this);
return NodeType::template traverse<Vis>(*this, visitor, reverse);
}

Expand Down Expand Up @@ -603,8 +612,9 @@ class Tree final
auto empty() const noexcept { return root_ == nullptr; }

private:
static_assert(!std::is_polymorphic_v<NodeType>);
static_assert(std::is_same_v<Tree<Derived>, typename NodeType::TreeType>);
static_assert(!std::is_polymorphic<NodeType>::value,
"Internal check: The node type must not be a polymorphic type");
static_assert(std::is_same<Tree<Derived>, typename NodeType::TreeType>::value, "Internal check: Bad type alias");

/// We use a simple boolean flag instead of a nesting counter to avoid race conditions on the counter update.
/// This implies that in the case of concurrent or recursive traversal (more than one call to traverse() within
Expand Down
Loading

0 comments on commit 8110aaa

Please sign in to comment.