diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 75b96e9144..6682a09b89 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -4,7 +4,7 @@ "ghcr.io/devcontainers/features/python:1": {}, "ghcr.io/devcontainers/features/sshd:1": {} }, - "postCreateCommand": "sudo sh -ce 'apt update && apt install g++-12 -y && echo export CC=gcc-12 CXX=g++-12 >> /etc/profile.d/set-compiler.sh'", + "postCreateCommand": "sudo sh -ce 'apt update && apt install g++-14 -y && echo export CC=gcc-14 CXX=g++-14 >> /etc/profile.d/set-compiler.sh'", "customizations": { "vscode": { "extensions": ["llvm-vs-code-extensions.vscode-clangd", "DavidAnson.vscode-markdownlint"] diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4127b02841..37535c51e4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -12,9 +12,9 @@ on: - "src/**" - "cmake/**" - "CMakeLists.txt" - - "MAA.sln" - ".github/workflows/ci.yml" - "!**/*.md" + - "tools/maadeps-download.py" pull_request: branches: - "dev" @@ -24,8 +24,8 @@ on: - "src/**" - "cmake/**" - "CMakeLists.txt" - - "MAA.sln" - "!**/*.md" + - "tools/maadeps-download.py" workflow_dispatch: concurrency: @@ -108,13 +108,8 @@ jobs: needs: meta strategy: matrix: - include: - - msbuild_target: x64 - lowercase_target: x64 - - msbuild_target: ARM64 - lowercase_target: arm64 - env: - MAABUILDER_TARGET_PLATFORM: ${{ matrix.msbuild_target }} + arch: [arm64, x64] + fail-fast: false runs-on: windows-latest steps: @@ -127,6 +122,35 @@ jobs: run: | git submodule update --init --depth 1 3rdparty/EmulatorExtras + - name: Cache MaaDeps + id: cache-maadeps + uses: actions/cache@v4 + with: + path: | + ./MaaDeps + key: ${{ runner.os }}-${{ matrix.arch }}-maadeps-${{ hashFiles('./tools/maadeps-download.py') }} + + - name: Bootstrap MaaDeps + if: steps.cache-maadeps.outputs.cache-hit != 'true' + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + python3 tools/maadeps-download.py ${{ matrix.arch }}-windows + + - name: Config cmake + run: | + mkdir -p build + cmake -B build ${{ matrix.arch == 'arm64' && '-A ARM64' }} -DCMAKE_BUILD_TYPE=Release -DMAADEPS_TRIPLET='maa-${{ matrix.arch }}-windows' -DINSTALL_RESOURCE=ON -DINSTALL_PYTHON=ON -DMAA_VERSION='${{ needs.meta.outputs.tag }}' -DWITH_WPF_GUI=OFF + + - name: Build + run: | + cmake --build build --config Release + + - name: Install + run: | + mkdir -p install + cmake --install build --prefix install --config Release + - name: Cache .nuke/temp, ~/.nuget/packages id: cache-nuget uses: actions/cache@v4 @@ -134,38 +158,13 @@ jobs: path: | .nuke/temp ~/.nuget/packages - key: ${{ runner.os }}-${{ matrix.msbuild_target }}-${{ hashFiles('**/global.json', '**/*.csproj') }} + key: ${{ runner.os }}-${{ matrix.arch }}-${{ hashFiles('**/*.csproj') }} - name: Restore dependencies if: steps.cache-nuget.outputs.cache-hit != 'true' - run: dotnet restore + run: dotnet restore src/MaaWpfGui/MaaWpfGui.csproj - - name: Cache MaaDeps - id: cache-maadeps - uses: actions/cache@v4 - with: - path: | - ./MaaDeps - key: ${{ runner.os }}-${{ matrix.lowercase_target }}-maadeps-${{ hashFiles('./maadeps-download.py') }} - - - name: Bootstrap MaaDeps - if: steps.cache-maadeps.outputs.cache-hit != 'true' - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - run: | - python3 maadeps-download.py ${{ matrix.lowercase_target }}-windows - - - name: Create fake event file - shell: bash - run: cp -v "$GITHUB_EVENT_PATH" ./event.json - - - name: Merge Event Data with Inputs - if: startsWith(github.ref, 'refs/tags/v') - shell: bash - run: | - cat "$GITHUB_EVENT_PATH" | jq '. + { "inputs": {"ReleaseSimulation": "${{ needs.meta.outputs.tag }}"} }' | tee ./event.json - - - name: Taggify Version + - name: Taggify Version for csproj run: | $csprojPath = "src/MaaWpfGui/MaaWpfGui.csproj" $csprojPath = Resolve-Path -Path $csprojPath @@ -182,29 +181,26 @@ jobs: $node.FileVersion = $match $node.AssemblyVersion = $match $csproj.Save($csprojPath) - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - - name: Build + - name: Publish WPF GUI + continue-on-error: true run: | - $env:GITHUB_WORKFLOW = 'dev-build-win' # pretend this is a dev-build-win workflow - $env:GITHUB_EVENT_PATH = "$pwd\\event.json" - $env:GITHUB_REF = "refs/heads/not_master" - $env:MAA_BUILDER_MAA_VERSION = '${{ needs.meta.outputs.tag }}' - ./build.cmd DevBuild - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + dotnet publish src/MaaWpfGui/MaaWpfGui.csproj -c Release -o install - - name: Cleanup checksum file - run: | - rm -vf ./artifacts/checksum.json + - name: Organize Install Files shell: bash + run: | + rm -rf install/*.pdb + rm -rf install/msvc-debug + rm -rf *.h + + cp tools/DependencySetup_依赖库安装.bat install - name: Upload MAA to Github uses: actions/upload-artifact@v4 with: - name: MAA-win-${{ matrix.lowercase_target }} - path: artifacts + name: MAA-win-${{ matrix.arch }} + path: install ubuntu: # Prevent duplicate runs on organization branches with PRs @@ -213,12 +209,11 @@ jobs: github.event.pull_request.base.repo.full_name needs: meta - runs-on: ubuntu-22.04 + runs-on: ubuntu-latest strategy: matrix: arch: [aarch64, x86_64] - permissions: - actions: write + fail-fast: false steps: - name: Checkout code uses: actions/checkout@v4 @@ -234,36 +229,39 @@ jobs: if: ${{ matrix.arch != 'x86_64' }} run: | sudo apt-get update - sudo apt-get install g++-12-aarch64-linux-gnu g++-12-aarch64-linux-gnu + sudo apt-get install gcc-14-aarch64-linux-gnu g++-14-aarch64-linux-gnu # Caching not necessary on ubuntu runner - name: Bootstrap MaaDeps env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | - python3 maadeps-download.py ${{ matrix.arch == 'x86_64' && 'x64' || 'arm64' }}-linux + python3 tools/maadeps-download.py ${{ matrix.arch == 'x86_64' && 'x64' || 'arm64' }}-linux - - name: Build MAA + - name: Config cmake run: | mkdir -p build cmake -B build \ - -DCMAKE_COMPILE_WARNING_AS_ERROR=ON \ + -DCMAKE_BUILD_TYPE=Release \ -DMAADEPS_TRIPLET='maa-${{ matrix.arch == 'x86_64' && 'x64' || 'arm64' }}-linux' \ - -DINSTALL_THIRD_LIBS=ON \ -DINSTALL_RESOURCE=ON \ -DINSTALL_PYTHON=ON \ -DMAA_VERSION='${{ needs.meta.outputs.tag }}' - cmake --build build --parallel $(nproc --all) - - mkdir -p install - cmake --install build --prefix install env: - CC: ${{ matrix.arch == 'x86_64' && 'gcc-12' || 'aarch64-linux-gnu-gcc-12' }} - CXX: ${{ matrix.arch == 'x86_64' && 'g++-12' || 'aarch64-linux-gnu-g++-12' }} - CMAKE_COLOR_DIAGNOSTICS: ON + CC: ${{ matrix.arch == 'x86_64' && 'gcc-14' || 'aarch64-linux-gnu-gcc-14' }} + CXX: ${{ matrix.arch == 'x86_64' && 'g++-14' || 'aarch64-linux-gnu-g++-14' }} + + - name: Build + run: | + cmake --build build --config Release + env: CLICOLOR_FORCE: 1 - CXXFLAGS: | # workaround for gcc bugs - -Wno-error=restrict -Wno-error=array-bounds -Wno-error=stringop-overread -Wno-error=missing-field-initializers + + - name: Install + run: | + mkdir -p install + cmake --install build --prefix install --config Release + - name: Setup Cross Compile Toolchains for CLI uses: ./src/maa-cli/.github/actions/setup @@ -337,10 +335,11 @@ jobs: github.event.pull_request.base.repo.full_name needs: meta - runs-on: macos-14 + runs-on: macos-latest strategy: matrix: arch: [arm64, x86_64] + fail-fast: false steps: - name: Checkout code uses: actions/checkout@v4 @@ -355,18 +354,24 @@ jobs: - name: Bootstrap MaaDeps run: | [[ ${{ matrix.arch }} = "arm64" ]] && triplet="arm64-osx" || triplet="x64-osx" - python3 maadeps-download.py ${triplet} + python3 tools/maadeps-download.py ${triplet} env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - - name: Configure MaaCore + - name: Config cmake run: | - cmake -B build -GNinja -DCMAKE_BUILD_TYPE=Release -DCMAKE_OSX_ARCHITECTURES='${{ matrix.arch }}' -DMAA_VERSION='${{ needs.meta.outputs.tag }}' + cmake -B build -GNinja \ + -DCMAKE_BUILD_TYPE=Release \ + -DCMAKE_OSX_ARCHITECTURES='${{ matrix.arch }}' \ + -DMAA_VERSION='${{ needs.meta.outputs.tag }}' - - name: Build MaaCore + - name: Build run: | - cmake --build build - cmake --install build --prefix install + cmake --build build --config Release + + - name: Install + run: | + cmake --install build --prefix install --config Release - name: Upload MAA to Github uses: actions/upload-artifact@v4 @@ -381,7 +386,7 @@ jobs: github.event.pull_request.base.repo.full_name needs: [meta, macOS-Core] - runs-on: macos-14 + runs-on: macos-latest steps: - name: Checkout code uses: actions/checkout@v4 diff --git a/.github/workflows/codeql-core.yml b/.github/workflows/codeql-core.yml index f0fef18ac5..86ef075725 100644 --- a/.github/workflows/codeql-core.yml +++ b/.github/workflows/codeql-core.yml @@ -9,7 +9,6 @@ on: - "src/**" - "cmake/**" - "CMakeLists.txt" - - "MAA.sln" - ".github/workflows/codeql-core.yml" - "!**/*.md" - "!**/*.xaml" @@ -55,14 +54,14 @@ jobs: with: path: | ./MaaDeps - key: ${{ runner.os }}-x64-maadeps-${{ hashFiles('./maadeps-download.py') }} + key: ${{ runner.os }}-x64-maadeps-${{ hashFiles('./tools/maadeps-download.py') }} - name: Bootstrap MaaDeps if: steps.cache-maadeps.outputs.cache-hit != 'true' env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | - python3 maadeps-download.py x64-windows + python3 tools/maadeps-download.py x64-windows - name: Create fake event file shell: bash diff --git a/.github/workflows/release-nightly-ota.yml b/.github/workflows/release-nightly-ota.yml index 02147740f6..6f204116dd 100644 --- a/.github/workflows/release-nightly-ota.yml +++ b/.github/workflows/release-nightly-ota.yml @@ -30,10 +30,8 @@ jobs: runs-on: windows-latest strategy: matrix: - msbuild_target: [x64] + arch: [x64] fail-fast: false - env: - MAABUILDER_TARGET_PLATFORM: ${{ matrix.msbuild_target }} outputs: tag: ${{ steps.set_tag.outputs.tag }} pre_version: ${{ steps.set_tag.outputs.pre_version }} @@ -168,6 +166,35 @@ jobs: with: path: ./changelog_notag.md + - name: Cache MaaDeps + id: cache-maadeps + uses: actions/cache@v4 + with: + path: | + ./MaaDeps + key: ${{ runner.os }}-${{ matrix.arch }}-maadeps-${{ hashFiles('./tools/maadeps-download.py') }} + + - name: Bootstrap MaaDeps + if: steps.cache-maadeps.outputs.cache-hit != 'true' + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + python3 tools/maadeps-download.py ${{ matrix.arch }}-windows + + - name: Config cmake + run: | + mkdir -p build + cmake -B build ${{ matrix.arch == 'arm64' && '-A ARM64' }} -DCMAKE_BUILD_TYPE=Release -DMAADEPS_TRIPLET='maa-${{ matrix.arch }}-windows' -DINSTALL_RESOURCE=ON -DINSTALL_PYTHON=ON -DMAA_VERSION='${{ needs.meta.outputs.tag }}' -DWITH_WPF_GUI=OFF + + - name: Build + run: | + cmake --build build --config Release + + - name: Install + run: | + mkdir -p install + cmake --install build --prefix install --config Release + - name: Cache .nuke/temp, ~/.nuget/packages id: cache-nuget uses: actions/cache@v4 @@ -175,28 +202,13 @@ jobs: path: | .nuke/temp ~/.nuget/packages - key: ${{ runner.os }}-${{ matrix.msbuild_target }}-${{ hashFiles('**/global.json', '**/*.csproj') }} + key: ${{ runner.os }}-${{ matrix.arch }}-${{ hashFiles('**/*.csproj') }} - name: Restore dependencies if: steps.cache-nuget.outputs.cache-hit != 'true' - run: dotnet restore + run: dotnet restore src/MaaWpfGui/MaaWpfGui.csproj - - name: Cache MaaDeps - id: cache-maadeps - uses: actions/cache@v4 - with: - path: | - ./MaaDeps - key: ${{ runner.os }}-${{ matrix.msbuild_target }}-maadeps-${{ hashFiles('./maadeps-download.py') }} - - - name: Bootstrap MaaDeps - if: steps.cache-maadeps.outputs.cache-hit != 'true' - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - run: | - python3 maadeps-download.py ${{ matrix.msbuild_target }}-windows - - - name: Taggify Version + - name: Taggify Version for csproj run: | $csprojPath = "src/MaaWpfGui/MaaWpfGui.csproj" $csprojPath = Resolve-Path -Path $csprojPath @@ -206,8 +218,6 @@ jobs: } else { $match = "0.0.1" } - echo "Tag value: $tag" - echo "Version value: $match" [xml]$csproj = Get-Content -Path $csprojPath $node = $csproj.Project.PropertyGroup | where {$_.ApplicationVersion -ne $null} $node.InformationalVersion = $tag @@ -215,25 +225,25 @@ jobs: $node.FileVersion = $match $node.AssemblyVersion = $match $csproj.Save($csprojPath) - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - - name: Run './build.cmd DevBuild' + - name: Publish WPF GUI + continue-on-error: true run: | - $env:GITHUB_WORKFLOW = 'dev-build-win' # pretend this is a dev-build-win workflow - $env:MAA_BUILDER_MAA_VERSION = "${{ steps.set_tag.outputs.tag }}" - echo "tag: " $env:MAA_BUILDER_MAA_VERSION + dotnet publish src/MaaWpfGui/MaaWpfGui.csproj -c Release -o install - ./build.cmd DevBuild - env: - Reason: "Build nightly version" - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + - name: Organize Install Files + run: | + rm -rf install/*.pdb + rm -rf install/msvc-debug + rm -rf *.h + + cp tools/DependencySetup_依赖库安装.bat install - name: Upload MAA to Github uses: actions/upload-artifact@v4 with: - name: MAA-win-${{ matrix.msbuild_target }} - path: artifacts + name: MAA-win-${{ matrix.arch }} + path: install - name: Push tag to main repo id: push_main_tag diff --git a/.github/workflows/res-update-game.yml b/.github/workflows/res-update-game.yml index 35744ce48b..82487d8f8d 100644 --- a/.github/workflows/res-update-game.yml +++ b/.github/workflows/res-update-game.yml @@ -156,14 +156,14 @@ jobs: with: path: | ./MaaDeps - key: ${{ runner.os }}-x64-maadeps-${{ hashFiles('./maadeps-download.py') }} + key: ${{ runner.os }}-x64-maadeps-${{ hashFiles('./tools/maadeps-download.py') }} - name: Bootstrap MaaDeps if: steps.cache-resupd.outputs.cache-hit != 'true' && steps.cache-maadeps.outputs.cache-hit != 'true' env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | - python3 maadeps-download.py x64-windows + python3 tools/maadeps-download.py x64-windows - name: Add msbuild to PATH if: steps.cache-resupd.outputs.cache-hit != 'true' diff --git a/.github/workflows/smoke-testing.yml b/.github/workflows/smoke-testing.yml index 8196ecfa3b..b234a6fd1d 100644 --- a/.github/workflows/smoke-testing.yml +++ b/.github/workflows/smoke-testing.yml @@ -7,20 +7,22 @@ on: - "include/**" - "src/Cpp/**" - "src/MaaCore/**" - - "src/SyncRes/**" - - "MAA.sln" - "resource/**" - "!**/*.md" + - "cmake/**" + - "CMakeLists.txt" + - "tools/maadeps-download.py" pull_request: paths: - "3rdparty/include/**" - "include/**" - "src/Cpp/**" - "src/MaaCore/**" - - "src/SyncRes/**" - - "MAA.sln" - "resource/**" - "!**/*.md" + - "cmake/**" + - "CMakeLists.txt" + - "tools/maadeps-download.py" workflow_dispatch: concurrency: @@ -43,7 +45,7 @@ jobs: - name: Generate cache key id: cache_key run: | - Write-Output "key=Smoke-testing-${{ hashFiles('src/Cpp/**', 'src/MaaCore/**', 'src/SyncRes/**', '3rdparty/include/**', 'include/**', 'MAA.sln') }}" >> $env:GITHUB_OUTPUT + Write-Output "key=Smoke-testing-${{ hashFiles('src/Cpp/**', 'src/MaaCore/**', '3rdparty/include/**', 'include/**', 'cmake/**', 'CMakeLists.txt', 'tools/maadeps-download.py') }}" >> $env:GITHUB_OUTPUT - name: Restore cache smoke-testing id: cache-exe @@ -51,11 +53,11 @@ jobs: with: key: ${{ steps.cache_key.outputs.key }} path: | - ./x64/Debug/Sample.exe - ./x64/Debug/fastdeploy_ppocr.dll - ./x64/Debug/MaaCore.dll - ./x64/Debug/onnxruntime_maa.dll - ./x64/Debug/opencv_world4d_maa.dll + ./install/smoke_test.exe + ./install/fastdeploy_ppocr.dll + ./install/MaaCore.dll + ./install/onnxruntime_maa.dll + ./install/opencv_world4d_maa.dll - name: Fetch submodules if: steps.cache-exe.outputs.cache-hit != 'true' @@ -69,53 +71,48 @@ jobs: with: path: | ./MaaDeps - key: ${{ runner.os }}-x64-maadeps-${{ hashFiles('./maadeps-download.py') }} + key: ${{ runner.os }}-x64-maadeps-${{ hashFiles('./tools/maadeps-download.py') }} - name: Bootstrap MaaDeps if: steps.cache-exe.outputs.cache-hit != 'true' && steps.cache-maadeps.outputs.cache-hit != 'true' env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | - python3 maadeps-download.py x64-windows + python3 tools/maadeps-download.py x64-windows - - name: Add msbuild to PATH - if: steps.cache-exe.outputs.cache-hit != 'true' - uses: microsoft/setup-msbuild@v2 - with: - msbuild-architecture: x64 - - - name: Build MaaSample - if: steps.cache-exe.outputs.cache-hit != 'true' + - name: Config cmake run: | - MSBUILD src/Cpp/MaaSample.slnf -t:restore,build -p:Configuration="Debug" -p:Platform="x64" -m - - - name: Copy resource to build directory - if: steps.cache-exe.outputs.cache-hit == 'true' + mkdir -p build + cmake -B build -DCMAKE_BUILD_TYPE=Debug -DMAADEPS_TRIPLET='maa-x64-windows' -DINSTALL_RESOURCE=ON -DBUILD_TEST=ON -DWITH_WPF_GUI=OFF + + - name: Build run: | - (robocopy "resource" "x64/Debug/resource" /E /XD "Arknights-Tile-Pos" /MT:4 > $null); - if ($LASTEXITCODE -lt 2) { $global:LASTEXITCODE = $null } - (robocopy "resource/Arknights-Tile-Pos" "x64/Debug/resource/Arknights-Tile-Pos" "overview.json" > $null); - if ($LASTEXITCODE -lt 2) { $global:LASTEXITCODE= $null } + cmake --build build --config Debug + + - name: Install + run: | + mkdir -p install + cmake --install build --prefix install --config Debug - name: Run tests run: | pwsh tools/SmokeTesting/run_tests.ps1 - - name: Save cache smoke-testing (only in dev) - if: steps.cache-exe.outputs.cache-hit != 'true' && github.ref == 'refs/heads/dev' + - name: Save cache smoke-testing + if: steps.cache-exe.outputs.cache-hit != 'true' uses: actions/cache/save@v4 with: key: ${{ steps.cache_key.outputs.key }} path: | - ./x64/Debug/Sample.exe - ./x64/Debug/fastdeploy_ppocr.dll - ./x64/Debug/MaaCore.dll - ./x64/Debug/onnxruntime_maa.dll - ./x64/Debug/opencv_world4d_maa.dll + ./install/smoke_test.exe + ./install/fastdeploy_ppocr.dll + ./install/MaaCore.dll + ./install/onnxruntime_maa.dll + ./install/opencv_world4d_maa.dll - name: Upload logs if: always() uses: actions/upload-artifact@v4 with: name: logs - path: ./x64/Debug/debug + path: ./install/debug diff --git a/.gitignore b/.gitignore index 1458e1bd3d..873c6f748b 100644 --- a/.gitignore +++ b/.gitignore @@ -471,6 +471,6 @@ Thumbs.db # MaaSupportExtension config /config/maa_pi_config.json -# MaaBuilder -.github/workflows/release-maa-win.yml -.github/workflows/dev-build-win.yml +# build & install +build +install \ No newline at end of file diff --git a/.nuke/build.schema.json b/.nuke/build.schema.json index 6540f34e6c..b9b760c0ad 100644 --- a/.nuke/build.schema.json +++ b/.nuke/build.schema.json @@ -42,8 +42,7 @@ "UseRsVersion", "UseTagVersion", "WithCompileCoreRelease", - "WithCompileWpfRelease", - "WithSyncRes" + "WithCompileWpfRelease" ] }, "Verbosity": { diff --git a/3rdparty/include/meojson/common/array.hpp b/3rdparty/include/meojson/common/array.hpp index af6e2fbdc5..675e938332 100644 --- a/3rdparty/include/meojson/common/array.hpp +++ b/3rdparty/include/meojson/common/array.hpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include "exception.hpp" @@ -40,50 +41,49 @@ public: // explicit basic_array(basic_value&& val); template < - typename collection_t, + typename jsonization_t, std::enable_if_t< - _utils::is_collection - && std::is_constructible_v>, + _utils::has_to_json_in_templ_spec::value + && !_utils::has_to_json_array_in_templ_spec::value, bool> = true> - basic_array(collection_t arr) - : _array_data(std::make_move_iterator(arr.begin()), std::make_move_iterator(arr.end())) - { - } - - template < - typename fixed_array_t, - std::enable_if_t<_utils::is_fixed_array, bool> = true> - basic_array(const fixed_array_t& arr) - : _array_data(arr.begin(), arr.end()) + basic_array(const jsonization_t& value) + : basic_array(ext::jsonization().to_json(value)) { } template < typename jsonization_t, - std::enable_if_t<_utils::has_to_json_in_member::value, bool> = true> + std::enable_if_t< + _utils::has_to_json_array_in_templ_spec::value, + bool> = true> basic_array(const jsonization_t& value) - : basic_array(value.to_json()) + : basic_array(ext::jsonization().to_json_array(value)) { } template < typename jsonization_t, - std::enable_if_t<_utils::has_to_json_in_templ_spec::value, bool> = true> - basic_array(const jsonization_t& value) - : basic_array(ext::jsonization().to_json(value)) + std::enable_if_t< + std::is_rvalue_reference_v + && _utils::has_move_to_json_in_templ_spec::value + && !_utils::has_move_to_json_array_in_templ_spec::value, + bool> = true> + basic_array(jsonization_t&& value) + : basic_array(ext::jsonization().move_to_json(std::move(value))) { } - template - basic_array(const std::tuple& tup) - { - foreach_tuple(tup, std::make_index_sequence>>()); - } - template - basic_array(std::pair pair) - : _array_data({ std::move(pair.first), std::move(pair.second) }) - { - } + //template < + // typename jsonization_t, + // std::enable_if_t< + // std::is_rvalue_reference_v + // && _utils::has_move_to_json_array_in_templ_spec::value, + // bool> = true> + //basic_array(jsonization_t&& value) + // : basic_array( + // ext::jsonization().move_to_json_array(std::move(value))) + //{ + //} ~basic_array() noexcept = default; @@ -120,6 +120,30 @@ public: template std::pair as_pair() const; + template < + typename value_t, + std::enable_if_t< + _utils::has_from_json_array_in_templ_spec::value, + bool> = true> + value_t as() const& + { + value_t res; + ext::jsonization().from_json_array(*this, res); + return res; + } + + template < + typename value_t, + std::enable_if_t< + _utils::has_move_from_json_array_in_templ_spec::value, + bool> = true> + value_t as() && + { + value_t res; + ext::jsonization().move_from_json_array(std::move(*this), res); + return res; + } + // Usage: get(key_1, key_2, ..., default_value); template auto get(key_then_default_value_t&&... keys_then_default_value) const; @@ -192,29 +216,19 @@ public: std::enable_if_t<_utils::is_fixed_array>, bool> = true> explicit operator fixed_array_t() const { - return as_fixed_array(); - } - - template - explicit operator std::tuple() const - { - return as_tuple(); - } - - template - explicit operator std::pair() const - { - return as_pair(); + return as>(); } template < typename jsonization_t, - std::enable_if_t<_utils::has_from_json_in_member::value, bool> = - true> - explicit operator jsonization_t() const + std::enable_if_t< + _utils::has_from_json_in_templ_spec::value + && !_utils::has_from_json_array_in_templ_spec::value, + bool> = true> + explicit operator jsonization_t() const& { jsonization_t dst {}; - if (!dst.from_json(*this)) { + if (!ext::jsonization().from_json(*this, dst)) { throw exception("Wrong JSON"); } return dst; @@ -223,12 +237,43 @@ public: template < typename jsonization_t, std::enable_if_t< - _utils::has_from_json_in_templ_spec::value, + _utils::has_from_json_array_in_templ_spec::value, bool> = true> - explicit operator jsonization_t() const + explicit operator jsonization_t() const& { jsonization_t dst {}; - if (!ext::jsonization().from_json(*this, dst)) { + if (!ext::jsonization().from_json_array(*this, dst)) { + throw exception("Wrong JSON"); + } + return dst; + } + + template < + typename jsonization_t, + std::enable_if_t< + _utils::has_move_from_json_in_templ_spec::value + && !_utils::has_move_from_json_array_in_templ_spec::value, + bool> = true> + explicit operator jsonization_t() && + { + jsonization_t dst {}; + if (!ext::jsonization().from_json(std::move(*this), dst)) { + throw exception("Wrong JSON"); + } + return dst; + } + + template < + typename jsonization_t, + std::enable_if_t< + _utils::has_move_from_json_array_in_templ_spec::value, + bool> = true> + explicit operator jsonization_t() && + { + jsonization_t dst {}; + if (!ext::jsonization().move_from_json_array( + std::move(*this), + dst)) { throw exception("Wrong JSON"); } return dst; @@ -244,17 +289,6 @@ private: template auto get_helper(const value_t& default_value, size_t pos) const; - template - tuple_t as_tuple_templ() const; - template - void set_tuple(tuple_t& tup) const; - - template - void foreach_tuple(const Tuple& t, std::index_sequence) - { - (_array_data.emplace_back(std::get(t)), ...); - } - string_t format(size_t indent, size_t indent_times) const; private: @@ -368,95 +402,32 @@ inline bool basic_array::all() const return true; } -namespace _as_collection_helper -{ -template -class has_emplace_back -{ - template - static auto test(int) -> decltype(std::declval().emplace_back(), std::true_type()); - - template - static std::false_type test(...); - -public: - static constexpr bool value = decltype(test(0))::value; -}; -} - template template typename collection_t> inline collection_t basic_array::as_collection() const { - collection_t result; - if constexpr (_as_collection_helper::has_emplace_back>::value) { - for (const auto& elem : _array_data) { - result.emplace_back(elem.template as()); - } - } - else { - for (const auto& elem : _array_data) { - result.emplace(elem.template as()); - } - } - return result; + return as>(); } template template typename fixed_array_t> inline fixed_array_t basic_array::as_fixed_array() const { - if (size() != Size) { - throw exception("Wrong array size"); - } - - fixed_array_t result; - for (size_t i = 0; i < Size; ++i) { - result.at(i) = _array_data.at(i).template as(); - } - return result; -} - -template -template -inline void basic_array::set_tuple(tuple_t& tup) const -{ - using elem_t = std::tuple_element_t; - - if constexpr (index > 0) { - set_tuple(tup); - } - - std::get(tup) = static_cast(at(index)); -} - -template -template -inline tuple_t basic_array::as_tuple_templ() const -{ - constexpr size_t tuple_size = std::tuple_size_v; - - if (size() != tuple_size) { - throw exception("Wrong array size"); - } - - tuple_t result; - set_tuple(result); - return result; + return as>(); } template template inline std::tuple basic_array::as_tuple() const { - return as_tuple_templ>(); + return as>(); } template template inline std::pair basic_array::as_pair() const { - return as_tuple_templ>(); + return as>(); } template diff --git a/3rdparty/include/meojson/common/object.hpp b/3rdparty/include/meojson/common/object.hpp index a2337ba031..161b4a5d0d 100644 --- a/3rdparty/include/meojson/common/object.hpp +++ b/3rdparty/include/meojson/common/object.hpp @@ -39,32 +39,50 @@ public: // explicit basic_object(basic_value&& val); template < - typename map_t, + typename jsonization_t, std::enable_if_t< - _utils::is_map - && std::is_constructible_v>, + _utils::has_to_json_in_templ_spec::value + && !_utils::has_to_json_object_in_templ_spec::value, bool> = true> - basic_object(map_t map) - : _object_data(std::make_move_iterator(map.begin()), std::make_move_iterator(map.end())) + basic_object(const jsonization_t& value) + : basic_object(ext::jsonization().to_json(value)) { } template < typename jsonization_t, - std::enable_if_t<_utils::has_to_json_in_member::value, bool> = true> + std::enable_if_t< + _utils::has_to_json_object_in_templ_spec::value, + bool> = true> basic_object(const jsonization_t& value) - : basic_object(value.to_json()) + : basic_object(ext::jsonization().to_json_object(value)) { } template < typename jsonization_t, - std::enable_if_t<_utils::has_to_json_in_templ_spec::value, bool> = true> - basic_object(const jsonization_t& value) - : basic_object(ext::jsonization().to_json(value)) + std::enable_if_t< + std::is_rvalue_reference_v + && _utils::has_move_to_json_in_templ_spec::value + && !_utils::has_move_to_json_object_in_templ_spec::value, + bool> = true> + basic_object(jsonization_t&& value) + : basic_object(ext::jsonization().move_to_json(std::move(value))) { } + //template < + // typename jsonization_t, + // std::enable_if_t< + // std::is_rvalue_reference_v + // && _utils::has_move_to_json_object_in_templ_spec::value, + // bool> = true> + //basic_object(jsonization_t&& value) + // : basic_object( + // ext::jsonization().move_to_json_object(std::move(value))) + //{ + //} + ~basic_object() = default; bool empty() const noexcept { return _object_data.empty(); } @@ -91,6 +109,30 @@ public: template typename map_t = std::map> map_t as_map() const; + template < + typename value_t, + std::enable_if_t< + _utils::has_from_json_object_in_templ_spec::value, + bool> = true> + value_t as() const& + { + value_t res; + ext::jsonization().from_json_object(*this, res); + return res; + } + + template < + typename value_t, + std::enable_if_t< + _utils::has_move_from_json_object_in_templ_spec::value, + bool> = true> + value_t as() && + { + value_t res; + ext::jsonization().move_from_json_object(std::move(*this), res); + return res; + } + // Usage: get(key_1, key_2, ..., default_value); template auto get(key_then_default_value_t&&... keys_then_default_value) const; @@ -151,12 +193,14 @@ public: template < typename jsonization_t, - std::enable_if_t<_utils::has_from_json_in_member::value, bool> = - true> - explicit operator jsonization_t() const + std::enable_if_t< + _utils::has_from_json_in_templ_spec::value + && !_utils::has_from_json_object_in_templ_spec::value, + bool> = true> + explicit operator jsonization_t() const& { jsonization_t dst {}; - if (!dst.from_json(*this)) { + if (!ext::jsonization().from_json(*this, dst)) { throw exception("Wrong JSON"); } return dst; @@ -165,12 +209,43 @@ public: template < typename jsonization_t, std::enable_if_t< - _utils::has_from_json_in_templ_spec::value, + _utils::has_from_json_object_in_templ_spec::value, bool> = true> - explicit operator jsonization_t() const + explicit operator jsonization_t() const& { jsonization_t dst {}; - if (!ext::jsonization().from_json(*this, dst)) { + if (!ext::jsonization().from_json_object(*this, dst)) { + throw exception("Wrong JSON"); + } + return dst; + } + + template < + typename jsonization_t, + std::enable_if_t< + _utils::has_move_from_json_in_templ_spec::value + && !_utils::has_move_from_json_object_in_templ_spec::value, + bool> = true> + explicit operator jsonization_t() && + { + jsonization_t dst {}; + if (!ext::jsonization().from_json(std::move(*this), dst)) { + throw exception("Wrong JSON"); + } + return dst; + } + + template < + typename jsonization_t, + std::enable_if_t< + _utils::has_move_from_json_object_in_templ_spec::value, + bool> = true> + explicit operator jsonization_t() && + { + jsonization_t dst {}; + if (!ext::jsonization().move_from_json_object( + std::move(*this), + dst)) { throw exception("Wrong JSON"); } return dst; @@ -195,8 +270,8 @@ private: template inline basic_object::basic_object(std::initializer_list init_list) : _object_data( - std::make_move_iterator(init_list.begin()), - std::make_move_iterator(init_list.end())) + std::make_move_iterator(init_list.begin()), + std::make_move_iterator(init_list.end())) { } @@ -309,11 +384,7 @@ template template typename map_t> inline map_t basic_object::as_map() const { - map_t result; - for (const auto& [key, val] : _object_data) { - result.emplace(key, val.template as()); - } - return result; + return as>(); } template diff --git a/3rdparty/include/meojson/common/utils.hpp b/3rdparty/include/meojson/common/utils.hpp index 12281b3dea..a416853e38 100644 --- a/3rdparty/include/meojson/common/utils.hpp +++ b/3rdparty/include/meojson/common/utils.hpp @@ -32,7 +32,7 @@ using wobject = basic_object; namespace json::ext { -template +template class jsonization { public: @@ -101,11 +101,19 @@ constexpr bool is_pair = false; template constexpr bool is_pair> = true; +template +constexpr bool is_tuple_like = false; +template