diff --git a/.clang-format b/.clang-format new file mode 100644 index 00000000..b0e1de10 --- /dev/null +++ b/.clang-format @@ -0,0 +1,211 @@ +# TODO Improve this; currently it's not much more than a dump of LLVM style + +--- +Language: Cpp +# BasedOnStyle: LLVM +AccessModifierOffset: -2 +AlignAfterOpenBracket: Align +AlignArrayOfStructures: None +AlignConsecutiveAssignments: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + PadOperators: true +AlignConsecutiveBitFields: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + PadOperators: false +AlignConsecutiveDeclarations: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + PadOperators: false +AlignConsecutiveMacros: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + PadOperators: false +AlignEscapedNewlines: Right +AlignOperands: Align +AlignTrailingComments: true +AllowAllArgumentsOnNextLine: true +AllowAllParametersOfDeclarationOnNextLine: true +AllowShortBlocksOnASingleLine: Never +AllowShortCaseLabelsOnASingleLine: false +AllowShortEnumsOnASingleLine: true +AllowShortFunctionsOnASingleLine: All +AllowShortIfStatementsOnASingleLine: Never +AllowShortLambdasOnASingleLine: All +AllowShortLoopsOnASingleLine: false +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: false +AlwaysBreakTemplateDeclarations: MultiLine +AttributeMacros: + - __capability +BinPackArguments: true +BinPackParameters: true +BitFieldColonSpacing: Both +BraceWrapping: + AfterCaseLabel: false + AfterClass: false + AfterControlStatement: Never + AfterEnum: false + AfterExternBlock: false + AfterFunction: false + AfterNamespace: false + AfterObjCDeclaration: false + AfterStruct: false + AfterUnion: false + BeforeCatch: false + BeforeElse: false + BeforeLambdaBody: false + BeforeWhile: false + IndentBraces: false + SplitEmptyFunction: true + SplitEmptyRecord: true + SplitEmptyNamespace: true +BreakAfterJavaFieldAnnotations: false +BreakBeforeBinaryOperators: None +BreakBeforeConceptDeclarations: Always +BreakBeforeBraces: WebKit +BreakBeforeTernaryOperators: true +BreakConstructorInitializers: BeforeColon +BreakInheritanceList: BeforeColon +BreakStringLiterals: true +ColumnLimit: 80 +CommentPragmas: '^ IWYU pragma:' +CompactNamespaces: false +ConstructorInitializerIndentWidth: 4 +ContinuationIndentWidth: 4 +Cpp11BracedListStyle: true +DerivePointerAlignment: false +DisableFormat: false +EmptyLineAfterAccessModifier: Never +EmptyLineBeforeAccessModifier: LogicalBlock +ExperimentalAutoDetectBinPacking: false +FixNamespaceComments: true +ForEachMacros: + - foreach + - Q_FOREACH + - BOOST_FOREACH +IfMacros: + - KJ_IF_MAYBE +IncludeBlocks: Preserve +IncludeCategories: + - Regex: '^"(llvm|llvm-c|clang|clang-c)/' + Priority: 2 + SortPriority: 0 + CaseSensitive: false + - Regex: '^(<|"(gtest|gmock|isl|json)/)' + Priority: 3 + SortPriority: 0 + CaseSensitive: false + - Regex: '.*' + Priority: 1 + SortPriority: 0 + CaseSensitive: false +IncludeIsMainRegex: '(Test)?$' +IncludeIsMainSourceRegex: '' +IndentAccessModifiers: false +IndentCaseBlocks: false +IndentCaseLabels: false +IndentExternBlock: AfterExternBlock +IndentGotoLabels: true +IndentPPDirectives: None +IndentRequiresClause: true +IndentWidth: 2 +IndentWrappedFunctionNames: false +InsertBraces: false +InsertTrailingCommas: None +JavaScriptQuotes: Leave +JavaScriptWrapImports: true +KeepEmptyLinesAtTheStartOfBlocks: true +LambdaBodyIndentation: Signature +MacroBlockBegin: '' +MacroBlockEnd: '' +MaxEmptyLinesToKeep: 1 +NamespaceIndentation: None +ObjCBinPackProtocolList: Auto +ObjCBlockIndentWidth: 2 +ObjCBreakBeforeNestedBlockParam: true +ObjCSpaceAfterProperty: false +ObjCSpaceBeforeProtocolList: true +PackConstructorInitializers: BinPack +PenaltyBreakAssignment: 2 +PenaltyBreakBeforeFirstCallParameter: 19 +PenaltyBreakComment: 300 +PenaltyBreakFirstLessLess: 120 +PenaltyBreakOpenParenthesis: 0 +PenaltyBreakString: 1000 +PenaltyBreakTemplateDeclaration: 10 +PenaltyExcessCharacter: 1000000 +PenaltyIndentedWhitespace: 0 +PenaltyReturnTypeOnItsOwnLine: 60 +PointerAlignment: Right +PPIndentWidth: -1 +QualifierAlignment: Leave +ReferenceAlignment: Pointer +ReflowComments: true +RemoveBracesLLVM: false +RequiresClausePosition: OwnLine +SeparateDefinitionBlocks: Leave +ShortNamespaceLines: 1 +SortIncludes: CaseSensitive +SortJavaStaticImport: Before +SortUsingDeclarations: true +SpaceAfterCStyleCast: false +SpaceAfterLogicalNot: false +SpaceAfterTemplateKeyword: true +SpaceAroundPointerQualifiers: Default +SpaceBeforeAssignmentOperators: true +SpaceBeforeCaseColon: false +SpaceBeforeCpp11BracedList: false +SpaceBeforeCtorInitializerColon: true +SpaceBeforeInheritanceColon: true +SpaceBeforeParens: ControlStatements +SpaceBeforeParensOptions: + AfterControlStatements: true + AfterForeachMacros: true + AfterFunctionDefinitionName: false + AfterFunctionDeclarationName: false + AfterIfMacros: true + AfterOverloadedOperator: false + AfterRequiresInClause: false + AfterRequiresInExpression: false + BeforeNonEmptyParentheses: false +SpaceBeforeRangeBasedForLoopColon: true +SpaceBeforeSquareBrackets: false +SpaceInEmptyBlock: false +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 1 +SpacesInAngles: Never +SpacesInConditionalStatement: false +SpacesInContainerLiterals: true +SpacesInCStyleCastParentheses: false +SpacesInLineCommentPrefix: + Minimum: 1 + Maximum: -1 +SpacesInParentheses: false +SpacesInSquareBrackets: false +Standard: Latest +StatementAttributeLikeMacros: + - Q_EMIT +StatementMacros: + - Q_UNUSED + - QT_REQUIRE_VERSION +TabWidth: 8 +UseTab: Never +WhitespaceSensitiveMacros: + - BOOST_PP_STRINGIZE + - CF_SWIFT_NAME + - NS_SWIFT_NAME + - PP_STRINGIZE + - STRINGIZE +... + diff --git a/.github/workflows/clang-format.yml b/.github/workflows/clang-format.yml new file mode 100644 index 00000000..5d9d35b5 --- /dev/null +++ b/.github/workflows/clang-format.yml @@ -0,0 +1,56 @@ +name: clang-format + +on: [push, pull_request] + +jobs: + check: + runs-on: ubuntu-22.04 + env: + # TODO Bump when newer ubuntu version is available + CLANG_VERSION: 15 + steps: + - uses: actions/checkout@v3 + - name: Install clang-format + env: + DEBIAN_FRONTEND: noninteractive + run: | + sudo apt-get update + sudo apt-get install -y --no-install-recommends clang-format-${CLANG_VERSION} + sudo apt-get clean + - name: Format source files + run: find include tests -type f \( -name '*.cpp' -o -name '*.hpp' -o -name '*.ipp' \) -print0 | xargs -0 clang-format-${CLANG_VERSION} -i + - name: Check for differences + id: assert + run: | + set -o pipefail + git diff --exit-code | tee "clang-format.patch" + - name: Upload patch + if: failure() && steps.assert.outcome == 'failure' + uses: actions/upload-artifact@v2 + continue-on-error: true + with: + name: clang-format.patch + if-no-files-found: ignore + path: clang-format.patch + - name: What happened? + if: failure() && steps.assert.outcome == 'failure' + env: + PREAMBLE: | + If you are reading this, you are looking at a failed Github Actions + job. That means you pushed one or more source files that did not conform + to the formatting specified in .clang-format. That may be because + you neglected to run 'git clang-format' or 'clang-format' before + committing, or perhaps your version of clang-format is not compatible + with the one on this machine, which is: + SUGGESTION: | + + To fix it, you can do one of two things: + 1. Download and apply the patch generated as an artifact of this + job to your repo, commit, and push. + 2. Run 'git-clang-format --extensions cpp,hpp,ipp main' + in your repo, commit, and push. + run: | + echo "${PREAMBLE}" + clang-format-${CLANG_VERSION} --version + echo "${SUGGESTION}" + exit 1 diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 00000000..dfa220fc --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "external/catch2"] + path = external/catch2 + url = git@github.com:catchorg/Catch2.git diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 00000000..526ce28d --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,15 @@ +cmake_minimum_required(VERSION 3.25) +project(functional VERSION 0.0.1 LANGUAGES CXX) + +set(CMAKE_CXX_EXTENSIONS OFF) +set(CMAKE_CXX_STANDARD 23) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native -Wall -Wextra -Wpedantic") + +set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH}) +include(Ccache) + +add_subdirectory(include) + +enable_testing() +add_subdirectory(tests) diff --git a/cmake/Ccache.cmake b/cmake/Ccache.cmake new file mode 100644 index 00000000..0095d9e4 --- /dev/null +++ b/cmake/Ccache.cmake @@ -0,0 +1,13 @@ +# Simple ccache detection + +if((DEFINED ENV{DISABLE_CCACHE_DETECTION}) OR (DEFINED DISABLE_CCACHE_DETECTION)) + message(VERBOSE "Suppressing ccache detection: ${DISABLE_CCACHE_DETECTION}") +elseif(DEFINED CMAKE_CXX_COMPILER_LAUNCHER) + message(VERBOSE "Explicitly set compiler launcher: ${CMAKE_CXX_COMPILER_LAUNCHER}") +else() + find_program(CCACHE_FOUND "ccache") + if(CCACHE_FOUND) + set(CMAKE_CXX_COMPILER_LAUNCHER "${CCACHE_FOUND}") + message(VERBOSE "Found ccache at: ${CCACHE_FOUND}") + endif(CCACHE_FOUND) +endif() diff --git a/external/catch2 b/external/catch2 new file mode 160000 index 00000000..f981c9cb --- /dev/null +++ b/external/catch2 @@ -0,0 +1 @@ +Subproject commit f981c9cbcac07a2690e5a86767eba490b5465463 diff --git a/include/CMakeLists.txt b/include/CMakeLists.txt new file mode 100644 index 00000000..60e74b05 --- /dev/null +++ b/include/CMakeLists.txt @@ -0,0 +1,20 @@ +cmake_minimum_required(VERSION 3.25) +project(include) + +set(CMAKE_CXX_EXTENSIONS OFF) +set(CMAKE_CXX_STANDARD 23) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +# Pls keep the filenames sorted +set(SOURCE_FILES + functional/and_then.hpp + functional/fwd.hpp + functional/operator.hpp + functional/or_else.hpp + functional/recover.hpp + functional/transform_error.hpp + functional/transform.hpp +) + +add_library(${PROJECT_NAME} INTERFACE ${SOURCE_FILES}) +target_include_directories(${PROJECT_NAME} INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}) diff --git a/include/functional/and_then.hpp b/include/functional/and_then.hpp new file mode 100644 index 00000000..e69de29b diff --git a/include/functional/fwd.hpp b/include/functional/fwd.hpp new file mode 100644 index 00000000..e69de29b diff --git a/include/functional/operator.hpp b/include/functional/operator.hpp new file mode 100644 index 00000000..e69de29b diff --git a/include/functional/or_else.hpp b/include/functional/or_else.hpp new file mode 100644 index 00000000..e69de29b diff --git a/include/functional/recover.hpp b/include/functional/recover.hpp new file mode 100644 index 00000000..e69de29b diff --git a/include/functional/transform.hpp b/include/functional/transform.hpp new file mode 100644 index 00000000..e69de29b diff --git a/include/functional/transform_error.hpp b/include/functional/transform_error.hpp new file mode 100644 index 00000000..e69de29b diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 00000000..89574d51 --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,10 @@ +cmake_minimum_required(VERSION 3.25) + +set(CMAKE_CXX_EXTENSIONS OFF) +set(CMAKE_CXX_STANDARD 23) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +add_subdirectory(catch2) +add_subdirectory(main) +add_subdirectory(tests) +add_subdirectory(examples) diff --git a/tests/catch2 b/tests/catch2 new file mode 120000 index 00000000..48e7b8e3 --- /dev/null +++ b/tests/catch2 @@ -0,0 +1 @@ +../external/catch2 \ No newline at end of file diff --git a/tests/examples/CMakeLists.txt b/tests/examples/CMakeLists.txt new file mode 100644 index 00000000..ae3671f4 --- /dev/null +++ b/tests/examples/CMakeLists.txt @@ -0,0 +1,15 @@ +cmake_minimum_required(VERSION 3.25) +project(examples) + +set(CMAKE_CXX_EXTENSIONS OFF) +set(CMAKE_CXX_STANDARD 23) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +# Pls keep the filenames sorted +set(SOURCE_FILES + dummy.cpp +) +add_executable(${PROJECT_NAME} ${SOURCE_FILES}) +target_link_libraries(${PROJECT_NAME} include catch2_main Catch2::Catch2) + +add_test(NAME ${PROJECT_NAME} COMMAND ${PROJECT_NAME} -r junit) diff --git a/tests/examples/dummy.cpp b/tests/examples/dummy.cpp new file mode 100644 index 00000000..87ef5aa5 --- /dev/null +++ b/tests/examples/dummy.cpp @@ -0,0 +1,13 @@ +// Copyright (c) 2024 Bronek Kozicki +// +// Distributed under the ISC License. See accompanying file LICENSE.md +// or copy at https://opensource.org/licenses/ISC + +#include "functional/fwd.hpp" + +#include + +TEST_CASE("Dummy example") +{ // + SUCCEED(); +} diff --git a/tests/main/CMakeLists.txt b/tests/main/CMakeLists.txt new file mode 100644 index 00000000..032f25d2 --- /dev/null +++ b/tests/main/CMakeLists.txt @@ -0,0 +1,10 @@ +cmake_minimum_required(VERSION 3.25) +project(catch2_main) + +set(CMAKE_CXX_EXTENSIONS OFF) +set(CMAKE_CXX_STANDARD 23) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +set(SOURCE_FILES main.cpp) +add_library(${PROJECT_NAME} ${SOURCE_FILES}) +target_link_libraries(${PROJECT_NAME} PUBLIC Catch2::Catch2) diff --git a/tests/main/main.cpp b/tests/main/main.cpp new file mode 100644 index 00000000..3ada6145 --- /dev/null +++ b/tests/main/main.cpp @@ -0,0 +1,11 @@ +// Copyright (c) 2024 Bronek Kozicki +// +// Distributed under the ISC License. See accompanying file LICENSE.md +// or copy at https://opensource.org/licenses/ISC + +#include + +int main(int argc, char *argv[]) +{ // + return Catch::Session().run(argc, argv); +} diff --git a/tests/tests/CMakeLists.txt b/tests/tests/CMakeLists.txt new file mode 100644 index 00000000..505b5a1a --- /dev/null +++ b/tests/tests/CMakeLists.txt @@ -0,0 +1,15 @@ +cmake_minimum_required(VERSION 3.25) +project(tests) + +set(CMAKE_CXX_EXTENSIONS OFF) +set(CMAKE_CXX_STANDARD 23) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +# Pls keep the filenames sorted +set(SOURCE_FILES + dummy.cpp +) +add_executable(${PROJECT_NAME} ${SOURCE_FILES}) +target_link_libraries(${PROJECT_NAME} include catch2_main Catch2::Catch2) + +add_test(NAME ${PROJECT_NAME} COMMAND ${PROJECT_NAME} -r junit) diff --git a/tests/tests/dummy.cpp b/tests/tests/dummy.cpp new file mode 100644 index 00000000..84be1d6c --- /dev/null +++ b/tests/tests/dummy.cpp @@ -0,0 +1,13 @@ +// Copyright (c) 2024 Bronek Kozicki +// +// Distributed under the ISC License. See accompanying file LICENSE.md +// or copy at https://opensource.org/licenses/ISC + +#include "functional/fwd.hpp" + +#include + +TEST_CASE("Dummy test") +{ // + SUCCEED(); +}