diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index 5008ddfcf..000000000 Binary files a/.DS_Store and /dev/null differ diff --git a/.clang-format b/.clang-format index d61f342e7..80f4f718b 100644 --- a/.clang-format +++ b/.clang-format @@ -1,5 +1,5 @@ --- -BasedOnStyle: Google +BasedOnStyle: Google AccessModifierOffset: -2 ConstructorInitializerIndentWidth: 2 AlignEscapedNewlinesLeft: false @@ -8,14 +8,13 @@ AllowAllParametersOfDeclarationOnNextLine: false AllowShortIfStatementsOnASingleLine: false AllowShortLoopsOnASingleLine: false AllowShortFunctionsOnASingleLine: None -AllowShortLoopsOnASingleLine: false AlwaysBreakTemplateDeclarations: true AlwaysBreakBeforeMultilineStrings: false BreakBeforeBinaryOperators: false BreakBeforeTernaryOperators: false -BreakConstructorInitializersBeforeComma: true +BreakConstructorInitializers: BeforeComma BinPackParameters: true -ColumnLimit: 100 +ColumnLimit: 90 ConstructorInitializerAllOnOneLineOrOnePerLine: true DerivePointerBinding: false PointerBindsToType: true @@ -30,19 +29,20 @@ PenaltyBreakString: 1 PenaltyBreakFirstLessLess: 1000 PenaltyExcessCharacter: 1000 PenaltyReturnTypeOnItsOwnLine: 90 -SpacesBeforeTrailingComments: 3 -Cpp11BracedListStyle: true -Standard: Auto -IndentWidth: 4 -TabWidth: 4 -UseTab: Never +SpacesBeforeTrailingComments: 2 +Cpp11BracedListStyle: false +Standard: Auto +IndentWidth: 2 +TabWidth: 2 +UseTab: Never IndentFunctionDeclarationAfterType: false SpacesInParentheses: false -SpacesInAngles: false +SpacesInAngles: false SpaceInEmptyParentheses: false SpacesInCStyleCastParentheses: false SpaceAfterControlStatementKeyword: true SpaceBeforeAssignmentOperators: true +SpaceBeforeParens: Never ContinuationIndentWidth: 4 SortIncludes: false SpaceAfterCStyleCast: false @@ -53,16 +53,16 @@ BreakBeforeBraces: Custom # Control of individual brace wrapping cases BraceWrapping: { - AfterClass: 'true' - AfterControlStatement: 'true' - AfterEnum : 'true' - AfterFunction : 'true' - AfterNamespace : 'true' - AfterStruct : 'true' - AfterUnion : 'true' - BeforeCatch : 'true' - BeforeElse : 'true' - IndentBraces : 'false' + AfterClass: 'true', + AfterControlStatement: 'true', + AfterEnum : 'true', + AfterFunction : 'true', + AfterNamespace : 'true', + AfterStruct : 'true', + AfterUnion : 'true', + BeforeCatch : 'true', + BeforeElse : 'true', + IndentBraces : 'false', + SplitEmptyFunction: 'false' } ... - diff --git a/.codespell_ignore_words b/.codespell_ignore_words new file mode 100644 index 000000000..ab09b3c2f --- /dev/null +++ b/.codespell_ignore_words @@ -0,0 +1,7 @@ +INOUT +InOut +delimeter +Succesful +worl +valu +Exeption diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md new file mode 100644 index 000000000..9eb010608 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -0,0 +1,19 @@ +--- +name: Bug report +about: Help me help you... +title: '' +labels: '' +assignees: '' + +--- + +**Describe the bug** +A clear and concise description of what the bug is. + +If you are experiencing a crash, provide a backtrace (GDB or similar). + +*How to Reproduce** + +Please provide a specific description of how to reproduce the issue or source code that can be compiled and executed. Please attach a file/project that is easy to compile, don't copy and paste code snippets! + +Even better, create a Pull Request with a failing unit test. diff --git a/.github/ISSUE_TEMPLATE/feature_request.md b/.github/ISSUE_TEMPLATE/feature_request.md new file mode 100644 index 000000000..021458556 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/feature_request.md @@ -0,0 +1,20 @@ +--- +name: Feature request +about: Suggest an idea for this project +title: '' +labels: '' +assignees: '' + +--- + +**Is your feature request related to a problem? Please describe.** +A clear and concise description of what the problem is. + +**Describe the solution you'd like** +A clear and concise description of what you want to happen. + +**Describe alternatives you've considered** +A clear and concise description of any alternative solutions or features you've considered. + +**Additional context** +Add any other context or screenshots about the feature request here. diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md new file mode 100644 index 000000000..27c6ae66a --- /dev/null +++ b/.github/pull_request_template.md @@ -0,0 +1,11 @@ + diff --git a/.github/workflows/build_ubuntu.yml b/.github/workflows/build_ubuntu.yml deleted file mode 100644 index 73328e998..000000000 --- a/.github/workflows/build_ubuntu.yml +++ /dev/null @@ -1,28 +0,0 @@ -name: build and run tests -on: - push: - branches: [ master ] - pull_request: - branches: [ master ] -jobs: - build: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - # install dependencies - - name: apt - run: sudo apt-get update && sudo apt-get install -yq libzmq3-dev libdw-dev libgtest-dev cmake - - name: Install gtest manually - working-directory: /usr/src/gtest - run: sudo cmake CMakeLists.txt && sudo make && sudo cp lib/*.a /usr/lib - # build project - - name: mkdir - run: mkdir build - - name: cmake build - run: cmake -Bbuild -H. - - name: cmake make - run: cmake --build build/ --target all - # run tests - - name: run test - run: build/bin/behaviortree_cpp_v3_test - diff --git a/.github/workflows/cmake_ubuntu.yml b/.github/workflows/cmake_ubuntu.yml new file mode 100644 index 000000000..41ed9a196 --- /dev/null +++ b/.github/workflows/cmake_ubuntu.yml @@ -0,0 +1,59 @@ +name: cmake Ubuntu + +on: + push: + branches: + - master + pull_request: + types: [opened, synchronize, reopened] + +env: + # Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.) + BUILD_TYPE: Release + +jobs: + build: + # The CMake configure and build commands are platform agnostic and should work equally + # well on Windows or Mac. You can convert this to a matrix build if you need + # cross-platform coverage. + # See: https://docs.github.com/en/free-pro-team@latest/actions/learn-github-actions/managing-complex-workflows#using-a-build-matrix + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: [ubuntu-22.04] + + steps: + - uses: actions/checkout@v2 + + - name: Install Conan + id: conan + uses: turtlebrowser/get-conan@main + + - name: Create default profile + run: conan profile detect + + - name: Create Build Environment + # Some projects don't allow in-source building, so create a separate build directory + # We'll use this as our working directory for all subsequent commands + run: cmake -E make_directory ${{github.workspace}}/build + + - name: Install conan dependencies + working-directory: ${{github.workspace}}/build + run: conan install ${{github.workspace}}/conanfile.txt -s build_type=${{env.BUILD_TYPE}} --build=missing + + - name: Configure CMake + shell: bash + working-directory: ${{github.workspace}}/build + run: cmake ${{github.workspace}} -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake + + - name: Build + shell: bash + working-directory: ${{github.workspace}}/build + run: cmake --build . --config ${{env.BUILD_TYPE}} + + - name: run test (Linux) + working-directory: ${{github.workspace}}/build/tests + run: ctest + + - name: Upload coverage reports to Codecov + uses: codecov/codecov-action@v3 diff --git a/.github/workflows/cmake_windows.yml b/.github/workflows/cmake_windows.yml new file mode 100644 index 000000000..34f4f97ce --- /dev/null +++ b/.github/workflows/cmake_windows.yml @@ -0,0 +1,56 @@ +name: cmake Windows + +on: + push: + branches: + - master + pull_request: + types: [opened, synchronize, reopened] + +env: + # Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.) + BUILD_TYPE: Release + +jobs: + build: + # The CMake configure and build commands are platform agnostic and should work equally + # well on Windows or Mac. You can convert this to a matrix build if you need + # cross-platform coverage. + # See: https://docs.github.com/en/free-pro-team@latest/actions/learn-github-actions/managing-complex-workflows#using-a-build-matrix + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: [windows-latest] + + steps: + - uses: actions/checkout@v4 + + - name: Install Conan + id: conan + uses: turtlebrowser/get-conan@main + + - name: Create default profile + run: conan profile detect + + - name: Create Build Environment + # Some projects don't allow in-source building, so create a separate build directory + # We'll use this as our working directory for all subsequent commands + run: cmake -E make_directory ${{github.workspace}}/build + + - name: Install conan dependencies + working-directory: ${{github.workspace}}/build + run: conan install ${{github.workspace}}/conanfile.txt -s build_type=${{env.BUILD_TYPE}} --build=missing + + - name: Configure CMake + shell: bash + working-directory: ${{github.workspace}}/build + run: cmake $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake + + - name: Build + working-directory: ${{github.workspace}}/build + shell: bash + run: cmake --build . --config ${{env.BUILD_TYPE}} + + - name: run test (Windows) + working-directory: ${{github.workspace}}/build + run: $env:PATH+=";${{env.BUILD_TYPE}}"; tests/${{env.BUILD_TYPE}}/behaviortree_cpp_test.exe diff --git a/.github/workflows/codeql.yml.bkp b/.github/workflows/codeql.yml.bkp new file mode 100644 index 000000000..a76a623ca --- /dev/null +++ b/.github/workflows/codeql.yml.bkp @@ -0,0 +1,63 @@ +name: "CodeQL" + +on: + push: + branches: [ 'master' ] + pull_request: + # The branches below must be a subset of the branches above + branches: [ 'master' ] + schedule: + - cron: '36 19 * * 2' + +jobs: + analyze: + name: Analyze + runs-on: ubuntu-latest + permissions: + actions: read + contents: read + security-events: write + + strategy: + fail-fast: false + matrix: + language: [ 'cpp' ] + # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ] + # Learn more about CodeQL language support at https://aka.ms/codeql-docs/language-support + + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + # Initializes the CodeQL tools for scanning. + - name: Initialize CodeQL + uses: github/codeql-action/init@v2 + with: + languages: ${{ matrix.language }} + # If you wish to specify custom queries, you can do so here or in a config file. + # By default, queries listed here will override any specified in a config file. + # Prefix the list here with "+" to use these queries and those in the config file. + + # Details on CodeQL's query packs refer to : https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs + queries: +security-and-quality + + + # Autobuild attempts to build any compiled languages (C/C++, C#, Go, or Java). + # If this step fails, then you should remove it and run the build manually (see below) + - name: Autobuild + uses: github/codeql-action/autobuild@v2 + + # ℹ️ Command-line programs to run using the OS shell. + # 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun + + # If the Autobuild fails above, remove it and uncomment the following three lines. + # modify them (or add more) to build your code if your project, please refer to the EXAMPLE below for guidance. + + # - run: | + # echo "Run, Build Application using script" + # ./location_of_script_within_repo/buildscript.sh + + - name: Perform CodeQL Analysis + uses: github/codeql-action/analyze@v2 + with: + category: "/language:${{matrix.language}}" diff --git a/.github/workflows/doxygen-gh-pages.yml b/.github/workflows/doxygen-gh-pages.yml new file mode 100644 index 000000000..b8fb8f46b --- /dev/null +++ b/.github/workflows/doxygen-gh-pages.yml @@ -0,0 +1,18 @@ +name: Doxygen GitHub Pages Deploy Action + +on: + push: + branches: + - main + - master + +jobs: + deploy: + runs-on: ubuntu-latest + permissions: + contents: write + steps: + - uses: DenverCoder1/doxygen-github-pages-action@v2.0.0 + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + folder: doc/html diff --git a/.github/workflows/pixi.yaml b/.github/workflows/pixi.yaml new file mode 100644 index 000000000..ddd1cbfb8 --- /dev/null +++ b/.github/workflows/pixi.yaml @@ -0,0 +1,27 @@ +name: Pixi (conda) + +on: + push: + branches: + - master + pull_request: + types: [opened, synchronize, reopened] + +jobs: + pixi_conda_build: + strategy: + matrix: + os: + - windows-latest + - ubuntu-latest + runs-on: ${{ matrix.os }} + steps: + # Pixi is the tool used to create/manage conda environment + - uses: actions/checkout@v3 + - uses: prefix-dev/setup-pixi@v0.8.1 + with: + pixi-version: v0.40.3 + - name: Build + run: pixi run build + - name: Run tests + run: pixi run test diff --git a/.github/workflows/pre-commit.yaml b/.github/workflows/pre-commit.yaml new file mode 100644 index 000000000..ee7fa9229 --- /dev/null +++ b/.github/workflows/pre-commit.yaml @@ -0,0 +1,16 @@ +name: pre-commit + +on: + push: + branches: + - master + pull_request: + types: [opened, synchronize, reopened] + +jobs: + pre-commit: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-python@v3 + - uses: pre-commit/action@v3.0.1 diff --git a/.github/workflows/ros1.yaml b/.github/workflows/ros1.yaml deleted file mode 100644 index e6d2f9be2..000000000 --- a/.github/workflows/ros1.yaml +++ /dev/null @@ -1,18 +0,0 @@ -name: ros1 - -on: [push, pull_request] - -jobs: - industrial_ci: - strategy: - matrix: - env: - - {ROS_DISTRO: melodic, ROS_REPO: main} - - {ROS_DISTRO: kinetic, ROS_REPO: main} - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v1 - - uses: 'ros-industrial/industrial_ci@master' - env: ${{matrix.env}} - with: - package-name: behaviortree_cpp_v3 diff --git a/.github/workflows/ros2-rolling.yaml b/.github/workflows/ros2-rolling.yaml new file mode 100644 index 000000000..446c49879 --- /dev/null +++ b/.github/workflows/ros2-rolling.yaml @@ -0,0 +1,22 @@ +name: ros2-rolling + +on: + push: + branches: + - master + pull_request: + types: [opened, synchronize, reopened] + +jobs: + industrial_ci: + strategy: + matrix: + env: + - {ROS_DISTRO: rolling, ROS_REPO: main} + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: 'ros-industrial/industrial_ci@master' + env: ${{matrix.env}} + with: + package-name: plotjuggler diff --git a/.github/workflows/ros2.yaml b/.github/workflows/ros2.yaml index 9f8c5e764..099cc04f2 100644 --- a/.github/workflows/ros2.yaml +++ b/.github/workflows/ros2.yaml @@ -1,17 +1,23 @@ name: ros2 -on: [push, pull_request] +on: + push: + branches: + - master + pull_request: + types: [opened, synchronize, reopened] jobs: industrial_ci: strategy: matrix: env: - - {ROS_DISTRO: eloquent, ROS_REPO: main} + - {ROS_DISTRO: humble, ROS_REPO: main} + - {ROS_DISTRO: jazzy, ROS_REPO: main} runs-on: ubuntu-latest steps: - - uses: actions/checkout@v1 + - uses: actions/checkout@v3 - uses: 'ros-industrial/industrial_ci@master' env: ${{matrix.env}} with: - package-name: behaviortree_cpp_v3 + package-name: plotjuggler diff --git a/.github/workflows/sonarcube.yml.bkp b/.github/workflows/sonarcube.yml.bkp new file mode 100644 index 000000000..926306125 --- /dev/null +++ b/.github/workflows/sonarcube.yml.bkp @@ -0,0 +1,42 @@ +name: Sonarcube Scan + +on: + push: + branches: + - master + pull_request: + types: [opened, synchronize, reopened] + +jobs: + build: + name: Build + runs-on: ubuntu-latest + env: + BUILD_WRAPPER_OUT_DIR: build_wrapper_output_directory # Directory where build-wrapper output will be placed + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis + - name: Install Build Wrapper + uses: SonarSource/sonarqube-scan-action/install-build-wrapper@v4 + + - name: Install Dependencies + run: | + sudo apt-get update + sudo apt-get install -y libzmq3-dev libsqlite3-dev + + - name: Install googletest + uses: Bacondish2023/setup-googletest@v1 + + - name: Run Build Wrapper + run: | + mkdir build + cmake -S . -B build + build-wrapper-linux-x86-64 --out-dir ${{ env.BUILD_WRAPPER_OUT_DIR }} cmake --build build/ --config Release + - name: SonarQube Scan + uses: SonarSource/sonarqube-scan-action@v4 + env: + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} # Put the name of your token here + with: + args: > + --define sonar.cfamily.compile-commands="${{ env.BUILD_WRAPPER_OUT_DIR }}/compile_commands.json" diff --git a/.gitignore b/.gitignore index 5e1d496b2..9d5bd4326 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,18 @@ build* site/* /.vscode/ +.vs/ + +# clangd cache +/.cache/* +CMakeSettings.json + +# OSX junk +.DS_Store + +# pixi environments +.pixi + +CMakeUserPresets.json + +tags diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 000000000..d491f36d9 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,54 @@ + +# To use: +# +# pre-commit run -a +# +# Or: +# +# pre-commit install # (runs every time you commit in git) +# +# To update this file: +# +# pre-commit autoupdate +# +# See https://github.com/pre-commit/pre-commit + +exclude: ^3rdparty/|3rdparty|^include/behaviortree_cpp/contrib/ +repos: + + # Standard hooks + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.5.0 + hooks: + - id: check-added-large-files + - id: check-ast + - id: check-case-conflict + - id: check-docstring-first + - id: check-merge-conflict + - id: check-symlinks + - id: check-xml + - id: check-yaml + - id: debug-statements + - id: end-of-file-fixer + exclude_types: [svg] + - id: mixed-line-ending + - id: trailing-whitespace + exclude_types: [svg] + - id: fix-byte-order-marker + + # CPP hooks + - repo: https://github.com/pre-commit/mirrors-clang-format + rev: v17.0.6 + hooks: + - id: clang-format + args: ['-fallback-style=none', '-i'] + + # Spell check + - repo: https://github.com/codespell-project/codespell + rev: v2.4.1 + hooks: + - id: codespell + additional_dependencies: + - tomli + args: + [--toml=./pyproject.toml] diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index ae4fd354d..000000000 --- a/.travis.yml +++ /dev/null @@ -1,40 +0,0 @@ -# This config file for Travis CI utilizes ros-industrial/industrial_ci package. -# For more info for the package, see https://github.com/ros-industrial/industrial_ci/blob/master/README.rst - -sudo: required -dist: xenial -language: cpp - -os: - - linux - -compiler: - - gcc - -matrix: - include: - - bare_linux: - env: ROS_DISTRO="none" - fast_finish: false - -before_install: - - sudo apt-get update && sudo apt-get --reinstall install -qq build-essential - - if [ "$ROS_DISTRO" = "none" ]; then sudo apt-get --reinstall install -qq libzmq3-dev libdw-dev; fi - # GTest: see motivation here https://www.eriksmistad.no/getting-started-with-google-test-on-ubuntu/ - - sudo apt-get --reinstall install -qq libgtest-dev cmake - - cd /usr/src/gtest - - sudo cmake CMakeLists.txt - - sudo make - - sudo cp *.a /usr/lib - - cd $TRAVIS_BUILD_DIR - -install: - - if [ "$ROS_DISTRO" != "none" ]; then git clone https://github.com/ros-industrial/industrial_ci.git .ci_config; fi - -before_script: - # Prepare build directory - - mkdir -p build - -script: - - if [ "$ROS_DISTRO" = "none" ]; then (cd build; cmake .. ; sudo cmake --build . --target install; ./bin/behaviortree_cpp_v3_test); fi - - if [ "$ROS_DISTRO" != "none" ]; then (.ci_config/travis.sh); fi diff --git a/3rdparty/cpp-sqlite/README.md b/3rdparty/cpp-sqlite/README.md new file mode 100644 index 000000000..913354947 --- /dev/null +++ b/3rdparty/cpp-sqlite/README.md @@ -0,0 +1,37 @@ +## Single file header only sqlite wrapper for C++ + +## Example +```cpp +#include "sqlite.hpp" +#include + +int main() +{ + sqlite::Connection connection("example.db"); + + sqlite::Statement(connection, "CREATE TABLE IF NOT EXISTS exampleTable (" + "textData TEXT, " + "intData INTEGER, " + "floatData REAL)"); + + sqlite::Statement(connection, + "INSERT INTO exampleTable VALUES (?, ?, ?)", + "Hello world", + 1234, + 5.6789); + + sqlite::Result res = sqlite::Query(connection, "SELECT * FROM exampleTable"); + + while(res.Next()) + { + std::string textData = res.Get(0); + int intData = res.Get(1); + float floatData = res.Get(2); + + std::cout << textData << " " << intData << " " << floatData << std::endl; + } + + return 0; +} + +``` diff --git a/3rdparty/cpp-sqlite/sqlite.hpp b/3rdparty/cpp-sqlite/sqlite.hpp new file mode 100644 index 000000000..c512d891c --- /dev/null +++ b/3rdparty/cpp-sqlite/sqlite.hpp @@ -0,0 +1,603 @@ +/** + Copyright (C) 2023 Toni Lipponen + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + + 3. This notice may not be removed or altered from any source distribution. + */ + +#pragma once +#include +#include +#include +#include +#include +#include + +#if __cplusplus > 201402L + #define CPP_SQLITE_NODISCARD [[nodiscard]] +#else + #define CPP_SQLITE_NODISCARD +#endif + +#if defined(CPP_SQLITE_NOTHROW) + #define CPP_SQLITE_THROW(...) return false +#else + #define CPP_SQLITE_THROW(...) throw sqlite::Error(__VA_ARGS__) +#endif + +namespace sqlite +{ + class Error : public std::runtime_error + { + public: + explicit Error(const char* message, int errorCode = SQLITE_ERROR) + : std::runtime_error(message), m_errorCode(errorCode) + { + + } + + explicit Error(const std::string& message, int errorCode = SQLITE_ERROR) + : std::runtime_error(message), m_errorCode(errorCode) + { + + } + + CPP_SQLITE_NODISCARD + int GetCode() const + { + return m_errorCode; + } + + private: + int m_errorCode; + }; + + namespace Priv + { + inline bool CheckError(sqlite3* db, int code) + { + if(code != SQLITE_OK && code != SQLITE_DONE) + { + const int extendedCode = sqlite3_extended_errcode(db); + std::string errstr = sqlite3_errstr(extendedCode); + std::string errmsg = sqlite3_errmsg(db); + + CPP_SQLITE_THROW(errstr + ": " + errmsg, extendedCode); + } + + return true; + } + + inline bool CheckError(int code) + { + if(code != SQLITE_OK && code != SQLITE_DONE) + { + std::string errstr = std::string("SQL error: ") + sqlite3_errstr(code); + CPP_SQLITE_THROW(errstr, code); + } + + return true; + } + } + + class Connection + { + public: + Connection() : m_connection(nullptr) {} + + explicit Connection(const std::string& filename) + { + this->Open(filename); + } + + Connection(const Connection&) = delete; + + Connection(Connection&& other) noexcept + { + this->m_connection = other.m_connection; + other.m_connection = nullptr; + } + + virtual ~Connection() noexcept + { + try + { + this->Close(); + } + catch(...) + { + + } + } + + Connection& operator=(const Connection&) = delete; + + Connection& operator=(Connection&& other) noexcept + { + if(&other != this) + { + this->m_connection = other.m_connection; + other.m_connection = nullptr; + } + + return *this; + } + + bool Open(const std::string& filename) + { + return sqlite::Priv::CheckError(sqlite3_open(filename.data(), &m_connection)); + } + + bool Close() + { + const auto result = Priv::CheckError(sqlite3_close(m_connection)); + m_connection = nullptr; + + return result; + } + + CPP_SQLITE_NODISCARD + int GetExtendedResult() const + { + return sqlite3_extended_errcode(m_connection); + } + + CPP_SQLITE_NODISCARD + sqlite3* GetPtr() + { + return m_connection; + } + + private: + sqlite3* m_connection = nullptr; + }; + + class Blob + { + public: + Blob(const void* data, int32_t bytes) + { + m_data.resize(bytes); + std::memcpy(&m_data.at(0), data, bytes); + } + + explicit Blob(std::vector data) + : m_data(std::move(data)) + { + + } + + CPP_SQLITE_NODISCARD + uint32_t GetSize() const + { + return m_data.size(); + } + + CPP_SQLITE_NODISCARD + unsigned char* GetData() + { + return m_data.data(); + } + + CPP_SQLITE_NODISCARD + const unsigned char* GetData() const + { + return m_data.data(); + } + + private: + std::vector m_data; + }; + + /** Non-owning blob*/ + class NOBlob + { + public: + NOBlob(const void* ptr, uint32_t bytes) + : m_ptr(ptr), m_bytes(bytes) + { + + } + + CPP_SQLITE_NODISCARD + uint32_t GetSize() const + { + return m_bytes; + } + + const void* GetData() + { + return m_ptr; + } + + CPP_SQLITE_NODISCARD + const void* GetData() const + { + return m_ptr; + } + + private: + const void* m_ptr; + uint32_t m_bytes; + }; + + namespace Priv + { + inline void Append(sqlite3_stmt* statement, int index, const int32_t& data) + { + sqlite::Priv::CheckError(sqlite3_bind_int(statement, index, data)); + } + + inline void Append(sqlite3_stmt* statement, int index, const int64_t& data) + { + sqlite::Priv::CheckError(sqlite3_bind_int64(statement, index, data)); + } + + inline void Append(sqlite3_stmt* statement, int index, const float& data) + { + sqlite::Priv::CheckError(sqlite3_bind_double(statement, index, static_cast(data))); + } + + inline void Append(sqlite3_stmt* statement, int index, const double& data) + { + sqlite::Priv::CheckError(sqlite3_bind_double(statement, index, data)); + } + + inline void Append(sqlite3_stmt* statement, int index, const std::string& data) + { + sqlite::Priv::CheckError(sqlite3_bind_text(statement, index, data.data(), static_cast(data.size()), nullptr)); + } + + inline void Append(sqlite3_stmt* statement, int index, const char* data) + { + sqlite::Priv::CheckError(sqlite3_bind_text(statement, index, data, static_cast(std::strlen(data)), nullptr)); + } + + inline void Append(sqlite3_stmt* statement, int index, const sqlite::Blob& blob) + { + sqlite::Priv::CheckError(sqlite3_bind_blob(statement, index, blob.GetData(), static_cast(blob.GetSize()), nullptr)); + } + + inline void Append(sqlite3_stmt* statement, int index, const sqlite::NOBlob& blob) + { + sqlite::Priv::CheckError(sqlite3_bind_blob(statement, index, blob.GetData(), static_cast(blob.GetSize()), nullptr)); + } + + template + inline void AppendToQuery(sqlite3_stmt* statement, int index, const Arg& arg) + { + sqlite::Priv::Append(statement, index, arg); + } + + template + inline void AppendToQuery(sqlite3_stmt* statement, int index, const First& first, const Args&... args) + { + sqlite::Priv::Append(statement, index, first); + sqlite::Priv::AppendToQuery(statement, ++index, args...); + } + + struct Statement + { + Statement() : handle(nullptr) {} + + Statement(sqlite::Connection& connection, const std::string& command) + { + auto* db = connection.GetPtr(); + + const int code = sqlite3_prepare_v2( + db, + command.data(), + static_cast(command.size()), + &handle, + nullptr); + + Priv::CheckError(db, code); + } + + Statement(Statement&& other) noexcept + { + std::swap(handle, other.handle); + } + + ~Statement() + { + sqlite::Priv::CheckError(sqlite3_finalize(handle)); + } + + Statement& operator=(Statement&& other) noexcept + { + handle = other.handle; + other.handle = nullptr; + + return *this; + } + + CPP_SQLITE_NODISCARD + bool Advance() const + { + const int code = sqlite3_step(handle); + + if(code == SQLITE_ROW) + { + return true; + } + + sqlite::Priv::CheckError(code); + Reset(); + + return false; + } + + bool Reset() const + { + return sqlite::Priv::CheckError(sqlite3_reset(handle)); + } + + CPP_SQLITE_NODISCARD + int ColumnCount() const + { + Reset(); + + if(!Advance()) + { + return 0; + } + + const int count = sqlite3_column_count(handle); + Reset(); + + return count; + } + + CPP_SQLITE_NODISCARD + std::string GetColumnName(int columnIndex) const + { + Reset(); + + if(!Advance()) + { +#ifndef CPP_SQLITE_NOTHROW + throw sqlite::Error("SQL error: invalid column index"); +#endif + } + + std::string name = sqlite3_column_name(handle, columnIndex); + + if(name.empty()) + { +#ifndef CPP_SQLITE_NOTHROW + throw sqlite::Error("SQL error: failed to get column name at index " + std::to_string(columnIndex)); +#endif + } + + Reset(); + + return name; + } + + template + CPP_SQLITE_NODISCARD + T Get(int) const + { + static_assert(sizeof(T) == -1, "SQL error: invalid column data type"); + } + + sqlite3_stmt* handle = nullptr; + }; + + template<> + inline float Statement::Get(int col) const + { + return static_cast(sqlite3_column_double(handle, col)); + } + + template<> + inline double Statement::Get(int col) const + { + return sqlite3_column_double(handle, col); + } + + template<> + inline int32_t Statement::Get(int col) const + { + return sqlite3_column_int(handle, col); + } + + template<> + inline int64_t Statement::Get(int col) const + { + return sqlite3_column_int64(handle, col); + } + + template<> + inline std::string Statement::Get(int col) const + { + const unsigned char* bytes = sqlite3_column_text(handle, col); + const int size = sqlite3_column_bytes(handle, col); + + if(size == 0) + { + return ""; + } + + return {reinterpret_cast(bytes), static_cast(size)}; + } + + template<> + inline sqlite::Blob Statement::Get(int col) const + { + const void* bytes = sqlite3_column_blob(handle, col); + const int size = sqlite3_column_bytes(handle, col); + + return {bytes, size}; + } + } + + class Type + { + private: + Type(const sqlite::Priv::Statement& statement, int col) + : m_statement(statement), m_columnIndex(col) + { + + } + public: + template + operator T() const + { + return m_statement.Get(m_columnIndex); + } + + friend class Result; + + private: + const sqlite::Priv::Statement& m_statement; + const int m_columnIndex; + }; + + class Result + { + explicit Result(sqlite::Priv::Statement&& statement) + : m_statement(std::move(statement)) + { + + } + + public: + Result() = default; + + Result(Result&& other) noexcept + { + m_statement = std::move(other.m_statement); + } + + Result& operator=(Result&& other) noexcept + { + m_statement = std::move(other.m_statement); + + return *this; + } + + CPP_SQLITE_NODISCARD + bool HasData() const + { + return ColumnCount() > 0; + } + + CPP_SQLITE_NODISCARD + int ColumnCount() const + { + return m_statement.ColumnCount(); + } + + bool Reset() const + { + return m_statement.Reset(); + } + + CPP_SQLITE_NODISCARD + bool Next() const + { + return m_statement.Advance(); + } + + CPP_SQLITE_NODISCARD + Type Get(int columnIndex) const + { + return {m_statement, columnIndex}; + } + + CPP_SQLITE_NODISCARD + std::string GetColumnName(int columnIndex) const + { + return m_statement.GetColumnName(columnIndex); + } + + friend void Statement(sqlite::Connection&, const std::string&); + + template + friend Result Query(sqlite::Connection& connection, const std::string& command, const First& first, const Args... args); + friend Result Query(sqlite::Connection& connection, const std::string& command); + + private: + sqlite::Priv::Statement m_statement; + }; + + template + inline void Statement(sqlite::Connection& connection, const std::string& command, const First& first, const Args... args) + { + sqlite::Priv::Statement statement(connection, command); + sqlite::Priv::AppendToQuery(statement.handle, 1, first, args...); + + (void)statement.Advance(); + } + + inline void Statement(sqlite::Connection& connection, const std::string& command) + { + sqlite::Priv::Statement statement(connection, command); + + (void)statement.Advance(); + } + + template + CPP_SQLITE_NODISCARD + inline Result Query(sqlite::Connection& connection, const std::string& command, const First& first, const Args... args) + { + sqlite::Priv::Statement statement(connection, command); + sqlite::Priv::AppendToQuery(statement.handle, 1, first, args...); + + return Result(std::move(statement)); + } + + CPP_SQLITE_NODISCARD + inline Result Query(sqlite::Connection& connection, const std::string& command) + { + sqlite::Priv::Statement statement(connection, command); + + return Result(std::move(statement)); + } + + inline bool Backup(sqlite::Connection& from, sqlite::Connection& to) + { + sqlite3_backup* backup = sqlite3_backup_init(to.GetPtr(), "main", from.GetPtr(), "main"); + + if(!backup) + { + CPP_SQLITE_THROW("SQL error: failed to initialize backup"); + } + + if(!Priv::CheckError(sqlite3_backup_step(backup, -1))) + { + return false; + } + + if(!Priv::CheckError(sqlite3_backup_finish(backup))) + { + return false; + } + + return true; + } + + inline bool Backup(sqlite::Connection& from, const std::string& filename) + { + sqlite::Connection to(filename); + + return sqlite::Backup(from, to); + } +} diff --git a/3rdparty/cppzmq/LICENSE b/3rdparty/cppzmq/LICENSE new file mode 100644 index 000000000..ae98bd859 --- /dev/null +++ b/3rdparty/cppzmq/LICENSE @@ -0,0 +1,17 @@ + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to + deal in the Software without restriction, including without limitation the + rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + sell copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + IN THE SOFTWARE. diff --git a/3rdparty/cppzmq/README.md b/3rdparty/cppzmq/README.md new file mode 100644 index 000000000..e2bea0b63 --- /dev/null +++ b/3rdparty/cppzmq/README.md @@ -0,0 +1,196 @@ +[![CI](https://github.com/zeromq/cppzmq/actions/workflows/ci.yml/badge.svg)](https://github.com/zeromq/cppzmq/actions) +[![Coverage Status](https://coveralls.io/repos/github/zeromq/cppzmq/badge.svg?branch=master)](https://coveralls.io/github/zeromq/cppzmq?branch=master) +[![License](https://img.shields.io/github/license/zeromq/cppzmq.svg)](https://github.com/zeromq/cppzmq/blob/master/LICENSE) + +Introduction & Design Goals +=========================== + +cppzmq is a C++ binding for libzmq. It has the following design goals: + - cppzmq maps the libzmq C API to C++ concepts. In particular: + - it is type-safe (the libzmq C API exposes various class-like concepts as void*) + - it provides exception-based error handling (the libzmq C API provides errno-based error handling) + - it provides RAII-style classes that automate resource management (the libzmq C API requires the user to take care to free resources explicitly) + - cppzmq is a light-weight, header-only binding. You only need to include the header file zmq.hpp (and maybe zmq_addon.hpp) to use it. + - zmq.hpp is meant to contain direct mappings of the abstractions provided by the libzmq C API, while zmq_addon.hpp provides additional higher-level abstractions. + +There are other C++ bindings for ZeroMQ with different design goals. In particular, none of the following bindings are header-only: + - [zmqpp](https://github.com/zeromq/zmqpp) is a high-level binding to libzmq. + - [czmqpp](https://github.com/zeromq/czmqpp) is a binding based on the high-level czmq API. + - [fbzmq](https://github.com/facebook/fbzmq) is a binding that integrates with Apache Thrift and provides higher-level abstractions in addition. It requires C++14. + +Supported platforms +=================== + + - Only a subset of the platforms that are supported by libzmq itself are supported. Some features already require a compiler supporting C++11. In the future, probably all features will require C++11. To build and run the tests, CMake and Catch are required. + - Any libzmq 4.x version is expected to work. DRAFT features may only work for the most recent tested version. Currently explicitly tested libzmq versions are + - 4.2.0 (without DRAFT API) + - 4.3.4 (with and without DRAFT API) + - Platforms with full support (i.e. CI executing build and tests) + - Ubuntu 18.04 x64 (with gcc 4.8.5, 5.5.0, 7.5.0) + - Ubuntu 20.04 x64 (with gcc 9.3.0, 10.3.0 and clang 12) + - Visual Studio 2017 x64 + - Visual Studio 2019 x64 + - macOS 10.15 (with clang 12, without DRAFT API) + - Additional platforms that are known to work: + - We have no current reports on additional platforms that are known to work yet. Please add your platform here. If CI can be provided for them with a cloud-based CI service working with GitHub, you are invited to add CI, and make it possible to be included in the list above. + - Additional platforms that probably work: + - Any platform supported by libzmq that provides a sufficiently recent gcc (4.8.1 or newer) or clang (3.4.1 or newer) + - Visual Studio 2012+ x86/x64 + +Examples +======== +These examples require at least C++11. +```c++ +#include + +int main() +{ + zmq::context_t ctx; + zmq::socket_t sock(ctx, zmq::socket_type::push); + sock.bind("inproc://test"); + sock.send(zmq::str_buffer("Hello, world"), zmq::send_flags::dontwait); +} +``` +This a more complex example where we send and receive multi-part messages over TCP with a wildcard port. +```c++ +#include +#include + +int main() +{ + zmq::context_t ctx; + zmq::socket_t sock1(ctx, zmq::socket_type::push); + zmq::socket_t sock2(ctx, zmq::socket_type::pull); + sock1.bind("tcp://127.0.0.1:*"); + const std::string last_endpoint = + sock1.get(zmq::sockopt::last_endpoint); + std::cout << "Connecting to " + << last_endpoint << std::endl; + sock2.connect(last_endpoint); + + std::array send_msgs = { + zmq::str_buffer("foo"), + zmq::str_buffer("bar!") + }; + if (!zmq::send_multipart(sock1, send_msgs)) + return 1; + + std::vector recv_msgs; + const auto ret = zmq::recv_multipart( + sock2, std::back_inserter(recv_msgs)); + if (!ret) + return 1; + std::cout << "Got " << *ret + << " messages" << std::endl; + return 0; +} +``` + +See the `examples` directory for more examples. When the project is compiled with tests enabled, each example gets compiled to an executable. + + +API Overview +============ + +For an extensive overview of the `zmq.hpp` API in use, see this [Tour of CPPZMQ by @brettviren](https://brettviren.github.io/cppzmq-tour/index.html). + +Bindings for libzmq in `zmq.hpp`: + +Types: +* class `zmq::context_t` +* enum `zmq::ctxopt` +* class `zmq::socket_t` +* class `zmq::socket_ref` +* enum `zmq::socket_type` +* enum `zmq::sockopt` +* enum `zmq::send_flags` +* enum `zmq::recv_flags` +* class `zmq::message_t` +* class `zmq::const_buffer` +* class `zmq::mutable_buffer` +* struct `zmq::recv_buffer_size` +* alias `zmq::send_result_t` +* alias `zmq::recv_result_t` +* alias `zmq::recv_buffer_result_t` +* class `zmq::error_t` +* class `zmq::monitor_t` +* struct `zmq_event_t`, +* alias `zmq::free_fn`, +* alias `zmq::pollitem_t`, +* alias `zmq::fd_t` +* class `zmq::poller_t` DRAFT +* enum `zmq::event_flags` DRAFT +* enum `zmq::poller_event` DRAFT + +Functions: +* `zmq::version` +* `zmq::poll` +* `zmq::proxy` +* `zmq::proxy_steerable` +* `zmq::buffer` +* `zmq::str_buffer` + +Extra high-level types and functions `zmq_addon.hpp`: + +Types: +* class `zmq::multipart_t` +* class `zmq::active_poller_t` DRAFT + +Functions: +* `zmq::recv_multipart` +* `zmq::send_multipart` +* `zmq::send_multipart_n` +* `zmq::encode` +* `zmq::decode` + +Compatibility Guidelines +======================== + +The users of cppzmq are expected to follow the guidelines below to ensure not to break when upgrading cppzmq to newer versions (non-exhaustive list): + +* Do not depend on any macros defined in cppzmq unless explicitly declared public here. + +The following macros may be used by consumers of cppzmq: `CPPZMQ_VERSION`, `CPPZMQ_VERSION_MAJOR`, `CPPZMQ_VERSION_MINOR`, `CPPZMQ_VERSION_PATCH`. + +Contribution policy +=================== + +The contribution policy is at: http://rfc.zeromq.org/spec:22 + +Build instructions +================== + +Build steps: + +1. Build [libzmq](https://github.com/zeromq/libzmq) via cmake. This does an out of source build and installs the build files + - download and unzip the lib, cd to directory + - mkdir build + - cd build + - cmake .. + - sudo make -j4 install + +2. Build cppzmq via cmake. This does an out of source build and installs the build files + - download and unzip the lib, cd to directory + - mkdir build + - cd build + - cmake .. + - sudo make -j4 install + +3. Build cppzmq via [vcpkg](https://github.com/Microsoft/vcpkg/). This does an out of source build and installs the build files + - git clone https://github.com/Microsoft/vcpkg.git + - cd vcpkg + - ./bootstrap-vcpkg.sh # bootstrap-vcpkg.bat for Powershell + - ./vcpkg integrate install + - ./vcpkg install cppzmq + +Using this: + +A cmake find package scripts is provided for you to easily include this library. +Add these lines in your CMakeLists.txt to include the headers and library files of +cpp zmq (which will also include libzmq for you). + +``` +#find cppzmq wrapper, installed by make of cppzmq +find_package(cppzmq) +target_link_libraries(*Your Project Name* cppzmq) +``` diff --git a/3rdparty/cppzmq/zmq.hpp b/3rdparty/cppzmq/zmq.hpp index d59eb55e5..3fa484c6c 100644 --- a/3rdparty/cppzmq/zmq.hpp +++ b/3rdparty/cppzmq/zmq.hpp @@ -67,6 +67,8 @@ #define ZMQ_DEPRECATED(msg) __declspec(deprecated(msg)) #elif defined(__GNUC__) #define ZMQ_DEPRECATED(msg) __attribute__((deprecated(msg))) +#else +#define ZMQ_DEPRECATED(msg) #endif #if defined(ZMQ_CPP17) @@ -92,7 +94,7 @@ #define ZMQ_CONSTEXPR_VAR const #define ZMQ_CPP11_DEPRECATED(msg) #endif -#if defined(ZMQ_CPP14) && (!defined(_MSC_VER) || _MSC_VER > 1900) +#if defined(ZMQ_CPP14) && (!defined(_MSC_VER) || _MSC_VER > 1900) && (!defined(__GNUC__) || __GNUC__ > 5 || (__GNUC__ == 5 && __GNUC_MINOR__ > 3)) #define ZMQ_EXTENDED_CONSTEXPR #endif #if defined(ZMQ_CPP17) @@ -145,7 +147,7 @@ /* Version macros for compile-time API version detection */ #define CPPZMQ_VERSION_MAJOR 4 -#define CPPZMQ_VERSION_MINOR 8 +#define CPPZMQ_VERSION_MINOR 10 #define CPPZMQ_VERSION_PATCH 0 #define CPPZMQ_VERSION \ @@ -299,18 +301,30 @@ class error_t : public std::exception int errnum; }; -inline int poll(zmq_pollitem_t *items_, size_t nitems_, long timeout_ = -1) +namespace detail { +inline int poll(zmq_pollitem_t *items_, size_t nitems_, long timeout_) { int rc = zmq_poll(items_, static_cast(nitems_), timeout_); if (rc < 0) throw error_t(); return rc; } +} + +#ifdef ZMQ_CPP11 +ZMQ_DEPRECATED("from 4.8.0, use poll taking std::chrono::duration instead of long") +inline int poll(zmq_pollitem_t *items_, size_t nitems_, long timeout_) +#else +inline int poll(zmq_pollitem_t *items_, size_t nitems_, long timeout_ = -1) +#endif +{ + return detail::poll(items_, nitems_, timeout_); +} ZMQ_DEPRECATED("from 4.3.1, use poll taking non-const items") inline int poll(zmq_pollitem_t const *items_, size_t nitems_, long timeout_ = -1) { - return poll(const_cast(items_), nitems_, timeout_); + return detail::poll(const_cast(items_), nitems_, timeout_); } #ifdef ZMQ_CPP11 @@ -318,7 +332,7 @@ ZMQ_DEPRECATED("from 4.3.1, use poll taking non-const items") inline int poll(zmq_pollitem_t const *items, size_t nitems, std::chrono::milliseconds timeout) { - return poll(const_cast(items), nitems, + return detail::poll(const_cast(items), nitems, static_cast(timeout.count())); } @@ -326,39 +340,39 @@ ZMQ_DEPRECATED("from 4.3.1, use poll taking non-const items") inline int poll(std::vector const &items, std::chrono::milliseconds timeout) { - return poll(const_cast(items.data()), items.size(), + return detail::poll(const_cast(items.data()), items.size(), static_cast(timeout.count())); } ZMQ_DEPRECATED("from 4.3.1, use poll taking non-const items") inline int poll(std::vector const &items, long timeout_ = -1) { - return poll(const_cast(items.data()), items.size(), timeout_); + return detail::poll(const_cast(items.data()), items.size(), timeout_); } inline int -poll(zmq_pollitem_t *items, size_t nitems, std::chrono::milliseconds timeout) +poll(zmq_pollitem_t *items, size_t nitems, std::chrono::milliseconds timeout = std::chrono::milliseconds{-1}) { - return poll(items, nitems, static_cast(timeout.count())); + return detail::poll(items, nitems, static_cast(timeout.count())); } inline int poll(std::vector &items, - std::chrono::milliseconds timeout) + std::chrono::milliseconds timeout = std::chrono::milliseconds{-1}) { - return poll(items.data(), items.size(), static_cast(timeout.count())); + return detail::poll(items.data(), items.size(), static_cast(timeout.count())); } -ZMQ_DEPRECATED("from 4.3.1, use poll taking std::chrono instead of long") -inline int poll(std::vector &items, long timeout_ = -1) +ZMQ_DEPRECATED("from 4.3.1, use poll taking std::chrono::duration instead of long") +inline int poll(std::vector &items, long timeout_) { - return poll(items.data(), items.size(), timeout_); + return detail::poll(items.data(), items.size(), timeout_); } template inline int poll(std::array &items, - std::chrono::milliseconds timeout) + std::chrono::milliseconds timeout = std::chrono::milliseconds{-1}) { - return poll(items.data(), items.size(), static_cast(timeout.count())); + return detail::poll(items.data(), items.size(), static_cast(timeout.count())); } #endif @@ -524,6 +538,11 @@ class message_t throw error_t(); memcpy(data(), data_, size_); } + + void rebuild(const std::string &str) + { + rebuild(str.data(), str.size()); + } void rebuild(void *data_, size_t size_, free_fn *ffn_, void *hint_ = ZMQ_NULLPTR) { @@ -851,7 +870,7 @@ class context_t int rc; do { - rc = zmq_ctx_destroy(ptr); + rc = zmq_ctx_term(ptr); } while (rc == -1 && errno == EINTR); ZMQ_ASSERT(rc == 0); @@ -1362,6 +1381,39 @@ constexpr const_buffer operator"" _zbuf(const char32_t *str, size_t len) noexcep } } +#ifdef ZMQ_CPP11 +enum class socket_type : int +{ + req = ZMQ_REQ, + rep = ZMQ_REP, + dealer = ZMQ_DEALER, + router = ZMQ_ROUTER, + pub = ZMQ_PUB, + sub = ZMQ_SUB, + xpub = ZMQ_XPUB, + xsub = ZMQ_XSUB, + push = ZMQ_PUSH, + pull = ZMQ_PULL, +#if defined(ZMQ_BUILD_DRAFT_API) && ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 0) + server = ZMQ_SERVER, + client = ZMQ_CLIENT, + radio = ZMQ_RADIO, + dish = ZMQ_DISH, + gather = ZMQ_GATHER, + scatter = ZMQ_SCATTER, + dgram = ZMQ_DGRAM, +#endif +#if defined(ZMQ_BUILD_DRAFT_API) && ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 3, 3) + peer = ZMQ_PEER, + channel = ZMQ_CHANNEL, +#endif +#if ZMQ_VERSION_MAJOR >= 4 + stream = ZMQ_STREAM, +#endif + pair = ZMQ_PAIR +}; +#endif + namespace sockopt { // There are two types of options, @@ -1430,6 +1482,9 @@ ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_CURVE_SERVER, curve_server, int); #ifdef ZMQ_CURVE_SERVERKEY ZMQ_DEFINE_ARRAY_OPT_BIN_OR_Z85(ZMQ_CURVE_SERVERKEY, curve_serverkey); #endif +#ifdef ZMQ_DISCONNECT_MSG +ZMQ_DEFINE_ARRAY_OPT_BINARY(ZMQ_DISCONNECT_MSG, disconnect_msg); +#endif #ifdef ZMQ_EVENTS ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_EVENTS, events, int); #endif @@ -1470,6 +1525,9 @@ ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_HEARTBEAT_TIMEOUT, heartbeat_timeout, int); #ifdef ZMQ_HEARTBEAT_TTL ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_HEARTBEAT_TTL, heartbeat_ttl, int); #endif +#ifdef ZMQ_HELLO_MSG +ZMQ_DEFINE_ARRAY_OPT_BINARY(ZMQ_HELLO_MSG, hello_msg); +#endif #ifdef ZMQ_IMMEDIATE ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_IMMEDIATE, immediate, int); #endif @@ -1503,6 +1561,9 @@ ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_MULTICAST_LOOP, multicast_loop, int); #ifdef ZMQ_MULTICAST_MAXTPDU ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_MULTICAST_MAXTPDU, multicast_maxtpdu, int); #endif +#ifdef ZMQ_ONLY_FIRST_SUBSCRIBE +ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_ONLY_FIRST_SUBSCRIBE, only_first_subscribe, int); +#endif #ifdef ZMQ_PLAIN_SERVER ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_PLAIN_SERVER, plain_server, int); #endif @@ -1512,6 +1573,9 @@ ZMQ_DEFINE_ARRAY_OPT(ZMQ_PLAIN_PASSWORD, plain_password); #ifdef ZMQ_PLAIN_USERNAME ZMQ_DEFINE_ARRAY_OPT(ZMQ_PLAIN_USERNAME, plain_username); #endif +#ifdef ZMQ_PRIORITY +ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_PRIORITY, priority, int); +#endif #ifdef ZMQ_USE_FD ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_USE_FD, use_fd, int); #endif @@ -1539,6 +1603,9 @@ ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_RECONNECT_IVL, reconnect_ivl, int); #ifdef ZMQ_RECONNECT_IVL_MAX ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_RECONNECT_IVL_MAX, reconnect_ivl_max, int); #endif +#ifdef ZMQ_RECONNECT_STOP +ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_RECONNECT_STOP, reconnect_stop, int); +#endif #ifdef ZMQ_RECOVERY_IVL ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_RECOVERY_IVL, recovery_ivl, int); #endif @@ -1569,9 +1636,15 @@ ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_SNDHWM, sndhwm, int); #ifdef ZMQ_SNDTIMEO ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_SNDTIMEO, sndtimeo, int); #endif +#ifdef ZMQ_SOCKS_PASSWORD +ZMQ_DEFINE_ARRAY_OPT(ZMQ_SOCKS_PASSWORD, socks_password); +#endif #ifdef ZMQ_SOCKS_PROXY ZMQ_DEFINE_ARRAY_OPT(ZMQ_SOCKS_PROXY, socks_proxy); #endif +#ifdef ZMQ_SOCKS_USERNAME +ZMQ_DEFINE_ARRAY_OPT(ZMQ_SOCKS_USERNAME, socks_username); +#endif #ifdef ZMQ_STREAM_NOTIFY ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_STREAM_NOTIFY, stream_notify, int); #endif @@ -1601,7 +1674,10 @@ ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_TOS, tos, int); #endif #ifdef ZMQ_TYPE ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_TYPE, type, int); -#endif +#ifdef ZMQ_CPP11 +ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_TYPE, socket_type, socket_type); +#endif // ZMQ_CPP11 +#endif // ZMQ_TYPE #ifdef ZMQ_UNSUBSCRIBE ZMQ_DEFINE_ARRAY_OPT(ZMQ_UNSUBSCRIBE, unsubscribe); #endif @@ -1626,6 +1702,9 @@ ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_XPUB_VERBOSER, xpub_verboser, int); #ifdef ZMQ_XPUB_MANUAL ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_XPUB_MANUAL, xpub_manual, int); #endif +#ifdef ZMQ_XPUB_MANUAL_LAST_VALUE +ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_XPUB_MANUAL_LAST_VALUE, xpub_manual_last_value, int); +#endif #ifdef ZMQ_XPUB_NODROP ZMQ_DEFINE_INTEGRAL_BOOL_UNIT_OPT(ZMQ_XPUB_NODROP, xpub_nodrop, int); #endif @@ -1743,7 +1822,7 @@ class socket_base template ZMQ_NODISCARD T get(sockopt::integral_option) const { - static_assert(std::is_integral::value, "T must be integral"); + static_assert(std::is_scalar::value, "T must be scalar"); T val; size_t size = sizeof val; get_option(Opt, &val, &size); @@ -2012,32 +2091,6 @@ class socket_base }; } // namespace detail -#ifdef ZMQ_CPP11 -enum class socket_type : int -{ - req = ZMQ_REQ, - rep = ZMQ_REP, - dealer = ZMQ_DEALER, - router = ZMQ_ROUTER, - pub = ZMQ_PUB, - sub = ZMQ_SUB, - xpub = ZMQ_XPUB, - xsub = ZMQ_XSUB, - push = ZMQ_PUSH, - pull = ZMQ_PULL, -#if defined(ZMQ_BUILD_DRAFT_API) && ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 0) - server = ZMQ_SERVER, - client = ZMQ_CLIENT, - radio = ZMQ_RADIO, - dish = ZMQ_DISH, -#endif -#if ZMQ_VERSION_MAJOR >= 4 - stream = ZMQ_STREAM, -#endif - pair = ZMQ_PAIR -}; -#endif - struct from_handle_t { struct _private @@ -2315,7 +2368,11 @@ class monitor_t {_monitor_socket.handle(), 0, ZMQ_POLLIN, 0}, }; + #ifdef ZMQ_CPP11 + zmq::poll(&items[0], 1, std::chrono::milliseconds(timeout)); + #else zmq::poll(&items[0], 1, timeout); + #endif if (items[0].revents & ZMQ_POLLIN) { int rc = zmq_msg_recv(eventMsg.handle(), _monitor_socket.handle(), 0); @@ -2390,8 +2447,7 @@ class monitor_t case ZMQ_EVENT_DISCONNECTED: on_event_disconnected(*event, address.c_str()); break; -#ifdef ZMQ_BUILD_DRAFT_API -#if ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 3) +#if ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 3, 0) || (defined(ZMQ_BUILD_DRAFT_API) && ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 3)) case ZMQ_EVENT_HANDSHAKE_FAILED_NO_DETAIL: on_event_handshake_failed_no_detail(*event, address.c_str()); break; @@ -2404,14 +2460,13 @@ class monitor_t case ZMQ_EVENT_HANDSHAKE_SUCCEEDED: on_event_handshake_succeeded(*event, address.c_str()); break; -#elif ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 1) +#elif defined(ZMQ_BUILD_DRAFT_API) && ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 1) case ZMQ_EVENT_HANDSHAKE_FAILED: on_event_handshake_failed(*event, address.c_str()); break; case ZMQ_EVENT_HANDSHAKE_SUCCEED: on_event_handshake_succeed(*event, address.c_str()); break; -#endif #endif default: on_event_unknown(*event, address.c_str()); @@ -2608,6 +2663,17 @@ template class poller_t add_impl(socket, events, nullptr); } + template< + typename Dummy = void, + typename = + typename std::enable_if::value, Dummy>::type> + void add(fd_t fd, event_flags events, T *user_data) + { + add_impl(fd, events, user_data); + } + + void add(fd_t fd, event_flags events) { add_impl(fd, events, nullptr); } + void remove(zmq::socket_ref socket) { if (0 != zmq_poller_remove(poller_ptr.get(), socket.handle())) { @@ -2674,6 +2740,15 @@ template class poller_t throw error_t(); } } + + void add_impl(fd_t fd, event_flags events, T *user_data) + { + if (0 + != zmq_poller_add_fd(poller_ptr.get(), fd, user_data, + static_cast(events))) { + throw error_t(); + } + } }; #endif // defined(ZMQ_BUILD_DRAFT_API) && defined(ZMQ_CPP11) && defined(ZMQ_HAVE_POLLER) @@ -2685,4 +2760,3 @@ inline std::ostream &operator<<(std::ostream &os, const message_t &msg) } // namespace zmq #endif // __ZMQ_HPP_INCLUDED__ - diff --git a/3rdparty/cppzmq/zmq_addon.hpp b/3rdparty/cppzmq/zmq_addon.hpp new file mode 100644 index 000000000..958eec56d --- /dev/null +++ b/3rdparty/cppzmq/zmq_addon.hpp @@ -0,0 +1,753 @@ +/* + Copyright (c) 2016-2017 ZeroMQ community + Copyright (c) 2016 VOCA AS / Harald Nøkland + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to + deal in the Software without restriction, including without limitation the + rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + sell copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + IN THE SOFTWARE. +*/ + +#ifndef __ZMQ_ADDON_HPP_INCLUDED__ +#define __ZMQ_ADDON_HPP_INCLUDED__ + +#include "zmq.hpp" + +#include +#include +#include +#include +#ifdef ZMQ_CPP11 +#include +#include +#include +#endif + +namespace zmq +{ +#ifdef ZMQ_CPP11 + +namespace detail +{ +template +recv_result_t +recv_multipart_n(socket_ref s, OutputIt out, size_t n, recv_flags flags) +{ + size_t msg_count = 0; + message_t msg; + while (true) { + if ZMQ_CONSTEXPR_IF (CheckN) { + if (msg_count >= n) + throw std::runtime_error( + "Too many message parts in recv_multipart_n"); + } + if (!s.recv(msg, flags)) { + // zmq ensures atomic delivery of messages + assert(msg_count == 0); + return {}; + } + ++msg_count; + const bool more = msg.more(); + *out++ = std::move(msg); + if (!more) + break; + } + return msg_count; +} + +inline bool is_little_endian() +{ + const uint16_t i = 0x01; + return *reinterpret_cast(&i) == 0x01; +} + +inline void write_network_order(unsigned char *buf, const uint32_t value) +{ + if (is_little_endian()) { + ZMQ_CONSTEXPR_VAR uint32_t mask = (std::numeric_limits::max)(); + *buf++ = static_cast((value >> 24) & mask); + *buf++ = static_cast((value >> 16) & mask); + *buf++ = static_cast((value >> 8) & mask); + *buf++ = static_cast(value & mask); + } else { + std::memcpy(buf, &value, sizeof(value)); + } +} + +inline uint32_t read_u32_network_order(const unsigned char *buf) +{ + if (is_little_endian()) { + return (static_cast(buf[0]) << 24) + + (static_cast(buf[1]) << 16) + + (static_cast(buf[2]) << 8) + + static_cast(buf[3]); + } else { + uint32_t value; + std::memcpy(&value, buf, sizeof(value)); + return value; + } +} +} // namespace detail + +/* Receive a multipart message. + + Writes the zmq::message_t objects to OutputIterator out. + The out iterator must handle an unspecified number of writes, + e.g. by using std::back_inserter. + + Returns: the number of messages received or nullopt (on EAGAIN). + Throws: if recv throws. Any exceptions thrown + by the out iterator will be propagated and the message + may have been only partially received with pending + message parts. It is adviced to close this socket in that event. +*/ +template +ZMQ_NODISCARD recv_result_t recv_multipart(socket_ref s, + OutputIt out, + recv_flags flags = recv_flags::none) +{ + return detail::recv_multipart_n(s, std::move(out), 0, flags); +} + +/* Receive a multipart message. + + Writes at most n zmq::message_t objects to OutputIterator out. + If the number of message parts of the incoming message exceeds n + then an exception will be thrown. + + Returns: the number of messages received or nullopt (on EAGAIN). + Throws: if recv throws. Throws std::runtime_error if the number + of message parts exceeds n (exactly n messages will have been written + to out). Any exceptions thrown + by the out iterator will be propagated and the message + may have been only partially received with pending + message parts. It is adviced to close this socket in that event. +*/ +template +ZMQ_NODISCARD recv_result_t recv_multipart_n(socket_ref s, + OutputIt out, + size_t n, + recv_flags flags = recv_flags::none) +{ + return detail::recv_multipart_n(s, std::move(out), n, flags); +} + +/* Send a multipart message. + + The range must be a ForwardRange of zmq::message_t, + zmq::const_buffer or zmq::mutable_buffer. + The flags may be zmq::send_flags::sndmore if there are + more message parts to be sent after the call to this function. + + Returns: the number of messages sent (exactly msgs.size()) or nullopt (on EAGAIN). + Throws: if send throws. Any exceptions thrown + by the msgs range will be propagated and the message + may have been only partially sent. It is adviced to close this socket in that event. +*/ +template::value + && (std::is_same, message_t>::value + || detail::is_buffer>::value)>::type +#endif + > +send_result_t +send_multipart(socket_ref s, Range &&msgs, send_flags flags = send_flags::none) +{ + using std::begin; + using std::end; + auto it = begin(msgs); + const auto end_it = end(msgs); + size_t msg_count = 0; + while (it != end_it) { + const auto next = std::next(it); + const auto msg_flags = + flags | (next == end_it ? send_flags::none : send_flags::sndmore); + if (!s.send(*it, msg_flags)) { + // zmq ensures atomic delivery of messages + assert(it == begin(msgs)); + return {}; + } + ++msg_count; + it = next; + } + return msg_count; +} + +/* Encode a multipart message. + + The range must be a ForwardRange of zmq::message_t. A + zmq::multipart_t or STL container may be passed for encoding. + + Returns: a zmq::message_t holding the encoded multipart data. + + Throws: std::range_error is thrown if the size of any single part + can not fit in an unsigned 32 bit integer. + + The encoding is compatible with that used by the CZMQ function + zmsg_encode(), see https://rfc.zeromq.org/spec/50/. + Each part consists of a size followed by the data. + These are placed contiguously into the output message. A part of + size less than 255 bytes will have a single byte size value. + Larger parts will have a five byte size value with the first byte + set to 0xFF and the remaining four bytes holding the size of the + part's data. +*/ +template::value + && (std::is_same, message_t>::value + || detail::is_buffer>::value)>::type +#endif + > +message_t encode(const Range &parts) +{ + size_t mmsg_size = 0; + + // First pass check sizes + for (const auto &part : parts) { + const size_t part_size = part.size(); + if (part_size > (std::numeric_limits::max)()) { + // Size value must fit into uint32_t. + throw std::range_error("Invalid size, message part too large"); + } + const size_t count_size = + part_size < (std::numeric_limits::max)() ? 1 : 5; + mmsg_size += part_size + count_size; + } + + message_t encoded(mmsg_size); + unsigned char *buf = encoded.data(); + for (const auto &part : parts) { + const uint32_t part_size = static_cast(part.size()); + const unsigned char *part_data = + static_cast(part.data()); + + if (part_size < (std::numeric_limits::max)()) { + // small part + *buf++ = (unsigned char) part_size; + } else { + // big part + *buf++ = (std::numeric_limits::max)(); + detail::write_network_order(buf, part_size); + buf += sizeof(part_size); + } + std::memcpy(buf, part_data, part_size); + buf += part_size; + } + + assert(static_cast(buf - encoded.data()) == mmsg_size); + return encoded; +} + +/* Decode an encoded message to multiple parts. + + The given output iterator must be a ForwardIterator to a container + holding zmq::message_t such as a zmq::multipart_t or various STL + containers. + + Returns the ForwardIterator advanced once past the last decoded + part. + + Throws: a std::out_of_range is thrown if the encoded part sizes + lead to exceeding the message data bounds. + + The decoding assumes the message is encoded in the manner + performed by zmq::encode(), see https://rfc.zeromq.org/spec/50/. + */ +template OutputIt decode(const message_t &encoded, OutputIt out) +{ + const unsigned char *source = encoded.data(); + const unsigned char *const limit = source + encoded.size(); + + while (source < limit) { + size_t part_size = *source++; + if (part_size == (std::numeric_limits::max)()) { + if (static_cast(limit - source) < sizeof(uint32_t)) { + throw std::out_of_range( + "Malformed encoding, overflow in reading size"); + } + part_size = detail::read_u32_network_order(source); + // the part size is allowed to be less than 0xFF + source += sizeof(uint32_t); + } + + if (static_cast(limit - source) < part_size) { + throw std::out_of_range("Malformed encoding, overflow in reading part"); + } + *out = message_t(source, part_size); + ++out; + source += part_size; + } + + assert(source == limit); + return out; +} + +#endif + + +#ifdef ZMQ_HAS_RVALUE_REFS + +/* + This class handles multipart messaging. It is the C++ equivalent of zmsg.h, + which is part of CZMQ (the high-level C binding). Furthermore, it is a major + improvement compared to zmsg.hpp, which is part of the examples in the ØMQ + Guide. Unnecessary copying is avoided by using move semantics to efficiently + add/remove parts. +*/ +class multipart_t +{ + private: + std::deque m_parts; + + public: + typedef std::deque::value_type value_type; + + typedef std::deque::iterator iterator; + typedef std::deque::const_iterator const_iterator; + + typedef std::deque::reverse_iterator reverse_iterator; + typedef std::deque::const_reverse_iterator const_reverse_iterator; + + // Default constructor + multipart_t() {} + + // Construct from socket receive + multipart_t(socket_ref socket) { recv(socket); } + + // Construct from memory block + multipart_t(const void *src, size_t size) { addmem(src, size); } + + // Construct from string + multipart_t(const std::string &string) { addstr(string); } + + // Construct from message part + multipart_t(message_t &&message) { add(std::move(message)); } + + // Move constructor + multipart_t(multipart_t &&other) ZMQ_NOTHROW { m_parts = std::move(other.m_parts); } + + // Move assignment operator + multipart_t &operator=(multipart_t &&other) ZMQ_NOTHROW + { + m_parts = std::move(other.m_parts); + return *this; + } + + // Destructor + virtual ~multipart_t() { clear(); } + + message_t &operator[](size_t n) { return m_parts[n]; } + + const message_t &operator[](size_t n) const { return m_parts[n]; } + + message_t &at(size_t n) { return m_parts.at(n); } + + const message_t &at(size_t n) const { return m_parts.at(n); } + + iterator begin() { return m_parts.begin(); } + + const_iterator begin() const { return m_parts.begin(); } + + const_iterator cbegin() const { return m_parts.cbegin(); } + + reverse_iterator rbegin() { return m_parts.rbegin(); } + + const_reverse_iterator rbegin() const { return m_parts.rbegin(); } + + iterator end() { return m_parts.end(); } + + const_iterator end() const { return m_parts.end(); } + + const_iterator cend() const { return m_parts.cend(); } + + reverse_iterator rend() { return m_parts.rend(); } + + const_reverse_iterator rend() const { return m_parts.rend(); } + + // Delete all parts + void clear() { m_parts.clear(); } + + // Get number of parts + size_t size() const { return m_parts.size(); } + + // Check if number of parts is zero + bool empty() const { return m_parts.empty(); } + + // Receive multipart message from socket + bool recv(socket_ref socket, int flags = 0) + { + clear(); + bool more = true; + while (more) { + message_t message; +#ifdef ZMQ_CPP11 + if (!socket.recv(message, static_cast(flags))) + return false; +#else + if (!socket.recv(&message, flags)) + return false; +#endif + more = message.more(); + add(std::move(message)); + } + return true; + } + + // Send multipart message to socket + bool send(socket_ref socket, int flags = 0) + { + flags &= ~(ZMQ_SNDMORE); + bool more = size() > 0; + while (more) { + message_t message = pop(); + more = size() > 0; +#ifdef ZMQ_CPP11 + if (!socket.send(message, static_cast( + (more ? ZMQ_SNDMORE : 0) | flags))) + return false; +#else + if (!socket.send(message, (more ? ZMQ_SNDMORE : 0) | flags)) + return false; +#endif + } + clear(); + return true; + } + + // Concatenate other multipart to front + void prepend(multipart_t &&other) + { + while (!other.empty()) + push(other.remove()); + } + + // Concatenate other multipart to back + void append(multipart_t &&other) + { + while (!other.empty()) + add(other.pop()); + } + + // Push memory block to front + void pushmem(const void *src, size_t size) + { + m_parts.push_front(message_t(src, size)); + } + + // Push memory block to back + void addmem(const void *src, size_t size) + { + m_parts.push_back(message_t(src, size)); + } + + // Push string to front + void pushstr(const std::string &string) + { + m_parts.push_front(message_t(string.data(), string.size())); + } + + // Push string to back + void addstr(const std::string &string) + { + m_parts.push_back(message_t(string.data(), string.size())); + } + + // Push type (fixed-size) to front + template void pushtyp(const T &type) + { + static_assert(!std::is_same::value, + "Use pushstr() instead of pushtyp()"); + m_parts.push_front(message_t(&type, sizeof(type))); + } + + // Push type (fixed-size) to back + template void addtyp(const T &type) + { + static_assert(!std::is_same::value, + "Use addstr() instead of addtyp()"); + m_parts.push_back(message_t(&type, sizeof(type))); + } + + // Push message part to front + void push(message_t &&message) { m_parts.push_front(std::move(message)); } + + // Push message part to back + void add(message_t &&message) { m_parts.push_back(std::move(message)); } + + // Alias to allow std::back_inserter() + void push_back(message_t &&message) { m_parts.push_back(std::move(message)); } + + // Pop string from front + std::string popstr() + { + std::string string(m_parts.front().data(), m_parts.front().size()); + m_parts.pop_front(); + return string; + } + + // Pop type (fixed-size) from front + template T poptyp() + { + static_assert(!std::is_same::value, + "Use popstr() instead of poptyp()"); + if (sizeof(T) != m_parts.front().size()) + throw std::runtime_error( + "Invalid type, size does not match the message size"); + T type = *m_parts.front().data(); + m_parts.pop_front(); + return type; + } + + // Pop message part from front + message_t pop() + { + message_t message = std::move(m_parts.front()); + m_parts.pop_front(); + return message; + } + + // Pop message part from back + message_t remove() + { + message_t message = std::move(m_parts.back()); + m_parts.pop_back(); + return message; + } + + // get message part from front + const message_t &front() { return m_parts.front(); } + + // get message part from back + const message_t &back() { return m_parts.back(); } + + // Get pointer to a specific message part + const message_t *peek(size_t index) const { return &m_parts[index]; } + + // Get a string copy of a specific message part + std::string peekstr(size_t index) const + { + std::string string(m_parts[index].data(), m_parts[index].size()); + return string; + } + + // Peek type (fixed-size) from front + template T peektyp(size_t index) const + { + static_assert(!std::is_same::value, + "Use peekstr() instead of peektyp()"); + if (sizeof(T) != m_parts[index].size()) + throw std::runtime_error( + "Invalid type, size does not match the message size"); + T type = *m_parts[index].data(); + return type; + } + + // Create multipart from type (fixed-size) + template static multipart_t create(const T &type) + { + multipart_t multipart; + multipart.addtyp(type); + return multipart; + } + + // Copy multipart + multipart_t clone() const + { + multipart_t multipart; + for (size_t i = 0; i < size(); i++) + multipart.addmem(m_parts[i].data(), m_parts[i].size()); + return multipart; + } + + // Dump content to string + std::string str() const + { + std::stringstream ss; + for (size_t i = 0; i < m_parts.size(); i++) { + const unsigned char *data = m_parts[i].data(); + size_t size = m_parts[i].size(); + + // Dump the message as text or binary + bool isText = true; + for (size_t j = 0; j < size; j++) { + if (data[j] < 32 || data[j] > 127) { + isText = false; + break; + } + } + ss << "\n[" << std::dec << std::setw(3) << std::setfill('0') << size + << "] "; + if (size >= 1000) { + ss << "... (too big to print)"; + continue; + } + for (size_t j = 0; j < size; j++) { + if (isText) + ss << static_cast(data[j]); + else + ss << std::hex << std::setw(2) << std::setfill('0') + << static_cast(data[j]); + } + } + return ss.str(); + } + + // Check if equal to other multipart + bool equal(const multipart_t *other) const ZMQ_NOTHROW + { + return *this == *other; + } + + bool operator==(const multipart_t &other) const ZMQ_NOTHROW + { + if (size() != other.size()) + return false; + for (size_t i = 0; i < size(); i++) + if (at(i) != other.at(i)) + return false; + return true; + } + + bool operator!=(const multipart_t &other) const ZMQ_NOTHROW + { + return !(*this == other); + } + +#ifdef ZMQ_CPP11 + + // Return single part message_t encoded from this multipart_t. + message_t encode() const { return zmq::encode(*this); } + + // Decode encoded message into multiple parts and append to self. + void decode_append(const message_t &encoded) + { + zmq::decode(encoded, std::back_inserter(*this)); + } + + // Return a new multipart_t containing the decoded message_t. + static multipart_t decode(const message_t &encoded) + { + multipart_t tmp; + zmq::decode(encoded, std::back_inserter(tmp)); + return tmp; + } + +#endif + + private: + // Disable implicit copying (moving is more efficient) + multipart_t(const multipart_t &other) ZMQ_DELETED_FUNCTION; + void operator=(const multipart_t &other) ZMQ_DELETED_FUNCTION; +}; // class multipart_t + +inline std::ostream &operator<<(std::ostream &os, const multipart_t &msg) +{ + return os << msg.str(); +} + +#endif // ZMQ_HAS_RVALUE_REFS + +#if defined(ZMQ_BUILD_DRAFT_API) && defined(ZMQ_CPP11) && defined(ZMQ_HAVE_POLLER) +class active_poller_t +{ + public: + active_poller_t() = default; + ~active_poller_t() = default; + + active_poller_t(const active_poller_t &) = delete; + active_poller_t &operator=(const active_poller_t &) = delete; + + active_poller_t(active_poller_t &&src) = default; + active_poller_t &operator=(active_poller_t &&src) = default; + + using handler_type = std::function; + + void add(zmq::socket_ref socket, event_flags events, handler_type handler) + { + if (!handler) + throw std::invalid_argument("null handler in active_poller_t::add"); + auto ret = handlers.emplace( + socket, std::make_shared(std::move(handler))); + if (!ret.second) + throw error_t(EINVAL); // already added + try { + base_poller.add(socket, events, ret.first->second.get()); + need_rebuild = true; + } + catch (...) { + // rollback + handlers.erase(socket); + throw; + } + } + + void remove(zmq::socket_ref socket) + { + base_poller.remove(socket); + handlers.erase(socket); + need_rebuild = true; + } + + void modify(zmq::socket_ref socket, event_flags events) + { + base_poller.modify(socket, events); + } + + size_t wait(std::chrono::milliseconds timeout) + { + if (need_rebuild) { + poller_events.resize(handlers.size()); + poller_handlers.clear(); + poller_handlers.reserve(handlers.size()); + for (const auto &handler : handlers) { + poller_handlers.push_back(handler.second); + } + need_rebuild = false; + } + const auto count = base_poller.wait_all(poller_events, timeout); + std::for_each(poller_events.begin(), + poller_events.begin() + static_cast(count), + [](decltype(base_poller)::event_type &event) { + assert(event.user_data != nullptr); + (*event.user_data)(event.events); + }); + return count; + } + + ZMQ_NODISCARD bool empty() const noexcept { return handlers.empty(); } + + size_t size() const noexcept { return handlers.size(); } + + private: + bool need_rebuild{false}; + + poller_t base_poller{}; + std::unordered_map> handlers{}; + std::vector poller_events{}; + std::vector> poller_handlers{}; +}; // class active_poller_t +#endif // defined(ZMQ_BUILD_DRAFT_API) && defined(ZMQ_CPP11) && defined(ZMQ_HAVE_POLLER) + + +} // namespace zmq + +#endif // __ZMQ_ADDON_HPP_INCLUDED__ diff --git a/3rdparty/filesystem/fwd.h b/3rdparty/filesystem/fwd.h deleted file mode 100644 index 3552199e2..000000000 --- a/3rdparty/filesystem/fwd.h +++ /dev/null @@ -1,24 +0,0 @@ -/* - fwd.h -- Forward declarations for path.h and resolver.h - - Copyright (c) 2015 Wenzel Jakob - - All rights reserved. Use of this source code is governed by a - BSD-style license that can be found in the LICENSE file. -*/ - -#pragma once - -#if !defined(NAMESPACE_BEGIN) -#define NAMESPACE_BEGIN(name) namespace name { -#endif -#if !defined(NAMESPACE_END) -#define NAMESPACE_END(name) } -#endif - -NAMESPACE_BEGIN(filesystem) - -class path; -class resolver; - -NAMESPACE_END(filesystem) diff --git a/3rdparty/filesystem/path.h b/3rdparty/filesystem/path.h deleted file mode 100644 index a5b582c5f..000000000 --- a/3rdparty/filesystem/path.h +++ /dev/null @@ -1,339 +0,0 @@ -/* - path.h -- A simple class for manipulating paths on Linux/Windows/Mac OS - - Copyright (c) 2015 Wenzel Jakob - - All rights reserved. Use of this source code is governed by a - BSD-style license that can be found in the LICENSE file. -*/ - -#pragma once - -#include "fwd.h" -#include -#include -#include -#include -#include -#include -#include -#include - -#if defined(_WIN32) -# include -#else -# include -#endif -#include - -#if defined(__linux) -# include -#endif - -NAMESPACE_BEGIN(filesystem) - -/** - * \brief Simple class for manipulating paths on Linux/Windows/Mac OS - * - * This class is just a temporary workaround to avoid the heavy boost - * dependency until boost::filesystem is integrated into the standard template - * library at some point in the future. - */ -class path { -public: - enum path_type { - windows_path = 0, - posix_path = 1, -#if defined(_WIN32) - native_path = windows_path -#else - native_path = posix_path -#endif - }; - - path() : m_type(native_path), m_absolute(false) { } - - path(const path &path) - : m_type(path.m_type), m_path(path.m_path), m_absolute(path.m_absolute) {} - - path(path &&path) - : m_type(path.m_type), m_path(std::move(path.m_path)), - m_absolute(path.m_absolute) {} - - path(const char *string) { set(string); } - - path(const std::string &string) { set(string); } - -#if defined(_WIN32) - path(const std::wstring &wstring) { set(wstring); } - path(const wchar_t *wstring) { set(wstring); } -#endif - - size_t length() const { return m_path.size(); } - - bool empty() const { return m_path.empty(); } - - bool is_absolute() const { return m_absolute; } - - path make_absolute() const { -#if !defined(_WIN32) - char temp[PATH_MAX]; - if (realpath(str().c_str(), temp) == NULL) - throw std::runtime_error("Internal error in realpath(): " + std::string(strerror(errno))); - return path(temp); -#else - std::wstring value = wstr(), out(MAX_PATH, '\0'); - DWORD length = GetFullPathNameW(value.c_str(), MAX_PATH, &out[0], NULL); - if (length == 0) - throw std::runtime_error("Internal error in realpath(): " + std::to_string(GetLastError())); - return path(out.substr(0, length)); -#endif - } - - bool exists() const { -#if defined(_WIN32) - return GetFileAttributesW(wstr().c_str()) != INVALID_FILE_ATTRIBUTES; -#else - struct stat sb; - return stat(str().c_str(), &sb) == 0; -#endif - } - - size_t file_size() const { -#if defined(_WIN32) - struct _stati64 sb; - if (_wstati64(wstr().c_str(), &sb) != 0) - throw std::runtime_error("path::file_size(): cannot stat file \"" + str() + "\"!"); -#else - struct stat sb; - if (stat(str().c_str(), &sb) != 0) - throw std::runtime_error("path::file_size(): cannot stat file \"" + str() + "\"!"); -#endif - return (size_t) sb.st_size; - } - - bool is_directory() const { -#if defined(_WIN32) - DWORD result = GetFileAttributesW(wstr().c_str()); - if (result == INVALID_FILE_ATTRIBUTES) - return false; - return (result & FILE_ATTRIBUTE_DIRECTORY) != 0; -#else - struct stat sb; - if (stat(str().c_str(), &sb)) - return false; - return S_ISDIR(sb.st_mode); -#endif - } - - bool is_file() const { -#if defined(_WIN32) - DWORD attr = GetFileAttributesW(wstr().c_str()); - return (attr != INVALID_FILE_ATTRIBUTES && (attr & FILE_ATTRIBUTE_DIRECTORY) == 0); -#else - struct stat sb; - if (stat(str().c_str(), &sb)) - return false; - return S_ISREG(sb.st_mode); -#endif - } - - std::string extension() const { - const std::string &name = filename(); - size_t pos = name.find_last_of("."); - if (pos == std::string::npos) - return ""; - return name.substr(pos+1); - } - - std::string filename() const { - if (empty()) - return ""; - const std::string &last = m_path[m_path.size()-1]; - return last; - } - - path parent_path() const { - path result; - result.m_absolute = m_absolute; - - if (m_path.empty()) { - if (!m_absolute) - result.m_path.push_back(".."); - } else { - size_t until = m_path.size() - 1; - for (size_t i = 0; i < until; ++i) - result.m_path.push_back(m_path[i]); - } - return result; - } - - path operator/(const path &other) const { - if (other.m_absolute) - throw std::runtime_error("path::operator/(): expected a relative path!"); - if (m_type != other.m_type) - throw std::runtime_error("path::operator/(): expected a path of the same type!"); - - path result(*this); - - for (size_t i=0; i= 2 && std::isalpha(str[0]) && str[1] == ':'; - } else { - m_path = tokenize(str, "/"); - m_absolute = !str.empty() && str[0] == '/'; - } - } - - path &operator=(const path &path) { - m_type = path.m_type; - m_path = path.m_path; - m_absolute = path.m_absolute; - return *this; - } - - path &operator=(path &&path) { - if (this != &path) { - m_type = path.m_type; - m_path = std::move(path.m_path); - m_absolute = path.m_absolute; - } - return *this; - } - - friend std::ostream &operator<<(std::ostream &os, const path &path) { - os << path.str(); - return os; - } - - bool remove_file() { -#if !defined(_WIN32) - return std::remove(str().c_str()) == 0; -#else - return DeleteFileW(wstr().c_str()) != 0; -#endif - } - - bool resize_file(size_t target_length) { -#if !defined(_WIN32) - return ::truncate(str().c_str(), (off_t) target_length) == 0; -#else - HANDLE handle = CreateFileW(wstr().c_str(), GENERIC_WRITE, 0, nullptr, 0, FILE_ATTRIBUTE_NORMAL, nullptr); - if (handle == INVALID_HANDLE_VALUE) - return false; - LARGE_INTEGER size; - size.QuadPart = (LONGLONG) target_length; - if (SetFilePointerEx(handle, size, NULL, FILE_BEGIN) == 0) { - CloseHandle(handle); - return false; - } - if (SetEndOfFile(handle) == 0) { - CloseHandle(handle); - return false; - } - CloseHandle(handle); - return true; -#endif - } - - static path getcwd() { -#if !defined(_WIN32) - char temp[PATH_MAX]; - if (::getcwd(temp, PATH_MAX) == NULL) - throw std::runtime_error("Internal error in getcwd(): " + std::string(strerror(errno))); - return path(temp); -#else - std::wstring temp(MAX_PATH, '\0'); - if (!_wgetcwd(&temp[0], MAX_PATH)) - throw std::runtime_error("Internal error in getcwd(): " + std::to_string(GetLastError())); - return path(temp.c_str()); -#endif - } - -#if defined(_WIN32) - std::wstring wstr(path_type type = native_path) const { - std::string temp = str(type); - int size = MultiByteToWideChar(CP_UTF8, 0, &temp[0], (int)temp.size(), NULL, 0); - std::wstring result(size, 0); - MultiByteToWideChar(CP_UTF8, 0, &temp[0], (int)temp.size(), &result[0], size); - return result; - } - - - void set(const std::wstring &wstring, path_type type = native_path) { - std::string string; - if (!wstring.empty()) { - int size = WideCharToMultiByte(CP_UTF8, 0, &wstring[0], (int)wstring.size(), - NULL, 0, NULL, NULL); - string.resize(size, 0); - WideCharToMultiByte(CP_UTF8, 0, &wstring[0], (int)wstring.size(), - &string[0], size, NULL, NULL); - } - set(string, type); - } - - path &operator=(const std::wstring &str) { set(str); return *this; } -#endif - - bool operator==(const path &p) const { return p.m_path == m_path; } - bool operator!=(const path &p) const { return p.m_path != m_path; } - -protected: - static std::vector tokenize(const std::string &string, const std::string &delim) { - std::string::size_type lastPos = 0, pos = string.find_first_of(delim, lastPos); - std::vector tokens; - - while (lastPos != std::string::npos) { - if (pos != lastPos) - tokens.push_back(string.substr(lastPos, pos - lastPos)); - lastPos = pos; - if (lastPos == std::string::npos || lastPos + 1 == string.length()) - break; - pos = string.find_first_of(delim, ++lastPos); - } - - return tokens; - } - -protected: - path_type m_type; - std::vector m_path; - bool m_absolute; -}; - -inline bool create_directory(const path& p) { -#if defined(_WIN32) - return CreateDirectoryW(p.wstr().c_str(), NULL) != 0; -#else - return mkdir(p.str().c_str(), S_IRUSR | S_IWUSR | S_IXUSR) == 0; -#endif -} - -NAMESPACE_END(filesystem) diff --git a/3rdparty/filesystem/resolver.h b/3rdparty/filesystem/resolver.h deleted file mode 100644 index 0c7576841..000000000 --- a/3rdparty/filesystem/resolver.h +++ /dev/null @@ -1,72 +0,0 @@ -/* - resolver.h -- A simple class for cross-platform path resolution - - Copyright (c) 2015 Wenzel Jakob - - All rights reserved. Use of this source code is governed by a - BSD-style license that can be found in the LICENSE file. -*/ - -#pragma once - -#include "path.h" - -NAMESPACE_BEGIN(filesystem) - -/** - * \brief Simple class for resolving paths on Linux/Windows/Mac OS - * - * This convenience class looks for a file or directory given its name - * and a set of search paths. The implementation walks through the - * search paths in order and stops once the file is found. - */ -class resolver { -public: - typedef std::vector::iterator iterator; - typedef std::vector::const_iterator const_iterator; - - resolver() { - m_paths.push_back(path::getcwd()); - } - - size_t size() const { return m_paths.size(); } - - iterator begin() { return m_paths.begin(); } - iterator end() { return m_paths.end(); } - - const_iterator begin() const { return m_paths.begin(); } - const_iterator end() const { return m_paths.end(); } - - void erase(iterator it) { m_paths.erase(it); } - - void prepend(const path &path) { m_paths.insert(m_paths.begin(), path); } - void append(const path &path) { m_paths.push_back(path); } - const path &operator[](size_t index) const { return m_paths[index]; } - path &operator[](size_t index) { return m_paths[index]; } - - path resolve(const path &value) const { - for (const_iterator it = m_paths.begin(); it != m_paths.end(); ++it) { - path combined = *it / value; - if (combined.exists()) - return combined; - } - return value; - } - - friend std::ostream &operator<<(std::ostream &os, const resolver &r) { - os << "resolver[" << std::endl; - for (size_t i = 0; i < r.m_paths.size(); ++i) { - os << " \"" << r.m_paths[i] << "\""; - if (i + 1 < r.m_paths.size()) - os << ","; - os << std::endl; - } - os << "]"; - return os; - } - -private: - std::vector m_paths; -}; - -NAMESPACE_END(filesystem) diff --git a/include/behaviortree_cpp_v3/flatbuffers/base.h b/3rdparty/flatbuffers/base.h similarity index 76% rename from include/behaviortree_cpp_v3/flatbuffers/base.h rename to 3rdparty/flatbuffers/base.h index 54a51aacb..1c19dde98 100644 --- a/include/behaviortree_cpp_v3/flatbuffers/base.h +++ b/3rdparty/flatbuffers/base.h @@ -32,7 +32,7 @@ #include #include -#if defined(ARDUINO) && !defined(ARDUINOSTL_M_H) +#if defined(ARDUINO) && !defined(ARDUINOSTL_M_H) && defined(__AVR__) #include #else #include @@ -43,6 +43,7 @@ #include #include #include +#include #include #include @@ -50,10 +51,6 @@ #include #endif -#ifdef _STLPORT_VERSION - #define FLATBUFFERS_CPP98_STL -#endif - #ifdef __ANDROID__ #include #endif @@ -142,9 +139,9 @@ #endif #endif // !defined(FLATBUFFERS_LITTLEENDIAN) -#define FLATBUFFERS_VERSION_MAJOR 1 -#define FLATBUFFERS_VERSION_MINOR 12 -#define FLATBUFFERS_VERSION_REVISION 0 +#define FLATBUFFERS_VERSION_MAJOR 24 +#define FLATBUFFERS_VERSION_MINOR 3 +#define FLATBUFFERS_VERSION_REVISION 25 #define FLATBUFFERS_STRING_EXPAND(X) #X #define FLATBUFFERS_STRING(X) FLATBUFFERS_STRING_EXPAND(X) namespace flatbuffers { @@ -158,7 +155,7 @@ namespace flatbuffers { #define FLATBUFFERS_FINAL_CLASS final #define FLATBUFFERS_OVERRIDE override #define FLATBUFFERS_EXPLICIT_CPP11 explicit - #define FLATBUFFERS_VTABLE_UNDERLYING_TYPE : flatbuffers::voffset_t + #define FLATBUFFERS_VTABLE_UNDERLYING_TYPE : ::flatbuffers::voffset_t #else #define FLATBUFFERS_FINAL_CLASS #define FLATBUFFERS_OVERRIDE @@ -237,16 +234,26 @@ namespace flatbuffers { } #define FLATBUFFERS_HAS_STRING_VIEW 1 // Check for absl::string_view - #elif __has_include("absl/strings/string_view.h") - #include "absl/strings/string_view.h" - namespace flatbuffers { - typedef absl::string_view string_view; - } - #define FLATBUFFERS_HAS_STRING_VIEW 1 + #elif __has_include("absl/strings/string_view.h") && \ + __has_include("absl/base/config.h") && \ + (__cplusplus >= 201411) + #include "absl/base/config.h" + #if !defined(ABSL_USES_STD_STRING_VIEW) + #include "absl/strings/string_view.h" + namespace flatbuffers { + typedef absl::string_view string_view; + } + #define FLATBUFFERS_HAS_STRING_VIEW 1 + #endif #endif #endif // __has_include #endif // !FLATBUFFERS_HAS_STRING_VIEW +#ifndef FLATBUFFERS_GENERAL_HEAP_ALLOC_OK + // Allow heap allocations to be used + #define FLATBUFFERS_GENERAL_HEAP_ALLOC_OK 1 +#endif // !FLATBUFFERS_GENERAL_HEAP_ALLOC_OK + #ifndef FLATBUFFERS_HAS_NEW_STRTOD // Modern (C++11) strtod and strtof functions are available for use. // 1) nan/inf strings as argument of strtod; @@ -259,9 +266,12 @@ namespace flatbuffers { #endif // !FLATBUFFERS_HAS_NEW_STRTOD #ifndef FLATBUFFERS_LOCALE_INDEPENDENT - // Enable locale independent functions {strtof_l, strtod_l,strtoll_l, strtoull_l}. - #if ((defined(_MSC_VER) && _MSC_VER >= 1800) || \ - (defined(_XOPEN_VERSION) && (_XOPEN_VERSION>=700)) && (!defined(__ANDROID_API__) || (defined(__ANDROID_API__) && (__ANDROID_API__>=21)))) + // Enable locale independent functions {strtof_l, strtod_l,strtoll_l, + // strtoull_l}. + #if (defined(_MSC_VER) && _MSC_VER >= 1800) || \ + (defined(__ANDROID_API__) && __ANDROID_API__>= 21) || \ + (defined(_XOPEN_VERSION) && (_XOPEN_VERSION >= 700)) && \ + (!defined(__Fuchsia__) && !defined(__ANDROID_API__)) #define FLATBUFFERS_LOCALE_INDEPENDENT 1 #else #define FLATBUFFERS_LOCALE_INDEPENDENT 0 @@ -269,27 +279,29 @@ namespace flatbuffers { #endif // !FLATBUFFERS_LOCALE_INDEPENDENT // Suppress Undefined Behavior Sanitizer (recoverable only). Usage: -// - __supress_ubsan__("undefined") -// - __supress_ubsan__("signed-integer-overflow") +// - FLATBUFFERS_SUPPRESS_UBSAN("undefined") +// - FLATBUFFERS_SUPPRESS_UBSAN("signed-integer-overflow") #if defined(__clang__) && (__clang_major__ > 3 || (__clang_major__ == 3 && __clang_minor__ >=7)) - #define __supress_ubsan__(type) __attribute__((no_sanitize(type))) + #define FLATBUFFERS_SUPPRESS_UBSAN(type) __attribute__((no_sanitize(type))) #elif defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 409) - #define __supress_ubsan__(type) __attribute__((no_sanitize_undefined)) + #define FLATBUFFERS_SUPPRESS_UBSAN(type) __attribute__((no_sanitize_undefined)) #else - #define __supress_ubsan__(type) + #define FLATBUFFERS_SUPPRESS_UBSAN(type) #endif -// This is constexpr function used for checking compile-time constants. -// Avoid `#pragma warning(disable: 4127) // C4127: expression is constant`. -template FLATBUFFERS_CONSTEXPR inline bool IsConstTrue(T t) { - return !!t; +namespace flatbuffers { + // This is constexpr function used for checking compile-time constants. + // Avoid `#pragma warning(disable: 4127) // C4127: expression is constant`. + template FLATBUFFERS_CONSTEXPR inline bool IsConstTrue(T t) { + return !!t; + } } // Enable C++ attribute [[]] if std:c++17 or higher. #if ((__cplusplus >= 201703L) \ || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201703L))) // All attributes unknown to an implementation are ignored without causing an error. - #define FLATBUFFERS_ATTRIBUTE(attr) [[attr]] + #define FLATBUFFERS_ATTRIBUTE(attr) attr #define FLATBUFFERS_FALLTHROUGH() [[fallthrough]] #else @@ -314,9 +326,11 @@ namespace flatbuffers { // Also, using a consistent offset type maintains compatibility of serialized // offset values between 32bit and 64bit systems. typedef uint32_t uoffset_t; +typedef uint64_t uoffset64_t; // Signed offsets for references that can go in both directions. typedef int32_t soffset_t; +typedef int64_t soffset64_t; // Offset/index used in v-tables, can be changed to uint8_t in // format forks to save a bit of space if desired. @@ -325,10 +339,23 @@ typedef uint16_t voffset_t; typedef uintmax_t largest_scalar_t; // In 32bits, this evaluates to 2GB - 1 -#define FLATBUFFERS_MAX_BUFFER_SIZE ((1ULL << (sizeof(::flatbuffers::soffset_t) * 8 - 1)) - 1) +#define FLATBUFFERS_MAX_BUFFER_SIZE std::numeric_limits<::flatbuffers::soffset_t>::max() +#define FLATBUFFERS_MAX_64_BUFFER_SIZE std::numeric_limits<::flatbuffers::soffset64_t>::max() + +// The minimum size buffer that can be a valid flatbuffer. +// Includes the offset to the root table (uoffset_t), the offset to the vtable +// of the root table (soffset_t), the size of the vtable (uint16_t), and the +// size of the referring table (uint16_t). +#define FLATBUFFERS_MIN_BUFFER_SIZE sizeof(uoffset_t) + sizeof(soffset_t) + \ + sizeof(uint16_t) + sizeof(uint16_t) // We support aligning the contents of buffers up to this size. -#define FLATBUFFERS_MAX_ALIGNMENT 16 +#ifndef FLATBUFFERS_MAX_ALIGNMENT + #define FLATBUFFERS_MAX_ALIGNMENT 32 +#endif + +/// @brief The length of a FlatBuffer file header. +static const size_t kFileIdentifierLength = 4; inline bool VerifyAlignmentRequirements(size_t align, size_t min_align = 1) { return (min_align <= align) && (align <= (FLATBUFFERS_MAX_ALIGNMENT)) && @@ -336,7 +363,6 @@ inline bool VerifyAlignmentRequirements(size_t align, size_t min_align = 1) { } #if defined(_MSC_VER) - #pragma warning(disable: 4351) // C4351: new behavior: elements of array ... will be default initialized #pragma warning(push) #pragma warning(disable: 4127) // C4127: conditional expression is constant #endif @@ -397,7 +423,7 @@ template T EndianScalar(T t) { template // UBSAN: C++ aliasing type rules, see std::bit_cast<> for details. -__supress_ubsan__("alignment") +FLATBUFFERS_SUPPRESS_UBSAN("alignment") T ReadScalar(const void *p) { return EndianScalar(*reinterpret_cast(p)); } @@ -411,13 +437,13 @@ T ReadScalar(const void *p) { template // UBSAN: C++ aliasing type rules, see std::bit_cast<> for details. -__supress_ubsan__("alignment") +FLATBUFFERS_SUPPRESS_UBSAN("alignment") void WriteScalar(void *p, T t) { *reinterpret_cast(p) = EndianScalar(t); } template struct Offset; -template __supress_ubsan__("alignment") void WriteScalar(void *p, Offset t) { +template FLATBUFFERS_SUPPRESS_UBSAN("alignment") void WriteScalar(void *p, Offset t) { *reinterpret_cast(p) = EndianScalar(t.o); } @@ -428,10 +454,43 @@ template __supress_ubsan__("alignment") void WriteScalar(void *p, Of // Computes how many bytes you'd have to pad to be able to write an // "scalar_size" scalar if the buffer had grown to "buf_size" (downwards in // memory). -__supress_ubsan__("unsigned-integer-overflow") +FLATBUFFERS_SUPPRESS_UBSAN("unsigned-integer-overflow") inline size_t PaddingBytes(size_t buf_size, size_t scalar_size) { return ((~buf_size) + 1) & (scalar_size - 1); } +// Generic 'operator==' with conditional specialisations. +// T e - new value of a scalar field. +// T def - default of scalar (is known at compile-time). +template inline bool IsTheSameAs(T e, T def) { return e == def; } + +#if defined(FLATBUFFERS_NAN_DEFAULTS) && \ + defined(FLATBUFFERS_HAS_NEW_STRTOD) && (FLATBUFFERS_HAS_NEW_STRTOD > 0) +// Like `operator==(e, def)` with weak NaN if T=(float|double). +template inline bool IsFloatTheSameAs(T e, T def) { + return (e == def) || ((def != def) && (e != e)); +} +template<> inline bool IsTheSameAs(float e, float def) { + return IsFloatTheSameAs(e, def); +} +template<> inline bool IsTheSameAs(double e, double def) { + return IsFloatTheSameAs(e, def); +} +#endif + +// Check 'v' is out of closed range [low; high]. +// Workaround for GCC warning [-Werror=type-limits]: +// comparison is always true due to limited range of data type. +template +inline bool IsOutRange(const T &v, const T &low, const T &high) { + return (v < low) || (high < v); +} + +// Check 'v' is in closed range [low; high]. +template +inline bool IsInRange(const T &v, const T &low, const T &high) { + return !IsOutRange(v, low, high); +} + } // namespace flatbuffers #endif // FLATBUFFERS_BASE_H_ diff --git a/3rdparty/lexy/CMakeLists.txt b/3rdparty/lexy/CMakeLists.txt new file mode 100644 index 000000000..a76693a9e --- /dev/null +++ b/3rdparty/lexy/CMakeLists.txt @@ -0,0 +1,79 @@ +# Copyright (C) 2020-2024 Jonathan Müller and lexy contributors +# SPDX-License-Identifier: BSL-1.0 + +cmake_minimum_required(VERSION 3.8) +project(lexy VERSION 2022.12.1 LANGUAGES CXX) + +set(LEXY_USER_CONFIG_HEADER "" CACHE FILEPATH "The user config header for lexy.") +option(LEXY_FORCE_CPP17 "Whether or not lexy should use C++17 even if compiler supports C++20." OFF) + +add_subdirectory(src) + +option(LEXY_ENABLE_INSTALL "whether or not to enable the install rule" ON) +if(LEXY_ENABLE_INSTALL) + include(CMakePackageConfigHelpers) + include(GNUInstallDirs) + + install(TARGETS lexy lexy_core lexy_file lexy_unicode lexy_ext _lexy_base lexy_dev + EXPORT ${PROJECT_NAME}Targets + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) + + install(EXPORT ${PROJECT_NAME}Targets + NAMESPACE foonathan:: + DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}") + + configure_package_config_file( + cmake/lexyConfig.cmake.in + "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" + INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}") + install(FILES "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" + DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}") + + # YYYY.MM.N1 is compatible with YYYY.MM.N2. + write_basic_package_version_file( + "${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake" + COMPATIBILITY SameMinorVersion) + + install(FILES "${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake" + DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}") + + install(DIRECTORY include/lexy include/lexy_ext + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} + FILES_MATCHING + PATTERN "*.hpp") +endif() + +if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) + cmake_minimum_required(VERSION 3.18) + option(LEXY_BUILD_BENCHMARKS "whether or not benchmarks should be built" OFF) + option(LEXY_BUILD_EXAMPLES "whether or not examples should be built" ON) + option(LEXY_BUILD_TESTS "whether or not tests should be built" ON) + option(LEXY_BUILD_DOCS "whether or not docs should be built" OFF) + option(LEXY_BUILD_PACKAGE "whether or not the package should be built" ON) + + if(LEXY_BUILD_PACKAGE) + set(package_files include/ src/ cmake/ CMakeLists.txt LICENSE) + add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/lexy-src.zip + COMMAND ${CMAKE_COMMAND} -E tar c ${CMAKE_CURRENT_BINARY_DIR}/lexy-src.zip --format=zip -- ${package_files} + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} + DEPENDS ${package_files}) + add_custom_target(lexy_package DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/lexy-src.zip) + endif() + + if(LEXY_BUILD_EXAMPLES) + add_subdirectory(examples) + endif() + if(LEXY_BUILD_BENCHMARKS) + add_subdirectory(benchmarks) + endif() + if(LEXY_BUILD_TESTS) + set(DOCTEST_NO_INSTALL ON) + enable_testing() + add_subdirectory(tests) + endif() + if(LEXY_BUILD_DOCS) + add_subdirectory(docs EXCLUDE_FROM_ALL) + endif() +endif() diff --git a/3rdparty/lexy/LICENSE b/3rdparty/lexy/LICENSE new file mode 100644 index 000000000..36b7cd93c --- /dev/null +++ b/3rdparty/lexy/LICENSE @@ -0,0 +1,23 @@ +Boost Software License - Version 1.0 - August 17th, 2003 + +Permission is hereby granted, free of charge, to any person or organization +obtaining a copy of the software and accompanying documentation covered by +this license (the "Software") to use, reproduce, display, distribute, +execute, and transmit the Software, and to prepare derivative works of the +Software, and to permit third-parties to whom the Software is furnished to +do so, all subject to the following: + +The copyright notices in the Software and this entire statement, including +the above license grant, this restriction and the following disclaimer, +must be included in all copies of the Software, in whole or in part, and +all derivative works of the Software, unless such copies or derivative +works are solely in the form of machine-executable object code generated by +a source language processor. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. diff --git a/3rdparty/lexy/README.adoc b/3rdparty/lexy/README.adoc new file mode 100644 index 000000000..6bc88487f --- /dev/null +++ b/3rdparty/lexy/README.adoc @@ -0,0 +1,175 @@ += lexy + +ifdef::env-github[] +image:https://img.shields.io/endpoint?url=https%3A%2F%2Fwww.jonathanmueller.dev%2Fproject%2Flexy%2Findex.json[Project Status,link=https://www.jonathanmueller.dev/project/] +image:https://github.com/foonathan/lexy/workflows/Main%20CI/badge.svg[Build Status] +image:https://img.shields.io/badge/try_it_online-blue[Playground,link=https://lexy.foonathan.net/playground] +endif::[] + +lexy is a parser combinator library for {cpp}17 and onwards. +It allows you to write a parser by specifying it in a convenient {cpp} DSL, +which gives you all the flexibility and control of a handwritten parser without any of the manual work. + +ifdef::env-github[] +*Documentation*: https://lexy.foonathan.net/[lexy.foonathan.net] +endif::[] + +.IPv4 address parser +-- +ifndef::env-github[] +[.godbolt-example] +.+++{{< svg "icons/play.svg" >}}+++ +endif::[] +[source,cpp] +---- +namespace dsl = lexy::dsl; + +// Parse an IPv4 address into a `std::uint32_t`. +struct ipv4_address +{ + // What is being matched. + static constexpr auto rule = []{ + // Match a sequence of (decimal) digits and convert it into a std::uint8_t. + auto octet = dsl::integer; + + // Match four of them separated by periods. + return dsl::times<4>(octet, dsl::sep(dsl::period)) + dsl::eof; + }(); + + // How the matched output is being stored. + static constexpr auto value + = lexy::callback([](std::uint8_t a, std::uint8_t b, std::uint8_t c, std::uint8_t d) { + return (a << 24) | (b << 16) | (c << 8) | d; + }); +}; +---- +-- + +== Features + +Full control:: + * *Describe the parser, not some abstract grammar*: + Unlike parser generators that use some table driven magic for parsing, lexy's grammar is just syntax sugar for a hand-written recursive descent parser. + The parsing algorithm does exactly what you've instructed it to do -- no more ambiguities or weird shift/reduce errors! + * *No implicit backtracking or lookahead*: + It will only backtrack when you say it should, and only lookahead when and how far you want it. + Don't worry about rules that have side-effects, they won't be executed unnecessarily thanks to the user-specified lookahead conditions. + https://lexy.foonathan.net/playground?example=peek[Try it online]. + * *Escape hatch for manual parsing*: + Sometimes you want to parse something that can't be expressed easily with lexy's facilities. + Don't worry, you can integrate a hand-written parser into the grammar at any point. + https://lexy.foonathan.net/playground/?example=scan[Try it online]. + * *Tracing*: + Figure out why the grammar isn't working the way you want it to. + https://lexy.foonathan.net/playground/?example=trace&mode=trace[Try it online]. + +Easily integrated:: + * *A pure {cpp} DSL*: + No need to use an external grammar file; embed the grammar directly in your {cpp} project using operator overloading and functions. + * *Bring your own data structures*: + You can directly store results into your own types and have full control over all heap allocations. + * *Fully `constexpr` parsing*: + You want to parse a string literal at compile-time? You can do so. + * *Minimal standard library dependencies*: + The core parsing library only depends on fundamental headers such as `` or ``; no big includes like `` or ``. + * *Header-only core library* (by necessity, not by choice -- it's `constexpr` after all). + +ifdef::env-github[Designed for text::] +ifndef::env-github[Designed for text (e.g. {{< github-example json >}}, {{< github-example xml >}}, {{< github-example email >}}) ::] + * *Unicode support*: parse UTF-8, UTF-16, or UTF-32, and access the Unicode character database to query char classes or perform case folding. + https://lexy.foonathan.net/playground?example=identifier-unicode[Try it online]. + * *Convenience*: + Built-in rules for parsing nested structures, quotes and escape sequences. + https://lexy.foonathan.net/playground?example=parenthesized[Try it online]. + * *Automatic whitespace skipping*: + No need to manually handle whitespace or comments. + https://lexy.foonathan.net/playground/?example=whitespace_comment[Try it online]. + +ifdef::env-github[Designed for programming languages::] +ifndef::env-github[Designed for programming languages (e.g. {{< github-example calculator >}}, {{< github-example shell >}})::] + * *Keyword and identifier parsing*: + Reserve a set of keywords that won't be matched as regular identifiers. + https://lexy.foonathan.net/playground/?example=reserved_identifier[Try it online]. + * *Operator parsing*: + Parse unary/binary operators with different precedences and associativity, including chained comparisons `a < b < c`. + https://lexy.foonathan.net/playground/?example=expr[Try it online]. + * *Automatic error recovery*: + Log an error, recover, and continue parsing! + https://lexy.foonathan.net/playground/?example=recover[Try it online]. + +ifdef::env-github[Designed for binary input::] +ifndef::env-github[Designed for binary input (e.g. {{< github-example protobuf >}})::] + * *Bytes*: Rules for parsing `N` bytes or Nbit big/little endian integer. + * *Bits*: Rules for parsing individual bit patterns. + * *Blobs*: Rules for parsing TLV formats. + +== FAQ + +Why should I use lexy over XYZ?:: + lexy is closest to other PEG parsers. + However, they usually do more implicit backtracking, which can hurt performance and you need to be very careful with rules that have side-effects. + This is not the case for lexy, where backtracking is controlled using branch conditions. + lexy also gives you a lot of control over error reporting, supports error recovery, special support for operator precedence parsing, and other advanced features. + + http://boost-spirit.com/home/[Boost.Spirit]::: + The main difference: it is not a Boost library. + In addition, Boost.Spirit is quite old and doesn't support e.g. non-common ranges as input. + Boost.Spirit also eagerly creates attributes from the rules, which can lead to nested tuples/variants while lexy uses callbacks which enables zero-copy parsing directly into your own data structure. + However, lexy's grammar is more verbose and designed to parser bigger grammars instead of the small one-off rules that Boost.Spirit is good at. + https://github.com/taocpp/PEGTL[PEGTL]::: + PEGTL is very similar and was a big inspiration. + The biggest difference is that lexy uses an operator based DSL instead of inheriting from templated classes as PEGTL does; + depending on your preference this can be an advantage or disadvantage. + Hand-written Parsers::: + Writing a handwritten parser is more manual work and error prone. + lexy automates that away without having to sacrifice control. + You can use it to quickly prototype a parser and then slowly replace more and more with a handwritten parser over time; + mixing a hand-written parser and a lexy grammar works seamlessly. + +How bad are the compilation times?:: +They're not as bad as you might expect (in debug mode, that is). ++ +The example JSON parser compiles in about 2s on my machine. +If we remove all the lexy specific parts and just benchmark the time it takes for the compiler to process the datastructure (and stdlib includes), +that takes about 700ms. +If we validate JSON only instead of parsing it, so remove the data structures and keep only the lexy specific parts, we're looking at about 840ms. ++ +Keep in mind, that you can fully isolate lexy in a single translation unit that only needs to be touched when you change the parser. +You can also split a lexy grammar into multiple translation units using the `dsl::subgrammar` rule. + +How bad are the {cpp} error messages if you mess something up?:: + They're certainly worse than the error message lexy gives you. + The big problem here is that the first line gives you the error, followed by dozens of template instantiations, which end at your `lexy::parse` call. + Besides providing an external tool to filter those error messages, there is nothing I can do about that. + +How fast is it?:: + Benchmarks are available in the `benchmarks/` directory. + A sample result of the JSON validator benchmark which compares the example JSON parser with various other implementations is available https://lexy.foonathan.net/benchmark_json/[here]. + +Why is it called lexy?:: + I previously had a tokenizer library called foonathan/lex. + I've tried adding a parser to it, but found that the line between pure tokenization and parsing has become increasingly blurred. + lexy is a re-imagination on of the parser I've added to foonathan/lex, and I've simply kept a similar name. + +ifdef::env-github[] +== Documentation + +The documentation, including tutorials, reference documentation, and an interactive playground can be found at https://lexy.foonathan.net/[lexy.foonathan.net]. + +A minimal `CMakeLists.txt` that uses lexy can look like this: + +.`CMakeLists.txt` +```cmake +project(lexy-example) + +include(FetchContent) +FetchContent_Declare(lexy URL https://lexy.foonathan.net/download/lexy-src.zip) +FetchContent_MakeAvailable(lexy) + +add_executable(lexy_example) +target_sources(lexy_example PRIVATE main.cpp) +target_link_libraries(lexy_example PRIVATE foonathan::lexy) +``` + +endif::[] + diff --git a/3rdparty/lexy/cmake/lexyConfig.cmake.in b/3rdparty/lexy/cmake/lexyConfig.cmake.in new file mode 100644 index 000000000..e6dc89d30 --- /dev/null +++ b/3rdparty/lexy/cmake/lexyConfig.cmake.in @@ -0,0 +1,8 @@ +# Copyright (C) 2020-2024 Jonathan Müller and lexy contributors +# SPDX-License-Identifier: BSL-1.0 + +# lexy CMake configuration file. + +@PACKAGE_INIT@ + +include ("${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@Targets.cmake") diff --git a/3rdparty/lexy/include/lexy/_detail/any_ref.hpp b/3rdparty/lexy/include/lexy/_detail/any_ref.hpp new file mode 100644 index 000000000..9eca714b2 --- /dev/null +++ b/3rdparty/lexy/include/lexy/_detail/any_ref.hpp @@ -0,0 +1,68 @@ +// Copyright (C) 2020-2024 Jonathan Müller and lexy contributors +// SPDX-License-Identifier: BSL-1.0 + +#ifndef LEXY_DETAIL_ANY_REF_HPP_INCLUDED +#define LEXY_DETAIL_ANY_REF_HPP_INCLUDED + +#include + +// Essentially a void*, but we can cast it in a constexpr context. +// The cost is an extra layer of indirection. + +namespace lexy::_detail +{ +template +class any_holder; + +// Store a pointer to this instead of a void*. +class any_base +{ +public: + any_base(const any_base&) = delete; + any_base& operator=(const any_base&) = delete; + + template + constexpr T& get() noexcept + { + return static_cast*>(this)->get(); + } + template + constexpr const T& get() const noexcept + { + return static_cast*>(this)->get(); + } + +private: + constexpr any_base() = default; + ~any_base() = default; + + template + friend class any_holder; +}; + +using any_ref = any_base*; +using any_cref = const any_base*; + +// Need to store the object in here. +template +class any_holder : public any_base +{ +public: + constexpr explicit any_holder(T&& obj) : _obj(LEXY_MOV(obj)) {} + + constexpr T& get() noexcept + { + return _obj; + } + constexpr const T& get() const noexcept + { + return _obj; + } + +private: + T _obj; +}; +} // namespace lexy::_detail + +#endif // LEXY_DETAIL_ANY_REF_HPP_INCLUDED + diff --git a/3rdparty/lexy/include/lexy/_detail/assert.hpp b/3rdparty/lexy/include/lexy/_detail/assert.hpp new file mode 100644 index 000000000..52aa115de --- /dev/null +++ b/3rdparty/lexy/include/lexy/_detail/assert.hpp @@ -0,0 +1,51 @@ +// Copyright (C) 2020-2024 Jonathan Müller and lexy contributors +// SPDX-License-Identifier: BSL-1.0 + +#ifndef LEXY_DETAIL_ASSERT_HPP_INCLUDED +#define LEXY_DETAIL_ASSERT_HPP_INCLUDED + +#include + +#ifndef LEXY_ENABLE_ASSERT + +// By default, enable assertions if NDEBUG is not defined. + +# if NDEBUG +# define LEXY_ENABLE_ASSERT 0 +# else +# define LEXY_ENABLE_ASSERT 1 +# endif + +#endif + +#if LEXY_ENABLE_ASSERT + +// We want assertions: use assert() if that's available, otherwise abort. +// We don't use assert() directly as that's not constexpr. + +# if NDEBUG + +# include +# define LEXY_PRECONDITION(Expr) ((Expr) ? void(0) : std::abort()) +# define LEXY_ASSERT(Expr, Msg) ((Expr) ? void(0) : std::abort()) + +# else + +# include + +# define LEXY_PRECONDITION(Expr) ((Expr) ? void(0) : assert(Expr)) +# define LEXY_ASSERT(Expr, Msg) ((Expr) ? void(0) : assert((Expr) && (Msg))) + +# endif + +#else + +// We don't want assertions. + +# define LEXY_PRECONDITION(Expr) static_cast(sizeof(Expr)) +# define LEXY_ASSERT(Expr, Msg) static_cast(sizeof(Expr)) + +#endif + +#endif // LEXY_DETAIL_ASSERT_HPP_INCLUDED + diff --git a/3rdparty/lexy/include/lexy/_detail/buffer_builder.hpp b/3rdparty/lexy/include/lexy/_detail/buffer_builder.hpp new file mode 100644 index 000000000..94ba1fd27 --- /dev/null +++ b/3rdparty/lexy/include/lexy/_detail/buffer_builder.hpp @@ -0,0 +1,160 @@ +// Copyright (C) 2020-2024 Jonathan Müller and lexy contributors +// SPDX-License-Identifier: BSL-1.0 + +#ifndef LEXY_DETAIL_BUFFER_BUILDER_HPP_INCLUDED +#define LEXY_DETAIL_BUFFER_BUILDER_HPP_INCLUDED + +#include +#include +#include +#include +#include + +namespace lexy::_detail +{ +// Builds a buffer: it has a read are and a write area. +// The characters in the read area are already valid and can be read. +// The characters in the write area are not valid, but can be written too. +template +class buffer_builder +{ + static_assert(std::is_trivial_v); + + static constexpr std::size_t total_size_bytes = 1024; + static constexpr std::size_t stack_buffer_size + = (total_size_bytes - 3 * sizeof(T*)) / sizeof(T); + static constexpr auto growth_factor = 2; + +public: + buffer_builder() noexcept : _data(_stack_buffer), _read_size(0), _write_size(stack_buffer_size) + { + static_assert(sizeof(*this) == total_size_bytes, "invalid buffer size calculation"); + } + + ~buffer_builder() noexcept + { + // Free memory if we allocated any. + if (_data != _stack_buffer) + ::operator delete(_data); + } + + buffer_builder(const buffer_builder&) = delete; + buffer_builder& operator=(const buffer_builder&) = delete; + + // The total capacity: read + write. + std::size_t capacity() const noexcept + { + return _read_size + _write_size; + } + + // The read area. + const T* read_data() const noexcept + { + return _data; + } + std::size_t read_size() const noexcept + { + return _read_size; + } + + // The write area. + T* write_data() noexcept + { + return _data + _read_size; + } + std::size_t write_size() const noexcept + { + return _write_size; + } + + // Clears the read area. + void clear() noexcept + { + _write_size += _read_size; + _read_size = 0; + } + + // Takes the first n characters of the write area and appends them to the read area. + void commit(std::size_t n) noexcept + { + LEXY_PRECONDITION(n <= _write_size); + _read_size += n; + _write_size -= n; + } + + // Increases the write area, invalidates all pointers. + void grow() + { + const auto cur_cap = capacity(); + const auto new_cap = growth_factor * cur_cap; + + // Allocate new memory. + auto memory = static_cast(::operator new(new_cap * sizeof(T))); + // Copy the read area into the new memory. + std::memcpy(memory, _data, _read_size); + + // Release the old memory, if there was any. + if (_data != _stack_buffer) + ::operator delete(_data); + + // Update for the new area. + _data = memory; + // _read_size hasn't been changed + _write_size = new_cap - _read_size; + } + + //=== iterator ===// + // Stable iterator over the memory. + class stable_iterator : public forward_iterator_base + { + public: + constexpr stable_iterator() = default; + + explicit constexpr stable_iterator(const _detail::buffer_builder& buffer, + std::size_t idx) noexcept + : _buffer(&buffer), _idx(idx) + {} + + constexpr const T& deref() const noexcept + { + LEXY_PRECONDITION(_idx != _buffer->read_size()); + return _buffer->read_data()[_idx]; + } + + constexpr void increment() noexcept + { + LEXY_PRECONDITION(_idx != _buffer->read_size()); + ++_idx; + } + + constexpr bool equal(stable_iterator rhs) const noexcept + { + if (!_buffer || !rhs._buffer) + return !_buffer && !rhs._buffer; + else + { + LEXY_PRECONDITION(_buffer == rhs._buffer); + return _idx == rhs._idx; + } + } + + constexpr std::size_t index() const noexcept + { + return _idx; + } + + private: + const _detail::buffer_builder* _buffer = nullptr; + std::size_t _idx = 0; + }; + +private: + T* _data; + std::size_t _read_size; + std::size_t _write_size; + T _stack_buffer[stack_buffer_size]; +}; +} // namespace lexy::_detail + +#endif // LEXY_DETAIL_BUFFER_BUILDER_HPP_INCLUDED + diff --git a/3rdparty/lexy/include/lexy/_detail/code_point.hpp b/3rdparty/lexy/include/lexy/_detail/code_point.hpp new file mode 100644 index 000000000..bc805b11e --- /dev/null +++ b/3rdparty/lexy/include/lexy/_detail/code_point.hpp @@ -0,0 +1,368 @@ +// Copyright (C) 2020-2024 Jonathan Müller and lexy contributors +// SPDX-License-Identifier: BSL-1.0 + +#ifndef LEXY_DETAIL_CODE_POINT_HPP_INCLUDED +#define LEXY_DETAIL_CODE_POINT_HPP_INCLUDED + +#include + +//=== encoding ===// +namespace lexy::_detail +{ +template +constexpr std::size_t encode_code_point(char32_t cp, typename Encoding::char_type* buffer, + std::size_t size) +{ + if constexpr (std::is_same_v) + { + LEXY_PRECONDITION(size >= 1); + + *buffer = char(cp); + return 1; + } + else if constexpr (std::is_same_v // + || std::is_same_v) + { + using char_type = typename Encoding::char_type; + // Taken from http://www.herongyang.com/Unicode/UTF-8-UTF-8-Encoding-Algorithm.html. + if (cp <= 0x7F) + { + LEXY_PRECONDITION(size >= 1); + + buffer[0] = char_type(cp); + return 1; + } + else if (cp <= 0x07'FF) + { + LEXY_PRECONDITION(size >= 2); + + auto first = (cp >> 6) & 0x1F; + auto second = (cp >> 0) & 0x3F; + + buffer[0] = char_type(0xC0 | first); + buffer[1] = char_type(0x80 | second); + return 2; + } + else if (cp <= 0xFF'FF) + { + LEXY_PRECONDITION(size >= 3); + + auto first = (cp >> 12) & 0x0F; + auto second = (cp >> 6) & 0x3F; + auto third = (cp >> 0) & 0x3F; + + buffer[0] = char_type(0xE0 | first); + buffer[1] = char_type(0x80 | second); + buffer[2] = char_type(0x80 | third); + return 3; + } + else + { + LEXY_PRECONDITION(size >= 4); + + auto first = (cp >> 18) & 0x07; + auto second = (cp >> 12) & 0x3F; + auto third = (cp >> 6) & 0x3F; + auto fourth = (cp >> 0) & 0x3F; + + buffer[0] = char_type(0xF0 | first); + buffer[1] = char_type(0x80 | second); + buffer[2] = char_type(0x80 | third); + buffer[3] = char_type(0x80 | fourth); + return 4; + } + } + else if constexpr (std::is_same_v) + { + if (cp <= 0xFF'FF) + { + LEXY_PRECONDITION(size >= 1); + + buffer[0] = char16_t(cp); + return 1; + } + else + { + // Algorithm implemented from + // https://en.wikipedia.org/wiki/UTF-16#Code_points_from_U+010000_to_U+10FFFF. + LEXY_PRECONDITION(size >= 2); + + auto u_prime = cp - 0x1'0000; + auto high_ten_bits = u_prime >> 10; + auto low_ten_bits = u_prime & 0b0000'0011'1111'1111; + + buffer[0] = char16_t(0xD800 + high_ten_bits); + buffer[1] = char16_t(0xDC00 + low_ten_bits); + return 2; + } + } + else if constexpr (std::is_same_v) + { + LEXY_PRECONDITION(size >= 1); + + *buffer = char32_t(cp); + return 1; + } + else + { + static_assert(lexy::_detail::error, + "cannot encode a code point in this encoding"); + (void)cp; + (void)buffer; + (void)size; + return 0; + } +} +} // namespace lexy::_detail + +//=== parsing ===// +namespace lexy::_detail +{ +enum class cp_error +{ + success, + eof, + leads_with_trailing, + missing_trailing, + surrogate, + overlong_sequence, + out_of_range, +}; + +template +struct cp_result +{ + char32_t cp; + cp_error error; + typename Reader::marker end; +}; + +template +constexpr cp_result parse_code_point(Reader reader) +{ + if constexpr (std::is_same_v) + { + if (reader.peek() == Reader::encoding::eof()) + return {{}, cp_error::eof, reader.current()}; + + auto cur = reader.peek(); + reader.bump(); + + auto cp = static_cast(cur); + if (cp <= 0x7F) + return {cp, cp_error::success, reader.current()}; + else + return {cp, cp_error::out_of_range, reader.current()}; + } + else if constexpr (std::is_same_v // + || std::is_same_v) + { + using uchar_t = unsigned char; + constexpr auto payload_lead1 = 0b0111'1111; + constexpr auto payload_lead2 = 0b0001'1111; + constexpr auto payload_lead3 = 0b0000'1111; + constexpr auto payload_lead4 = 0b0000'0111; + constexpr auto payload_cont = 0b0011'1111; + + constexpr auto pattern_lead1 = 0b0 << 7; + constexpr auto pattern_lead2 = 0b110 << 5; + constexpr auto pattern_lead3 = 0b1110 << 4; + constexpr auto pattern_lead4 = 0b11110 << 3; + constexpr auto pattern_cont = 0b10 << 6; + + auto first = uchar_t(reader.peek()); + if ((first & ~payload_lead1) == pattern_lead1) + { + // ASCII character. + reader.bump(); + return {first, cp_error::success, reader.current()}; + } + else if ((first & ~payload_cont) == pattern_cont) + { + return {{}, cp_error::leads_with_trailing, reader.current()}; + } + else if ((first & ~payload_lead2) == pattern_lead2) + { + reader.bump(); + + auto second = uchar_t(reader.peek()); + if ((second & ~payload_cont) != pattern_cont) + return {{}, cp_error::missing_trailing, reader.current()}; + reader.bump(); + + auto result = char32_t(first & payload_lead2); + result <<= 6; + result |= char32_t(second & payload_cont); + + // C0 and C1 are overlong ASCII. + if (first == 0xC0 || first == 0xC1) + return {result, cp_error::overlong_sequence, reader.current()}; + else + return {result, cp_error::success, reader.current()}; + } + else if ((first & ~payload_lead3) == pattern_lead3) + { + reader.bump(); + + auto second = uchar_t(reader.peek()); + if ((second & ~payload_cont) != pattern_cont) + return {{}, cp_error::missing_trailing, reader.current()}; + reader.bump(); + + auto third = uchar_t(reader.peek()); + if ((third & ~payload_cont) != pattern_cont) + return {{}, cp_error::missing_trailing, reader.current()}; + reader.bump(); + + auto result = char32_t(first & payload_lead3); + result <<= 6; + result |= char32_t(second & payload_cont); + result <<= 6; + result |= char32_t(third & payload_cont); + + auto cp = result; + if (0xD800 <= cp && cp <= 0xDFFF) + return {cp, cp_error::surrogate, reader.current()}; + else if (first == 0xE0 && second < 0xA0) + return {cp, cp_error::overlong_sequence, reader.current()}; + else + return {cp, cp_error::success, reader.current()}; + } + else if ((first & ~payload_lead4) == pattern_lead4) + { + reader.bump(); + + auto second = uchar_t(reader.peek()); + if ((second & ~payload_cont) != pattern_cont) + return {{}, cp_error::missing_trailing, reader.current()}; + reader.bump(); + + auto third = uchar_t(reader.peek()); + if ((third & ~payload_cont) != pattern_cont) + return {{}, cp_error::missing_trailing, reader.current()}; + reader.bump(); + + auto fourth = uchar_t(reader.peek()); + if ((fourth & ~payload_cont) != pattern_cont) + return {{}, cp_error::missing_trailing, reader.current()}; + reader.bump(); + + auto result = char32_t(first & payload_lead4); + result <<= 6; + result |= char32_t(second & payload_cont); + result <<= 6; + result |= char32_t(third & payload_cont); + result <<= 6; + result |= char32_t(fourth & payload_cont); + + auto cp = result; + if (cp > 0x10'FFFF) + return {cp, cp_error::out_of_range, reader.current()}; + else if (first == 0xF0 && second < 0x90) + return {cp, cp_error::overlong_sequence, reader.current()}; + else + return {cp, cp_error::success, reader.current()}; + } + else // FE or FF + { + return {{}, cp_error::eof, reader.current()}; + } + } + else if constexpr (std::is_same_v) + { + constexpr auto payload1 = 0b0000'0011'1111'1111; + constexpr auto payload2 = payload1; + + constexpr auto pattern1 = 0b110110 << 10; + constexpr auto pattern2 = 0b110111 << 10; + + if (reader.peek() == Reader::encoding::eof()) + return {{}, cp_error::eof, reader.current()}; + + auto first = char16_t(reader.peek()); + if ((first & ~payload1) == pattern1) + { + reader.bump(); + if (reader.peek() == Reader::encoding::eof()) + return {{}, cp_error::missing_trailing, reader.current()}; + + auto second = char16_t(reader.peek()); + if ((second & ~payload2) != pattern2) + return {{}, cp_error::missing_trailing, reader.current()}; + reader.bump(); + + // We've got a valid code point. + auto result = char32_t(first & payload1); + result <<= 10; + result |= char32_t(second & payload2); + result |= 0x10000; + return {result, cp_error::success, reader.current()}; + } + else if ((first & ~payload2) == pattern2) + { + return {{}, cp_error::leads_with_trailing, reader.current()}; + } + else + { + // Single code unit code point; always valid. + reader.bump(); + return {first, cp_error::success, reader.current()}; + } + } + else if constexpr (std::is_same_v) + { + if (reader.peek() == Reader::encoding::eof()) + return {{}, cp_error::eof, reader.current()}; + + auto cur = reader.peek(); + reader.bump(); + + auto cp = cur; + if (cp > 0x10'FFFF) + return {cp, cp_error::out_of_range, reader.current()}; + else if (0xD800 <= cp && cp <= 0xDFFF) + return {cp, cp_error::surrogate, reader.current()}; + else + return {cp, cp_error::success, reader.current()}; + } + else + { + static_assert(lexy::_detail::error, + "no known code point for this encoding"); + return {}; + } +} + +template +constexpr void recover_code_point(Reader& reader, cp_result result) +{ + switch (result.error) + { + case cp_error::success: + // Consume the entire code point. + reader.reset(result.end); + break; + case cp_error::eof: + // We don't need to do anything to "recover" from EOF. + break; + + case cp_error::leads_with_trailing: + // Invalid code unit, consume to recover. + LEXY_PRECONDITION(result.end.position() == reader.position()); + reader.bump(); + break; + + case cp_error::missing_trailing: + case cp_error::surrogate: + case cp_error::out_of_range: + case cp_error::overlong_sequence: + // Consume all the invalid code units to recover. + reader.reset(result.end); + break; + } +} +} // namespace lexy::_detail + +#endif // LEXY_DETAIL_CODE_POINT_HPP_INCLUDED + diff --git a/3rdparty/lexy/include/lexy/_detail/config.hpp b/3rdparty/lexy/include/lexy/_detail/config.hpp new file mode 100644 index 000000000..4aa40135b --- /dev/null +++ b/3rdparty/lexy/include/lexy/_detail/config.hpp @@ -0,0 +1,199 @@ +// Copyright (C) 2020-2024 Jonathan Müller and lexy contributors +// SPDX-License-Identifier: BSL-1.0 + +#ifndef LEXY_DETAIL_CONFIG_HPP_INCLUDED +#define LEXY_DETAIL_CONFIG_HPP_INCLUDED + +#include +#include + +#if defined(LEXY_USER_CONFIG_HEADER) +# include LEXY_USER_CONFIG_HEADER +#elif defined(__has_include) +# if __has_include() +# include +# elif __has_include("lexy_user_config.hpp") +# include "lexy_user_config.hpp" +# endif +#endif + +#ifndef LEXY_HAS_UNICODE_DATABASE +# define LEXY_HAS_UNICODE_DATABASE 0 +#endif + +#ifndef LEXY_EXPERIMENTAL +# define LEXY_EXPERIMENTAL 0 +#endif + +//=== utility traits===// +#define LEXY_MOV(...) static_cast&&>(__VA_ARGS__) +#define LEXY_FWD(...) static_cast(__VA_ARGS__) + +#define LEXY_DECLVAL(...) lexy::_detail::declval<__VA_ARGS__>() + +#define LEXY_DECAY_DECLTYPE(...) std::decay_t + +/// Creates a new type from the instantiation of a template. +/// This is used to shorten type names. +#define LEXY_INSTANTIATION_NEWTYPE(Name, Templ, ...) \ + struct Name : Templ<__VA_ARGS__> \ + { \ + using Templ<__VA_ARGS__>::Templ; \ + } + +namespace lexy::_detail +{ +template +constexpr bool error = false; + +template +std::add_rvalue_reference_t declval(); + +template +constexpr void swap(T& lhs, T& rhs) +{ + T tmp = LEXY_MOV(lhs); + lhs = LEXY_MOV(rhs); + rhs = LEXY_MOV(tmp); +} + +template +constexpr bool is_decayed_same = std::is_same_v, std::decay_t>; + +template +using type_or = std::conditional_t, Fallback, T>; +} // namespace lexy::_detail + +//=== NTTP ===// +#ifndef LEXY_HAS_NTTP +// See https://github.com/foonathan/lexy/issues/15. +# if __cpp_nontype_template_parameter_class >= 201806 || __cpp_nontype_template_args >= 201911 +# define LEXY_HAS_NTTP 1 +# else +# define LEXY_HAS_NTTP 0 +# endif +#endif + +#if LEXY_HAS_NTTP +# define LEXY_NTTP_PARAM auto +#else +# define LEXY_NTTP_PARAM const auto& +#endif + +//=== consteval ===// +#ifndef LEXY_HAS_CONSTEVAL +# if defined(_MSC_VER) && !defined(__clang__) +// Currently can't handle returning strings from consteval, check back later. +# define LEXY_HAS_CONSTEVAL 0 +# elif __cpp_consteval +# define LEXY_HAS_CONSTEVAL 1 +# else +# define LEXY_HAS_CONSTEVAL 0 +# endif +#endif + +#if LEXY_HAS_CONSTEVAL +# define LEXY_CONSTEVAL consteval +#else +# define LEXY_CONSTEVAL constexpr +#endif + +//=== constexpr ===// +#ifndef LEXY_HAS_CONSTEXPR_DTOR +# if __cpp_constexpr_dynamic_alloc +# define LEXY_HAS_CONSTEXPR_DTOR 1 +# else +# define LEXY_HAS_CONSTEXPR_DTOR 0 +# endif +#endif + +#if LEXY_HAS_CONSTEXPR_DTOR +# define LEXY_CONSTEXPR_DTOR constexpr +#else +# define LEXY_CONSTEXPR_DTOR +#endif + +//=== char8_t ===// +#ifndef LEXY_HAS_CHAR8_T +# if __cpp_char8_t +# define LEXY_HAS_CHAR8_T 1 +# else +# define LEXY_HAS_CHAR8_T 0 +# endif +#endif + +#if LEXY_HAS_CHAR8_T + +# define LEXY_CHAR_OF_u8 char8_t +# define LEXY_CHAR8_T char8_t +# define LEXY_CHAR8_STR(Str) u8##Str + +#else + +namespace lexy +{ +using _char8_t = unsigned char; +} // namespace lexy + +# define LEXY_CHAR_OF_u8 char +# define LEXY_CHAR8_T ::lexy::_char8_t +# define LEXY_CHAR8_STR(Str) \ + LEXY_NTTP_STRING(::lexy::_detail::type_string, u8##Str)::c_str + +#endif + +//=== endianness ===// +#ifndef LEXY_IS_LITTLE_ENDIAN +# if defined(__BYTE_ORDER__) +# if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ +# define LEXY_IS_LITTLE_ENDIAN 1 +# elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ +# define LEXY_IS_LITTLE_ENDIAN 0 +# else +# error "unsupported byte order" +# endif +# elif defined(_MSC_VER) +# define LEXY_IS_LITTLE_ENDIAN 1 +# else +# error "unknown endianness" +# endif +#endif + +//=== force inline ===// +#ifndef LEXY_FORCE_INLINE +# if defined(__has_cpp_attribute) +# if __has_cpp_attribute(gnu::always_inline) +# define LEXY_FORCE_INLINE [[gnu::always_inline]] +# endif +# endif +# +# ifndef LEXY_FORCE_INLINE +# define LEXY_FORCE_INLINE inline +# endif +#endif + +//=== empty_member ===// +#ifndef LEXY_EMPTY_MEMBER + +# if defined(__has_cpp_attribute) +# if defined(__GNUC__) && !defined(__clang__) && __GNUC__ <= 11 +// GCC <= 11 has buggy support, see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101040 +# define LEXY_HAS_EMPTY_MEMBER 0 +# elif __has_cpp_attribute(no_unique_address) +# define LEXY_HAS_EMPTY_MEMBER 1 +# endif +# endif +# ifndef LEXY_HAS_EMPTY_MEMBER +# define LEXY_HAS_EMPTY_MEMBER 0 +# endif + +# if LEXY_HAS_EMPTY_MEMBER +# define LEXY_EMPTY_MEMBER [[no_unique_address]] +# else +# define LEXY_EMPTY_MEMBER +# endif + +#endif + +#endif // LEXY_DETAIL_CONFIG_HPP_INCLUDED + diff --git a/3rdparty/lexy/include/lexy/_detail/detect.hpp b/3rdparty/lexy/include/lexy/_detail/detect.hpp new file mode 100644 index 000000000..7534c44c4 --- /dev/null +++ b/3rdparty/lexy/include/lexy/_detail/detect.hpp @@ -0,0 +1,35 @@ +// Copyright (C) 2020-2024 Jonathan Müller and lexy contributors +// SPDX-License-Identifier: BSL-1.0 + +#ifndef LEXY_DETAIL_DETECT_HPP_INCLUDED +#define LEXY_DETAIL_DETECT_HPP_INCLUDED + +#include + +namespace lexy::_detail +{ +template +using void_t = void; + +template