diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..93f5b17 --- /dev/null +++ b/.clang-format @@ -0,0 +1,109 @@ +--- +BasedOnStyle: Microsoft +AccessModifierOffset: '-2' +AlignAfterOpenBracket: Align +AlignConsecutiveMacros: 'true' +AlignConsecutiveDeclarations: 'false' +AlignEscapedNewlines: Left +AlignOperands: 'true' +AlignTrailingComments: 'true' +AllowAllArgumentsOnNextLine: 'true' +AllowAllConstructorInitializersOnNextLine: 'true' +AllowAllParametersOfDeclarationOnNextLine: 'true' +AllowShortBlocksOnASingleLine: 'false' +AllowShortCaseLabelsOnASingleLine: 'true' +AllowShortFunctionsOnASingleLine: InlineOnly +AllowShortIfStatementsOnASingleLine: Never +AllowShortLambdasOnASingleLine: Inline +AllowShortLoopsOnASingleLine: 'false' +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: 'false' +AlwaysBreakTemplateDeclarations: 'Yes' +BinPackArguments: 'false' +BinPackParameters: 'false' +BreakBeforeBinaryOperators: NonAssignment +BreakBeforeBraces: Custom +BreakBeforeTernaryOperators: 'true' +BreakConstructorInitializers: AfterColon +BreakInheritanceList: AfterColon +BreakStringLiterals: 'true' +ColumnLimit: '110' +CompactNamespaces: 'false' +ConstructorInitializerAllOnOneLineOrOnePerLine: 'true' +ConstructorInitializerIndentWidth: '4' +ContinuationIndentWidth: '4' +Cpp11BracedListStyle: 'false' +DerivePointerAlignment: 'false' +FixNamespaceComments: 'true' +IncludeBlocks: Regroup +IndentCaseLabels: true +IndentPPDirectives: BeforeHash +IndentWidth: '4' +IndentWrappedFunctionNames: 'false' +Language: Cpp +MaxEmptyLinesToKeep: '1' +NamespaceIndentation: Inner +PenaltyBreakAssignment: '0' +PointerAlignment: Left +ReflowComments: 'true' +SortIncludes: 'true' +SortUsingDeclarations: 'true' +SpaceAfterCStyleCast: 'true' +SpaceAfterLogicalNot: 'false' +SpaceAfterTemplateKeyword: 'true' +SpaceBeforeAssignmentOperators: 'true' +SpaceBeforeCpp11BracedList: 'true' +SpaceBeforeCtorInitializerColon: 'false' +SpaceBeforeInheritanceColon: 'false' +SpaceBeforeParens: ControlStatements +SpaceBeforeRangeBasedForLoopColon: 'false' +SpaceInEmptyParentheses: 'false' +SpacesInAngles: 'false' +SpacesInCStyleCastParentheses: 'false' +SpacesInContainerLiterals: 'false' +SpacesInParentheses: 'false' +SpacesInSquareBrackets: 'false' +Standard: Cpp11 +TabWidth: '4' +UseTab: Never +IncludeCategories: + - Regex: '^<(contour)/' + Priority: 0 + - Regex: '^<(vtbackend)/' + Priority: 1 + - Regex: '^<(vtparser)/' + Priority: 2 + - Regex: '^<(vtpty)/' + Priority: 3 + - Regex: '^<(vtrasterizer)/' + Priority: 4 + - Regex: '^<(text_shaper)/' + Priority: 5 + - Regex: '^<(crispy)/' + Priority: 6 + - Regex: '^<(libunicode)/' + Priority: 40 + - Regex: '^<(fmt)/' + Priority: 42 + - Regex: '^<(yaml-cpp)/' + Priority: 44 + - Regex: '^<(range)/' + Priority: 46 + - Regex: '^' + Priority: 81 + - Regex: '<[[:alnum:]_]+\.h>' + Priority: 82 + - Regex: '.*' + Priority: 99 diff --git a/.editorconfig b/.editorconfig index 409c923..791c869 100644 --- a/.editorconfig +++ b/.editorconfig @@ -7,3 +7,14 @@ insert_final_newline = true end_of_line = lf charset = utf-8 trim_trailing_whitespace = true + +[LICENSE.txt] +end_of_line = unset +indent_size = unset +insert_final_newline = unset + +[.clang-format] +indent_size = 2 + +[*.yml] +indent_size = 2 diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 2d92e60..b4ef19d 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -9,7 +9,27 @@ on: - master jobs: - ubuntu_2204: + check_clang_format: + name: "Check C++ style" + runs-on: ubuntu-22.04 + steps: + - uses: actions/checkout@v3 + - name: Run clang-format style check for C/C++/Protobuf programs. + uses: jidicula/clang-format-action@v4.11.0 + with: + clang-format-version: '16' + check-path: '.' + #exclude-regex: 'sse2neon.h' + + editorconfig: + name: "Check editorconfig" + runs-on: ubuntu-22.04 + steps: + - uses: actions/checkout@v2 + - uses: editorconfig-checker/action-editorconfig-checker@main + - run: editorconfig-checker + + ubuntu: strategy: fail-fast: false matrix: @@ -17,8 +37,8 @@ jobs: build_type: ["RelWithDebInfo"] compiler: [ - "g++-12", - "clang++-14" + "g++-11", + "clang++-15" ] name: "Ubuntu 22.04 (${{ matrix.compiler }}, C++${{ matrix.cxx }}, ${{matrix.build_type}})" runs-on: ubuntu-22.04 @@ -34,15 +54,30 @@ jobs: - name: "update APT database" run: sudo apt -q update - - name: Install Compilers - run: sudo apt install -y g++-11 clang-14 + - name: Set up Clang + uses: egor-tensin/setup-clang@v1 + with: + version: 15 - name: "Download dependencies" - run: sudo apt install cmake ninja-build + run: sudo apt install cmake ninja-build # catch2 + # workaround for broken clang on ubuntu runner until https://github.com/actions/runner-images/issues/8659 get fixed + #- uses: mjp41/workaround8649@7929373c0fe5caf844d8115adccef39e3b5362e7 + - name: Install Compilers + run: sudo apt install -y g++-12 - name: "Cmake configure" - run: cmake -S . -B build -G Ninja -D BOXED_CPP_TESTS=ON -D ENABLE_TIDY=ON -DPEDANTIC_COMPILER=ON -D CMAKE_CXX_FLAGS="-Wno-unknown-warning-option" -DCMAKE_CXX_COMPILER=${{ matrix.compiler }} -DCMAKE_CXX_STANDARD=${{ matrix.cxx }} -DCMAKE_BUILD_TYPE=${{matrix.build_type}} - - name: "build " + run: | + cmake -S . -B build -G Ninja \ + -DBOXED_CPP_TESTS=OFF \ + -DENABLE_TIDY=ON \ + -DPEDANTIC_COMPILER=ON \ + -DCMAKE_CXX_FLAGS="-Wno-unknown-warning-option" \ + -DCMAKE_CXX_COMPILER=${{ matrix.compiler }} \ + -DCMAKE_CXX_STANDARD=${{ matrix.cxx }} \ + -DCMAKE_BUILD_TYPE=${{matrix.build_type}} + - name: "build" run: cmake --build build --parallel 3 - name: "run test" + if: ${{ false }} # disabled, because of Github runner image bug in compiler-vs-stdlib run: ./build/test-boxed-cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 6b6741f..77084ba 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -54,16 +54,15 @@ install(EXPORT boxed-cpp-targets option(BOXED_CPP_TESTS "Enables building of unittests for boxed-cpp [default: OFF]" OFF) if(BOXED_CPP_TESTS) - find_package(Catch2) - if (NOT Catch2_FOUND) + find_package(Catch2 3.4.0 QUIET) + if(NOT Catch2_FOUND) ThirdPartiesAdd_Catch2() endif() enable_testing() add_executable(test-boxed-cpp test-boxed-cpp.cpp - test-main.cpp ) - target_link_libraries(test-boxed-cpp boxed-cpp Catch2::Catch2) + target_link_libraries(test-boxed-cpp boxed-cpp Catch2::Catch2WithMain) add_test(test-boxed-cpp ./test-boxed-cpp) endif() message(STATUS "[boxed-cpp] Compile unit tests: ${BOXED_CPP_TESTS}") diff --git a/README.md b/README.md index 0369a6d..246fa93 100644 --- a/README.md +++ b/README.md @@ -91,8 +91,8 @@ struct Wrap auto x_coord = Wrap([](rho_type rho){ return unbox(rho); }, - [](theta_type theta){ return sin(unbox(theta)); }, - [](phi_type phi){ return cos(unbox(phi)); } + [](theta_type theta){ return sin(unbox(theta)); }, + [](phi_type phi){ return cos(unbox(phi)); } ); int main() diff --git a/cmake/ThirdParties.cmake b/cmake/ThirdParties.cmake index 5e5ec4a..b3062fd 100644 --- a/cmake/ThirdParties.cmake +++ b/cmake/ThirdParties.cmake @@ -47,8 +47,8 @@ macro(ThirdPartiesAdd_fmtlib) endmacro() macro(ThirdPartiesAdd_Catch2) - set(3rdparty_Catch2_VERSION "2.13.6" CACHE STRING "Embedded catch2 version") - set(3rdparty_Catch2_CHECKSUM "SHA256=48dfbb77b9193653e4e72df9633d2e0383b9b625a47060759668480fdf24fbd4" CACHE STRING "Embedded catch2 checksum") + set(3rdparty_Catch2_VERSION "3.4.0" CACHE STRING "Embedded catch2 version") + set(3rdparty_Catch2_CHECKSUM "SHA256=122928b814b75717316c71af69bd2b43387643ba076a6ec16e7882bfb2dfacbb" CACHE STRING "Embedded catch2 checksum") set(3rdparty_Catch2_URL "https://github.com/catchorg/Catch2/archive/refs/tags/v${3rdparty_Catch2_VERSION}.tar.gz") set(CATCH_BUILD_EXAMPLES OFF CACHE INTERNAL "") set(CATCH_BUILD_EXTRA_TESTS OFF CACHE INTERNAL "") diff --git a/include/boxed-cpp/boxed.hpp b/include/boxed-cpp/boxed.hpp index 97d0812..2301c87 100644 --- a/include/boxed-cpp/boxed.hpp +++ b/include/boxed-cpp/boxed.hpp @@ -1,13 +1,12 @@ // SPDX-License-Identifier: Apache-2.0 #pragma once -#include -#include -#include -#include #include +#include +#include -namespace boxed { +namespace boxed +{ // {{{ forward decls template struct boxed; @@ -38,8 +37,12 @@ constexpr bool is_boxed = helper::is_boxed::value; template struct boxed { - static_assert(std::is_enum_v || std::is_integral_v || std::is_floating_point_v, - "Boxing is only useful on integral & floating point types."); + // clang-format off + static_assert( + std::is_enum_v || std::is_integral_v || std::is_floating_point_v, + "Boxing is only useful on integral & floating point types." + ); + // clang-format on using inner_type = T; using element_type = T; @@ -56,7 +59,7 @@ struct boxed [[nodiscard]] constexpr T& get() noexcept { return value; } [[nodiscard]] constexpr T const& get() const noexcept { return value; } - constexpr operator T() const && {return value;} + constexpr operator T() const&& { return value; } template [[nodiscard]] constexpr auto as() const noexcept @@ -158,10 +161,7 @@ constexpr auto unbox(boxed::boxed const& from) noexcept } template -concept con_boxed = requires(T t) -{ - typename T::inner_type; -}; +concept con_boxed = requires(T t) { typename T::inner_type; }; // Casting a boxed type out of the box. template @@ -203,20 +203,30 @@ struct hash> }; } // namespace std // {{{ fmtlib integration +// clang-format off #if __has_include() + #include +// clang-format on + namespace fmt { - template - struct formatter> { - template - constexpr auto parse(ParseContext& ctx) { return ctx.begin(); } - template - auto format(const boxed::boxed _value, FormatContext& ctx) - { - return fmt::format_to(ctx.out(), "{}", _value.value); - } - }; -} + +template +struct formatter> +{ + template + constexpr auto parse(ParseContext& ctx) + { + return ctx.begin(); + } + template + auto format(const boxed::boxed _value, FormatContext& ctx) + { + return fmt::format_to(ctx.out(), "{}", _value.value); + } +}; + +} // namespace fmt #endif // }}} diff --git a/test-boxed-cpp.cpp b/test-boxed-cpp.cpp index f299639..c53ed52 100644 --- a/test-boxed-cpp.cpp +++ b/test-boxed-cpp.cpp @@ -1,45 +1,54 @@ // SPDX-License-Identifier: Apache-2.0 -#include -#include +#include + +#include +#include #include #include +#include + +// clang-format off namespace tags { struct Length{}; struct From{}; struct To{}; } using Length = boxed::boxed; using From = boxed::boxed; using To = boxed::boxed; struct Range { From from; To to; }; +// clang-format on constexpr Length length(Range range) noexcept { auto result = static_cast(range.to.value - range.from.value); ++result; - return Length{static_cast(result)}; + return Length { static_cast(result) }; } TEST_CASE("boxed") { - auto constexpr r = Range{ From{2}, To{4} }; + auto constexpr r = Range { From { 2 }, To { 4 } }; auto constexpr l = length(r); - static_assert(l == Length{3}); + static_assert(l == Length { 3 }); } TEST_CASE("boxed_cast") { - auto constexpr f = From{3}; + auto constexpr f = From { 3 }; auto constexpr t = boxed_cast(f); static_assert(*f == *t); static_assert(std::is_same_v); } -namespace tags { struct N{}; struct Z{}; } +// clang-format off +namespace tags { struct N {}; struct Z {}; } +// clang-format on + using N = boxed::boxed; using Z = boxed::boxed; TEST_CASE("boxed_cast with different inner types") { - auto constexpr a = N{3}; + auto constexpr a = N { 3 }; auto constexpr b = boxed_cast(a); #ifndef __GNUG__ static_assert(*a == *b); @@ -47,25 +56,28 @@ TEST_CASE("boxed_cast with different inner types") static_assert(std::is_same_v); } -struct Speed_tag{}; -struct Permittivity_tag{}; +struct Speed_tag +{ +}; +struct Permittivity_tag +{ +}; struct Permeability_tag; -using Speed = boxed::boxed; -using Permittivity = boxed::boxed; -using Permeability = boxed::boxed; +using Speed = boxed::boxed; +using Permittivity = boxed::boxed; +using Permeability = boxed::boxed; TEST_CASE("function with boxed variables") { - auto wave_speed = [](Permittivity epsilon, Permeability mu) -> Speed - { + auto wave_speed = [](Permittivity epsilon, Permeability mu) -> Speed { return Speed(1.0 / std::sqrt(unbox(epsilon) * unbox(mu))); }; - REQUIRE(wave_speed(Permittivity(1.0),Permeability(1.0)) == Speed(1.0)); + REQUIRE(wave_speed(Permittivity(1.0), Permeability(1.0)) == Speed(1.0)); auto speed_of_light = Speed(299792458.0); auto vacuum_permittivity = Permittivity(8.85418781762039e-12); auto pi = 3.14159265358979323846; auto vacuum_permeability = Permeability(4 * pi * 1e-7); - auto value = std::abs(unbox( wave_speed(vacuum_permittivity, vacuum_permeability) - speed_of_light )); + auto value = std::abs(unbox(wave_speed(vacuum_permittivity, vacuum_permeability) - speed_of_light)); REQUIRE(value < std::numeric_limits::epsilon()); } @@ -73,18 +85,17 @@ TEST_CASE("unbox types check") { auto speed_of_light = Speed(299792458.0); auto speed_value_native = unbox(speed_of_light); - static_assert(std::is_same_v); + static_assert(std::is_same_v); auto speed_value_float = unbox(speed_of_light); - static_assert(std::is_same_v); + static_assert(std::is_same_v); auto speed_value_int = unbox(speed_of_light); - static_assert(std::is_same_v); + static_assert(std::is_same_v); } - TEST_CASE("unbox without template parameters to initial type") { auto speed_of_light = Speed(299792458.0); - REQUIRE( std::abs(unbox(speed_of_light) - 299792458.0) < std::numeric_limits::epsilon()); + REQUIRE(std::abs(unbox(speed_of_light) - 299792458.0) < std::numeric_limits::epsilon()); } TEST_CASE("cast inside rvalue") @@ -93,11 +104,11 @@ TEST_CASE("cast inside rvalue") auto distance_auto_right = speed_of_light * 2.0; auto distance_auto_left = 2.0 * speed_of_light; - static_assert(std::is_same_v); - static_assert(std::is_same_v); + static_assert(std::is_same_v); + static_assert(std::is_same_v); double distance_d_right = speed_of_light * 2.0; - double distance_d_left = 2.0 * speed_of_light; + double distance_d_left = 2.0 * speed_of_light; REQUIRE(distance_d_right - 2.0 * 299792458.0 < std::numeric_limits::epsilon()); REQUIRE(distance_d_left - 2.0 * 299792458.0 < std::numeric_limits::epsilon()); } @@ -112,14 +123,15 @@ TEST_CASE("all options for unbox") // advanced usage test +// clang-format off template - struct not_same{}; + struct not_same{}; template - struct not_same : virtual std::false_type {}; + struct not_same : virtual std::false_type {}; template - struct not_same : virtual std::true_type {} ; + struct not_same : virtual std::true_type {} ; template struct all_different : std::false_type {}; @@ -138,62 +150,60 @@ namespace Tag{ struct Rho{}; struct Theta{}; struct Phi{};} using rho_type = boxed::boxed; using theta_type = boxed::boxed; using phi_type = boxed::boxed; +// clang-format on +template +struct Wrap +{ +}; -template -struct Wrap{}; - -template -struct Wrap +template +struct Wrap { constexpr static inline std::size_t n = 1 + sizeof...(Rest); using fun_type = std::function; - Wrap(fun_type&& first, std::function&& ...rest) - : first(std::forward(first)) - , rest(std::forward>(rest)...) - {} + Wrap(fun_type&& first, std::function&&... rest): + first(std::forward(first)), rest(std::forward>(rest)...) + { + } const fun_type first; Wrap rest; - auto operator()(T v) - { - return first(v); - } + auto operator()(T v) { return first(v); } - template - requires (!std::is_same_v) + template + requires(!std::is_same_v) decltype(auto) operator()(F v) { return rest(v); } - - template - requires (!std::derived_from::type...>, std::false_type>) - decltype(auto) operator()(Args &&... args) + template + requires(!std::derived_from::type...>, std::false_type>) + decltype(auto) operator()(Args&&... args) { - static_assert( (sizeof...(Args) == n) ); - return ( operator()(std::forward(args)) * ... ); + static_assert((sizeof...(Args) == n)); + return (operator()(std::forward(args)) * ...); } }; -auto x_coord = Wrap{ - [](rho_type rho){ return unbox(rho); }, - [](theta_type theta){ return sin(unbox(theta)); }, - [](phi_type phi){ return cos(unbox(phi)); } - }; - +// clang-format off +auto x_coord = Wrap { + [](rho_type rho) { return unbox(rho); }, + [](theta_type theta) { return sin(unbox(theta)); }, + [](phi_type phi) { return cos(unbox(phi)); } +}; +// clang-format on TEST_CASE("advanced usage") { - rho_type rho{1.0}; - theta_type theta{3.14 / 3.0}; - phi_type phi{3.14/2.0}; - + rho_type rho { 1.0 }; + theta_type theta { 3.14 / 3.0 }; + phi_type phi { 3.14 / 2.0 }; - REQUIRE(x_coord(rho,theta,phi) == x_coord(theta,rho,phi)); - REQUIRE(x_coord(rho,theta,phi) == x_coord(phi,rho,theta)); + REQUIRE(x_coord(rho, theta, phi) == x_coord(theta, rho, phi)); + REQUIRE(x_coord(rho, theta, phi) == x_coord(phi, rho, theta)); } diff --git a/test-main.cpp b/test-main.cpp deleted file mode 100644 index 9173a48..0000000 --- a/test-main.cpp +++ /dev/null @@ -1,3 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -#define CATCH_CONFIG_MAIN -#include