mirror of
https://github.com/microsoft/GSL.git
synced 2026-08-06 13:44:09 +02:00
Compare commits
347 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 882720bd73 | |||
| 81c10b8017 | |||
| 0b80ac7d02 | |||
| e269976910 | |||
| 60beb7a363 | |||
| d5fc4efbf6 | |||
| 25c0dee6e3 | |||
| e02baff283 | |||
| 71fd73fa04 | |||
| c78ad8661c | |||
| 070db845f8 | |||
| 0c5a66b666 | |||
| 248a7cbdce | |||
| b77eaa87ee | |||
| 9033021831 | |||
| a45265a75e | |||
| 8bd828744f | |||
| 1c37688ccd | |||
| 8dfd03feeb | |||
| d83e9ea05f | |||
| 59f38376b9 | |||
| dd45e5cfbd | |||
| 0cbb9e221d | |||
| b7548fd1cd | |||
| cbfd8cd734 | |||
| c16e4ce59f | |||
| b34dc221a1 | |||
| c20a54c8e7 | |||
| 0140ab1d73 | |||
| 475b785d2c | |||
| 959ce58bbc | |||
| 804a14fe8b | |||
| 00d4a5aab6 | |||
| 9150ce9a95 | |||
| a150aaa4ed | |||
| e8978c01ab | |||
| 2c3ab0211c | |||
| 179fba51f5 | |||
| d74ae54b60 | |||
| b6c57e2403 | |||
| 736b62a838 | |||
| 6f4529395c | |||
| 9fa2c5d5b9 | |||
| 8009a703c2 | |||
| cfb4db0c1e | |||
| 6432f920ea | |||
| 2af9b11fe9 | |||
| dc6fdff4c0 | |||
| 2ccf36b4b9 | |||
| bd23bdc0ad | |||
| 1665f07cf6 | |||
| 3f68a0034a | |||
| 7721de88f3 | |||
| a64c489c78 | |||
| 5e773556fe | |||
| 5de956aaf0 | |||
| bd803cc7cf | |||
| e7e3bf4f5f | |||
| 21b69e5cce | |||
| d58e50c6f4 | |||
| 5fb0c8611e | |||
| 6c518638ac | |||
| 4a4bb3c13a | |||
| b6451c5db0 | |||
| 81957f6f91 | |||
| 6aa755e9ce | |||
| e426750695 | |||
| c25a7544b8 | |||
| 06c46195ee | |||
| 83ce710d6c | |||
| ec471abe44 | |||
| dad3d80c25 | |||
| a9ffcf7f65 | |||
| e09326eefd | |||
| d09f1544fd | |||
| 74968d3ef8 | |||
| 0c80f51f7c | |||
| ef714fa49e | |||
| 9355982fc5 | |||
| 4da6a264c4 | |||
| 2469db7a5a | |||
| afe824490e | |||
| 72ddfb7a40 | |||
| bf0697be71 | |||
| 97cb97c685 | |||
| 13f57a1e70 | |||
| c482c82d3a | |||
| 01eaf5bef1 | |||
| 63379b7935 | |||
| 2231d733c6 | |||
| f8d9f2e2e3 | |||
| 552eedb390 | |||
| ca3bf7710f | |||
| 559f8cfaae | |||
| f79ed1bb5c | |||
| bf6e103dfd | |||
| 4c66cea06c | |||
| 478d876951 | |||
| 853b061141 | |||
| 301030310e | |||
| be3bc0e754 | |||
| 98002ab601 | |||
| cfe9baf02e | |||
| 0f6dbc9e29 | |||
| 78744ed530 | |||
| 794d7bb69b | |||
| 72803a7ecb | |||
| 4d2090ebc4 | |||
| c4a2ce6cc8 | |||
| 9720cc552a | |||
| c143a07f61 | |||
| 6c405a1b7f | |||
| 689abc2982 | |||
| 0dbdf322fa | |||
| 283e31478f | |||
| 8cd19412b4 | |||
| 25e7af9c7d | |||
| 7e4ed8da98 | |||
| 34750814fb | |||
| 313a1121bf | |||
| 1e44481646 | |||
| ed3fea6d93 | |||
| 31604f44f6 | |||
| d3468230b1 | |||
| 552cd20472 | |||
| 2085c7acde | |||
| 33544a706e | |||
| f0160fcb1b | |||
| 2a483c1728 | |||
| 031231466d | |||
| a6d382109e | |||
| 09caa20d99 | |||
| 0843ea444f | |||
| 1e4e23738c | |||
| 839de21707 | |||
| 91858ca3f3 | |||
| 0ab350f5de | |||
| 84847041ee | |||
| 1e5f44d3ea | |||
| de053645bb | |||
| 29297e7dac | |||
| 879335cebf | |||
| 1999b48a51 | |||
| 3da256d03b | |||
| 601b55ea61 | |||
| 3a5b83db35 | |||
| 94d6a35726 | |||
| 14acdcd7a8 | |||
| 4eb554d7c2 | |||
| 70e1317ab6 | |||
| 61534ca3ad | |||
| afa2f3fd9a | |||
| fb1243d735 | |||
| f5cf01083b | |||
| 9f6a9a5807 | |||
| aeb65efada | |||
| b43855631a | |||
| 9cb376c050 | |||
| 7341c5d1b5 | |||
| 044849d6fa | |||
| 24dc828cbb | |||
| 8b8c25fa66 | |||
| f7aae78ff6 | |||
| efbce17ca4 | |||
| ffbfcc0a9f | |||
| 5ca02327c4 | |||
| 8d907dadfb | |||
| b7d9d754ac | |||
| 809aee2315 | |||
| 6ef56d73da | |||
| 2f9d873043 | |||
| f85166aa8b | |||
| b0b933b842 | |||
| bc7ea7aac7 | |||
| 611674d60b | |||
| 6e2398b524 | |||
| ff45864dba | |||
| 15dd17aa5b | |||
| c5dd0e6d40 | |||
| 01d206f4d8 | |||
| 830b081de7 | |||
| 226a854a97 | |||
| 5e21831494 | |||
| 04809f8559 | |||
| 511faf07d4 | |||
| 610c40333d | |||
| 30c068781f | |||
| f4c2292f9d | |||
| 0357a02921 | |||
| ddde9e153d | |||
| 6eab19d3c1 | |||
| f8bcb7d9eb | |||
| 9b3ac8d681 | |||
| 1dd1320c8b | |||
| d90fefea6d | |||
| 06adf557e9 | |||
| ce4d689f4f | |||
| b30524088a | |||
| 769f01900d | |||
| 4b643918a1 | |||
| a3b86971d9 | |||
| 4a6a7a095d | |||
| d2fdeda06e | |||
| fc54e0d89a | |||
| 5d54cada8e | |||
| 32e5fea6ac | |||
| 0f60c68ab9 | |||
| 60372b6468 | |||
| 89271b89c1 | |||
| ac37004274 | |||
| 3144082909 | |||
| c853017be3 | |||
| 94f43d4adf | |||
| 691a78c016 | |||
| 46603698ec | |||
| f4c608fd39 | |||
| 8ba6cb1074 | |||
| 8d6ca323fa | |||
| 6b01a0488b | |||
| b6b1e9c3cf | |||
| 67a7f7eaef | |||
| 41ae38f197 | |||
| 926aaeca56 | |||
| cce6ee563e | |||
| dd78144c2e | |||
| 849f14083a | |||
| 9c0c6b246c | |||
| 886fc95142 | |||
| ebe8844344 | |||
| d8fa68c4a5 | |||
| b81d6e40e1 | |||
| 377b2db537 | |||
| 202f332702 | |||
| 3f0d733466 | |||
| a49ff1b8df | |||
| 3b9d15f49f | |||
| 45f016d96f | |||
| ff5f7973a2 | |||
| 7fcc142ffc | |||
| 432be4852c | |||
| 5cf1610cfe | |||
| d7e1611137 | |||
| 5a1e4f3953 | |||
| 61c6ef8d42 | |||
| db2134485a | |||
| 0c6ce424ab | |||
| e9fea77bcb | |||
| 4ec7058b56 | |||
| 49e7ed1ebf | |||
| 592c28c6d1 | |||
| 877816faa4 | |||
| 2b7a8f1f2f | |||
| e0dc8095b3 | |||
| 81c2a1da15 | |||
| 827fafd32c | |||
| 24646c6f7c | |||
| f4ee6ee73b | |||
| 1815791af8 | |||
| d9d6ea8196 | |||
| 3e40b3fa4b | |||
| d0fdbdbffa | |||
| a430823b43 | |||
| 9bb900e834 | |||
| 17e372c155 | |||
| 5b8cf5422b | |||
| eabd9358f0 | |||
| ad71477183 | |||
| c31593dd0d | |||
| 8579165d0a | |||
| b4dd39615a | |||
| 6b23937baf | |||
| 6e20bfbba2 | |||
| 57dd11b20f | |||
| c61b1503ad | |||
| 7ba7f87475 | |||
| d976d41494 | |||
| 26a68c882b | |||
| 95730a80c4 | |||
| 72312b6474 | |||
| adc5fb0d14 | |||
| b29a18d3a8 | |||
| 5082a21b04 | |||
| 8edc449efb | |||
| 5caf336e02 | |||
| ecff9e623c | |||
| f828c55ec7 | |||
| 586c4e52f8 | |||
| e1a37d0430 | |||
| f0061bfa2a | |||
| 8655dc1583 | |||
| d768179ff6 | |||
| 81f56796a1 | |||
| 4cbd894812 | |||
| d08ff53e61 | |||
| 186f5d21c1 | |||
| cdedae897c | |||
| 888b9b9723 | |||
| 395a5bf550 | |||
| 96b6964b30 | |||
| a1e6fcbc46 | |||
| 2d878b94ee | |||
| b076205338 | |||
| 75936c3109 | |||
| cfd82aef4e | |||
| 3c0b38b777 | |||
| 2bc6a7cb12 | |||
| c7f9b3301a | |||
| 6eccc81c5f | |||
| 4b823b1651 | |||
| 3bf4106a44 | |||
| fa8a8117a0 | |||
| b186b6cc68 | |||
| b33bd4aebe | |||
| 0fefba89da | |||
| f04907c2f6 | |||
| 7786da91c9 | |||
| 40bc3c3f00 | |||
| 8577033f13 | |||
| 3539bd8008 | |||
| 17ad4495fb | |||
| 6c5c708877 | |||
| 0931262acf | |||
| 5099e8fa3e | |||
| 4f6f05d463 | |||
| ec05ecd6b1 | |||
| 0784866608 | |||
| 9a88262544 | |||
| b39a9732f8 | |||
| 87eaa45445 | |||
| 43aca2646b | |||
| 603b4671c1 | |||
| 2b10729386 | |||
| 7004ef506c | |||
| 5e0d76a1a7 | |||
| b213c89700 | |||
| 0551cad467 | |||
| 457fb25609 | |||
| 8428e083e4 | |||
| 73022d1103 | |||
| ffe8ffb452 | |||
| e3ffe1c5cb | |||
| e2b57d16f9 | |||
| 9e1645b990 | |||
| 0a31a14d09 | |||
| 030454e3e9 | |||
| d452c3b061 | |||
| 423841e965 |
@@ -0,0 +1,50 @@
|
||||
name: CI_Android
|
||||
on:
|
||||
push:
|
||||
branches: [ master ]
|
||||
pull_request:
|
||||
branches: [ master ]
|
||||
|
||||
jobs:
|
||||
Android:
|
||||
runs-on: macos-latest
|
||||
defaults:
|
||||
run:
|
||||
working-directory: build
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
|
||||
- name: Create build directory
|
||||
run: mkdir -p build
|
||||
working-directory: .
|
||||
|
||||
- name: Start emulator
|
||||
run: |
|
||||
echo "y" | $ANDROID_HOME/tools/bin/sdkmanager --install 'system-images;android-24;default;x86_64'
|
||||
echo "no" | $ANDROID_HOME/tools/bin/avdmanager create avd -n xamarin_android_emulator -k 'system-images;android-24;default;x86_64' --force
|
||||
$ANDROID_HOME/emulator/emulator -list-avds
|
||||
echo "Starting emulator"
|
||||
# Start emulator in background
|
||||
nohup $ANDROID_HOME/emulator/emulator -avd xamarin_android_emulator -no-snapshot > /dev/null 2>&1 &
|
||||
echo "Emulator starting"
|
||||
|
||||
- name: Configure
|
||||
run: cmake -DCMAKE_TOOLCHAIN_FILE=$ANDROID_HOME/ndk-bundle/build/cmake/android.toolchain.cmake -DANDROID_PLATFORM=16 -DANDROID_ABI=x86_64 -DCMAKE_BUILD_TYPE=Debug ..
|
||||
|
||||
- name: Build
|
||||
run: cmake --build . --parallel
|
||||
|
||||
- name: Wait for emulator ready
|
||||
run: |
|
||||
$ANDROID_HOME/platform-tools/adb wait-for-device shell 'while [[ -z $(getprop sys.boot_completed | tr -d '\r') ]]; do sleep 10; done; input keyevent 82'
|
||||
$ANDROID_HOME/platform-tools/adb devices
|
||||
$ANDROID_HOME/platform-tools/adb shell getprop ro.product.cpu.abi
|
||||
echo "Emulator started"
|
||||
|
||||
- name: Deploy tests
|
||||
run: |
|
||||
adb push tests /data/local/tmp
|
||||
adb shell find /data/local/tmp/tests -maxdepth 1 -exec chmod +x {} \\\;
|
||||
|
||||
- name: Test
|
||||
run: adb shell find /data/local/tmp/tests -name "*_tests" -maxdepth 1 -exec {} \\\;
|
||||
@@ -0,0 +1,51 @@
|
||||
name: CI_iOS
|
||||
on:
|
||||
push:
|
||||
branches: [ master ]
|
||||
pull_request:
|
||||
branches: [ master ]
|
||||
|
||||
jobs:
|
||||
iOS:
|
||||
runs-on: macos-latest
|
||||
defaults:
|
||||
run:
|
||||
working-directory: build
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
|
||||
- name: Create build directory
|
||||
run: mkdir -p build
|
||||
working-directory: .
|
||||
|
||||
- name: Configure
|
||||
run: |
|
||||
cmake \
|
||||
-GXcode \
|
||||
-DCMAKE_SYSTEM_NAME=iOS \
|
||||
"-DCMAKE_OSX_ARCHITECTURES=arm64;x86_64" \
|
||||
-DCMAKE_OSX_DEPLOYMENT_TARGET=8 \
|
||||
-DCMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY \
|
||||
"-DMACOSX_BUNDLE_GUI_IDENTIFIER=GSL.\$(EXECUTABLE_NAME)" \
|
||||
-DMACOSX_BUNDLE_BUNDLE_VERSION=3.0.1 \
|
||||
-DMACOSX_BUNDLE_SHORT_VERSION_STRING=3.0.1 \
|
||||
..
|
||||
|
||||
- name: Build
|
||||
run: cmake --build . --parallel `sysctl -n hw.ncpu` --config Release -- -sdk iphonesimulator
|
||||
|
||||
- name: Start simulator
|
||||
run: |
|
||||
RUNTIME=`xcrun simctl list runtimes iOS -j|jq '.runtimes|last.identifier'`
|
||||
UDID=`xcrun simctl list devices iPhone available -j|jq -r ".devices[$RUNTIME]|last.udid"`
|
||||
xcrun simctl bootstatus $UDID -b
|
||||
|
||||
- name: Test
|
||||
run: |
|
||||
for TEST in `find tests/Release-iphonesimulator -depth 1 -name "*.app"`
|
||||
do
|
||||
xcrun simctl install booted $TEST
|
||||
TEST_ID=`plutil -convert json -o - $TEST/Info.plist|jq -r ".CFBundleIdentifier"`
|
||||
xcrun simctl launch --console booted $TEST_ID
|
||||
xcrun simctl uninstall booted $TEST_ID
|
||||
done
|
||||
+257
-215
@@ -1,268 +1,305 @@
|
||||
# Based on https://github.com/ldionne/hana/blob/master/.travis.yml
|
||||
|
||||
language: cpp
|
||||
sudo: false
|
||||
notifications:
|
||||
email: false
|
||||
|
||||
# Use Linux unless specified otherwise
|
||||
os: linux
|
||||
dist: trusty
|
||||
dist: bionic
|
||||
|
||||
cache:
|
||||
directories:
|
||||
- ${TRAVIS_BUILD_DIR}/deps
|
||||
|
||||
matrix:
|
||||
stages:
|
||||
- name: Latest # Compiler with the latest major version
|
||||
- name: Previous # Compilers with the major version Latest - 1
|
||||
- name: Validation # run other jobs
|
||||
|
||||
jobs:
|
||||
include:
|
||||
|
||||
##########################################################################
|
||||
# Clang on OSX
|
||||
# Travis seems to take longer to start OSX instances,
|
||||
# so leave this first for the overall build to be faster
|
||||
# Validate CMake configuration
|
||||
##########################################################################
|
||||
|
||||
# XCode 8.3
|
||||
- env: COMPILER=clang++ BUILD_TYPE=Debug GSL_CXX_STANDARD=14
|
||||
os: osx
|
||||
osx_image: xcode8.3
|
||||
compiler: clang
|
||||
- name: CMake 3.1.3 - latest
|
||||
stage: Validation
|
||||
env: &CMAKE_VERSION_LIST
|
||||
- CMAKE_VERSION: '"3.17.0 3.16.5 3.15.7 3.14.7 3.13.5 3.12.4 3.11.4 3.10.3 3.9.6 3.8.2 3.7.2 3.6.3 3.5.2 3.4.3 3.3.2 3.2.3 3.1.3"'
|
||||
- GSL_CXX_STANDARD: 14
|
||||
addons: # Get latest release (candidate)
|
||||
apt:
|
||||
sources:
|
||||
- sourceline: 'deb https://apt.kitware.com/ubuntu/ bionic main'
|
||||
key_url: 'https://apt.kitware.com/keys/kitware-archive-latest.asc'
|
||||
- sourceline: 'deb https://apt.kitware.com/ubuntu/ bionic-rc main'
|
||||
packages:
|
||||
- cmake
|
||||
script:
|
||||
- |
|
||||
cd ./build
|
||||
( set -eu
|
||||
for CMAKE in ${CMAKE_path[@]}; do test_CMake_generate $CMAKE; done
|
||||
export CXX=clang++
|
||||
for CMAKE in ${CMAKE_path[@]}; do test_CMake_generate $CMAKE; done
|
||||
)
|
||||
|
||||
- env: COMPILER=clang++ BUILD_TYPE=Release GSL_CXX_STANDARD=14
|
||||
- name: CMake 3.2.3 - 3.17.0
|
||||
stage: Validation
|
||||
os: osx
|
||||
osx_image: xcode8.3
|
||||
compiler: clang
|
||||
osx_image: xcode11.3
|
||||
env:
|
||||
- CMAKE_VERSION: '"3.17.0 3.16.5 3.15.7 3.14.7 3.13.5 3.12.4 3.11.4 3.10.3 3.9.6 3.8.2 3.7.2 3.6.3 3.5.2 3.4.3 3.3.2 3.2.3"'
|
||||
script:
|
||||
- |
|
||||
cd ./build
|
||||
( set -eu
|
||||
for CMAKE in ${CMAKE_path[@]}; do test_CMake_generate $CMAKE; done
|
||||
)
|
||||
|
||||
# XCode 9.1
|
||||
- env: COMPILER=clang++ BUILD_TYPE=Debug GSL_CXX_STANDARD=14
|
||||
os: osx
|
||||
osx_image: xcode9.1
|
||||
compiler: clang
|
||||
##########################################################################
|
||||
# AppleClang on OSX
|
||||
##########################################################################
|
||||
|
||||
- env: COMPILER=clang++ BUILD_TYPE=Release GSL_CXX_STANDARD=14
|
||||
|
||||
|
||||
# Xcode 10.3
|
||||
- name: AppleClang Xcode-10.3 C++14 Debug
|
||||
stage: Previous
|
||||
env: BUILD_TYPE=Debug GSL_CXX_STANDARD=14
|
||||
os: osx
|
||||
osx_image: xcode9.1
|
||||
compiler: clang
|
||||
osx_image: xcode10.3 # AppleClang 10.0.1 same compiler as Xcode 10.2
|
||||
- name: AppleClang Xcode-10.3 C++14 Release
|
||||
env: BUILD_TYPE=Release GSL_CXX_STANDARD=14
|
||||
os: osx
|
||||
osx_image: xcode10.3
|
||||
- name: AppleClang Xcode-10.3 C++17 Debug
|
||||
env: BUILD_TYPE=Debug GSL_CXX_STANDARD=17
|
||||
os: osx
|
||||
osx_image: xcode10.3
|
||||
- name: AppleClang Xcode-10.3 C++17 Release
|
||||
env: BUILD_TYPE=Release GSL_CXX_STANDARD=17
|
||||
os: osx
|
||||
osx_image: xcode10.3
|
||||
|
||||
|
||||
# Xcode 11.4
|
||||
- name: AppleClang Xcode-11.4 C++17 Debug
|
||||
stage: Latest
|
||||
env: BUILD_TYPE=Debug GSL_CXX_STANDARD=17
|
||||
os: osx
|
||||
osx_image: xcode11.4
|
||||
- name: AppleClang Xcode-11.4 C++17 Release
|
||||
env: BUILD_TYPE=Release GSL_CXX_STANDARD=17
|
||||
os: osx
|
||||
osx_image: xcode11.4
|
||||
- name: AppleClang Xcode-11.4 C++14 Debug
|
||||
env: BUILD_TYPE=Debug GSL_CXX_STANDARD=14
|
||||
os: osx
|
||||
osx_image: xcode11.4
|
||||
- name: AppleClang Xcode-11.4 C++14 Release
|
||||
env: BUILD_TYPE=Release GSL_CXX_STANDARD=14
|
||||
os: osx
|
||||
osx_image: xcode11.4
|
||||
|
||||
##########################################################################
|
||||
# Clang on Linux
|
||||
##########################################################################
|
||||
|
||||
# Clang 3.6
|
||||
- env: COMPILER=clang++-3.6 BUILD_TYPE=Debug GSL_CXX_STANDARD=14
|
||||
addons: &clang36
|
||||
# Clang 9
|
||||
- name: Clang-9 C++14 Debug
|
||||
stage: Previous
|
||||
env: CXX=clang++-9 BUILD_TYPE=Debug GSL_CXX_STANDARD=14
|
||||
addons: &clang9
|
||||
apt:
|
||||
packages:
|
||||
- clang-3.6
|
||||
- g++-5
|
||||
sources:
|
||||
- ubuntu-toolchain-r-test
|
||||
- llvm-toolchain-precise-3.6
|
||||
- sourceline: 'deb http://apt.llvm.org/bionic/ llvm-toolchain-bionic-9 main'
|
||||
key_url: https://apt.llvm.org/llvm-snapshot.gpg.key
|
||||
packages:
|
||||
- clang-9
|
||||
- name: Clang-9 C++14 Release
|
||||
env: CXX=clang++-9 BUILD_TYPE=Release GSL_CXX_STANDARD=14
|
||||
addons: *clang9
|
||||
- name: Clang-9 C++17 Debug
|
||||
env: CXX=clang++-9 BUILD_TYPE=Debug GSL_CXX_STANDARD=17
|
||||
addons: *clang9
|
||||
- name: Clang-9 C++17 Release
|
||||
env: CXX=clang++-9 BUILD_TYPE=Release GSL_CXX_STANDARD=17
|
||||
addons: *clang9
|
||||
|
||||
- env: COMPILER=clang++-3.6 BUILD_TYPE=Release GSL_CXX_STANDARD=14
|
||||
addons: *clang36
|
||||
|
||||
# Clang 3.7
|
||||
- env: COMPILER=clang++-3.7 BUILD_TYPE=Debug GSL_CXX_STANDARD=14
|
||||
addons: &clang37
|
||||
# Clang 10
|
||||
- name: Clang-10 C++14 Debug
|
||||
stage: Latest
|
||||
env: CXX=clang++-10 BUILD_TYPE=Debug GSL_CXX_STANDARD=14
|
||||
addons: &clang10
|
||||
apt:
|
||||
packages:
|
||||
- clang-3.7
|
||||
- g++-5
|
||||
sources:
|
||||
- ubuntu-toolchain-r-test
|
||||
- llvm-toolchain-precise-3.7
|
||||
|
||||
- env: COMPILER=clang++-3.7 BUILD_TYPE=Release GSL_CXX_STANDARD=14
|
||||
addons: *clang37
|
||||
|
||||
# Clang 3.8
|
||||
- env: COMPILER=clang++-3.8 BUILD_TYPE=Debug GSL_CXX_STANDARD=14
|
||||
addons: &clang38
|
||||
apt:
|
||||
- sourceline: 'deb http://apt.llvm.org/bionic/ llvm-toolchain-bionic-10 main'
|
||||
key_url: https://apt.llvm.org/llvm-snapshot.gpg.key
|
||||
packages:
|
||||
- clang-3.8
|
||||
- g++-5
|
||||
sources:
|
||||
- ubuntu-toolchain-r-test
|
||||
- llvm-toolchain-precise-3.8
|
||||
|
||||
- env: COMPILER=clang++-3.8 BUILD_TYPE=Release GSL_CXX_STANDARD=14
|
||||
addons: *clang38
|
||||
|
||||
# Clang 3.9
|
||||
- env: COMPILER=clang++-3.9 BUILD_TYPE=Debug GSL_CXX_STANDARD=14
|
||||
addons: &clang39
|
||||
apt:
|
||||
packages:
|
||||
- clang-3.9
|
||||
- g++-5
|
||||
sources:
|
||||
- ubuntu-toolchain-r-test
|
||||
- llvm-toolchain-precise-3.9
|
||||
|
||||
- env: COMPILER=clang++-3.9 BUILD_TYPE=Release GSL_CXX_STANDARD=14
|
||||
addons: *clang39
|
||||
|
||||
# Clang 4.0
|
||||
- env: COMPILER=clang++-4.0 BUILD_TYPE=Debug GSL_CXX_STANDARD=14
|
||||
addons: &clang40
|
||||
apt:
|
||||
packages:
|
||||
- clang-4.0
|
||||
- g++-5
|
||||
sources:
|
||||
- ubuntu-toolchain-r-test
|
||||
- llvm-toolchain-trusty-4.0
|
||||
|
||||
- env: COMPILER=clang++-4.0 BUILD_TYPE=Release GSL_CXX_STANDARD=14
|
||||
addons: *clang40
|
||||
|
||||
# Clang 5.0
|
||||
- env: COMPILER=clang++-5.0 BUILD_TYPE=Debug GSL_CXX_STANDARD=14
|
||||
addons: &clang50
|
||||
apt:
|
||||
packages:
|
||||
- clang-5.0
|
||||
- g++-7
|
||||
sources:
|
||||
- ubuntu-toolchain-r-test
|
||||
- llvm-toolchain-trusty-5.0
|
||||
- sourceline: 'deb http://apt.llvm.org/trusty/ llvm-toolchain-trusty-5.0 main'
|
||||
key_url: 'https://apt.llvm.org/llvm-snapshot.gpg.key'
|
||||
|
||||
- env: COMPILER=clang++-5.0 BUILD_TYPE=Release GSL_CXX_STANDARD=14
|
||||
addons: *clang50
|
||||
|
||||
- env: COMPILER=clang++-5.0 BUILD_TYPE=Debug GSL_CXX_STANDARD=17
|
||||
addons: *clang50
|
||||
|
||||
- env: COMPILER=clang++-5.0 BUILD_TYPE=Release GSL_CXX_STANDARD=17
|
||||
addons: *clang50
|
||||
|
||||
# Clang 6.0
|
||||
- env: COMPILER=clang++-6.0 BUILD_TYPE=Debug GSL_CXX_STANDARD=14
|
||||
addons: &clang60
|
||||
apt:
|
||||
packages:
|
||||
- clang-6.0
|
||||
- g++-7
|
||||
sources:
|
||||
- ubuntu-toolchain-r-test
|
||||
- llvm-toolchain-trusty-6.0
|
||||
- sourceline: 'deb http://apt.llvm.org/trusty/ llvm-toolchain-trusty-6.0 main'
|
||||
key_url: 'https://apt.llvm.org/llvm-snapshot.gpg.key'
|
||||
|
||||
- env: COMPILER=clang++-6.0 BUILD_TYPE=Release GSL_CXX_STANDARD=14
|
||||
addons: *clang60
|
||||
|
||||
# Clang 6.0 c++17
|
||||
- env: COMPILER=clang++-6.0 BUILD_TYPE=Debug GSL_CXX_STANDARD=17
|
||||
addons: *clang60
|
||||
|
||||
- env: COMPILER=clang++-6.0 BUILD_TYPE=Release GSL_CXX_STANDARD=17
|
||||
addons: *clang60
|
||||
|
||||
# Clang 7.0
|
||||
- env: COMPILER=clang++-7 BUILD_TYPE=Debug GSL_CXX_STANDARD=14
|
||||
addons: &clang70
|
||||
apt:
|
||||
packages:
|
||||
- clang-7
|
||||
- g++-7
|
||||
sources:
|
||||
- ubuntu-toolchain-r-test
|
||||
- llvm-toolchain-trusty-7
|
||||
|
||||
|
||||
- env: COMPILER=clang++-7 BUILD_TYPE=Release GSL_CXX_STANDARD=14
|
||||
addons: *clang70
|
||||
|
||||
# Clang 7.0 c++17
|
||||
- env: COMPILER=clang++-7 BUILD_TYPE=Debug GSL_CXX_STANDARD=17
|
||||
addons: *clang70
|
||||
|
||||
- env: COMPILER=clang++-7 BUILD_TYPE=Release GSL_CXX_STANDARD=17
|
||||
addons: *clang70
|
||||
- clang-10
|
||||
- name: Clang-10 C++14 Release
|
||||
env: CXX=clang++-10 BUILD_TYPE=Release GSL_CXX_STANDARD=14
|
||||
addons: *clang10
|
||||
- name: Clang-10 C++17 Debug
|
||||
env: CXX=clang++-10 BUILD_TYPE=Debug GSL_CXX_STANDARD=17
|
||||
addons: *clang10
|
||||
- name: Clang-10 C++17 Release
|
||||
env: CXX=clang++-10 BUILD_TYPE=Release GSL_CXX_STANDARD=17
|
||||
addons: *clang10
|
||||
|
||||
##########################################################################
|
||||
# GCC on Linux
|
||||
##########################################################################
|
||||
|
||||
# GCC 5
|
||||
- env: COMPILER=g++-5 BUILD_TYPE=Debug GSL_CXX_STANDARD=14
|
||||
addons: &gcc5
|
||||
# GCC 8
|
||||
- name: GCC-8 C++14 Debug
|
||||
stage: Previous
|
||||
env: CXX=g++-8 BUILD_TYPE=Debug GSL_CXX_STANDARD=14
|
||||
addons: &gcc8
|
||||
apt:
|
||||
packages: g++-5
|
||||
sources:
|
||||
- ubuntu-toolchain-r-test
|
||||
packages: g++-8
|
||||
- name: GCC-8 C++14 Release
|
||||
env: CXX=g++-8 BUILD_TYPE=Release GSL_CXX_STANDARD=14
|
||||
addons: *gcc8
|
||||
- name: GCC-8 C++17 Debug
|
||||
env: CXX=g++-8 BUILD_TYPE=Debug GSL_CXX_STANDARD=17
|
||||
addons: *gcc8
|
||||
- name: GCC-8 C++17 Release
|
||||
env: CXX=g++-8 BUILD_TYPE=Release GSL_CXX_STANDARD=17
|
||||
addons: *gcc8
|
||||
|
||||
- env: COMPILER=g++-5 BUILD_TYPE=Release GSL_CXX_STANDARD=14
|
||||
addons: *gcc5
|
||||
|
||||
# GCC 6
|
||||
- env: COMPILER=g++-6 BUILD_TYPE=Debug GSL_CXX_STANDARD=14
|
||||
addons: &gcc6
|
||||
# GCC 9
|
||||
- name: GCC-9 C++14 Debug
|
||||
stage: Latest
|
||||
env: CXX=g++-9 BUILD_TYPE=Debug GSL_CXX_STANDARD=14
|
||||
addons: &gcc9
|
||||
apt:
|
||||
packages: g++-6
|
||||
sources:
|
||||
- ubuntu-toolchain-r-test
|
||||
- sourceline: ppa:ubuntu-toolchain-r/test
|
||||
packages:
|
||||
- g++-9
|
||||
- name: GCC-9 C++14 Release
|
||||
env: CXX=g++-9 BUILD_TYPE=Release GSL_CXX_STANDARD=14
|
||||
addons: *gcc9
|
||||
- name: GCC-9 C++17 Debug
|
||||
env: CXX=g++-9 BUILD_TYPE=Debug GSL_CXX_STANDARD=17
|
||||
addons: *gcc9
|
||||
- name: GCC-9 C++17 Release
|
||||
env: CXX=g++-9 BUILD_TYPE=Release GSL_CXX_STANDARD=17
|
||||
addons: *gcc9
|
||||
|
||||
- env: COMPILER=g++-6 BUILD_TYPE=Release GSL_CXX_STANDARD=14
|
||||
addons: *gcc6
|
||||
|
||||
# GCC 7
|
||||
- env: COMPILER=g++-7 BUILD_TYPE=Debug GSL_CXX_STANDARD=14
|
||||
addons: &gcc7
|
||||
apt:
|
||||
packages: g++-7
|
||||
sources:
|
||||
- ubuntu-toolchain-r-test
|
||||
|
||||
- env: COMPILER=g++-7 BUILD_TYPE=Release GSL_CXX_STANDARD=14
|
||||
addons: *gcc7
|
||||
|
||||
# GCC 7 c++17
|
||||
- env: COMPILER=g++-7 BUILD_TYPE=Debug GSL_CXX_STANDARD=17
|
||||
addons: *gcc7
|
||||
|
||||
- env: COMPILER=g++-7 BUILD_TYPE=Release GSL_CXX_STANDARD=17
|
||||
addons: *gcc7
|
||||
before_install:
|
||||
- |
|
||||
# Configuration
|
||||
JOBS=2 # Travis machines have 2 cores
|
||||
# Dependencies required by the CI (cached directory)
|
||||
DEPS_DIR="${TRAVIS_BUILD_DIR}/deps"
|
||||
- |
|
||||
# Setup
|
||||
mkdir -p "${DEPS_DIR:?}" && cd "${DEPS_DIR:?}"
|
||||
mkdir -p ~/tools && cd ~/tools
|
||||
if [[ ${TRAVIS_OS_NAME:?} == "osx" ]]; then
|
||||
export PATH="/usr/local/opt/coreutils/libexec/gnubin:$PATH"
|
||||
fi
|
||||
- |
|
||||
# Helper functions
|
||||
# usage: if [[ $(check_url '<url>') ]]; then ...
|
||||
function check_url {( set +e
|
||||
if [[ "$1" =~ 'github.com' ]]; then # check for first byte
|
||||
if curl --fail --silent --output /dev/null --connect-timeout 12 --range 0-0 "$1"
|
||||
then echo true; fi
|
||||
else # request head
|
||||
if curl --fail --silent --output /dev/null --connect-timeout 12 --head "$1"
|
||||
then echo true; fi
|
||||
fi
|
||||
return
|
||||
)}
|
||||
|
||||
install:
|
||||
# Set the ${CXX} variable properly
|
||||
- export CXX=${COMPILER}
|
||||
- ${CXX} --version
|
||||
|
||||
# Dependencies required by the CI are installed in ${TRAVIS_BUILD_DIR}/deps/
|
||||
- DEPS_DIR="${TRAVIS_BUILD_DIR}/deps"
|
||||
- mkdir -p "${DEPS_DIR}"
|
||||
- cd "${DEPS_DIR}"
|
||||
|
||||
# Travis machines have 2 cores
|
||||
- JOBS=2
|
||||
|
||||
############################################################################
|
||||
# Install a recent CMake (unless already installed on OS X)
|
||||
# Install a different CMake version (or several)
|
||||
############################################################################
|
||||
- CMAKE_VERSION=3.7.2
|
||||
- |
|
||||
if [[ "${TRAVIS_OS_NAME}" == "linux" ]]; then
|
||||
CMAKE_URL="https://cmake.org/files/v${CMAKE_VERSION%.[0-9]}/cmake-${CMAKE_VERSION}-Linux-x86_64.tar.gz"
|
||||
mkdir cmake && travis_retry wget --no-check-certificate -O - ${CMAKE_URL} | tar --strip-components=1 -xz -C cmake
|
||||
export PATH=${DEPS_DIR}/cmake/bin:${PATH}
|
||||
else
|
||||
brew install cmake || brew upgrade cmake
|
||||
# Install CMake versions
|
||||
( set -euo pipefail
|
||||
if [[ ${CMAKE_VERSION:-} ]]; then
|
||||
if [[ "${TRAVIS_OS_NAME}" == "linux" ]]; then
|
||||
OS="Linux"; EXT="sh"
|
||||
if [[ ! ("${CMAKE_VERSION:-}" =~ .+[' '].+) ]]; then
|
||||
# Single entry -> default CMake version
|
||||
CMAKE_DEFAULT_DIR="/usr/local"
|
||||
fi
|
||||
elif [[ "${TRAVIS_OS_NAME}" == "osx" ]]; then OS="Darwin"; EXT="tar.gz"
|
||||
else echo "CMake install not supported for this OS."; exit 1
|
||||
fi
|
||||
CMAKE_INSTALLER="install-cmake.${EXT}"
|
||||
fi
|
||||
for VERSION in ${CMAKE_VERSION:-}; do
|
||||
CMAKE_URL="https://github.com/Kitware/CMake/releases/download/v${VERSION}/cmake-${VERSION}-${OS}-x86_64.${EXT}"
|
||||
if [[ $(check_url "$CMAKE_URL") ]]; then
|
||||
curl -sSL ${CMAKE_URL} -o ${CMAKE_INSTALLER}
|
||||
CMAKE_DIR="${CMAKE_DEFAULT_DIR:-"${HOME}/tools/cmake-${VERSION}"}"
|
||||
mkdir -p ${CMAKE_DIR}
|
||||
if [[ "${TRAVIS_OS_NAME}" == "linux" ]]; then
|
||||
chmod +x ${CMAKE_INSTALLER}
|
||||
sudo ./${CMAKE_INSTALLER} --prefix=${CMAKE_DIR} --skip-license
|
||||
else # OSX
|
||||
mkdir -p ./CMake_tmp
|
||||
tar --extract --gzip --file=${CMAKE_INSTALLER} --directory=./CMake_tmp
|
||||
mv ./CMake_tmp/*/CMake.app/Contents/* ${CMAKE_DIR}
|
||||
fi
|
||||
rm --recursive --force ./CMake_tmp ${CMAKE_INSTALLER}
|
||||
else echo 'Invalid url!'; echo "Version: ${VERSION}"
|
||||
fi
|
||||
done
|
||||
)
|
||||
if [[ ${CMAKE_VERSION:-} && "${TRAVIS_OS_NAME:?}" == "osx" && ! ("${CMAKE_VERSION:-}" =~ .+[' '].+) ]]
|
||||
then # Single entry -> default CMake version
|
||||
export PATH=${HOME}/tools/cmake-${CMAKE_VERSION:?}/bin:$PATH
|
||||
fi
|
||||
CMAKE_path=("cmake") # start with installed CMake version
|
||||
for VERSION in ${CMAKE_VERSION:-}; do
|
||||
tmp_path="$HOME/tools/cmake-${VERSION:?}/bin/cmake"
|
||||
if [[ -x "$(command -v ${tmp_path:?})" ]]; then CMAKE_path+=("${tmp_path:?}"); fi
|
||||
done
|
||||
function test_CMake_generate {
|
||||
# $1: cmake or full path to cmake
|
||||
shopt -s extglob
|
||||
if [[ "$1" == "cmake" || -x "$(command -v $1)" && "$1" =~ .*cmake$ ]]; then
|
||||
echo "----------------"
|
||||
$1 --version
|
||||
echo "Configuration = ${BUILD_TYPE:-Debug}"
|
||||
$1 -DCMAKE_BUILD_TYPE=${BUILD_TYPE:-Debug} ${CMAKE_GEN_FLAGS[@]:?} ..
|
||||
rm -rf !(tests/googletest-*)
|
||||
if [[ ! ${BUILD_TYPE:-} ]]; then echo "" && echo "Configuration = Release"
|
||||
$1 -DCMAKE_BUILD_TYPE=Release ${CMAKE_GEN_FLAGS[@]:?} ..
|
||||
rm -rf !(tests/googletest-*)
|
||||
fi
|
||||
else echo "Non existing command: $1"
|
||||
fi
|
||||
}
|
||||
- |
|
||||
# CMake wrapper (Trusty, Xenial & Bionic); restore default behaviour.
|
||||
if [[ "${TRAVIS_OS_NAME:?}" == "linux" &&
|
||||
"$(lsb_release --codename)" =~ (trusty|xenial|bionic)$ ]]
|
||||
then
|
||||
if [[ -x $(command -v /usr/local/bin/cmake) ]]; then
|
||||
function cmake { command /usr/local/bin/cmake $@; }
|
||||
elif [[ -x $(command -v /usr/bin/cmake) ]]; then
|
||||
function cmake { command /usr/bin/cmake $@; }
|
||||
fi
|
||||
fi
|
||||
- cmake --version
|
||||
|
||||
############################################################################
|
||||
# [linux]: Install the right version of libc++
|
||||
# Based on https://github.com/ldionne/hana/blob/master/.travis.yml
|
||||
############################################################################
|
||||
- |
|
||||
LLVM_INSTALL=${DEPS_DIR}/llvm/install
|
||||
LLVM_INSTALL=${DEPS_DIR:?}/llvm/install
|
||||
# if in linux and compiler clang and llvm not installed
|
||||
if [[ "${TRAVIS_OS_NAME}" == "linux" && "${CXX%%+*}" == "clang" && -n "$(ls -A ${LLVM_INSTALL})" ]]; then
|
||||
if [[ "${TRAVIS_OS_NAME:?}" == "linux" && "${CXX%%+*}" == "clang" && -n "$(ls -A ${LLVM_INSTALL:?})" ]]; then
|
||||
if [[ "${CXX}" == "clang++-3.6" ]]; then LLVM_VERSION="3.6.2";
|
||||
elif [[ "${CXX}" == "clang++-3.7" ]]; then LLVM_VERSION="3.7.1";
|
||||
elif [[ "${CXX}" == "clang++-3.8" ]]; then LLVM_VERSION="3.8.1";
|
||||
@@ -284,12 +321,17 @@ install:
|
||||
fi
|
||||
|
||||
before_script:
|
||||
# have CMake to generate build files
|
||||
- cd "${TRAVIS_BUILD_DIR}"
|
||||
- mkdir build && cd build
|
||||
- cmake .. -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DGSL_CXX_STANDARD=$GSL_CXX_STANDARD
|
||||
|
||||
- |
|
||||
cd "${TRAVIS_BUILD_DIR:?}"
|
||||
mkdir build && cd build
|
||||
if [[ ${GSL_CXX_STANDARD:-} ]]; then
|
||||
CMAKE_GEN_FLAGS=("-DGSL_CXX_STANDARD=$GSL_CXX_STANDARD")
|
||||
fi
|
||||
CMAKE_GEN_FLAGS+=("-Wdev -Werror=dev --warn-uninitialized")
|
||||
|
||||
script:
|
||||
# generate build files
|
||||
- cmake .. -DCMAKE_BUILD_TYPE=${BUILD_TYPE:?} ${CMAKE_GEN_FLAGS[@]:?}
|
||||
# build and run tests
|
||||
- cmake --build . -- -j${JOBS}
|
||||
- ctest --output-on-failure -j${JOBS}
|
||||
|
||||
+64
-41
@@ -1,43 +1,40 @@
|
||||
cmake_minimum_required(VERSION 3.1.3)
|
||||
cmake_minimum_required(VERSION 3.1.3...3.16)
|
||||
|
||||
project(GSL CXX)
|
||||
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/")
|
||||
include(guidelineSupportLibrary)
|
||||
|
||||
project(GSL VERSION 3.1.0 LANGUAGES CXX)
|
||||
|
||||
include(ExternalProject)
|
||||
find_package(Git)
|
||||
|
||||
# creates a library GSL which is an interface (header files only)
|
||||
# Use GNUInstallDirs to provide the right locations on all platforms
|
||||
include(GNUInstallDirs)
|
||||
|
||||
# Creates a library GSL which is an interface (header files only)
|
||||
add_library(GSL INTERFACE)
|
||||
|
||||
# determine whether this is a standalone project or included by other projects
|
||||
# NOTE: If you want to use GSL prefer to link against GSL using this alias target
|
||||
# EX:
|
||||
# target_link_libraries(foobar PRIVATE Microsoft.GSL::GSL)
|
||||
#
|
||||
# Add Microsoft.GSL::GSL alias for GSL so that dependents can be agnostic about
|
||||
# whether GSL was added via `add_subdirectory` or `find_package`
|
||||
add_library(Microsoft.GSL::GSL ALIAS GSL)
|
||||
|
||||
# Determine whether this is a standalone project or included by other projects
|
||||
set(GSL_STANDALONE_PROJECT OFF)
|
||||
if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
|
||||
set(GSL_STANDALONE_PROJECT ON)
|
||||
endif ()
|
||||
|
||||
set(GSL_CXX_STANDARD "14" CACHE STRING "Use c++ standard")
|
||||
set(GSL_CXX_STD "cxx_std_${GSL_CXX_STANDARD}")
|
||||
|
||||
if (MSVC)
|
||||
set(GSL_CXX_STD_OPT "-std:c++${GSL_CXX_STANDARD}")
|
||||
else()
|
||||
set(GSL_CXX_STD_OPT "-std=c++${GSL_CXX_STANDARD}")
|
||||
set(GSL_STANDALONE_PROJECT ON)
|
||||
endif()
|
||||
|
||||
# when minimum version required is 3.8.0 remove if below
|
||||
# both branches do exactly the same thing
|
||||
if (CMAKE_VERSION VERSION_LESS 3.7.9)
|
||||
include(CheckCXXCompilerFlag)
|
||||
CHECK_CXX_COMPILER_FLAG("${GSL_CXX_STD_OPT}" COMPILER_SUPPORTS_CXX_STANDARD)
|
||||
# This GSL implementation generally assumes a platform that implements C++14 support.
|
||||
set(gsl_min_cxx_standard "14")
|
||||
|
||||
if(COMPILER_SUPPORTS_CXX_STANDARD)
|
||||
target_compile_options(GSL INTERFACE "${GSL_CXX_STD_OPT}")
|
||||
else()
|
||||
message(FATAL_ERROR "The compiler ${CMAKE_CXX_COMPILER} has no c++${GSL_CXX_STANDARD} support. Please use a different C++ compiler.")
|
||||
endif()
|
||||
else ()
|
||||
target_compile_features(GSL INTERFACE "${GSL_CXX_STD}")
|
||||
# on *nix systems force the use of -std=c++XX instead of -std=gnu++XX (default)
|
||||
set(CMAKE_CXX_EXTENSIONS OFF)
|
||||
if (GSL_STANDALONE_PROJECT)
|
||||
gsl_set_default_cxx_standard(${gsl_min_cxx_standard})
|
||||
else()
|
||||
gsl_client_set_cxx_standard(${gsl_min_cxx_standard})
|
||||
endif()
|
||||
|
||||
# add definitions to the library and targets that consume it
|
||||
@@ -54,41 +51,67 @@ target_compile_definitions(GSL INTERFACE
|
||||
# the SYSTEM keyword suppresses warnings for users of the library
|
||||
if(GSL_STANDALONE_PROJECT)
|
||||
target_include_directories(GSL INTERFACE
|
||||
$<BUILD_INTERFACE:
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/include
|
||||
>
|
||||
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
|
||||
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
|
||||
)
|
||||
else()
|
||||
target_include_directories(GSL SYSTEM INTERFACE
|
||||
$<BUILD_INTERFACE:
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/include
|
||||
>
|
||||
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
|
||||
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
|
||||
)
|
||||
endif()
|
||||
|
||||
|
||||
if (CMAKE_VERSION VERSION_GREATER 3.7.8)
|
||||
if (MSVC_IDE)
|
||||
option(VS_ADD_NATIVE_VISUALIZERS "Configure project to use Visual Studio native visualizers" TRUE)
|
||||
option(GSL_VS_ADD_NATIVE_VISUALIZERS "Configure project to use Visual Studio native visualizers" TRUE)
|
||||
else()
|
||||
set(VS_ADD_NATIVE_VISUALIZERS FALSE CACHE INTERNAL "Native visualizers are Visual Studio extension" FORCE)
|
||||
set(GSL_VS_ADD_NATIVE_VISUALIZERS FALSE CACHE INTERNAL "Native visualizers are Visual Studio extension" FORCE)
|
||||
endif()
|
||||
|
||||
# add natvis file to the library so it will automatically be loaded into Visual Studio
|
||||
if(VS_ADD_NATIVE_VISUALIZERS)
|
||||
if(GSL_VS_ADD_NATIVE_VISUALIZERS)
|
||||
target_sources(GSL INTERFACE
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/GSL.natvis
|
||||
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/GSL.natvis>
|
||||
)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
install(TARGETS GSL EXPORT Microsoft.GSLConfig)
|
||||
install(
|
||||
DIRECTORY include/gsl
|
||||
DESTINATION include
|
||||
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
|
||||
)
|
||||
# Make library importable by other projects
|
||||
install(EXPORT Microsoft.GSLConfig NAMESPACE Microsoft.GSL:: DESTINATION ${CMAKE_INSTALL_DATADIR}/cmake/Microsoft.GSL)
|
||||
export(TARGETS GSL NAMESPACE Microsoft.GSL:: FILE Microsoft.GSLConfig.cmake)
|
||||
|
||||
# Add find_package() versioning support. The version for
|
||||
# generated Microsoft.GSLConfigVersion.cmake will be used from
|
||||
# last project() command. The version's compatibility is set between all
|
||||
# minor versions (as it was in prev. GSL releases).
|
||||
include(CMakePackageConfigHelpers)
|
||||
if(${CMAKE_VERSION} VERSION_LESS "3.14.0")
|
||||
write_basic_package_version_file(
|
||||
${CMAKE_CURRENT_BINARY_DIR}/Microsoft.GSLConfigVersion.cmake
|
||||
COMPATIBILITY SameMajorVersion
|
||||
)
|
||||
else()
|
||||
write_basic_package_version_file(
|
||||
${CMAKE_CURRENT_BINARY_DIR}/Microsoft.GSLConfigVersion.cmake
|
||||
COMPATIBILITY SameMajorVersion
|
||||
ARCH_INDEPENDENT
|
||||
)
|
||||
endif()
|
||||
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/Microsoft.GSLConfigVersion.cmake DESTINATION ${CMAKE_INSTALL_DATADIR}/cmake/Microsoft.GSL)
|
||||
|
||||
option(GSL_TEST "Generate tests." ${GSL_STANDALONE_PROJECT})
|
||||
if (GSL_TEST)
|
||||
enable_testing()
|
||||
add_subdirectory(tests)
|
||||
enable_testing()
|
||||
if(IOS)
|
||||
add_compile_definitions(
|
||||
GTEST_HAS_DEATH_TEST=1
|
||||
)
|
||||
endif()
|
||||
add_subdirectory(tests)
|
||||
endif ()
|
||||
|
||||
+6
-45
@@ -19,9 +19,7 @@
|
||||
</Expand>
|
||||
</Type>
|
||||
|
||||
<!-- These types are from the span header. -->
|
||||
<!-- This is for dynamic_extent spans. -->
|
||||
<Type Name="gsl::span<*, -1>">
|
||||
<Type Name="gsl::span<*, *>">
|
||||
<DisplayString>{{ extent = {storage_.size_} }}</DisplayString>
|
||||
<Expand>
|
||||
<ArrayItems>
|
||||
@@ -31,43 +29,7 @@
|
||||
</Expand>
|
||||
</Type>
|
||||
|
||||
<!-- This works for constexpr size spans. -->
|
||||
<Type Name="gsl::span<*, *>">
|
||||
<DisplayString>{{ extent = {extent} }}</DisplayString>
|
||||
<Expand>
|
||||
<ArrayItems>
|
||||
<Size>extent</Size>
|
||||
<ValuePointer>storage_.data_</ValuePointer>
|
||||
</ArrayItems>
|
||||
</Expand>
|
||||
</Type>
|
||||
|
||||
<!-- This is for dynamic_extent string_spans. -->
|
||||
<Type Name="gsl::basic_string_span<*, -1>">
|
||||
<DisplayString>{span_.storage_.data_,[span_.storage_.size_]na}</DisplayString>
|
||||
<Expand>
|
||||
<Item Name="[size]">span_.storage_.size_</Item>
|
||||
<ArrayItems>
|
||||
<Size>span_.storage_.size_</Size>
|
||||
<ValuePointer>span_.storage_.data_</ValuePointer>
|
||||
</ArrayItems>
|
||||
</Expand>
|
||||
</Type>
|
||||
|
||||
<!-- This works for constexpr size string_spans. -->
|
||||
<Type Name="gsl::basic_string_span<*, *>">
|
||||
<DisplayString>{span_.storage_.data_,[span_.extent]na}</DisplayString>
|
||||
<Expand>
|
||||
<Item Name="[size]">span_.extent</Item>
|
||||
<ArrayItems>
|
||||
<Size>span_.extent</Size>
|
||||
<ValuePointer>span_.storage_.data_</ValuePointer>
|
||||
</ArrayItems>
|
||||
</Expand>
|
||||
</Type>
|
||||
|
||||
<!-- This is for dynamic_extent zstring_spans. -->
|
||||
<Type Name="gsl::basic_zstring_span<*, -1>">
|
||||
<DisplayString>{span_.storage_.data_,[span_.storage_.size_]na}</DisplayString>
|
||||
<Expand>
|
||||
<Item Name="[size]">span_.storage_.size_</Item>
|
||||
@@ -77,19 +39,18 @@
|
||||
</ArrayItems>
|
||||
</Expand>
|
||||
</Type>
|
||||
|
||||
<!-- This works for constexpr size string_spans. -->
|
||||
|
||||
<Type Name="gsl::basic_zstring_span<*, *>">
|
||||
<DisplayString>{span_.storage_.data_,[span_.extent]na}</DisplayString>
|
||||
<DisplayString>{span_.storage_.data_,[span_.storage_.size_]na}</DisplayString>
|
||||
<Expand>
|
||||
<Item Name="[size]">span_.extent</Item>
|
||||
<Item Name="[size]">span_.storage_.size_</Item>
|
||||
<ArrayItems>
|
||||
<Size>span_.extent</Size>
|
||||
<Size>span_.storage_.size_</Size>
|
||||
<ValuePointer>span_.storage_.data_</ValuePointer>
|
||||
</ArrayItems>
|
||||
</Expand>
|
||||
</Type>
|
||||
|
||||
|
||||
<!-- These types are from the gsl header. -->
|
||||
<Type Name="gsl::not_null<*>">
|
||||
<!-- We can always dereference this since it's an invariant. -->
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
# GSL: Guidelines Support Library [](https://travis-ci.org/Microsoft/GSL) [](https://ci.appveyor.com/project/neilmacintosh/GSL)
|
||||
# GSL: Guidelines Support Library
|
||||
[](https://dev.azure.com/cppstat/GSL/_build/latest?definitionId=1&branchName=master)
|
||||
|
||||
The Guidelines Support Library (GSL) contains functions and types that are suggested for use by the
|
||||
[C++ Core Guidelines](https://github.com/isocpp/CppCoreGuidelines) maintained by the [Standard C++ Foundation](https://isocpp.org).
|
||||
This repo contains Microsoft's implementation of GSL.
|
||||
|
||||
The library includes types like `span<T>`, `string_span`, `owner<>` and others.
|
||||
|
||||
The entire implementation is provided inline in the headers under the [gsl](./include/gsl) directory. The implementation generally assumes a platform that implements C++14 support. There are specific workarounds to support MSVC 2015.
|
||||
The entire implementation is provided inline in the headers under the [gsl](./include/gsl) directory. The implementation generally assumes a platform that implements C++14 support.
|
||||
|
||||
While some types have been broken out into their own headers (e.g. [gsl/span](./include/gsl/span)),
|
||||
it is simplest to just include [gsl/gsl](./include/gsl/gsl) and gain access to the entire library.
|
||||
@@ -18,42 +17,103 @@ other platforms. Please see [CONTRIBUTING.md](./CONTRIBUTING.md) for more inform
|
||||
This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments.
|
||||
|
||||
# Usage of Third Party Libraries
|
||||
This project makes use of the [Catch](https://github.com/philsquared/catch) testing library. Please see the [ThirdPartyNotices.txt](./ThirdPartyNotices.txt) file for details regarding the licensing of Catch.
|
||||
This project makes use of the [Google Test](https://github.com/google/googletest) testing library. Please see the [ThirdPartyNotices.txt](./ThirdPartyNotices.txt) file for details regarding the licensing of Google Test.
|
||||
|
||||
# Supported features
|
||||
## Microsoft GSL implements the following from the C++ Core Guidelines:
|
||||
|
||||
Feature | Supported? | Description
|
||||
-----------------------------------|:----------:|-------------
|
||||
[**1. Views**][cg-views] | |
|
||||
owner | ☑ | an alias for a raw pointer
|
||||
not_null | ☑ | restricts a pointer / smart pointer to hold non-null values
|
||||
span | ☑ | a view over a contiguous sequence of memory. Based on the standardized verison of `std::span`, however `gsl::span` enforces bounds checking. See the [wiki](https://github.com/microsoft/GSL/wiki/gsl::span-and-std::span) for additional information.
|
||||
span_p | ☐ | spans a range starting from a pointer to the first place for which the predicate is true
|
||||
basic_zstring | ☑ | A pointer to a C-string (zero-terminated array) with a templated char type
|
||||
zstring | ☑ | An alias to `basic_zstring` with a char type of char
|
||||
czstring | ☑ | An alias to `basic_zstring` with a char type of const char
|
||||
wzstring | ☑ | An alias to `basic_zstring` with a char type of wchar_t
|
||||
cwzstring | ☑ | An alias to `basic_zstring` with a char type of const wchar_t
|
||||
u16zstring | ☑ | An alias to `basic_zstring` with a char type of char16_t
|
||||
cu16zstring | ☑ | An alias to `basic_zstring` with a char type of const char16_t
|
||||
u32zstring | ☑ | An alias to `basic_zstring` with a char type of char32_t
|
||||
cu32zstring | ☑ | An alias to `basic_zstring` with a char type of const char32_t
|
||||
[**2. Owners**][cg-owners] | |
|
||||
unique_ptr | ☑ | an alias to `std::unique_ptr`
|
||||
shared_ptr | ☑ | an alias to `std::shared_ptr`
|
||||
stack_array | ☐ | a stack-allocated array
|
||||
dyn_array | ☐ | a heap-allocated array
|
||||
[**3. Assertions**][cg-assertions] | |
|
||||
Expects | ☑ | a precondition assertion; on failure it terminates by default if no user-installed handler
|
||||
Ensures | ☑ | a postcondition assertion; on failure it terminates by default if no user-installed handler
|
||||
[**4. Utilities**][cg-utilities] | |
|
||||
move_owner | ☐ | a helper function that moves one `owner` to the other
|
||||
byte | ☑ | either an alias to std::byte or a byte type
|
||||
final_action | ☑ | a RAII style class that invokes a functor on its destruction
|
||||
finally | ☑ | a helper function instantiating `final_action`
|
||||
GSL_SUPPRESS | ☑ | a macro that takes an argument and turns it into `[[gsl::suppress(x)]]` or `[[gsl::suppress("x")]]`
|
||||
[[implicit]] | ☐ | a "marker" to put on single-argument constructors to explicitly make them non-explicit
|
||||
index | ☑ | a type to use for all container and array indexing (currently an alias for std::ptrdiff_t)
|
||||
joining_thread | ☐ | a RAII style version of `std::thread` that joins
|
||||
narrow | ☑ | a checked version of narrow_cast; it can throw `narrowing_error`
|
||||
narrow_cast | ☑ | a narrowing cast for values and a synonym for static_cast
|
||||
narrowing_error | ☑ | a custom exception type thrown by `narrow()`
|
||||
[**5. Concepts**][cg-concepts] | ☐ |
|
||||
|
||||
## The following features do not exist in or have been removed from the C++ Core Guidelines:
|
||||
Feature | Supported? | Description
|
||||
-----------------------------------|:----------:|-------------
|
||||
strict_not_null | ☑ | A stricter version of `not_null` with explicit constructors
|
||||
multi_span | ☐ | Deprecated. Multi-dimensional span.
|
||||
strided_span | ☐ | Deprecated. Support for this type has been discontinued.
|
||||
basic_string_span | ☐ | Deprecated. Like `span` but for strings with a templated char type
|
||||
string_span | ☐ | Deprecated. An alias to `basic_string_span` with a char type of char
|
||||
cstring_span | ☐ | Deprecated. An alias to `basic_string_span` with a char type of const char
|
||||
wstring_span | ☐ | Deprecated. An alias to `basic_string_span` with a char type of wchar_t
|
||||
cwstring_span | ☐ | Deprecated. An alias to `basic_string_span` with a char type of const wchar_t
|
||||
u16string_span | ☐ | Deprecated. An alias to `basic_string_span` with a char type of char16_t
|
||||
cu16string_span | ☐ | Deprecated. An alias to `basic_string_span` with a char type of const char16_t
|
||||
u32string_span | ☐ | Deprecated. An alias to `basic_string_span` with a char type of char32_t
|
||||
cu32string_span | ☐ | Deprecated. An alias to `basic_string_span` with a char type of const char32_t
|
||||
|
||||
This is based on [CppCoreGuidelines semi-specification](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#gsl-guidelines-support-library).
|
||||
|
||||
[cg-views]: https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#gslview-views
|
||||
[cg-owners]: https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#gslowner-ownership-pointers
|
||||
[cg-assertions]: https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#gslassert-assertions
|
||||
[cg-utilities]: https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#gslutil-utilities
|
||||
[cg-concepts]: https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#gslconcept-concepts
|
||||
|
||||
# Quick Start
|
||||
## Supported Platforms
|
||||
The test suite that exercises GSL has been built and passes successfully on the following platforms:<sup>1)</sup>
|
||||
## Supported Compilers
|
||||
The GSL officially supports the current and previous major release of MSVC, GCC, Clang, and XCode's Apple-Clang.
|
||||
See our latest test results for the most up-to-date list of supported configurations.
|
||||
|
||||
* Windows using Visual Studio 2015
|
||||
* Windows using Visual Studio 2017
|
||||
* Windows using Clang/LLVM 3.6
|
||||
* Windows using Clang/LLVM 7.0.0
|
||||
* Windows using GCC 5.1
|
||||
* Windows using Intel C++ Compiler 18.0
|
||||
* GNU/Linux using Clang/LLVM 3.6-3.9
|
||||
* GNU/Linux using Clang/LLVM 4.0
|
||||
* GNU/Linux using Clang/LLVM 5.0
|
||||
* GNU/Linux using Clang/LLVM 6.0
|
||||
* GNU/Linux using Clang/LLVM 7.0
|
||||
* GNU/Linux using GCC 5.1
|
||||
* OS X Mojave 10.14.4 using Apple LLVM version 10.0.0 (10.0.1.10010046)
|
||||
* OS X Mojave 10.14.3 using Apple LLVM version 10.0.0 (clang-1000.11.45.5)
|
||||
* OS X Yosemite using Xcode with Apple Clang 7.0.0.7000072
|
||||
* OS X Yosemite using GCC-5.2.0
|
||||
* OS X Sierra 10.12.4 using Apple LLVM version 8.1.0 (Clang-802.0.42)
|
||||
* OS X El Capitan (10.11) using Xcode with AppleClang 8.0.0.8000042
|
||||
* OS X High Sierra 10.13.2 (17C88) using Apple LLVM version 9.0.0 (clang-900.0.39.2)
|
||||
* FreeBSD 10.x with Clang/LLVM 3.6
|
||||
Compiler |Toolset Versions Currently Tested
|
||||
:------- |--:
|
||||
XCode |11.4 & 10.3
|
||||
GCC |9 & 8
|
||||
Clang |11 & 10
|
||||
Visual Studio with MSVC | VS2017 (15.9) & VS2019 (16.4)
|
||||
Visual Studio with LLVM | VS2017 (Clang 9) & VS2019 (Clang 10)
|
||||
|
||||
> If you successfully port GSL to another platform, we would love to hear from you. Please submit an issue to let us know. Also please consider
|
||||
contributing any changes that were necessary back to this project to benefit the wider community.
|
||||
---
|
||||
If you successfully port GSL to another platform, we would love to hear from you!
|
||||
- Submit an issue specifying the platform and target.
|
||||
- Consider contributing your changes by filing a pull request with any necessary changes.
|
||||
- If at all possible, add a CI/CD step and add the button to the table below!
|
||||
|
||||
<sup>1)</sup> For `gsl::byte` to work correctly with Clang and GCC you might have to use the ` -fno-strict-aliasing` compiler option.
|
||||
Target | CI/CD Status
|
||||
:------- | -----------:
|
||||
iOS | 
|
||||
Android | 
|
||||
|
||||
Note: These CI/CD steps are run with each pull request, however failures in them are non-blocking.
|
||||
|
||||
## Building the tests
|
||||
To build the tests, you will require the following:
|
||||
|
||||
* [CMake](http://cmake.org), version 3.1.3 or later to be installed and in your PATH.
|
||||
* [CMake](http://cmake.org), version 3.1.3 (3.2.3 for AppleClang) or later to be installed and in your PATH.
|
||||
|
||||
These steps assume the source code of this repository has been cloned into a directory named `c:\GSL`.
|
||||
|
||||
@@ -65,7 +125,7 @@ These steps assume the source code of this repository has been cloned into a dir
|
||||
|
||||
2. Configure CMake to use the compiler of your choice (you can see a list by running `cmake --help`).
|
||||
|
||||
cmake -G "Visual Studio 14 2015" c:\GSL
|
||||
cmake -G "Visual Studio 15 2017" c:\GSL
|
||||
|
||||
3. Build the test suite (in this case, in the Debug configuration, Release is another good choice).
|
||||
|
||||
@@ -77,6 +137,18 @@ These steps assume the source code of this repository has been cloned into a dir
|
||||
|
||||
All tests should pass - indicating your platform is fully supported and you are ready to use the GSL types!
|
||||
|
||||
## Building GSL - Using vcpkg
|
||||
|
||||
You can download and install GSL using the [vcpkg](https://github.com/Microsoft/vcpkg) dependency manager:
|
||||
|
||||
git clone https://github.com/Microsoft/vcpkg.git
|
||||
cd vcpkg
|
||||
./bootstrap-vcpkg.sh
|
||||
./vcpkg integrate install
|
||||
vcpkg install ms-gsl
|
||||
|
||||
The GSL port in vcpkg is kept up to date by Microsoft team members and community contributors. If the version is out of date, please [create an issue or pull request](https://github.com/Microsoft/vcpkg) on the vcpkg repository.
|
||||
|
||||
## Using the libraries
|
||||
As the types are entirely implemented inline in headers, there are no linking requirements.
|
||||
|
||||
@@ -97,5 +169,14 @@ Include the library using:
|
||||
|
||||
#include <gsl/gsl>
|
||||
|
||||
## Usage in CMake
|
||||
|
||||
The library provides a Config file for CMake, once installed it can be found via
|
||||
|
||||
find_package(Microsoft.GSL CONFIG)
|
||||
|
||||
Which, when successful, will add library target called `Microsoft.GSL::GSL` which you can use via the usual
|
||||
`target_link_libraries` mechanism.
|
||||
|
||||
## Debugging visualization support
|
||||
For Visual Studio users, the file [GSL.natvis](./GSL.natvis) in the root directory of the repository can be added to your project if you would like more helpful visualization of GSL types in the Visual Studio debugger than would be offered by default.
|
||||
|
||||
+33
-31
@@ -2,38 +2,40 @@
|
||||
THIRD-PARTY SOFTWARE NOTICES AND INFORMATION
|
||||
Do Not Translate or Localize
|
||||
|
||||
GSL: Guidelines Support Library incorporates third party material from the projects listed below. The original copyright notice and the license under which Microsoft received such third party material are set forth below. Microsoft reserves all other rights not expressly granted, whether by implication, estoppel or otherwise.
|
||||
GSL: Guidelines Support Library incorporates third party material from the projects listed below.
|
||||
|
||||
-------------------------------------------------------------------------------
|
||||
Software: Google Test
|
||||
Owner: Google Inc.
|
||||
Source URL: github.com/google/googletest
|
||||
License: BSD 3 - Clause
|
||||
Text:
|
||||
Copyright 2008, Google Inc.
|
||||
All rights reserved.
|
||||
|
||||
1. Catch (https://github.com/philsquared/Catch)
|
||||
|
||||
|
||||
%% Catch NOTICES, INFORMATION, AND LICENSE BEGIN HERE
|
||||
=========================================
|
||||
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.
|
||||
=========================================
|
||||
END OF Catch NOTICES, INFORMATION, AND LICENSE
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are
|
||||
met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above
|
||||
copyright notice, this list of conditions and the following disclaimer
|
||||
in the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
* Neither the name of Google Inc. nor the names of its
|
||||
contributors may be used to endorse or promote products derived from
|
||||
this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
-------------------------------------------------------------------------------
|
||||
|
||||
+5
-19
@@ -9,7 +9,6 @@ configuration:
|
||||
- Release
|
||||
|
||||
image:
|
||||
- Visual Studio 2015
|
||||
- Visual Studio 2017
|
||||
- Visual Studio 2019
|
||||
|
||||
@@ -17,7 +16,6 @@ environment:
|
||||
NINJA_TAG: v1.8.2
|
||||
NINJA_SHA512: 9B9CE248240665FCD6404B989F3B3C27ED9682838225E6DC9B67B551774F251E4FF8A207504F941E7C811E7A8BE1945E7BCB94472A335EF15E23A0200A32E6D5
|
||||
NINJA_PATH: C:\Tools\ninja\ninja-%NINJA_TAG%
|
||||
VCVAR2015: 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat'
|
||||
VCVAR2017: 'C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvarsall.bat'
|
||||
VCVAR2019: 'C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvarsall.bat'
|
||||
matrix:
|
||||
@@ -34,14 +32,6 @@ environment:
|
||||
USE_TOOLSET: LLVM
|
||||
USE_GENERATOR: Ninja
|
||||
|
||||
matrix:
|
||||
exclude:
|
||||
- image: Visual Studio 2015
|
||||
GSL_CXX_STANDARD: 17
|
||||
- image: Visual Studio 2015
|
||||
USE_TOOLSET: LLVM
|
||||
USE_GENERATOR: MSBuild
|
||||
|
||||
cache:
|
||||
- C:\cmake-3.14.4-win32-x86
|
||||
- C:\Tools\ninja
|
||||
@@ -72,9 +62,7 @@ before_build:
|
||||
if ("$env:USE_GENERATOR" -eq "Ninja") {
|
||||
$GeneratorFlags = '-k 10'
|
||||
$Architecture = $env:PLATFORM
|
||||
if ("$env:APPVEYOR_BUILD_WORKER_IMAGE" -eq "Visual Studio 2015") {
|
||||
$env:VCVARSALL = "`"$env:VCVAR2015`" $Architecture"
|
||||
} elseif ("$env:APPVEYOR_BUILD_WORKER_IMAGE" -eq "Visual Studio 2017") {
|
||||
if ("$env:APPVEYOR_BUILD_WORKER_IMAGE" -eq "Visual Studio 2017") {
|
||||
$env:VCVARSALL = "`"$env:VCVAR2017`" $Architecture"
|
||||
} else {
|
||||
$env:VCVARSALL = "`"$env:VCVAR2019`" $Architecture"
|
||||
@@ -82,17 +70,15 @@ before_build:
|
||||
$env:CMakeGenFlags = "-G Ninja -DGSL_CXX_STANDARD=$env:GSL_CXX_STANDARD"
|
||||
} else {
|
||||
$GeneratorFlags = '/m /v:minimal'
|
||||
if ("$env:APPVEYOR_BUILD_WORKER_IMAGE" -eq "Visual Studio 2015") {
|
||||
$Generator = 'Visual Studio 14 2015'
|
||||
} elseif ("$env:APPVEYOR_BUILD_WORKER_IMAGE" -eq "Visual Studio 2017") {
|
||||
if ("$env:APPVEYOR_BUILD_WORKER_IMAGE" -eq "Visual Studio 2017") {
|
||||
$Generator = 'Visual Studio 15 2017'
|
||||
} else {
|
||||
$Generator = 'Visual Studio 16 2019'
|
||||
}
|
||||
if ("$env:PLATFORM" -eq "x86") {
|
||||
if ("$env:PLATFORM" -eq "x86") {
|
||||
$Architecture = "Win32"
|
||||
} else {
|
||||
$Architecture = "x64"
|
||||
} else {
|
||||
$Architecture = "x64"
|
||||
}
|
||||
if ("$env:USE_TOOLSET" -eq "LLVM") {
|
||||
$env:CMakeGenFlags = "-G `"$Generator`" -A $Architecture -T llvm -DGSL_CXX_STANDARD=$env:GSL_CXX_STANDARD"
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
trigger:
|
||||
- master
|
||||
|
||||
pr:
|
||||
autoCancel: true
|
||||
|
||||
# GCC
|
||||
stages:
|
||||
- stage: GCC
|
||||
dependsOn: []
|
||||
variables:
|
||||
- name: CC
|
||||
value: gcc
|
||||
- name: CXX
|
||||
value: g++
|
||||
jobs:
|
||||
- template: ./pipelines/jobs.yml
|
||||
parameters:
|
||||
jobName: 'Validate GCC latest'
|
||||
imageName: ubuntu-20.04
|
||||
- template: ./pipelines/jobs.yml
|
||||
parameters:
|
||||
jobName: 'Validate GCC Previous'
|
||||
imageName: ubuntu-18.04
|
||||
|
||||
# Clang
|
||||
- stage: Clang
|
||||
dependsOn: []
|
||||
variables:
|
||||
- name: CC
|
||||
value: clang
|
||||
- name: CXX
|
||||
value: clang++
|
||||
jobs:
|
||||
- template: ./pipelines/jobs.yml
|
||||
parameters:
|
||||
jobName: 'Validate Clang latest'
|
||||
imageName: ubuntu-20.04
|
||||
- template: ./pipelines/jobs.yml
|
||||
parameters:
|
||||
jobName: 'Validate Clang Previous'
|
||||
imageName: ubuntu-18.04
|
||||
|
||||
# MSVC
|
||||
- stage: MSVC
|
||||
dependsOn: []
|
||||
jobs:
|
||||
- template: ./pipelines/jobs.yml
|
||||
parameters:
|
||||
jobName: 'Validate MSVC latest'
|
||||
imageName: windows-latest
|
||||
- template: ./pipelines/jobs.yml
|
||||
parameters:
|
||||
jobName: 'Validate MSVC Previous'
|
||||
imageName: vs2017-win2016
|
||||
|
||||
# Apple-Clang
|
||||
- stage: Apple_Clang
|
||||
dependsOn: []
|
||||
jobs:
|
||||
- template: ./pipelines/jobs.yml
|
||||
parameters:
|
||||
jobName: 'Validate Apple-Clang latest'
|
||||
imageName: macos-10.15
|
||||
- template: ./pipelines/jobs.yml
|
||||
parameters:
|
||||
jobName: 'Validate Apple-Clang Previous'
|
||||
imageName: macos-10.14
|
||||
@@ -0,0 +1,61 @@
|
||||
# This cmake module is meant to hold helper functions/macros
|
||||
# that make maintaining the cmake build system much easier.
|
||||
# This is especially helpful since gsl needs to provide coverage
|
||||
# for multiple versions of cmake.
|
||||
#
|
||||
# Any functions/macros should have a gsl_* prefix to avoid problems
|
||||
if (DEFINED guideline_support_library_include_guard)
|
||||
return()
|
||||
endif()
|
||||
set(guideline_support_library_include_guard ON)
|
||||
|
||||
function(gsl_set_default_cxx_standard min_cxx_standard)
|
||||
set(GSL_CXX_STANDARD "${min_cxx_standard}" CACHE STRING "Use c++ standard")
|
||||
|
||||
set(GSL_CXX_STD "cxx_std_${GSL_CXX_STANDARD}")
|
||||
|
||||
if (MSVC)
|
||||
set(GSL_CXX_STD_OPT "-std:c++${GSL_CXX_STANDARD}")
|
||||
else()
|
||||
set(GSL_CXX_STD_OPT "-std=c++${GSL_CXX_STANDARD}")
|
||||
endif()
|
||||
|
||||
# when minimum version required is 3.8.0 remove if below
|
||||
# both branches do exactly the same thing
|
||||
if (CMAKE_VERSION VERSION_LESS 3.7.9)
|
||||
include(CheckCXXCompilerFlag)
|
||||
CHECK_CXX_COMPILER_FLAG("${GSL_CXX_STD_OPT}" COMPILER_SUPPORTS_CXX_STANDARD)
|
||||
|
||||
if(COMPILER_SUPPORTS_CXX_STANDARD)
|
||||
target_compile_options(GSL INTERFACE "${GSL_CXX_STD_OPT}")
|
||||
else()
|
||||
message(FATAL_ERROR "The compiler ${CMAKE_CXX_COMPILER} has no c++${GSL_CXX_STANDARD} support. Please use a different C++ compiler.")
|
||||
endif()
|
||||
else()
|
||||
target_compile_features(GSL INTERFACE "${GSL_CXX_STD}")
|
||||
# on *nix systems force the use of -std=c++XX instead of -std=gnu++XX (default)
|
||||
set(CMAKE_CXX_EXTENSIONS OFF)
|
||||
endif()
|
||||
endfunction()
|
||||
|
||||
# The best way for a project to specify the GSL's C++ standard is by the client specifying
|
||||
# the CMAKE_CXX_STANDARD. However, this isn't always ideal. Since the CMAKE_CXX_STANDARD is
|
||||
# tied to the cmake version. And many projects have low cmake minimums.
|
||||
#
|
||||
# So provide an alternative approach in case that doesn't work.
|
||||
function(gsl_client_set_cxx_standard min_cxx_standard)
|
||||
if (DEFINED CMAKE_CXX_STANDARD)
|
||||
if (${CMAKE_CXX_STANDARD} VERSION_LESS ${min_cxx_standard})
|
||||
message(FATAL_ERROR "GSL: Requires at least CXX standard ${min_cxx_standard}, user provided ${CMAKE_CXX_STANDARD}")
|
||||
endif()
|
||||
|
||||
# Set the GSL standard to what the client desires
|
||||
set(GSL_CXX_STANDARD "${CMAKE_CXX_STANDARD}" PARENT_SCOPE)
|
||||
|
||||
# Exit out early to avoid extra unneccessary work
|
||||
return()
|
||||
endif()
|
||||
|
||||
# Otherwise pick a reasonable default
|
||||
gsl_set_default_cxx_standard(${min_cxx_standard})
|
||||
endfunction()
|
||||
@@ -0,0 +1,63 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
|
||||
//
|
||||
// This code is licensed under the MIT License (MIT).
|
||||
//
|
||||
// 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 GSL_ALGORITHM_H
|
||||
#define GSL_ALGORITHM_H
|
||||
|
||||
#include <gsl/assert> // for contracts
|
||||
#include <gsl/span> // for dynamic_extent, span
|
||||
|
||||
#include <algorithm> // for copy_n
|
||||
#include <cstddef> // for ptrdiff_t
|
||||
#include <type_traits> // for is_assignable
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(push)
|
||||
|
||||
// turn off some warnings that are noisy about our contract checks
|
||||
#pragma warning(disable : 4127) // conditional expression is constant
|
||||
#pragma warning(disable : 4996) // unsafe use of std::copy_n
|
||||
|
||||
#endif // _MSC_VER
|
||||
|
||||
namespace gsl
|
||||
{
|
||||
// Note: this will generate faster code than std::copy using span iterator in older msvc+stl
|
||||
// not necessary for msvc since VS2017 15.8 (_MSC_VER >= 1915)
|
||||
template <class SrcElementType, std::size_t SrcExtent, class DestElementType,
|
||||
std::size_t DestExtent>
|
||||
void copy(span<SrcElementType, SrcExtent> src, span<DestElementType, DestExtent> dest)
|
||||
{
|
||||
static_assert(std::is_assignable<decltype(*dest.data()), decltype(*src.data())>::value,
|
||||
"Elements of source span can not be assigned to elements of destination span");
|
||||
static_assert(SrcExtent == dynamic_extent || DestExtent == dynamic_extent ||
|
||||
(SrcExtent <= DestExtent),
|
||||
"Source range is longer than target range");
|
||||
|
||||
Expects(dest.size() >= src.size(), Bounds);
|
||||
// clang-format off
|
||||
GSL_SUPPRESS(stl.1) // NO-FORMAT: attribute
|
||||
// clang-format on
|
||||
std::copy_n(src.data(), src.size(), dest.data());
|
||||
}
|
||||
|
||||
} // namespace gsl
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(pop)
|
||||
#endif // _MSC_VER
|
||||
|
||||
#endif // GSL_ALGORITHM_H
|
||||
@@ -0,0 +1,149 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
|
||||
//
|
||||
// This code is licensed under the MIT License (MIT).
|
||||
//
|
||||
// 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 GSL_CONTRACTS_H
|
||||
#define GSL_CONTRACTS_H
|
||||
|
||||
|
||||
//
|
||||
// Temporary until MSVC STL supports no-exceptions mode.
|
||||
// Currently terminate is a no-op in this mode, so we add termination behavior back
|
||||
//
|
||||
#if defined(_MSC_VER) && (defined(_KERNEL_MODE) || (defined(_HAS_EXCEPTIONS) && !_HAS_EXCEPTIONS))
|
||||
|
||||
#define GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND
|
||||
#include <intrin.h>
|
||||
#define RANGE_CHECKS_FAILURE 0
|
||||
|
||||
#if defined(__clang__)
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Winvalid-noreturn"
|
||||
#endif // defined(__clang__)
|
||||
|
||||
#else // defined(_MSC_VER) && (defined(_KERNEL_MODE) || (defined(_HAS_EXCEPTIONS) &&
|
||||
// !_HAS_EXCEPTIONS))
|
||||
|
||||
#include <exception>
|
||||
|
||||
#endif // defined(_MSC_VER) && (defined(_KERNEL_MODE) || (defined(_HAS_EXCEPTIONS) &&
|
||||
// !_HAS_EXCEPTIONS))
|
||||
|
||||
//
|
||||
// make suppress attributes parse for some compilers
|
||||
// Hopefully temporary until suppression standardization occurs
|
||||
//
|
||||
#if defined(__clang__)
|
||||
#define GSL_SUPPRESS(x) [[gsl::suppress("x")]]
|
||||
#else
|
||||
#if defined(_MSC_VER) && !defined(__INTEL_COMPILER)
|
||||
#define GSL_SUPPRESS(x) [[gsl::suppress(x)]]
|
||||
#else
|
||||
#define GSL_SUPPRESS(x)
|
||||
#endif // _MSC_VER
|
||||
#endif // __clang__
|
||||
|
||||
#define GSL_STRINGIFY_DETAIL(x) #x
|
||||
#define GSL_STRINGIFY(x) GSL_STRINGIFY_DETAIL(x)
|
||||
|
||||
//
|
||||
// GSL.assert: assertions
|
||||
//
|
||||
|
||||
namespace gsl
|
||||
{
|
||||
|
||||
namespace details
|
||||
{
|
||||
#if defined(GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND)
|
||||
|
||||
typedef void(__cdecl* terminate_handler)();
|
||||
|
||||
// clang-format off
|
||||
GSL_SUPPRESS(f.6) // NO-FORMAT: attribute
|
||||
// clang-format on
|
||||
[[noreturn]] inline void __cdecl default_terminate_handler()
|
||||
{
|
||||
__fastfail(RANGE_CHECKS_FAILURE);
|
||||
}
|
||||
|
||||
inline gsl::details::terminate_handler& get_terminate_handler() noexcept
|
||||
{
|
||||
static terminate_handler handler = &default_terminate_handler;
|
||||
return handler;
|
||||
}
|
||||
|
||||
#endif // defined(GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND)
|
||||
|
||||
[[noreturn]] inline void terminate() noexcept
|
||||
{
|
||||
#if defined(GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND)
|
||||
(*gsl::details::get_terminate_handler())();
|
||||
#else
|
||||
std::terminate();
|
||||
#endif // defined(GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND)
|
||||
}
|
||||
|
||||
} // namespace details
|
||||
|
||||
|
||||
class contract_group {
|
||||
public:
|
||||
#if defined __cpp_noexcept_function_type
|
||||
using handler = void (*)() noexcept;
|
||||
#else // VESTIGIAL, remove when no longer needed for downlevel compilers
|
||||
using handler = void (*)();
|
||||
#endif
|
||||
constexpr contract_group (handler h = nullptr) : chandler(normalize(h)) { }
|
||||
constexpr auto set_handler(handler h) -> handler { auto old = chandler; chandler = normalize(h); return old; }
|
||||
constexpr auto get_handler() const -> handler { return chandler; }
|
||||
constexpr auto expects (bool b) -> void { assertion(b); }
|
||||
constexpr auto ensures (bool b) -> void { assertion(b); }
|
||||
private:
|
||||
constexpr auto assertion(bool b) -> void { if (!b) chandler(); }
|
||||
constexpr auto normalize(handler h) -> handler { return h ? h : []()noexcept{}; }
|
||||
handler chandler;
|
||||
};
|
||||
|
||||
// By default, there is one violation handler per translation unit.
|
||||
// Defining GSL_GLOBAL_CONTRACT_VIOLATION_HANDLERS and compiling as
|
||||
// C++17 (or later) opts into using a global violation handler.
|
||||
#if defined GSL_GLOBAL_CONTRACT_VIOLATION_HANDLERS && __cplusplus >= 201703L
|
||||
#define GSL_CONTRACT_VIOLATION_GRANULARITY inline
|
||||
#else
|
||||
#define GSL_CONTRACT_VIOLATION_GRANULARITY static
|
||||
#endif
|
||||
|
||||
auto GSL_CONTRACT_VIOLATION_GRANULARITY Default = contract_group(
|
||||
#if defined GSL_UNENFORCED_ON_CONTRACT_VIOLATION
|
||||
// use default == null handler
|
||||
#else // if defined GSL_TERMINATE_ON_CONTRACT_VIOLATION
|
||||
&gsl::details::terminate
|
||||
#endif
|
||||
);
|
||||
auto GSL_CONTRACT_VIOLATION_GRANULARITY Bounds = Default;
|
||||
auto GSL_CONTRACT_VIOLATION_GRANULARITY Null = Default;
|
||||
auto GSL_CONTRACT_VIOLATION_GRANULARITY Testing = Default;
|
||||
|
||||
} // namespace gsl
|
||||
|
||||
#define Expects(cond, kind) kind.expects(cond)
|
||||
#define Ensures(cond, kind) kind.ensures(cond)
|
||||
|
||||
#if defined(GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND) && defined(__clang__)
|
||||
#pragma clang diagnostic pop
|
||||
#endif
|
||||
|
||||
#endif // GSL_CONTRACTS_H
|
||||
@@ -0,0 +1,213 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
|
||||
//
|
||||
// This code is licensed under the MIT License (MIT).
|
||||
//
|
||||
// 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 GSL_BYTE_H
|
||||
#define GSL_BYTE_H
|
||||
|
||||
//
|
||||
// make suppress attributes work for some compilers
|
||||
// Hopefully temporary until suppression standardization occurs
|
||||
//
|
||||
#if defined(__clang__)
|
||||
#define GSL_SUPPRESS(x) [[gsl::suppress("x")]]
|
||||
#else
|
||||
#if defined(_MSC_VER) && !defined(__INTEL_COMPILER)
|
||||
#define GSL_SUPPRESS(x) [[gsl::suppress(x)]]
|
||||
#else
|
||||
#define GSL_SUPPRESS(x)
|
||||
#endif // _MSC_VER
|
||||
#endif // __clang__
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
// VS2017 15.8 added support for the __cpp_lib_byte definition
|
||||
// To do: drop _HAS_STD_BYTE when support for pre 15.8 expires
|
||||
#ifdef _MSC_VER
|
||||
|
||||
#pragma warning(push)
|
||||
|
||||
// Turn MSVC /analyze rules that generate too much noise. TODO: fix in the tool.
|
||||
#pragma warning(disable : 26493) // don't use c-style casts // TODO: MSVC suppression in templates
|
||||
// does not always work
|
||||
|
||||
#ifndef GSL_USE_STD_BYTE
|
||||
// this tests if we are under MSVC and the standard lib has std::byte and it is enabled
|
||||
#if (defined(_HAS_STD_BYTE) && _HAS_STD_BYTE) || \
|
||||
(defined(__cpp_lib_byte) && __cpp_lib_byte >= 201603)
|
||||
|
||||
#define GSL_USE_STD_BYTE 1
|
||||
|
||||
#else // (defined(_HAS_STD_BYTE) && _HAS_STD_BYTE) || (defined(__cpp_lib_byte) && __cpp_lib_byte >=
|
||||
// 201603)
|
||||
|
||||
#define GSL_USE_STD_BYTE 0
|
||||
|
||||
#endif // (defined(_HAS_STD_BYTE) && _HAS_STD_BYTE) || (defined(__cpp_lib_byte) && __cpp_lib_byte >=
|
||||
// 201603)
|
||||
#endif // GSL_USE_STD_BYTE
|
||||
|
||||
#else // _MSC_VER
|
||||
|
||||
#ifndef GSL_USE_STD_BYTE
|
||||
#include <cstddef> /* __cpp_lib_byte */
|
||||
// this tests if we are under GCC or Clang with enough -std=c++1z power to get us std::byte
|
||||
// also check if libc++ version is sufficient (> 5.0) or libstdc++ actually contains std::byte
|
||||
#if defined(__cplusplus) && (__cplusplus >= 201703L) && \
|
||||
(defined(__cpp_lib_byte) && (__cpp_lib_byte >= 201603) || \
|
||||
defined(_LIBCPP_VERSION) && (_LIBCPP_VERSION >= 5000))
|
||||
|
||||
#define GSL_USE_STD_BYTE 1
|
||||
|
||||
#else // defined(__cplusplus) && (__cplusplus >= 201703L) &&
|
||||
// (defined(__cpp_lib_byte) && (__cpp_lib_byte >= 201603) ||
|
||||
// defined(_LIBCPP_VERSION) && (_LIBCPP_VERSION >= 5000))
|
||||
|
||||
#define GSL_USE_STD_BYTE 0
|
||||
|
||||
#endif // defined(__cplusplus) && (__cplusplus >= 201703L) &&
|
||||
// (defined(__cpp_lib_byte) && (__cpp_lib_byte >= 201603) ||
|
||||
// defined(_LIBCPP_VERSION) && (_LIBCPP_VERSION >= 5000))
|
||||
#endif // GSL_USE_STD_BYTE
|
||||
|
||||
#endif // _MSC_VER
|
||||
|
||||
// Use __may_alias__ attribute on gcc and clang
|
||||
#if defined __clang__ || (defined(__GNUC__) && __GNUC__ > 5)
|
||||
#define byte_may_alias __attribute__((__may_alias__))
|
||||
#else // defined __clang__ || defined __GNUC__
|
||||
#define byte_may_alias
|
||||
#endif // defined __clang__ || defined __GNUC__
|
||||
|
||||
#if GSL_USE_STD_BYTE
|
||||
#include <cstddef>
|
||||
#endif
|
||||
|
||||
namespace gsl
|
||||
{
|
||||
#if GSL_USE_STD_BYTE
|
||||
|
||||
using std::byte;
|
||||
using std::to_integer;
|
||||
|
||||
#else // GSL_USE_STD_BYTE
|
||||
|
||||
// This is a simple definition for now that allows
|
||||
// use of byte within span<> to be standards-compliant
|
||||
enum class byte_may_alias byte : unsigned char
|
||||
{
|
||||
};
|
||||
|
||||
template <class IntegerType, class = std::enable_if_t<std::is_integral<IntegerType>::value>>
|
||||
constexpr byte& operator<<=(byte& b, IntegerType shift) noexcept
|
||||
{
|
||||
return b = byte(static_cast<unsigned char>(b) << shift);
|
||||
}
|
||||
|
||||
template <class IntegerType, class = std::enable_if_t<std::is_integral<IntegerType>::value>>
|
||||
constexpr byte operator<<(byte b, IntegerType shift) noexcept
|
||||
{
|
||||
return byte(static_cast<unsigned char>(b) << shift);
|
||||
}
|
||||
|
||||
template <class IntegerType, class = std::enable_if_t<std::is_integral<IntegerType>::value>>
|
||||
constexpr byte& operator>>=(byte& b, IntegerType shift) noexcept
|
||||
{
|
||||
return b = byte(static_cast<unsigned char>(b) >> shift);
|
||||
}
|
||||
|
||||
template <class IntegerType, class = std::enable_if_t<std::is_integral<IntegerType>::value>>
|
||||
constexpr byte operator>>(byte b, IntegerType shift) noexcept
|
||||
{
|
||||
return byte(static_cast<unsigned char>(b) >> shift);
|
||||
}
|
||||
|
||||
constexpr byte& operator|=(byte& l, byte r) noexcept
|
||||
{
|
||||
return l = byte(static_cast<unsigned char>(l) | static_cast<unsigned char>(r));
|
||||
}
|
||||
|
||||
constexpr byte operator|(byte l, byte r) noexcept
|
||||
{
|
||||
return byte(static_cast<unsigned char>(l) | static_cast<unsigned char>(r));
|
||||
}
|
||||
|
||||
constexpr byte& operator&=(byte& l, byte r) noexcept
|
||||
{
|
||||
return l = byte(static_cast<unsigned char>(l) & static_cast<unsigned char>(r));
|
||||
}
|
||||
|
||||
constexpr byte operator&(byte l, byte r) noexcept
|
||||
{
|
||||
return byte(static_cast<unsigned char>(l) & static_cast<unsigned char>(r));
|
||||
}
|
||||
|
||||
constexpr byte& operator^=(byte& l, byte r) noexcept
|
||||
{
|
||||
return l = byte(static_cast<unsigned char>(l) ^ static_cast<unsigned char>(r));
|
||||
}
|
||||
|
||||
constexpr byte operator^(byte l, byte r) noexcept
|
||||
{
|
||||
return byte(static_cast<unsigned char>(l) ^ static_cast<unsigned char>(r));
|
||||
}
|
||||
|
||||
constexpr byte operator~(byte b) noexcept { return byte(~static_cast<unsigned char>(b)); }
|
||||
|
||||
template <class IntegerType, class = std::enable_if_t<std::is_integral<IntegerType>::value>>
|
||||
constexpr IntegerType to_integer(byte b) noexcept
|
||||
{
|
||||
return static_cast<IntegerType>(b);
|
||||
}
|
||||
|
||||
#endif // GSL_USE_STD_BYTE
|
||||
|
||||
template <bool E, typename T>
|
||||
constexpr byte to_byte_impl(T t) noexcept
|
||||
{
|
||||
static_assert(
|
||||
E, "gsl::to_byte(t) must be provided an unsigned char, otherwise data loss may occur. "
|
||||
"If you are calling to_byte with an integer contant use: gsl::to_byte<t>() version.");
|
||||
return static_cast<byte>(t);
|
||||
}
|
||||
template <>
|
||||
// NOTE: need suppression since c++14 does not allow "return {t}"
|
||||
// GSL_SUPPRESS(type.4) // NO-FORMAT: attribute // TODO: suppression does not work
|
||||
constexpr byte to_byte_impl<true, unsigned char>(unsigned char t) noexcept
|
||||
{
|
||||
return byte(t);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
constexpr byte to_byte(T t) noexcept
|
||||
{
|
||||
return to_byte_impl<std::is_same<T, unsigned char>::value, T>(t);
|
||||
}
|
||||
|
||||
template <int I>
|
||||
constexpr byte to_byte() noexcept
|
||||
{
|
||||
static_assert(I >= 0 && I <= 255,
|
||||
"gsl::byte only has 8 bits of storage, values must be in range 0-255");
|
||||
return static_cast<byte>(I);
|
||||
}
|
||||
|
||||
} // namespace gsl
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(pop)
|
||||
#endif // _MSC_VER
|
||||
|
||||
#endif // GSL_BYTE_H
|
||||
+12
-8
@@ -17,13 +17,17 @@
|
||||
#ifndef GSL_GSL_H
|
||||
#define GSL_GSL_H
|
||||
|
||||
#include <gsl/gsl_algorithm> // copy
|
||||
#include <gsl/gsl_assert> // Ensures/Expects
|
||||
#include <gsl/gsl_byte> // byte
|
||||
#include <gsl/gsl_util> // finally()/narrow()/narrow_cast()...
|
||||
#include <gsl/multi_span> // multi_span, strided_span...
|
||||
#include <gsl/pointers> // owner, not_null
|
||||
#include <gsl/span> // span
|
||||
#include <gsl/string_span> // zstring, string_span, zstring_builder...
|
||||
#include <gsl/algorithm> // copy
|
||||
#include <gsl/assert> // contracts
|
||||
#include <gsl/byte> // byte
|
||||
#include <gsl/pointers> // owner, not_null
|
||||
#include <gsl/multi_span> // multi_span, strided_span...
|
||||
#include <gsl/span> // span
|
||||
#include <gsl/string_span> // zstring, string_span, zstring_builder...
|
||||
#include <gsl/util> // finally()/narrow_cast()...
|
||||
|
||||
#ifdef __cpp_exceptions
|
||||
#include <gsl/narrow> // narrow()
|
||||
#endif
|
||||
|
||||
#endif // GSL_GSL_H
|
||||
|
||||
@@ -1,61 +1,3 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
|
||||
//
|
||||
// This code is licensed under the MIT License (MIT).
|
||||
//
|
||||
// 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 GSL_ALGORITHM_H
|
||||
#define GSL_ALGORITHM_H
|
||||
|
||||
#include <gsl/gsl_assert> // for Expects
|
||||
#include <gsl/span> // for dynamic_extent, span
|
||||
|
||||
#include <algorithm> // for copy_n
|
||||
#include <cstddef> // for ptrdiff_t
|
||||
#include <type_traits> // for is_assignable
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(push)
|
||||
|
||||
// turn off some warnings that are noisy about our Expects statements
|
||||
#pragma warning(disable : 4127) // conditional expression is constant
|
||||
#pragma warning(disable : 4996) // unsafe use of std::copy_n
|
||||
|
||||
#endif // _MSC_VER
|
||||
|
||||
namespace gsl
|
||||
{
|
||||
// Note: this will generate faster code than std::copy using span iterator in older msvc+stl
|
||||
// not necessary for msvc since VS2017 15.8 (_MSC_VER >= 1915)
|
||||
template <class SrcElementType, std::ptrdiff_t SrcExtent, class DestElementType,
|
||||
std::ptrdiff_t DestExtent>
|
||||
void copy(span<SrcElementType, SrcExtent> src, span<DestElementType, DestExtent> dest)
|
||||
{
|
||||
static_assert(std::is_assignable<decltype(*dest.data()), decltype(*src.data())>::value,
|
||||
"Elements of source span can not be assigned to elements of destination span");
|
||||
static_assert(SrcExtent == dynamic_extent || DestExtent == dynamic_extent ||
|
||||
(SrcExtent <= DestExtent),
|
||||
"Source range is longer than target range");
|
||||
|
||||
Expects(dest.size() >= src.size());
|
||||
GSL_SUPPRESS(stl.1) // NO-FORMAT: attribute
|
||||
std::copy_n(src.data(), src.size(), dest.data());
|
||||
}
|
||||
|
||||
} // namespace gsl
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(pop)
|
||||
#endif // _MSC_VER
|
||||
|
||||
#endif // GSL_ALGORITHM_H
|
||||
#pragma once
|
||||
#pragma message("This header will soon be removed. Use <gsl/algorithm> instead of <gsl/gsl_algorithm>")
|
||||
#include <gsl/algorithm>
|
||||
|
||||
+3
-177
@@ -1,177 +1,3 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
|
||||
//
|
||||
// This code is licensed under the MIT License (MIT).
|
||||
//
|
||||
// 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 GSL_CONTRACTS_H
|
||||
#define GSL_CONTRACTS_H
|
||||
|
||||
#include <exception>
|
||||
#include <stdexcept> // for logic_error
|
||||
|
||||
//
|
||||
// make suppress attributes parse for some compilers
|
||||
// Hopefully temporary until suppression standardization occurs
|
||||
//
|
||||
#if defined(__clang__)
|
||||
#define GSL_SUPPRESS(x) [[gsl::suppress("x")]]
|
||||
#else
|
||||
#if defined(_MSC_VER)
|
||||
#define GSL_SUPPRESS(x) [[gsl::suppress(x)]]
|
||||
#else
|
||||
#define GSL_SUPPRESS(x)
|
||||
#endif // _MSC_VER
|
||||
#endif // __clang__
|
||||
|
||||
//
|
||||
// Temporary until MSVC STL supports no-exceptions mode.
|
||||
// Currently terminate is a no-op in this mode, so we add termination behavior back
|
||||
//
|
||||
#if defined(_MSC_VER) && defined(_HAS_EXCEPTIONS) && !_HAS_EXCEPTIONS
|
||||
#define GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND
|
||||
#include <intrin.h>
|
||||
#define RANGE_CHECKS_FAILURE 0
|
||||
|
||||
#if defined(__clang__)
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Winvalid-noreturn"
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
//
|
||||
// There are three configuration options for this GSL implementation's behavior
|
||||
// when pre/post conditions on the GSL types are violated:
|
||||
//
|
||||
// 1. GSL_TERMINATE_ON_CONTRACT_VIOLATION: std::terminate will be called (default)
|
||||
// 2. GSL_THROW_ON_CONTRACT_VIOLATION: a gsl::fail_fast exception will be thrown
|
||||
// 3. GSL_UNENFORCED_ON_CONTRACT_VIOLATION: nothing happens
|
||||
//
|
||||
#if !(defined(GSL_THROW_ON_CONTRACT_VIOLATION) || defined(GSL_TERMINATE_ON_CONTRACT_VIOLATION) || \
|
||||
defined(GSL_UNENFORCED_ON_CONTRACT_VIOLATION))
|
||||
#define GSL_TERMINATE_ON_CONTRACT_VIOLATION
|
||||
#endif
|
||||
|
||||
#define GSL_STRINGIFY_DETAIL(x) #x
|
||||
#define GSL_STRINGIFY(x) GSL_STRINGIFY_DETAIL(x)
|
||||
|
||||
#if defined(__clang__) || defined(__GNUC__)
|
||||
#define GSL_LIKELY(x) __builtin_expect(!!(x), 1)
|
||||
#define GSL_UNLIKELY(x) __builtin_expect(!!(x), 0)
|
||||
#else
|
||||
#define GSL_LIKELY(x) (!!(x))
|
||||
#define GSL_UNLIKELY(x) (!!(x))
|
||||
#endif
|
||||
|
||||
//
|
||||
// GSL_ASSUME(cond)
|
||||
//
|
||||
// Tell the optimizer that the predicate cond must hold. It is unspecified
|
||||
// whether or not cond is actually evaluated.
|
||||
//
|
||||
#ifdef _MSC_VER
|
||||
#define GSL_ASSUME(cond) __assume(cond)
|
||||
#elif defined(__GNUC__)
|
||||
#define GSL_ASSUME(cond) ((cond) ? static_cast<void>(0) : __builtin_unreachable())
|
||||
#else
|
||||
#define GSL_ASSUME(cond) static_cast<void>((cond) ? 0 : 0)
|
||||
#endif
|
||||
|
||||
//
|
||||
// GSL.assert: assertions
|
||||
//
|
||||
|
||||
namespace gsl
|
||||
{
|
||||
struct fail_fast : public std::logic_error
|
||||
{
|
||||
explicit fail_fast(char const* const message) : std::logic_error(message) {}
|
||||
};
|
||||
|
||||
namespace details
|
||||
{
|
||||
#if defined(GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND)
|
||||
|
||||
typedef void (__cdecl *terminate_handler)();
|
||||
|
||||
GSL_SUPPRESS(f.6) // NO-FORMAT: attribute
|
||||
[[noreturn]] inline void __cdecl default_terminate_handler()
|
||||
{
|
||||
__fastfail(RANGE_CHECKS_FAILURE);
|
||||
}
|
||||
|
||||
inline gsl::details::terminate_handler& get_terminate_handler() noexcept
|
||||
{
|
||||
static terminate_handler handler = &default_terminate_handler;
|
||||
return handler;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
[[noreturn]] inline void terminate() noexcept
|
||||
{
|
||||
#if defined(GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND)
|
||||
(*gsl::details::get_terminate_handler())();
|
||||
#else
|
||||
std::terminate();
|
||||
#endif
|
||||
}
|
||||
|
||||
#if defined(GSL_TERMINATE_ON_CONTRACT_VIOLATION)
|
||||
|
||||
template <typename Exception>
|
||||
[[noreturn]] void throw_exception(Exception&&) noexcept
|
||||
{
|
||||
gsl::details::terminate();
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
template <typename Exception>
|
||||
[[noreturn]] void throw_exception(Exception&& exception)
|
||||
{
|
||||
throw std::forward<Exception>(exception);
|
||||
}
|
||||
|
||||
#endif // GSL_TERMINATE_ON_CONTRACT_VIOLATION
|
||||
|
||||
} // namespace details
|
||||
} // namespace gsl
|
||||
|
||||
#if defined(GSL_THROW_ON_CONTRACT_VIOLATION)
|
||||
|
||||
#define GSL_CONTRACT_CHECK(type, cond) \
|
||||
(GSL_LIKELY(cond) ? static_cast<void>(0) \
|
||||
: gsl::details::throw_exception(gsl::fail_fast( \
|
||||
"GSL: " type " failure at " __FILE__ ": " GSL_STRINGIFY(__LINE__))))
|
||||
|
||||
#elif defined(GSL_TERMINATE_ON_CONTRACT_VIOLATION)
|
||||
|
||||
#define GSL_CONTRACT_CHECK(type, cond) \
|
||||
(GSL_LIKELY(cond) ? static_cast<void>(0) : gsl::details::terminate())
|
||||
|
||||
#elif defined(GSL_UNENFORCED_ON_CONTRACT_VIOLATION)
|
||||
|
||||
#define GSL_CONTRACT_CHECK(type, cond) GSL_ASSUME(cond)
|
||||
|
||||
#endif // GSL_THROW_ON_CONTRACT_VIOLATION
|
||||
|
||||
#define Expects(cond) GSL_CONTRACT_CHECK("Precondition", cond)
|
||||
#define Ensures(cond) GSL_CONTRACT_CHECK("Postcondition", cond)
|
||||
|
||||
#if defined(GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND) && defined(__clang__)
|
||||
#pragma clang diagnostic pop
|
||||
#endif
|
||||
|
||||
#endif // GSL_CONTRACTS_H
|
||||
#pragma once
|
||||
#pragma message("This header will soon be removed. Use <gsl/assert> instead of <gsl/gsl_assert>")
|
||||
#include <gsl/assert>
|
||||
|
||||
+3
-205
@@ -1,205 +1,3 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
|
||||
//
|
||||
// This code is licensed under the MIT License (MIT).
|
||||
//
|
||||
// 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 GSL_BYTE_H
|
||||
#define GSL_BYTE_H
|
||||
|
||||
//
|
||||
// make suppress attributes work for some compilers
|
||||
// Hopefully temporary until suppression standardization occurs
|
||||
//
|
||||
#if defined(__clang__)
|
||||
#define GSL_SUPPRESS(x) [[gsl::suppress("x")]]
|
||||
#else
|
||||
#if defined(_MSC_VER)
|
||||
#define GSL_SUPPRESS(x) [[gsl::suppress(x)]]
|
||||
#else
|
||||
#define GSL_SUPPRESS(x)
|
||||
#endif // _MSC_VER
|
||||
#endif // __clang__
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
// VS2017 15.8 added support for the __cpp_lib_byte definition
|
||||
// To do: drop _HAS_STD_BYTE when support for pre 15.8 expires
|
||||
#ifdef _MSC_VER
|
||||
|
||||
#pragma warning(push)
|
||||
|
||||
// Turn MSVC /analyze rules that generate too much noise. TODO: fix in the tool.
|
||||
#pragma warning(disable : 26493) // don't use c-style casts // TODO: MSVC suppression in templates does not always work
|
||||
|
||||
#ifndef GSL_USE_STD_BYTE
|
||||
// this tests if we are under MSVC and the standard lib has std::byte and it is enabled
|
||||
#if (defined(_HAS_STD_BYTE) && _HAS_STD_BYTE) || (defined(__cpp_lib_byte) && __cpp_lib_byte >= 201603)
|
||||
|
||||
#define GSL_USE_STD_BYTE 1
|
||||
|
||||
#else // (defined(_HAS_STD_BYTE) && _HAS_STD_BYTE) || (defined(__cpp_lib_byte) && __cpp_lib_byte >= 201603)
|
||||
|
||||
#define GSL_USE_STD_BYTE 0
|
||||
|
||||
#endif // (defined(_HAS_STD_BYTE) && _HAS_STD_BYTE) || (defined(__cpp_lib_byte) && __cpp_lib_byte >= 201603)
|
||||
#endif // GSL_USE_STD_BYTE
|
||||
|
||||
#else // _MSC_VER
|
||||
|
||||
#ifndef GSL_USE_STD_BYTE
|
||||
#include <cstddef> /* __cpp_lib_byte */
|
||||
// this tests if we are under GCC or Clang with enough -std:c++1z power to get us std::byte
|
||||
// also check if libc++ version is sufficient (> 5.0) or libstc++ actually contains std::byte
|
||||
#if defined(__cplusplus) && (__cplusplus >= 201703L) && \
|
||||
(defined(__cpp_lib_byte) && (__cpp_lib_byte >= 201603) || \
|
||||
defined(_LIBCPP_VERSION) && (_LIBCPP_VERSION >= 5000))
|
||||
|
||||
#define GSL_USE_STD_BYTE 1
|
||||
|
||||
#else // defined(__cplusplus) && (__cplusplus >= 201703L) &&
|
||||
// (defined(__cpp_lib_byte) && (__cpp_lib_byte >= 201603) ||
|
||||
// defined(_LIBCPP_VERSION) && (_LIBCPP_VERSION >= 5000))
|
||||
|
||||
#define GSL_USE_STD_BYTE 0
|
||||
|
||||
#endif //defined(__cplusplus) && (__cplusplus >= 201703L) &&
|
||||
// (defined(__cpp_lib_byte) && (__cpp_lib_byte >= 201603) ||
|
||||
// defined(_LIBCPP_VERSION) && (_LIBCPP_VERSION >= 5000))
|
||||
#endif // GSL_USE_STD_BYTE
|
||||
|
||||
#endif // _MSC_VER
|
||||
|
||||
// Use __may_alias__ attribute on gcc and clang
|
||||
#if defined __clang__ || (defined(__GNUC__) && __GNUC__ > 5)
|
||||
#define byte_may_alias __attribute__((__may_alias__))
|
||||
#else // defined __clang__ || defined __GNUC__
|
||||
#define byte_may_alias
|
||||
#endif // defined __clang__ || defined __GNUC__
|
||||
|
||||
namespace gsl
|
||||
{
|
||||
#if GSL_USE_STD_BYTE
|
||||
|
||||
using std::byte;
|
||||
using std::to_integer;
|
||||
|
||||
#else // GSL_USE_STD_BYTE
|
||||
|
||||
// This is a simple definition for now that allows
|
||||
// use of byte within span<> to be standards-compliant
|
||||
enum class byte_may_alias byte : unsigned char
|
||||
{
|
||||
};
|
||||
|
||||
template <class IntegerType, class = std::enable_if_t<std::is_integral<IntegerType>::value>>
|
||||
constexpr byte& operator<<=(byte& b, IntegerType shift) noexcept
|
||||
{
|
||||
return b = byte(static_cast<unsigned char>(b) << shift);
|
||||
}
|
||||
|
||||
template <class IntegerType, class = std::enable_if_t<std::is_integral<IntegerType>::value>>
|
||||
constexpr byte operator<<(byte b, IntegerType shift) noexcept
|
||||
{
|
||||
return byte(static_cast<unsigned char>(b) << shift);
|
||||
}
|
||||
|
||||
template <class IntegerType, class = std::enable_if_t<std::is_integral<IntegerType>::value>>
|
||||
constexpr byte& operator>>=(byte& b, IntegerType shift) noexcept
|
||||
{
|
||||
return b = byte(static_cast<unsigned char>(b) >> shift);
|
||||
}
|
||||
|
||||
template <class IntegerType, class = std::enable_if_t<std::is_integral<IntegerType>::value>>
|
||||
constexpr byte operator>>(byte b, IntegerType shift) noexcept
|
||||
{
|
||||
return byte(static_cast<unsigned char>(b) >> shift);
|
||||
}
|
||||
|
||||
constexpr byte& operator|=(byte& l, byte r) noexcept
|
||||
{
|
||||
return l = byte(static_cast<unsigned char>(l) | static_cast<unsigned char>(r));
|
||||
}
|
||||
|
||||
constexpr byte operator|(byte l, byte r) noexcept
|
||||
{
|
||||
return byte(static_cast<unsigned char>(l) | static_cast<unsigned char>(r));
|
||||
}
|
||||
|
||||
constexpr byte& operator&=(byte& l, byte r) noexcept
|
||||
{
|
||||
return l = byte(static_cast<unsigned char>(l) & static_cast<unsigned char>(r));
|
||||
}
|
||||
|
||||
constexpr byte operator&(byte l, byte r) noexcept
|
||||
{
|
||||
return byte(static_cast<unsigned char>(l) & static_cast<unsigned char>(r));
|
||||
}
|
||||
|
||||
constexpr byte& operator^=(byte& l, byte r) noexcept
|
||||
{
|
||||
return l = byte(static_cast<unsigned char>(l) ^ static_cast<unsigned char>(r));
|
||||
}
|
||||
|
||||
constexpr byte operator^(byte l, byte r) noexcept
|
||||
{
|
||||
return byte(static_cast<unsigned char>(l) ^ static_cast<unsigned char>(r));
|
||||
}
|
||||
|
||||
constexpr byte operator~(byte b) noexcept { return byte(~static_cast<unsigned char>(b)); }
|
||||
|
||||
template <class IntegerType, class = std::enable_if_t<std::is_integral<IntegerType>::value>>
|
||||
constexpr IntegerType to_integer(byte b) noexcept
|
||||
{
|
||||
return static_cast<IntegerType>(b);
|
||||
}
|
||||
|
||||
#endif // GSL_USE_STD_BYTE
|
||||
|
||||
template <bool E, typename T>
|
||||
constexpr byte to_byte_impl(T t) noexcept
|
||||
{
|
||||
static_assert(
|
||||
E, "gsl::to_byte(t) must be provided an unsigned char, otherwise data loss may occur. "
|
||||
"If you are calling to_byte with an integer contant use: gsl::to_byte<t>() version.");
|
||||
return static_cast<byte>(t);
|
||||
}
|
||||
template <>
|
||||
// NOTE: need suppression since c++14 does not allow "return {t}"
|
||||
// GSL_SUPPRESS(type.4) // NO-FORMAT: attribute // TODO: suppression does not work
|
||||
constexpr byte to_byte_impl<true, unsigned char>(unsigned char t) noexcept
|
||||
{
|
||||
return byte(t);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
constexpr byte to_byte(T t) noexcept
|
||||
{
|
||||
return to_byte_impl<std::is_same<T, unsigned char>::value, T>(t);
|
||||
}
|
||||
|
||||
template <int I>
|
||||
constexpr byte to_byte() noexcept
|
||||
{
|
||||
static_assert(I >= 0 && I <= 255,
|
||||
"gsl::byte only has 8 bits of storage, values must be in range 0-255");
|
||||
return static_cast<byte>(I);
|
||||
}
|
||||
|
||||
} // namespace gsl
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(pop)
|
||||
#endif // _MSC_VER
|
||||
|
||||
#endif // GSL_BYTE_H
|
||||
#pragma once
|
||||
#pragma message("This header will soon be removed. Use <gsl/byte> instead of <gsl/gsl_byte>")
|
||||
#include <gsl/byte>
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
#pragma once
|
||||
#pragma message("This header will soon be removed. Use <gsl/narrow> instead of <gsl/gsl_narrow>")
|
||||
#include <gsl/narrow>
|
||||
+3
-172
@@ -1,172 +1,3 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
|
||||
//
|
||||
// This code is licensed under the MIT License (MIT).
|
||||
//
|
||||
// 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 GSL_UTIL_H
|
||||
#define GSL_UTIL_H
|
||||
|
||||
#include <gsl/gsl_assert> // for Expects
|
||||
|
||||
#include <array>
|
||||
#include <cstddef> // for ptrdiff_t, size_t
|
||||
#include <exception> // for exception
|
||||
#include <initializer_list> // for initializer_list
|
||||
#include <type_traits> // for is_signed, integral_constant
|
||||
#include <utility> // for exchange, forward
|
||||
|
||||
#if defined(_MSC_VER) && !defined(__clang__)
|
||||
|
||||
#pragma warning(push)
|
||||
#pragma warning(disable : 4127) // conditional expression is constant
|
||||
|
||||
#if _MSC_VER < 1910
|
||||
#pragma push_macro("constexpr")
|
||||
#define constexpr /*constexpr*/
|
||||
#endif // _MSC_VER < 1910
|
||||
#endif // _MSC_VER
|
||||
|
||||
#if (defined(_MSC_VER) && _MSC_VER < 1910) || (!defined(__clang__) && defined(__GNUC__) && __GNUC__ < 6)
|
||||
#define GSL_CONSTEXPR_NARROW 0
|
||||
#else
|
||||
#define GSL_CONSTEXPR_NARROW 1
|
||||
#endif
|
||||
|
||||
namespace gsl
|
||||
{
|
||||
//
|
||||
// GSL.util: utilities
|
||||
//
|
||||
|
||||
// index type for all container indexes/subscripts/sizes
|
||||
using index = std::ptrdiff_t;
|
||||
|
||||
// final_action allows you to ensure something gets run at the end of a scope
|
||||
template <class F>
|
||||
class final_action
|
||||
{
|
||||
public:
|
||||
explicit final_action(F f) noexcept : f_(std::move(f)) {}
|
||||
|
||||
final_action(final_action&& other) noexcept : f_(std::move(other.f_)), invoke_(std::exchange(other.invoke_, false)) {}
|
||||
|
||||
final_action(const final_action&) = delete;
|
||||
final_action& operator=(const final_action&) = delete;
|
||||
final_action& operator=(final_action&&) = delete;
|
||||
|
||||
GSL_SUPPRESS(f.6) // NO-FORMAT: attribute // terminate if throws
|
||||
~final_action() noexcept
|
||||
{
|
||||
if (invoke_) f_();
|
||||
}
|
||||
|
||||
private:
|
||||
F f_;
|
||||
bool invoke_{true};
|
||||
};
|
||||
|
||||
// finally() - convenience function to generate a final_action
|
||||
template <class F>
|
||||
final_action<F> finally(const F& f) noexcept
|
||||
{
|
||||
return final_action<F>(f);
|
||||
}
|
||||
|
||||
template <class F>
|
||||
final_action<F> finally(F&& f) noexcept
|
||||
{
|
||||
return final_action<F>(std::forward<F>(f));
|
||||
}
|
||||
|
||||
// narrow_cast(): a searchable way to do narrowing casts of values
|
||||
template <class T, class U>
|
||||
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
|
||||
constexpr T narrow_cast(U&& u) noexcept
|
||||
{
|
||||
return static_cast<T>(std::forward<U>(u));
|
||||
}
|
||||
|
||||
struct narrowing_error : public std::exception
|
||||
{
|
||||
};
|
||||
|
||||
namespace details
|
||||
{
|
||||
template <class T, class U>
|
||||
struct is_same_signedness
|
||||
: public std::integral_constant<bool, std::is_signed<T>::value == std::is_signed<U>::value>
|
||||
{
|
||||
};
|
||||
} // namespace details
|
||||
|
||||
// narrow() : a checked version of narrow_cast() that throws if the cast changed the value
|
||||
template <class T, class U>
|
||||
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
|
||||
GSL_SUPPRESS(f.6) // NO-FORMAT: attribute // TODO: MSVC /analyze does not recognise noexcept(false)
|
||||
#if GSL_CONSTEXPR_NARROW
|
||||
constexpr
|
||||
#endif
|
||||
T narrow(U u) noexcept(false)
|
||||
{
|
||||
T t = narrow_cast<T>(u);
|
||||
if (static_cast<U>(t) != u) gsl::details::throw_exception(narrowing_error());
|
||||
if (!details::is_same_signedness<T, U>::value && ((t < T{}) != (u < U{})))
|
||||
gsl::details::throw_exception(narrowing_error());
|
||||
return t;
|
||||
}
|
||||
|
||||
//
|
||||
// at() - Bounds-checked way of accessing builtin arrays, std::array, std::vector
|
||||
//
|
||||
template <class T, std::size_t N>
|
||||
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
|
||||
GSL_SUPPRESS(bounds.2) // NO-FORMAT: attribute
|
||||
constexpr T& at(T (&arr)[N], const index i)
|
||||
{
|
||||
Expects(i >= 0 && i < narrow_cast<index>(N));
|
||||
return arr[narrow_cast<std::size_t>(i)];
|
||||
}
|
||||
|
||||
template <class Cont>
|
||||
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
|
||||
GSL_SUPPRESS(bounds.2) // NO-FORMAT: attribute
|
||||
constexpr auto at(Cont& cont, const index i) -> decltype(cont[cont.size()])
|
||||
{
|
||||
Expects(i >= 0 && i < narrow_cast<index>(cont.size()));
|
||||
using size_type = decltype(cont.size());
|
||||
return cont[narrow_cast<size_type>(i)];
|
||||
}
|
||||
|
||||
template <class T>
|
||||
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
|
||||
constexpr T at(const std::initializer_list<T> cont, const index i)
|
||||
{
|
||||
Expects(i >= 0 && i < narrow_cast<index>(cont.size()));
|
||||
return *(cont.begin() + i);
|
||||
}
|
||||
|
||||
} // namespace gsl
|
||||
|
||||
#if defined(_MSC_VER) && !defined(__clang__)
|
||||
#if _MSC_VER < 1910
|
||||
#undef constexpr
|
||||
#pragma pop_macro("constexpr")
|
||||
|
||||
#endif // _MSC_VER < 1910
|
||||
|
||||
#pragma warning(pop)
|
||||
|
||||
#endif // _MSC_VER
|
||||
|
||||
#endif // GSL_UTIL_H
|
||||
#pragma once
|
||||
#pragma message("This header will soon be removed. Use <gsl/util> instead of <gsl/gsl_util>")
|
||||
#include <gsl/util>
|
||||
|
||||
+184
-78
@@ -17,13 +17,13 @@
|
||||
#ifndef GSL_MULTI_SPAN_H
|
||||
#define GSL_MULTI_SPAN_H
|
||||
|
||||
#include <gsl/gsl_assert> // for Expects
|
||||
#include <gsl/gsl_byte> // for byte
|
||||
#include <gsl/gsl_util> // for narrow_cast
|
||||
#include <gsl/assert> // for Expects
|
||||
#include <gsl/byte> // for byte
|
||||
#include <gsl/util> // for narrow_cast
|
||||
|
||||
#include <algorithm> // for transform, lexicographical_compare
|
||||
#include <array> // for array
|
||||
#include <cstddef> // for ptrdiff_t, size_t, nullptr_t
|
||||
#include <algorithm> // for transform, lexicographical_compare
|
||||
#include <array> // for array
|
||||
#include <cstddef> // for std::ptrdiff_t, size_t, nullptr_t
|
||||
#include <cstdint> // for PTRDIFF_MAX
|
||||
#include <functional> // for divides, multiplies, minus, negate, plus
|
||||
#include <initializer_list> // for initializer_list
|
||||
@@ -50,19 +50,14 @@
|
||||
#pragma warning(disable : 26465) // TODO: bug - suppression does not work on template functions
|
||||
#pragma warning(disable : 4996) // use of function or classes marked [[deprecated]]
|
||||
|
||||
#if _MSC_VER < 1910
|
||||
#pragma push_macro("constexpr")
|
||||
#define constexpr /*constexpr*/
|
||||
|
||||
#endif // _MSC_VER < 1910
|
||||
#endif // _MSC_VER
|
||||
|
||||
#if __clang__ || __GNUC__
|
||||
#if defined(__GNUC__) || defined(__clang__)
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
||||
#endif
|
||||
|
||||
// GCC 7 does not like the signed unsigned missmatch (size_t ptrdiff_t)
|
||||
// GCC 7 does not like the signed unsigned missmatch (size_t std::ptrdiff_t)
|
||||
// While there is a conversion from signed to unsigned, it happens at
|
||||
// compiletime, so the compiler wouldn't have to warn indiscriminently, but
|
||||
// could check if the source value actually doesn't fit into the target type
|
||||
@@ -100,7 +95,8 @@ namespace details
|
||||
} // namespace details
|
||||
|
||||
template <std::size_t Rank>
|
||||
class [[deprecated]] multi_span_index final {
|
||||
class [[deprecated]] multi_span_index final
|
||||
{
|
||||
static_assert(Rank > 0, "Rank must be greater than 0!");
|
||||
|
||||
template <std::size_t OtherRank>
|
||||
@@ -115,10 +111,12 @@ public:
|
||||
|
||||
constexpr multi_span_index() noexcept {}
|
||||
|
||||
constexpr multi_span_index(const value_type(&values)[Rank]) noexcept
|
||||
constexpr multi_span_index(const value_type (&values)[Rank]) noexcept
|
||||
{
|
||||
// clang-format off
|
||||
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
|
||||
GSL_SUPPRESS(bounds.3) // NO-FORMAT: attribute
|
||||
// clang-format on
|
||||
std::copy(values, values + Rank, elems);
|
||||
}
|
||||
|
||||
@@ -132,8 +130,10 @@ public:
|
||||
constexpr multi_span_index& operator=(const multi_span_index& rhs) noexcept = default;
|
||||
|
||||
// Preconditions: component_idx < rank
|
||||
// clang-format off
|
||||
GSL_SUPPRESS(bounds.2) // NO-FORMAT: attribute
|
||||
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
|
||||
// clang-format on
|
||||
constexpr reference operator[](std::size_t component_idx)
|
||||
{
|
||||
Expects(component_idx < Rank); // Component index must be less than rank
|
||||
@@ -141,16 +141,20 @@ public:
|
||||
}
|
||||
|
||||
// Preconditions: component_idx < rank
|
||||
// clang-format off
|
||||
GSL_SUPPRESS(bounds.2) // NO-FORMAT: attribute
|
||||
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
|
||||
// clang-format on
|
||||
constexpr const_reference operator[](std::size_t component_idx) const
|
||||
{
|
||||
Expects(component_idx < Rank); // Component index must be less than rank
|
||||
return elems[component_idx];
|
||||
}
|
||||
|
||||
// clang-format off
|
||||
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
|
||||
GSL_SUPPRESS(bounds.3) // NO-FORMAT: attribute
|
||||
// clang-format on
|
||||
constexpr bool operator==(const multi_span_index& rhs) const
|
||||
{
|
||||
return std::equal(elems, elems + rank, rhs.elems);
|
||||
@@ -183,16 +187,20 @@ public:
|
||||
|
||||
constexpr multi_span_index& operator+=(const multi_span_index& rhs)
|
||||
{
|
||||
// clang-format off
|
||||
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
|
||||
GSL_SUPPRESS(bounds.3) // NO-FORMAT: attribute
|
||||
// clang-format on
|
||||
std::transform(elems, elems + rank, rhs.elems, elems, std::plus<value_type>{});
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr multi_span_index& operator-=(const multi_span_index& rhs)
|
||||
{
|
||||
// clang-format off
|
||||
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
|
||||
GSL_SUPPRESS(bounds.3) // NO-FORMAT: attribute
|
||||
// clang-format on
|
||||
std::transform(elems, elems + rank, rhs.elems, elems, std::minus<value_type>{});
|
||||
return *this;
|
||||
}
|
||||
@@ -218,8 +226,10 @@ public:
|
||||
|
||||
constexpr multi_span_index& operator*=(value_type v)
|
||||
{
|
||||
// clang-format off
|
||||
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
|
||||
GSL_SUPPRESS(bounds.3) // NO-FORMAT: attribute
|
||||
// clang-format on
|
||||
std::transform(elems, elems + rank, elems,
|
||||
[v](value_type x) { return std::multiplies<value_type>{}(x, v); });
|
||||
return *this;
|
||||
@@ -227,8 +237,10 @@ public:
|
||||
|
||||
constexpr multi_span_index& operator/=(value_type v)
|
||||
{
|
||||
// clang-format off
|
||||
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
|
||||
GSL_SUPPRESS(bounds.3) // NO-FORMAT: attribute
|
||||
// clang-format on
|
||||
std::transform(elems, elems + rank, elems,
|
||||
[v](value_type x) { return std::divides<value_type>{}(x, v); });
|
||||
return *this;
|
||||
@@ -291,7 +303,9 @@ const std::ptrdiff_t dynamic_range = -1;
|
||||
struct [[deprecated]] generalized_mapping_tag
|
||||
{
|
||||
};
|
||||
struct[[deprecated]] contiguous_mapping_tag : generalized_mapping_tag{};
|
||||
struct [[deprecated]] contiguous_mapping_tag : generalized_mapping_tag
|
||||
{
|
||||
};
|
||||
|
||||
namespace details
|
||||
{
|
||||
@@ -303,7 +317,8 @@ namespace details
|
||||
};
|
||||
|
||||
template <std::ptrdiff_t... Ranges>
|
||||
struct [[deprecated]] BoundsRanges {
|
||||
struct [[deprecated]] BoundsRanges
|
||||
{
|
||||
using size_type = std::ptrdiff_t;
|
||||
static const size_type Depth = 0;
|
||||
static const size_type DynamicNum = 0;
|
||||
@@ -342,7 +357,7 @@ namespace details
|
||||
};
|
||||
|
||||
template <std::ptrdiff_t... RestRanges>
|
||||
struct[[deprecated]] BoundsRanges<dynamic_range, RestRanges...> : BoundsRanges<RestRanges...>
|
||||
struct [[deprecated]] BoundsRanges<dynamic_range, RestRanges...> : BoundsRanges<RestRanges...>
|
||||
{
|
||||
using Base = BoundsRanges<RestRanges...>;
|
||||
using size_type = std::ptrdiff_t;
|
||||
@@ -355,9 +370,10 @@ namespace details
|
||||
size_type m_bound;
|
||||
|
||||
public:
|
||||
GSL_SUPPRESS(
|
||||
f.23) // NO-FORMAT: attribute // this pointer type is cannot be assigned nullptr - issue in not_null
|
||||
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
|
||||
// clang-format off
|
||||
GSL_SUPPRESS(f.23) // NO-FORMAT: attribute // this pointer type is cannot be assigned nullptr - issue in not_null
|
||||
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
|
||||
// clang-format on
|
||||
constexpr BoundsRanges(const std::ptrdiff_t* const arr)
|
||||
: Base(arr + 1), m_bound(*arr * this->Base::totalSize())
|
||||
{
|
||||
@@ -374,14 +390,16 @@ namespace details
|
||||
{}
|
||||
|
||||
template <typename T, std::size_t Dim = 0>
|
||||
constexpr void serialize(T & arr) const
|
||||
constexpr void serialize(T& arr) const
|
||||
{
|
||||
arr[Dim] = elementNum();
|
||||
this->Base::template serialize<T, Dim + 1>(arr);
|
||||
}
|
||||
|
||||
template <typename T, std::size_t Dim = 0>
|
||||
// clang-format off
|
||||
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
|
||||
// clang-format on
|
||||
constexpr size_type linearize(const T& arr) const
|
||||
{
|
||||
const size_type index = this->Base::totalSize() * arr[Dim];
|
||||
@@ -392,25 +410,28 @@ namespace details
|
||||
template <typename T, std::size_t Dim = 0>
|
||||
constexpr size_type contains(const T& arr) const
|
||||
{
|
||||
const ptrdiff_t last = this->Base::template contains<T, Dim + 1>(arr);
|
||||
const std::ptrdiff_t last = this->Base::template contains<T, Dim + 1>(arr);
|
||||
if (last == -1) return -1;
|
||||
const ptrdiff_t cur = this->Base::totalSize() * arr[Dim];
|
||||
const std::ptrdiff_t cur = this->Base::totalSize() * arr[Dim];
|
||||
return cur < m_bound ? cur + last : -1;
|
||||
}
|
||||
|
||||
GSL_SUPPRESS(
|
||||
c.128) // NO-FORMAT: attribute // no pointers to BoundsRanges should be ever used
|
||||
// clang-format off
|
||||
GSL_SUPPRESS(c.128) // NO-FORMAT: attribute // no pointers to BoundsRanges should be ever used
|
||||
// clang-format on
|
||||
constexpr size_type totalSize() const noexcept { return m_bound; }
|
||||
|
||||
GSL_SUPPRESS(
|
||||
c.128) // NO-FORMAT: attribute // no pointers to BoundsRanges should be ever used
|
||||
// clang-format off
|
||||
GSL_SUPPRESS(c.128) // NO-FORMAT: attribute // no pointers to BoundsRanges should be ever used
|
||||
// clang-format on
|
||||
constexpr size_type elementNum() const noexcept
|
||||
{
|
||||
return totalSize() / this->Base::totalSize();
|
||||
}
|
||||
|
||||
GSL_SUPPRESS(
|
||||
c.128) // NO-FORMAT: attribute // no pointers to BoundsRanges should be ever used
|
||||
// clang-format off
|
||||
GSL_SUPPRESS(c.128) // NO-FORMAT: attribute // no pointers to BoundsRanges should be ever used
|
||||
// clang-format on
|
||||
constexpr size_type elementNum(std::size_t dim) const noexcept
|
||||
{
|
||||
if (dim > 0)
|
||||
@@ -427,7 +448,7 @@ namespace details
|
||||
};
|
||||
|
||||
template <std::ptrdiff_t CurRange, std::ptrdiff_t... RestRanges>
|
||||
struct[[deprecated]] BoundsRanges<CurRange, RestRanges...> : BoundsRanges<RestRanges...>
|
||||
struct [[deprecated]] BoundsRanges<CurRange, RestRanges...> : BoundsRanges<RestRanges...>
|
||||
{
|
||||
using Base = BoundsRanges<RestRanges...>;
|
||||
using size_type = std::ptrdiff_t;
|
||||
@@ -445,12 +466,14 @@ namespace details
|
||||
bool firstLevel = true)
|
||||
: Base(static_cast<const BoundsRanges<RestOtherRanges...>&>(other), false)
|
||||
{
|
||||
// clang-format off
|
||||
GSL_SUPPRESS(type.4) // NO-FORMAT: attribute // TODO: false positive
|
||||
// clang-format on
|
||||
(void) firstLevel;
|
||||
}
|
||||
|
||||
template <typename T, std::size_t Dim = 0>
|
||||
constexpr void serialize(T & arr) const
|
||||
constexpr void serialize(T& arr) const
|
||||
{
|
||||
arr[Dim] = elementNum();
|
||||
this->Base::template serialize<T, Dim + 1>(arr);
|
||||
@@ -459,10 +482,14 @@ namespace details
|
||||
template <typename T, std::size_t Dim = 0>
|
||||
constexpr size_type linearize(const T& arr) const
|
||||
{
|
||||
// clang-format off
|
||||
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
|
||||
// clang-format on
|
||||
Expects(arr[Dim] >= 0 && arr[Dim] < CurrentRange); // Index is out of range
|
||||
// clang-format off
|
||||
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
|
||||
const ptrdiff_t d = arr[Dim];
|
||||
// clang-format on
|
||||
const std::ptrdiff_t d = arr[Dim];
|
||||
return this->Base::totalSize() * d + this->Base::template linearize<T, Dim + 1>(arr);
|
||||
}
|
||||
|
||||
@@ -475,19 +502,22 @@ namespace details
|
||||
return this->Base::totalSize() * arr[Dim] + last;
|
||||
}
|
||||
|
||||
GSL_SUPPRESS(
|
||||
c.128) // NO-FORMAT: attribute // no pointers to BoundsRanges should be ever used
|
||||
// clang-format off
|
||||
GSL_SUPPRESS(c.128) // NO-FORMAT: attribute // no pointers to BoundsRanges should be ever used
|
||||
// clang-format on
|
||||
constexpr size_type totalSize() const noexcept
|
||||
{
|
||||
return CurrentRange * this->Base::totalSize();
|
||||
}
|
||||
|
||||
GSL_SUPPRESS(
|
||||
c.128) // NO-FORMAT: attribute // no pointers to BoundsRanges should be ever used
|
||||
// clang-format off
|
||||
GSL_SUPPRESS(c.128) // NO-FORMAT: attribute // no pointers to BoundsRanges should be ever used
|
||||
// clang-format on
|
||||
constexpr size_type elementNum() const noexcept { return CurrentRange; }
|
||||
|
||||
GSL_SUPPRESS(
|
||||
c.128) // NO-FORMAT: attribute // no pointers to BoundsRanges should be ever used
|
||||
// clang-format off
|
||||
GSL_SUPPRESS(c.128) // NO-FORMAT: attribute // no pointers to BoundsRanges should be ever used
|
||||
// clang-format on
|
||||
constexpr size_type elementNum(std::size_t dim) const noexcept
|
||||
{
|
||||
if (dim > 0)
|
||||
@@ -503,14 +533,17 @@ namespace details
|
||||
};
|
||||
|
||||
template <typename SourceType, typename TargetType>
|
||||
struct[[deprecated]] BoundsRangeConvertible
|
||||
struct [[deprecated]] BoundsRangeConvertible
|
||||
: public std::integral_constant<bool, (SourceType::TotalSize >= TargetType::TotalSize ||
|
||||
TargetType::TotalSize == dynamic_range ||
|
||||
SourceType::TotalSize == dynamic_range ||
|
||||
TargetType::TotalSize == 0)>{};
|
||||
TargetType::TotalSize == 0)>
|
||||
{
|
||||
};
|
||||
|
||||
template <typename TypeChain>
|
||||
struct [[deprecated]] TypeListIndexer {
|
||||
struct [[deprecated]] TypeListIndexer
|
||||
{
|
||||
const TypeChain& obj_;
|
||||
constexpr TypeListIndexer(const TypeChain& obj) : obj_(obj) {}
|
||||
|
||||
@@ -523,13 +556,13 @@ namespace details
|
||||
template <std::size_t N, typename MyChain = TypeChain,
|
||||
typename MyBase = typename MyChain::Base>
|
||||
constexpr auto getObj(std::false_type)
|
||||
->decltype(TypeListIndexer<MyBase>(static_cast<const MyBase&>(obj_)).template get<N>())
|
||||
-> decltype(TypeListIndexer<MyBase>(static_cast<const MyBase&>(obj_)).template get<N>())
|
||||
{
|
||||
return TypeListIndexer<MyBase>(static_cast<const MyBase&>(obj_)).template get<N>();
|
||||
}
|
||||
|
||||
template <std::size_t N>
|
||||
constexpr auto get()->decltype(getObj<N - 1>(std::integral_constant<bool, N == 0>()))
|
||||
constexpr auto get() -> decltype(getObj<N - 1>(std::integral_constant<bool, N == 0>()))
|
||||
{
|
||||
return getObj<N - 1>(std::integral_constant<bool, N == 0>());
|
||||
}
|
||||
@@ -548,7 +581,9 @@ namespace details
|
||||
Ret ret{};
|
||||
for (std::size_t i = 0; i < Rank - 1; ++i)
|
||||
{
|
||||
// clang-format off
|
||||
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
|
||||
// clang-format on
|
||||
ret[i] = other[i + 1];
|
||||
}
|
||||
return ret;
|
||||
@@ -559,13 +594,14 @@ template <typename IndexType>
|
||||
class [[deprecated]] bounds_iterator;
|
||||
|
||||
template <std::ptrdiff_t... Ranges>
|
||||
class [[deprecated]] static_bounds {
|
||||
class [[deprecated]] static_bounds
|
||||
{
|
||||
public:
|
||||
static_bounds(const details::BoundsRanges<Ranges...>&) {}
|
||||
};
|
||||
|
||||
template <std::ptrdiff_t FirstRange, std::ptrdiff_t... RestRanges>
|
||||
class[[deprecated]] static_bounds<FirstRange, RestRanges...>
|
||||
class [[deprecated]] static_bounds<FirstRange, RestRanges...>
|
||||
{
|
||||
using MyRanges = details::BoundsRanges<FirstRange, RestRanges...>;
|
||||
|
||||
@@ -597,10 +633,10 @@ public:
|
||||
template <std::size_t Rank, typename SourceType, typename TargetType,
|
||||
typename Ret = BoundsRangeConvertible2<typename SourceType::Base,
|
||||
typename TargetType::Base, Rank>>
|
||||
static auto helpBoundsRangeConvertible(SourceType, TargetType, std::true_type)->Ret;
|
||||
static auto helpBoundsRangeConvertible(SourceType, TargetType, std::true_type) -> Ret;
|
||||
|
||||
template <std::size_t Rank, typename SourceType, typename TargetType>
|
||||
static auto helpBoundsRangeConvertible(SourceType, TargetType, ...)->std::false_type;
|
||||
static auto helpBoundsRangeConvertible(SourceType, TargetType, ...) -> std::false_type;
|
||||
|
||||
template <typename SourceType, typename TargetType, std::size_t Rank>
|
||||
struct BoundsRangeConvertible2
|
||||
@@ -726,7 +762,8 @@ public:
|
||||
};
|
||||
|
||||
template <std::size_t Rank>
|
||||
class [[deprecated]] strided_bounds {
|
||||
class [[deprecated]] strided_bounds
|
||||
{
|
||||
template <std::size_t OtherRank>
|
||||
friend class strided_bounds;
|
||||
|
||||
@@ -750,7 +787,7 @@ public:
|
||||
|
||||
constexpr strided_bounds& operator=(const strided_bounds&) noexcept = default;
|
||||
|
||||
constexpr strided_bounds(const value_type(&values)[rank], index_type strides)
|
||||
constexpr strided_bounds(const value_type (&values)[rank], index_type strides)
|
||||
: m_extents(values), m_strides(std::move(strides))
|
||||
{}
|
||||
|
||||
@@ -760,7 +797,9 @@ public:
|
||||
|
||||
constexpr index_type strides() const noexcept { return m_strides; }
|
||||
|
||||
// clang-format off
|
||||
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
|
||||
// clang-format on
|
||||
constexpr size_type total_size() const noexcept
|
||||
{
|
||||
size_type ret = 0;
|
||||
@@ -768,7 +807,9 @@ public:
|
||||
return ret + 1;
|
||||
}
|
||||
|
||||
// clang-format off
|
||||
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
|
||||
// clang-format on
|
||||
constexpr size_type size() const noexcept
|
||||
{
|
||||
size_type ret = 1;
|
||||
@@ -785,7 +826,9 @@ public:
|
||||
return true;
|
||||
}
|
||||
|
||||
// clang-format off
|
||||
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
|
||||
// clang-format on
|
||||
constexpr size_type linearize(const index_type& idx) const
|
||||
{
|
||||
size_type ret = 0;
|
||||
@@ -797,7 +840,9 @@ public:
|
||||
return ret;
|
||||
}
|
||||
|
||||
// clang-format off
|
||||
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
|
||||
// clang-format on
|
||||
constexpr size_type stride() const noexcept { return m_strides[0]; }
|
||||
|
||||
template <bool Enabled = (rank > 1), typename Ret = std::enable_if_t<Enabled, sliced_type>>
|
||||
@@ -808,7 +853,9 @@ public:
|
||||
|
||||
template <std::size_t Dim = 0>
|
||||
|
||||
// clang-format off
|
||||
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
|
||||
// clang-format on
|
||||
constexpr size_type extent() const noexcept
|
||||
{
|
||||
static_assert(Dim < Rank,
|
||||
@@ -828,14 +875,21 @@ private:
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct[[deprecated]] is_bounds : std::integral_constant<bool, false>{};
|
||||
struct [[deprecated]] is_bounds : std::integral_constant<bool, false>
|
||||
{
|
||||
};
|
||||
template <std::ptrdiff_t... Ranges>
|
||||
struct[[deprecated]] is_bounds<static_bounds<Ranges...>> : std::integral_constant<bool, true>{};
|
||||
struct [[deprecated]] is_bounds<static_bounds<Ranges...>> : std::integral_constant<bool, true>
|
||||
{
|
||||
};
|
||||
template <std::size_t Rank>
|
||||
struct[[deprecated]] is_bounds<strided_bounds<Rank>> : std::integral_constant<bool, true>{};
|
||||
struct [[deprecated]] is_bounds<strided_bounds<Rank>> : std::integral_constant<bool, true>
|
||||
{
|
||||
};
|
||||
|
||||
template <typename IndexType>
|
||||
class [[deprecated]] bounds_iterator {
|
||||
class [[deprecated]] bounds_iterator
|
||||
{
|
||||
public:
|
||||
static const std::size_t rank = IndexType::rank;
|
||||
using iterator_category = std::random_access_iterator_tag;
|
||||
@@ -856,8 +910,10 @@ public:
|
||||
|
||||
constexpr pointer operator->() const noexcept { return &curr_; }
|
||||
|
||||
// clang-format off
|
||||
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
|
||||
GSL_SUPPRESS(bounds.2) // NO-FORMAT: attribute
|
||||
// clang-format on
|
||||
constexpr bounds_iterator& operator++() noexcept
|
||||
|
||||
{
|
||||
@@ -882,7 +938,9 @@ public:
|
||||
return ret;
|
||||
}
|
||||
|
||||
// clang-format off
|
||||
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
|
||||
// clang-format on
|
||||
constexpr bounds_iterator& operator--()
|
||||
{
|
||||
if (!less(curr_, boundary_))
|
||||
@@ -919,7 +977,9 @@ public:
|
||||
return ret += n;
|
||||
}
|
||||
|
||||
// clang-format off
|
||||
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
|
||||
// clang-format on
|
||||
constexpr bounds_iterator& operator+=(difference_type n)
|
||||
{
|
||||
auto linear_idx = linearize(curr_) + n;
|
||||
@@ -969,15 +1029,17 @@ public:
|
||||
|
||||
constexpr bool operator>=(const bounds_iterator& rhs) const noexcept { return !(rhs > *this); }
|
||||
|
||||
void swap(bounds_iterator & rhs) noexcept
|
||||
void swap(bounds_iterator& rhs) noexcept
|
||||
{
|
||||
std::swap(boundary_, rhs.boundary_);
|
||||
std::swap(curr_, rhs.curr_);
|
||||
}
|
||||
|
||||
private:
|
||||
// clang-format off
|
||||
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
|
||||
constexpr bool less(index_type & one, index_type & other) const noexcept
|
||||
// clang-format on
|
||||
constexpr bool less(index_type& one, index_type& other) const noexcept
|
||||
{
|
||||
for (std::size_t i = 0; i < rank; ++i)
|
||||
{
|
||||
@@ -986,7 +1048,9 @@ private:
|
||||
return false;
|
||||
}
|
||||
|
||||
// clang-format off
|
||||
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
|
||||
// clang-format on
|
||||
constexpr index_size_type linearize(const value_type& idx) const noexcept
|
||||
{
|
||||
// TODO: Smarter impl.
|
||||
@@ -1048,8 +1112,10 @@ namespace details
|
||||
stride[Bounds::rank - 1] = 1;
|
||||
for (std::size_t i = 1; i < Bounds::rank; ++i)
|
||||
{
|
||||
// clang-format off
|
||||
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
|
||||
GSL_SUPPRESS(bounds.2) // NO-FORMAT: attribute
|
||||
// clang-format on
|
||||
stride[Bounds::rank - i - 1] = stride[Bounds::rank - i] * extents[Bounds::rank - i];
|
||||
}
|
||||
return {stride};
|
||||
@@ -1103,10 +1169,12 @@ constexpr dim_t<N> dim(std::ptrdiff_t n) noexcept
|
||||
|
||||
template <typename ValueType, std::ptrdiff_t FirstDimension = dynamic_range,
|
||||
std::ptrdiff_t... RestDimensions>
|
||||
class [[deprecated("gsl::multi_span is deprecated because it is not in the C++ Core Guidelines")]] multi_span;
|
||||
class [[deprecated(
|
||||
"gsl::multi_span is deprecated because it is not in the C++ Core Guidelines")]] multi_span;
|
||||
|
||||
template <typename ValueType, std::size_t Rank>
|
||||
class [[deprecated("gsl::strided_span is deprecated because it is not in the C++ Core Guidelines")]] strided_span;
|
||||
class [[deprecated(
|
||||
"gsl::strided_span is deprecated because it is not in the C++ Core Guidelines")]] strided_span;
|
||||
|
||||
namespace details
|
||||
{
|
||||
@@ -1193,8 +1261,8 @@ namespace details
|
||||
};
|
||||
|
||||
template <typename ValueType, std::ptrdiff_t FirstDimension, std::ptrdiff_t... RestDimensions>
|
||||
struct [[deprecated]] is_multi_span_oracle<multi_span<ValueType, FirstDimension, RestDimensions...>>
|
||||
: std::true_type
|
||||
struct [[deprecated]] is_multi_span_oracle<
|
||||
multi_span<ValueType, FirstDimension, RestDimensions...>> : std::true_type
|
||||
{
|
||||
};
|
||||
|
||||
@@ -1210,7 +1278,9 @@ namespace details
|
||||
} // namespace details
|
||||
|
||||
template <typename ValueType, std::ptrdiff_t FirstDimension, std::ptrdiff_t... RestDimensions>
|
||||
class [[deprecated("gsl::multi_span is deprecated because it is not in the C++ Core Guidelines")]] multi_span {
|
||||
class [[deprecated(
|
||||
"gsl::multi_span is deprecated because it is not in the C++ Core Guidelines")]] multi_span
|
||||
{
|
||||
// TODO do we still need this?
|
||||
template <typename ValueType2, std::ptrdiff_t FirstDimension2,
|
||||
std::ptrdiff_t... RestDimensions2>
|
||||
@@ -1242,7 +1312,9 @@ private:
|
||||
|
||||
public:
|
||||
// default constructor - same as constructing from nullptr_t
|
||||
// clang-format off
|
||||
GSL_SUPPRESS(type.6) // NO-FORMAT: attribute // TODO: false positive
|
||||
// clang-format on
|
||||
constexpr multi_span() noexcept : multi_span(nullptr, bounds_type{})
|
||||
{
|
||||
static_assert(bounds_type::dynamic_rank != 0 ||
|
||||
@@ -1252,7 +1324,9 @@ public:
|
||||
}
|
||||
|
||||
// construct from nullptr - get an empty multi_span
|
||||
// clang-format off
|
||||
GSL_SUPPRESS(type.6) // NO-FORMAT: attribute // TODO: false positive
|
||||
// clang-format on
|
||||
constexpr multi_span(std::nullptr_t) noexcept : multi_span(nullptr, bounds_type{})
|
||||
{
|
||||
static_assert(bounds_type::dynamic_rank != 0 ||
|
||||
@@ -1276,7 +1350,9 @@ public:
|
||||
|
||||
// construct from a single element
|
||||
|
||||
// clang-format off
|
||||
GSL_SUPPRESS(type.6) // NO-FORMAT: attribute // TODO: false positive
|
||||
// clang-format on
|
||||
constexpr multi_span(reference data) noexcept : multi_span(&data, bounds_type{1})
|
||||
{
|
||||
static_assert(bounds_type::dynamic_rank > 0 || bounds_type::static_size == 0 ||
|
||||
@@ -1289,7 +1365,9 @@ public:
|
||||
constexpr multi_span(value_type &&) = delete;
|
||||
|
||||
// construct from pointer + length
|
||||
// clang-format off
|
||||
GSL_SUPPRESS(type.6) // NO-FORMAT: attribute // TODO: false positive
|
||||
// clang-format on
|
||||
constexpr multi_span(pointer ptr, size_type size) : multi_span(ptr, bounds_type{size}) {}
|
||||
|
||||
// construct from pointer + length - multidimensional
|
||||
@@ -1526,7 +1604,9 @@ public:
|
||||
return this->operator[](idx);
|
||||
}
|
||||
|
||||
// clang-format off
|
||||
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
|
||||
// clang-format on
|
||||
constexpr reference operator[](const index_type& idx) const
|
||||
{
|
||||
return data_[bounds_.linearize(idx)];
|
||||
@@ -1534,7 +1614,9 @@ public:
|
||||
|
||||
template <bool Enabled = (Rank > 1), typename Ret = std::enable_if_t<Enabled, sliced_type>>
|
||||
|
||||
// clang-format off
|
||||
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
|
||||
// clang-format on
|
||||
constexpr Ret operator[](size_type idx) const
|
||||
{
|
||||
Expects(idx >= 0 && idx < bounds_.size()); // index is out of bounds of the array
|
||||
@@ -1549,7 +1631,9 @@ public:
|
||||
|
||||
constexpr iterator end() const noexcept { return iterator{this, false}; }
|
||||
|
||||
// clang-format off
|
||||
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
|
||||
// clang-format on
|
||||
constexpr const_iterator cbegin() const noexcept
|
||||
{
|
||||
return const_iterator{reinterpret_cast<const const_span*>(this), true};
|
||||
@@ -1647,8 +1731,7 @@ constexpr auto as_multi_span(SpanType s, Dimensions2... dims)
|
||||
|
||||
// convert a multi_span<T> to a multi_span<const byte>
|
||||
template <typename U, std::ptrdiff_t... Dimensions>
|
||||
multi_span<const byte, dynamic_range>
|
||||
as_bytes(multi_span<U, Dimensions...> s) noexcept
|
||||
multi_span<const byte, dynamic_range> as_bytes(multi_span<U, Dimensions...> s) noexcept
|
||||
{
|
||||
static_assert(std::is_trivial<std::decay_t<U>>::value,
|
||||
"The value_type of multi_span must be a trivial type.");
|
||||
@@ -1734,8 +1817,7 @@ constexpr auto as_multi_span(T* arr, std::ptrdiff_t len) ->
|
||||
}
|
||||
|
||||
template <typename T, std::size_t N>
|
||||
constexpr auto as_multi_span(T (&arr)[N]) ->
|
||||
typename details::SpanArrayTraits<T, N>::type
|
||||
constexpr auto as_multi_span(T (&arr)[N]) -> typename details::SpanArrayTraits<T, N>::type
|
||||
{
|
||||
return {arr};
|
||||
}
|
||||
@@ -1777,7 +1859,9 @@ constexpr auto as_multi_span(Cont&& arr) -> std::enable_if_t<
|
||||
|
||||
// from basic_string which doesn't have nonconst .data() member like other contiguous containers
|
||||
template <typename CharT, typename Traits, typename Allocator>
|
||||
// clang-format off
|
||||
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
|
||||
// clang-format on
|
||||
constexpr auto as_multi_span(std::basic_string<CharT, Traits, Allocator>& str)
|
||||
-> multi_span<CharT, dynamic_range>
|
||||
{
|
||||
@@ -1788,7 +1872,8 @@ constexpr auto as_multi_span(std::basic_string<CharT, Traits, Allocator>& str)
|
||||
// strided_span is an extension that is not strictly part of the GSL at this time.
|
||||
// It is kept here while the multidimensional interface is still being defined.
|
||||
template <typename ValueType, std::size_t Rank>
|
||||
class [[deprecated("gsl::strided_span is deprecated because it is not in the C++ Core Guidelines")]] strided_span
|
||||
class [[deprecated(
|
||||
"gsl::strided_span is deprecated because it is not in the C++ Core Guidelines")]] strided_span
|
||||
{
|
||||
public:
|
||||
using bounds_type = strided_bounds<Rank>;
|
||||
@@ -1823,13 +1908,15 @@ public:
|
||||
Expects((bounds_.size() > 0 && ptr != nullptr) || bounds_.size() == 0);
|
||||
// Bounds cross data boundaries
|
||||
Expects(this->bounds().total_size() <= size);
|
||||
// clang-format off
|
||||
GSL_SUPPRESS(type.4) // NO-FORMAT: attribute // TODO: false positive
|
||||
// clang-format on
|
||||
(void) size;
|
||||
}
|
||||
|
||||
// from static array of size N
|
||||
template <size_type N>
|
||||
constexpr strided_span(value_type (&values)[N], bounds_type bounds)
|
||||
constexpr strided_span(value_type(&values)[N], bounds_type bounds)
|
||||
: strided_span(values, N, std::move(bounds))
|
||||
{}
|
||||
|
||||
@@ -1844,7 +1931,7 @@ public:
|
||||
|
||||
// convertible
|
||||
template <typename OtherValueType, typename = std::enable_if_t<std::is_convertible<
|
||||
OtherValueType (*)[], value_type (*)[]>::value>>
|
||||
OtherValueType(*)[], value_type(*)[]>::value>>
|
||||
constexpr strided_span(const strided_span<OtherValueType, Rank>& other)
|
||||
: data_(other.data_), bounds_(other.bounds_)
|
||||
{}
|
||||
@@ -1863,7 +1950,9 @@ public:
|
||||
|
||||
const size_type size = this->bounds().total_size() / d;
|
||||
|
||||
// clang-format off
|
||||
GSL_SUPPRESS(type.3) // NO-FORMAT: attribute
|
||||
// clang-format on
|
||||
return {const_cast<OtherValueType*>(reinterpret_cast<const OtherValueType*>(this->data())),
|
||||
size,
|
||||
bounds_type{resize_extent(this->bounds().index_bounds(), d),
|
||||
@@ -1877,14 +1966,18 @@ public:
|
||||
bounds_type{extents, details::make_stride(bounds())}};
|
||||
}
|
||||
|
||||
// clang-format off
|
||||
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
|
||||
// clang-format on
|
||||
constexpr reference operator[](const index_type& idx) const
|
||||
{
|
||||
return data_[bounds_.linearize(idx)];
|
||||
}
|
||||
|
||||
template <bool Enabled = (Rank > 1), typename Ret = std::enable_if_t<Enabled, sliced_type>>
|
||||
// clang-format off
|
||||
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
|
||||
// clang-format on
|
||||
constexpr Ret operator[](size_type idx) const
|
||||
{
|
||||
Expects(idx < bounds_.size()); // index is out of bounds of the array
|
||||
@@ -1988,7 +2081,9 @@ private:
|
||||
static index_type resize_extent(const index_type& extent, std::ptrdiff_t d)
|
||||
{
|
||||
// The last dimension of the array needs to contain a multiple of new type elements
|
||||
// clang-format off
|
||||
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
|
||||
// clang-format on
|
||||
Expects(extent[Rank - 1] >= d && (extent[Rank - 1] % d == 0));
|
||||
|
||||
index_type ret = extent;
|
||||
@@ -2001,14 +2096,18 @@ private:
|
||||
static index_type resize_stride(const index_type& strides, std::ptrdiff_t, void* = nullptr)
|
||||
{
|
||||
// Only strided arrays with regular strides can be resized
|
||||
// clang-format off
|
||||
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
|
||||
// clang-format on
|
||||
Expects(strides[Rank - 1] == 1);
|
||||
|
||||
return strides;
|
||||
}
|
||||
|
||||
template <bool Enabled = (Rank > 1), typename = std::enable_if_t<Enabled>>
|
||||
// clang-format off
|
||||
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
|
||||
// clang-format on
|
||||
static index_type resize_stride(const index_type& strides, std::ptrdiff_t d)
|
||||
{
|
||||
// Only strided arrays with regular strides can be resized
|
||||
@@ -2031,7 +2130,8 @@ private:
|
||||
};
|
||||
|
||||
template <class Span>
|
||||
class [[deprecated]] contiguous_span_iterator {
|
||||
class [[deprecated]] contiguous_span_iterator
|
||||
{
|
||||
public:
|
||||
using iterator_category = std::random_access_iterator_tag;
|
||||
using value_type = typename Span::value_type;
|
||||
@@ -2046,14 +2146,18 @@ private:
|
||||
pointer data_;
|
||||
const Span* m_validator;
|
||||
|
||||
// clang-format off
|
||||
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
|
||||
// clang-format on
|
||||
void validateThis() const
|
||||
{
|
||||
// iterator is out of range of the array
|
||||
Expects(data_ >= m_validator->data_ && data_ < m_validator->data_ + m_validator->size());
|
||||
}
|
||||
|
||||
// clang-format off
|
||||
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
|
||||
// clang-format on
|
||||
contiguous_span_iterator(const Span* container, bool isbegin)
|
||||
: data_(isbegin ? container->data_ : container->data_ + container->size())
|
||||
, m_validator(container)
|
||||
@@ -2071,7 +2175,9 @@ public:
|
||||
return data_;
|
||||
}
|
||||
|
||||
// clang-format off
|
||||
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
|
||||
// clang-format on
|
||||
contiguous_span_iterator& operator++() noexcept
|
||||
{
|
||||
++data_;
|
||||
@@ -2084,7 +2190,9 @@ public:
|
||||
return ret;
|
||||
}
|
||||
|
||||
// clang-format off
|
||||
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
|
||||
// clang-format on
|
||||
contiguous_span_iterator& operator--() noexcept
|
||||
{
|
||||
--data_;
|
||||
@@ -2137,7 +2245,7 @@ public:
|
||||
bool operator>(const contiguous_span_iterator& rhs) const { return rhs < *this; }
|
||||
bool operator>=(const contiguous_span_iterator& rhs) const { return !(rhs > *this); }
|
||||
|
||||
void swap(contiguous_span_iterator & rhs) noexcept
|
||||
void swap(contiguous_span_iterator& rhs) noexcept
|
||||
{
|
||||
std::swap(data_, rhs.data_);
|
||||
std::swap(m_validator, rhs.m_validator);
|
||||
@@ -2152,7 +2260,8 @@ contiguous_span_iterator<Span> operator+(typename contiguous_span_iterator<Span>
|
||||
}
|
||||
|
||||
template <typename Span>
|
||||
class [[deprecated]] general_span_iterator {
|
||||
class [[deprecated]] general_span_iterator
|
||||
{
|
||||
public:
|
||||
using iterator_category = std::random_access_iterator_tag;
|
||||
using value_type = typename Span::value_type;
|
||||
@@ -2218,7 +2327,9 @@ public:
|
||||
return m_itr - rhs.m_itr;
|
||||
}
|
||||
|
||||
// clang-format off
|
||||
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
|
||||
// clang-format on
|
||||
value_type operator[](difference_type n) const { return (*m_container)[m_itr[n]]; }
|
||||
|
||||
bool operator==(const general_span_iterator& rhs) const
|
||||
@@ -2235,7 +2346,7 @@ public:
|
||||
bool operator<=(const general_span_iterator& rhs) const { return !(rhs < *this); }
|
||||
bool operator>(const general_span_iterator& rhs) const { return rhs < *this; }
|
||||
bool operator>=(const general_span_iterator& rhs) const { return !(rhs > *this); }
|
||||
void swap(general_span_iterator & rhs) noexcept
|
||||
void swap(general_span_iterator& rhs) noexcept
|
||||
{
|
||||
std::swap(m_itr, rhs.m_itr);
|
||||
std::swap(m_container, rhs.m_container);
|
||||
@@ -2252,11 +2363,6 @@ general_span_iterator<Span> operator+(typename general_span_iterator<Span>::diff
|
||||
} // namespace gsl
|
||||
|
||||
#if defined(_MSC_VER) && !defined(__clang__)
|
||||
#if _MSC_VER < 1910
|
||||
|
||||
#undef constexpr
|
||||
#pragma pop_macro("constexpr")
|
||||
#endif // _MSC_VER < 1910
|
||||
|
||||
#pragma warning(pop)
|
||||
|
||||
@@ -2266,7 +2372,7 @@ general_span_iterator<Span> operator+(typename general_span_iterator<Span>::diff
|
||||
#pragma GCC diagnostic pop
|
||||
#endif // __GNUC__ > 6
|
||||
|
||||
#if __clang__ || __GNUC__
|
||||
#if defined(__GNUC__) || defined(__clang__)
|
||||
#pragma GCC diagnostic pop
|
||||
#endif
|
||||
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
|
||||
//
|
||||
// This code is licensed under the MIT License (MIT).
|
||||
//
|
||||
// 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 GSL_NARROW_H
|
||||
#define GSL_NARROW_H
|
||||
#include <gsl/assert> // for contracts
|
||||
#include <gsl/util> // for narrow_cast
|
||||
namespace gsl
|
||||
{
|
||||
struct narrowing_error : public std::exception
|
||||
{
|
||||
const char* what() const noexcept override { return "narrowing_error"; }
|
||||
};
|
||||
|
||||
// narrow() : a checked version of narrow_cast() that throws if the cast changed the value
|
||||
template <class T, class U>
|
||||
// clang-format off
|
||||
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
|
||||
GSL_SUPPRESS(f.6) // NO-FORMAT: attribute // TODO: MSVC /analyze does not recognise noexcept(false)
|
||||
// clang-format on
|
||||
constexpr T narrow(U u) noexcept(false)
|
||||
{
|
||||
constexpr const bool is_different_signedness =
|
||||
(std::is_signed<T>::value != std::is_signed<U>::value);
|
||||
|
||||
const T t = narrow_cast<T>(u);
|
||||
|
||||
if (static_cast<U>(t) != u || (is_different_signedness && ((t < T{}) != (u < U{}))))
|
||||
{
|
||||
throw narrowing_error{};
|
||||
}
|
||||
|
||||
return t;
|
||||
}
|
||||
} // namespace gsl
|
||||
#endif // GSL_NARROW_H
|
||||
+56
-50
@@ -17,19 +17,17 @@
|
||||
#ifndef GSL_POINTERS_H
|
||||
#define GSL_POINTERS_H
|
||||
|
||||
#include <gsl/gsl_assert> // for Ensures, Expects
|
||||
#include <gsl/assert> // for contracts
|
||||
|
||||
#include <algorithm> // for forward
|
||||
#include <iosfwd> // for ptrdiff_t, nullptr_t, ostream, size_t
|
||||
#include <cstddef> // for ptrdiff_t, nullptr_t, size_t
|
||||
#include <memory> // for shared_ptr, unique_ptr
|
||||
#include <system_error> // for hash
|
||||
#include <type_traits> // for enable_if_t, is_convertible, is_assignable
|
||||
|
||||
#if defined(_MSC_VER) && _MSC_VER < 1910 && !defined(__clang__)
|
||||
#pragma push_macro("constexpr")
|
||||
#define constexpr /*constexpr*/
|
||||
|
||||
#endif // defined(_MSC_VER) && _MSC_VER < 1910
|
||||
#if !defined(GSL_NO_IOSTREAMS)
|
||||
#include <iosfwd> // for ostream
|
||||
#endif // !defined(GSL_NO_IOSTREAMS)
|
||||
|
||||
namespace gsl
|
||||
{
|
||||
@@ -37,13 +35,14 @@ namespace gsl
|
||||
//
|
||||
// GSL.owner: ownership pointers
|
||||
//
|
||||
using std::unique_ptr;
|
||||
using std::shared_ptr;
|
||||
using std::unique_ptr;
|
||||
|
||||
//
|
||||
// owner
|
||||
//
|
||||
// owner<T> is designed as a bridge for code that must deal directly with owning pointers for some reason
|
||||
// owner<T> is designed as a bridge for code that must deal directly with owning pointers for some
|
||||
// reason
|
||||
//
|
||||
// T must be a pointer type
|
||||
// - disallow construction from any type other than pointer type
|
||||
@@ -69,37 +68,35 @@ template <class T>
|
||||
class not_null
|
||||
{
|
||||
public:
|
||||
static_assert(std::is_assignable<T&, std::nullptr_t>::value, "T cannot be assigned nullptr.");
|
||||
static_assert(std::is_convertible<decltype(std::declval<T>() != nullptr), bool>::value,
|
||||
"T cannot be compared to nullptr.");
|
||||
|
||||
template <typename U, typename = std::enable_if_t<std::is_convertible<U, T>::value>>
|
||||
constexpr not_null(U&& u) : ptr_(std::forward<U>(u))
|
||||
{
|
||||
Expects(ptr_ != nullptr);
|
||||
Expects(ptr_ != nullptr, Null);
|
||||
}
|
||||
|
||||
template <typename = std::enable_if_t<!std::is_same<std::nullptr_t, T>::value>>
|
||||
constexpr not_null(T u) : ptr_(u)
|
||||
constexpr not_null(T u) : ptr_(std::move(u))
|
||||
{
|
||||
Expects(ptr_ != nullptr);
|
||||
Expects(ptr_ != nullptr, Null);
|
||||
}
|
||||
|
||||
template <typename U, typename = std::enable_if_t<std::is_convertible<U, T>::value>>
|
||||
constexpr not_null(const not_null<U>& other) : not_null(other.get())
|
||||
{
|
||||
}
|
||||
{}
|
||||
|
||||
not_null(not_null&& other) = default;
|
||||
not_null(const not_null& other) = default;
|
||||
not_null& operator=(const not_null& other) = default;
|
||||
|
||||
constexpr T get() const
|
||||
constexpr std::conditional_t<std::is_copy_constructible<T>::value, T, const T&> get() const
|
||||
{
|
||||
Ensures(ptr_ != nullptr);
|
||||
Ensures(ptr_ != nullptr, Null);
|
||||
return ptr_;
|
||||
}
|
||||
|
||||
constexpr operator T() const { return get(); }
|
||||
constexpr T operator->() const { return get(); }
|
||||
constexpr decltype(auto) operator->() const { return get(); }
|
||||
constexpr decltype(auto) operator*() const { return *get(); }
|
||||
|
||||
// prevents compilation when someone attempts to assign a null pointer constant
|
||||
@@ -120,49 +117,64 @@ private:
|
||||
};
|
||||
|
||||
template <class T>
|
||||
auto make_not_null(T&& t) {
|
||||
auto make_not_null(T&& t) noexcept
|
||||
{
|
||||
return not_null<std::remove_cv_t<std::remove_reference_t<T>>>{std::forward<T>(t)};
|
||||
}
|
||||
|
||||
#if !defined(GSL_NO_IOSTREAMS)
|
||||
template <class T>
|
||||
std::ostream& operator<<(std::ostream& os, const not_null<T>& val)
|
||||
{
|
||||
os << val.get();
|
||||
return os;
|
||||
}
|
||||
#endif // !defined(GSL_NO_IOSTREAMS)
|
||||
|
||||
template <class T, class U>
|
||||
auto operator==(const not_null<T>& lhs, const not_null<U>& rhs) -> decltype(lhs.get() == rhs.get())
|
||||
auto operator==(const not_null<T>& lhs,
|
||||
const not_null<U>& rhs) noexcept(noexcept(lhs.get() == rhs.get()))
|
||||
-> decltype(lhs.get() == rhs.get())
|
||||
{
|
||||
return lhs.get() == rhs.get();
|
||||
}
|
||||
|
||||
template <class T, class U>
|
||||
auto operator!=(const not_null<T>& lhs, const not_null<U>& rhs) -> decltype(lhs.get() != rhs.get())
|
||||
auto operator!=(const not_null<T>& lhs,
|
||||
const not_null<U>& rhs) noexcept(noexcept(lhs.get() != rhs.get()))
|
||||
-> decltype(lhs.get() != rhs.get())
|
||||
{
|
||||
return lhs.get() != rhs.get();
|
||||
}
|
||||
|
||||
template <class T, class U>
|
||||
auto operator<(const not_null<T>& lhs, const not_null<U>& rhs) -> decltype(lhs.get() < rhs.get())
|
||||
auto operator<(const not_null<T>& lhs,
|
||||
const not_null<U>& rhs) noexcept(noexcept(lhs.get() < rhs.get()))
|
||||
-> decltype(lhs.get() < rhs.get())
|
||||
{
|
||||
return lhs.get() < rhs.get();
|
||||
}
|
||||
|
||||
template <class T, class U>
|
||||
auto operator<=(const not_null<T>& lhs, const not_null<U>& rhs) -> decltype(lhs.get() <= rhs.get())
|
||||
auto operator<=(const not_null<T>& lhs,
|
||||
const not_null<U>& rhs) noexcept(noexcept(lhs.get() <= rhs.get()))
|
||||
-> decltype(lhs.get() <= rhs.get())
|
||||
{
|
||||
return lhs.get() <= rhs.get();
|
||||
}
|
||||
|
||||
template <class T, class U>
|
||||
auto operator>(const not_null<T>& lhs, const not_null<U>& rhs) -> decltype(lhs.get() > rhs.get())
|
||||
auto operator>(const not_null<T>& lhs,
|
||||
const not_null<U>& rhs) noexcept(noexcept(lhs.get() > rhs.get()))
|
||||
-> decltype(lhs.get() > rhs.get())
|
||||
{
|
||||
return lhs.get() > rhs.get();
|
||||
}
|
||||
|
||||
template <class T, class U>
|
||||
auto operator>=(const not_null<T>& lhs, const not_null<U>& rhs) -> decltype(lhs.get() >= rhs.get())
|
||||
auto operator>=(const not_null<T>& lhs,
|
||||
const not_null<U>& rhs) noexcept(noexcept(lhs.get() >= rhs.get()))
|
||||
-> decltype(lhs.get() >= rhs.get())
|
||||
{
|
||||
return lhs.get() >= rhs.get();
|
||||
}
|
||||
@@ -184,7 +196,7 @@ namespace std
|
||||
template <class T>
|
||||
struct hash<gsl::not_null<T>>
|
||||
{
|
||||
std::size_t operator()(const gsl::not_null<T>& value) const { return hash<T>{}(value); }
|
||||
std::size_t operator()(const gsl::not_null<T>& value) const { return hash<T>{}(value.get()); }
|
||||
};
|
||||
|
||||
} // namespace std
|
||||
@@ -210,28 +222,23 @@ namespace gsl
|
||||
// - remove unnecessary asserts
|
||||
//
|
||||
template <class T>
|
||||
class strict_not_null: public not_null<T>
|
||||
class strict_not_null : public not_null<T>
|
||||
{
|
||||
public:
|
||||
|
||||
template <typename U, typename = std::enable_if_t<std::is_convertible<U, T>::value>>
|
||||
constexpr explicit strict_not_null(U&& u) :
|
||||
not_null<T>(std::forward<U>(u))
|
||||
constexpr explicit strict_not_null(U&& u) : not_null<T>(std::forward<U>(u))
|
||||
{}
|
||||
|
||||
template <typename = std::enable_if_t<!std::is_same<std::nullptr_t, T>::value>>
|
||||
constexpr explicit strict_not_null(T u) :
|
||||
not_null<T>(u)
|
||||
constexpr explicit strict_not_null(T u) : not_null<T>(u)
|
||||
{}
|
||||
|
||||
template <typename U, typename = std::enable_if_t<std::is_convertible<U, T>::value>>
|
||||
constexpr strict_not_null(const not_null<U>& other) :
|
||||
not_null<T>(other)
|
||||
constexpr strict_not_null(const not_null<U>& other) : not_null<T>(other)
|
||||
{}
|
||||
|
||||
template <typename U, typename = std::enable_if_t<std::is_convertible<U, T>::value>>
|
||||
constexpr strict_not_null(const strict_not_null<U>& other) :
|
||||
not_null<T>(other)
|
||||
constexpr strict_not_null(const strict_not_null<U>& other) : not_null<T>(other)
|
||||
{}
|
||||
|
||||
strict_not_null(strict_not_null&& other) = default;
|
||||
@@ -268,15 +275,18 @@ template <class T>
|
||||
strict_not_null<T> operator+(std::ptrdiff_t, const strict_not_null<T>&) = delete;
|
||||
|
||||
template <class T>
|
||||
auto make_strict_not_null(T&& t) {
|
||||
auto make_strict_not_null(T&& t) noexcept
|
||||
{
|
||||
return strict_not_null<std::remove_cv_t<std::remove_reference_t<T>>>{std::forward<T>(t)};
|
||||
}
|
||||
|
||||
#if ( defined(__cpp_deduction_guides) && (__cpp_deduction_guides >= 201611L) )
|
||||
#if (defined(__cpp_deduction_guides) && (__cpp_deduction_guides >= 201611L))
|
||||
|
||||
// deduction guides to prevent the ctad-maybe-unsupported warning
|
||||
template <class T> not_null(T) -> not_null<T>;
|
||||
template <class T> strict_not_null(T) -> strict_not_null<T>;
|
||||
template <class T>
|
||||
not_null(T) -> not_null<T>;
|
||||
template <class T>
|
||||
strict_not_null(T) -> strict_not_null<T>;
|
||||
|
||||
#endif // ( defined(__cpp_deduction_guides) && (__cpp_deduction_guides >= 201611L) )
|
||||
|
||||
@@ -287,16 +297,12 @@ namespace std
|
||||
template <class T>
|
||||
struct hash<gsl::strict_not_null<T>>
|
||||
{
|
||||
std::size_t operator()(const gsl::strict_not_null<T>& value) const { return hash<T>{}(value); }
|
||||
std::size_t operator()(const gsl::strict_not_null<T>& value) const
|
||||
{
|
||||
return hash<T>{}(value.get());
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace std
|
||||
|
||||
#if defined(_MSC_VER) && _MSC_VER < 1910 && !defined(__clang__)
|
||||
|
||||
#undef constexpr
|
||||
#pragma pop_macro("constexpr")
|
||||
|
||||
#endif // defined(_MSC_VER) && _MSC_VER < 1910 && !defined(__clang__)
|
||||
|
||||
#endif // GSL_POINTERS_H
|
||||
|
||||
+407
-374
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,197 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
|
||||
//
|
||||
// This code is licensed under the MIT License (MIT).
|
||||
//
|
||||
// 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 GSL_SPAN_EXT_H
|
||||
#define GSL_SPAN_EXT_H
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// File: span_ext
|
||||
// Purpose: continue offering features that have been cut from the official
|
||||
// implementation of span.
|
||||
// While modernizing gsl::span a number of features needed to be removed to
|
||||
// be compliant with the design of std::span
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include <gsl/span> // for span
|
||||
#include <gsl/util> // for narrow_cast, narrow
|
||||
|
||||
#include <algorithm> // for lexicographical_compare
|
||||
#include <cstddef> // for ptrdiff_t, size_t
|
||||
#include <utility>
|
||||
|
||||
namespace gsl
|
||||
{
|
||||
|
||||
// [span.comparison], span comparison operators
|
||||
template <class ElementType, std::size_t FirstExtent, std::size_t SecondExtent>
|
||||
constexpr bool operator==(span<ElementType, FirstExtent> l, span<ElementType, SecondExtent> r)
|
||||
{
|
||||
return std::equal(l.begin(), l.end(), r.begin(), r.end());
|
||||
}
|
||||
|
||||
template <class ElementType, std::size_t Extent>
|
||||
constexpr bool operator!=(span<ElementType, Extent> l, span<ElementType, Extent> r)
|
||||
{
|
||||
return !(l == r);
|
||||
}
|
||||
|
||||
template <class ElementType, std::size_t Extent>
|
||||
constexpr bool operator<(span<ElementType, Extent> l, span<ElementType, Extent> r)
|
||||
{
|
||||
return std::lexicographical_compare(l.begin(), l.end(), r.begin(), r.end());
|
||||
}
|
||||
|
||||
template <class ElementType, std::size_t Extent>
|
||||
constexpr bool operator<=(span<ElementType, Extent> l, span<ElementType, Extent> r)
|
||||
{
|
||||
return !(l > r);
|
||||
}
|
||||
|
||||
template <class ElementType, std::size_t Extent>
|
||||
constexpr bool operator>(span<ElementType, Extent> l, span<ElementType, Extent> r)
|
||||
{
|
||||
return r < l;
|
||||
}
|
||||
|
||||
template <class ElementType, std::size_t Extent>
|
||||
constexpr bool operator>=(span<ElementType, Extent> l, span<ElementType, Extent> r)
|
||||
{
|
||||
return !(l < r);
|
||||
}
|
||||
|
||||
//
|
||||
// make_span() - Utility functions for creating spans
|
||||
//
|
||||
template <class ElementType>
|
||||
constexpr span<ElementType> make_span(ElementType* ptr, typename span<ElementType>::size_type count)
|
||||
{
|
||||
return span<ElementType>(ptr, count);
|
||||
}
|
||||
|
||||
template <class ElementType>
|
||||
constexpr span<ElementType> make_span(ElementType* firstElem, ElementType* lastElem)
|
||||
{
|
||||
return span<ElementType>(firstElem, lastElem);
|
||||
}
|
||||
|
||||
template <class ElementType, std::size_t N>
|
||||
constexpr span<ElementType, N> make_span(ElementType (&arr)[N]) noexcept
|
||||
{
|
||||
return span<ElementType, N>(arr);
|
||||
}
|
||||
|
||||
template <class Container>
|
||||
constexpr span<typename Container::value_type> make_span(Container& cont)
|
||||
{
|
||||
return span<typename Container::value_type>(cont);
|
||||
}
|
||||
|
||||
template <class Container>
|
||||
constexpr span<const typename Container::value_type> make_span(const Container& cont)
|
||||
{
|
||||
return span<const typename Container::value_type>(cont);
|
||||
}
|
||||
|
||||
template <class Ptr>
|
||||
constexpr span<typename Ptr::element_type> make_span(Ptr& cont, std::size_t count)
|
||||
{
|
||||
return span<typename Ptr::element_type>(cont, count);
|
||||
}
|
||||
|
||||
template <class Ptr>
|
||||
constexpr span<typename Ptr::element_type> make_span(Ptr& cont)
|
||||
{
|
||||
return span<typename Ptr::element_type>(cont);
|
||||
}
|
||||
|
||||
// Specialization of gsl::at for span
|
||||
template <class ElementType, std::size_t Extent>
|
||||
constexpr ElementType& at(span<ElementType, Extent> s, index i)
|
||||
{
|
||||
// No bounds checking here because it is done in span::operator[] called below
|
||||
Ensures(i >= 0, Bounds);
|
||||
return s[narrow_cast<std::size_t>(i)];
|
||||
}
|
||||
|
||||
// [span.obs] Free observer functions
|
||||
template <class ElementType, std::size_t Extent>
|
||||
constexpr std::ptrdiff_t ssize(const span<ElementType, Extent>& s) noexcept
|
||||
{
|
||||
return static_cast<std::ptrdiff_t>(s.size());
|
||||
}
|
||||
|
||||
// [span.iter] Free functions for begin/end functions
|
||||
template <class ElementType, std::size_t Extent>
|
||||
constexpr typename span<ElementType, Extent>::iterator
|
||||
begin(const span<ElementType, Extent>& s) noexcept
|
||||
{
|
||||
return s.begin();
|
||||
}
|
||||
|
||||
template <class ElementType, std::size_t Extent = dynamic_extent>
|
||||
constexpr typename span<ElementType, Extent>::iterator
|
||||
end(const span<ElementType, Extent>& s) noexcept
|
||||
{
|
||||
return s.end();
|
||||
}
|
||||
|
||||
template <class ElementType, std::size_t Extent>
|
||||
constexpr typename span<ElementType, Extent>::reverse_iterator
|
||||
rbegin(const span<ElementType, Extent>& s) noexcept
|
||||
{
|
||||
return s.rbegin();
|
||||
}
|
||||
|
||||
template <class ElementType, std::size_t Extent>
|
||||
constexpr typename span<ElementType, Extent>::reverse_iterator
|
||||
rend(const span<ElementType, Extent>& s) noexcept
|
||||
{
|
||||
return s.rend();
|
||||
}
|
||||
|
||||
template <class ElementType, std::size_t Extent>
|
||||
constexpr typename span<ElementType, Extent>::iterator
|
||||
cbegin(const span<ElementType, Extent>& s) noexcept
|
||||
{
|
||||
return s.begin();
|
||||
}
|
||||
|
||||
template <class ElementType, std::size_t Extent = dynamic_extent>
|
||||
constexpr typename span<ElementType, Extent>::iterator
|
||||
cend(const span<ElementType, Extent>& s) noexcept
|
||||
{
|
||||
return s.end();
|
||||
}
|
||||
|
||||
template <class ElementType, std::size_t Extent>
|
||||
constexpr typename span<ElementType, Extent>::reverse_iterator
|
||||
crbegin(const span<ElementType, Extent>& s) noexcept
|
||||
{
|
||||
return s.rbegin();
|
||||
}
|
||||
|
||||
template <class ElementType, std::size_t Extent>
|
||||
constexpr typename span<ElementType, Extent>::reverse_iterator
|
||||
crend(const span<ElementType, Extent>& s) noexcept
|
||||
{
|
||||
return s.rend();
|
||||
}
|
||||
|
||||
} // namespace gsl
|
||||
|
||||
#endif // GSL_SPAN_EXT_H
|
||||
+767
-721
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,145 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
|
||||
//
|
||||
// This code is licensed under the MIT License (MIT).
|
||||
//
|
||||
// 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 GSL_UTIL_H
|
||||
#define GSL_UTIL_H
|
||||
|
||||
#include <gsl/assert> // for contracts
|
||||
|
||||
#include <array>
|
||||
#include <cstddef> // for ptrdiff_t, size_t
|
||||
#include <initializer_list> // for initializer_list
|
||||
#include <type_traits> // for is_signed, integral_constant
|
||||
#include <utility> // for exchange, forward
|
||||
|
||||
#if defined(_MSC_VER) && !defined(__clang__)
|
||||
|
||||
#pragma warning(push)
|
||||
#pragma warning(disable : 4127) // conditional expression is constant
|
||||
|
||||
#endif // _MSC_VER
|
||||
|
||||
#if defined(__cplusplus) && (__cplusplus >= 201703L)
|
||||
#define GSL_NODISCARD [[nodiscard]]
|
||||
#else
|
||||
#define GSL_NODISCARD
|
||||
#endif // defined(__cplusplus) && (__cplusplus >= 201703L)
|
||||
|
||||
namespace gsl
|
||||
{
|
||||
//
|
||||
// GSL.util: utilities
|
||||
//
|
||||
|
||||
// index type for all container indexes/subscripts/sizes
|
||||
using index = std::ptrdiff_t;
|
||||
|
||||
// final_action allows you to ensure something gets run at the end of a scope
|
||||
template <class F>
|
||||
class final_action
|
||||
{
|
||||
public:
|
||||
static_assert(!std::is_reference<F>::value && !std::is_const<F>::value &&
|
||||
!std::is_volatile<F>::value,
|
||||
"Final_action should store its callable by value");
|
||||
|
||||
explicit final_action(F f) noexcept : f_(std::move(f)) {}
|
||||
|
||||
final_action(final_action&& other) noexcept
|
||||
: f_(std::move(other.f_)), invoke_(std::exchange(other.invoke_, false))
|
||||
{}
|
||||
|
||||
final_action(const final_action&) = delete;
|
||||
final_action& operator=(const final_action&) = delete;
|
||||
final_action& operator=(final_action&&) = delete;
|
||||
|
||||
// clang-format off
|
||||
GSL_SUPPRESS(f.6) // NO-FORMAT: attribute // terminate if throws
|
||||
// clang-format on
|
||||
~final_action() noexcept
|
||||
{
|
||||
if (invoke_) f_();
|
||||
}
|
||||
|
||||
private:
|
||||
F f_;
|
||||
bool invoke_{true};
|
||||
};
|
||||
|
||||
// finally() - convenience function to generate a final_action
|
||||
template <class F>
|
||||
GSL_NODISCARD final_action<typename std::remove_cv<typename std::remove_reference<F>::type>::type>
|
||||
finally(F&& f) noexcept
|
||||
{
|
||||
return final_action<typename std::remove_cv<typename std::remove_reference<F>::type>::type>(
|
||||
std::forward<F>(f));
|
||||
}
|
||||
|
||||
// narrow_cast(): a searchable way to do narrowing casts of values
|
||||
template <class T, class U>
|
||||
// clang-format off
|
||||
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
|
||||
// clang-format on
|
||||
constexpr T narrow_cast(U&& u) noexcept
|
||||
{
|
||||
return static_cast<T>(std::forward<U>(u));
|
||||
}
|
||||
|
||||
//
|
||||
// at() - Bounds-checked way of accessing builtin arrays, std::array, std::vector
|
||||
//
|
||||
template <class T, std::size_t N>
|
||||
// clang-format off
|
||||
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
|
||||
GSL_SUPPRESS(bounds.2) // NO-FORMAT: attribute
|
||||
// clang-format on
|
||||
constexpr T& at(T (&arr)[N], const index i)
|
||||
{
|
||||
Expects(i >= 0 && i < narrow_cast<index>(N), Bounds);
|
||||
return arr[narrow_cast<std::size_t>(i)];
|
||||
}
|
||||
|
||||
template <class Cont>
|
||||
// clang-format off
|
||||
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
|
||||
GSL_SUPPRESS(bounds.2) // NO-FORMAT: attribute
|
||||
// clang-format on
|
||||
constexpr auto at(Cont& cont, const index i) -> decltype(cont[cont.size()])
|
||||
{
|
||||
Expects(i >= 0 && i < narrow_cast<index>(cont.size()), Bounds);
|
||||
using size_type = decltype(cont.size());
|
||||
return cont[narrow_cast<size_type>(i)];
|
||||
}
|
||||
|
||||
template <class T>
|
||||
// clang-format off
|
||||
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
|
||||
// clang-format on
|
||||
constexpr T at(const std::initializer_list<T> cont, const index i)
|
||||
{
|
||||
Expects(i >= 0 && i < narrow_cast<index>(cont.size()), Bounds);
|
||||
return *(cont.begin() + i);
|
||||
}
|
||||
|
||||
} // namespace gsl
|
||||
|
||||
#if defined(_MSC_VER) && !defined(__clang__)
|
||||
|
||||
#pragma warning(pop)
|
||||
|
||||
#endif // _MSC_VER
|
||||
|
||||
#endif // GSL_UTIL_H
|
||||
@@ -0,0 +1,26 @@
|
||||
parameters:
|
||||
jobName: ''
|
||||
imageName: ''
|
||||
|
||||
jobs:
|
||||
- job:
|
||||
displayName: ${{ parameters.imageName }}
|
||||
pool:
|
||||
vmImage: ${{ parameters.imageName }}
|
||||
strategy:
|
||||
matrix:
|
||||
14_debug:
|
||||
GSL_CXX_STANDARD: '14'
|
||||
BUILD_TYPE: 'Debug'
|
||||
14_release:
|
||||
GSL_CXX_STANDARD: '14'
|
||||
BUILD_TYPE: 'Release'
|
||||
17_debug:
|
||||
GSL_CXX_STANDARD: '17'
|
||||
BUILD_TYPE: 'Debug'
|
||||
17_release:
|
||||
GSL_CXX_STANDARD: '17'
|
||||
BUILD_TYPE: 'Release'
|
||||
continueOnError: false
|
||||
steps:
|
||||
- template: ./steps.yml
|
||||
@@ -0,0 +1,17 @@
|
||||
steps:
|
||||
- task: CMake@1
|
||||
name: Configure
|
||||
inputs:
|
||||
workingDirectory: build
|
||||
cmakeArgs: '-DCMAKE_CXX_STANDARD=$(GSL_CXX_STANDARD) -DCMAKE_BUILD_TYPE=$(BUILD_TYPE) .. '
|
||||
|
||||
- task: CMake@1
|
||||
name: Build
|
||||
inputs:
|
||||
workingDirectory: build
|
||||
cmakeArgs: '--build . '
|
||||
|
||||
- script: ctest . --output-on-failure --no-compress-output
|
||||
name: CTest
|
||||
workingDirectory: build
|
||||
failOnStderr: true
|
||||
+120
-49
@@ -1,29 +1,40 @@
|
||||
cmake_minimum_required(VERSION 3.0.2)
|
||||
|
||||
project(GSLTests CXX)
|
||||
include(FindPkgConfig)
|
||||
|
||||
# will make visual studio generated project group files
|
||||
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
|
||||
|
||||
list(APPEND CATCH_CMAKE_ARGS
|
||||
"-DCMAKE_INSTALL_PREFIX=${CMAKE_BINARY_DIR}/external"
|
||||
"-DNO_SELFTEST=true"
|
||||
)
|
||||
pkg_search_module(GTestMain gtest_main)
|
||||
if (NOT GTestMain_FOUND)
|
||||
configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt)
|
||||
execute_process(
|
||||
COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
|
||||
RESULT_VARIABLE result
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download
|
||||
)
|
||||
if(result)
|
||||
message(FATAL_ERROR "CMake step for googletest failed: ${result}")
|
||||
endif()
|
||||
|
||||
if(GIT_FOUND)
|
||||
# add catch
|
||||
ExternalProject_Add(
|
||||
catch
|
||||
PREFIX ${CMAKE_BINARY_DIR}/catch
|
||||
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
|
||||
GIT_TAG v2.0.1
|
||||
CMAKE_ARGS ${CATCH_CMAKE_ARGS}
|
||||
LOG_DOWNLOAD 1
|
||||
UPDATE_DISCONNECTED 1
|
||||
execute_process(
|
||||
COMMAND ${CMAKE_COMMAND} --build .
|
||||
RESULT_VARIABLE result
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download
|
||||
)
|
||||
if(result)
|
||||
message(FATAL_ERROR "CMake step for googletest failed: ${result}")
|
||||
endif()
|
||||
|
||||
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
|
||||
set(GTestMain_LIBRARIES gtest_main)
|
||||
|
||||
add_subdirectory(
|
||||
${CMAKE_CURRENT_BINARY_DIR}/googletest-src
|
||||
${CMAKE_CURRENT_BINARY_DIR}/googletest-build
|
||||
EXCLUDE_FROM_ALL
|
||||
)
|
||||
else()
|
||||
# assume catch is installed in a system directory
|
||||
add_custom_target(catch)
|
||||
endif()
|
||||
|
||||
if (MSVC AND (GSL_CXX_STANDARD EQUAL 17))
|
||||
@@ -39,14 +50,39 @@ if(MSVC) # MSVC or simulating MSVC
|
||||
/EHsc
|
||||
/W4
|
||||
/WX
|
||||
$<$<CXX_COMPILER_ID:MSVC>:
|
||||
/wd4996 # Use of function or classes marked [[deprecated]]
|
||||
/wd26409 # CppCoreCheck - GTest
|
||||
/wd26426 # CppCoreCheck - GTest
|
||||
/wd26440 # CppCoreCheck - GTest
|
||||
/wd26446 # CppCoreCheck - prefer gsl::at()
|
||||
/wd26472 # CppCoreCheck - use gsl::narrow(_cast)
|
||||
/wd26481 # CppCoreCheck - use span instead of pointer arithmetic
|
||||
$<$<VERSION_LESS:$<CXX_COMPILER_VERSION>,1920>: # VS2015
|
||||
/wd4189 # variable is initialized but not referenced
|
||||
$<$<NOT:$<CONFIG:Debug>>: # Release, RelWithDebInfo
|
||||
/wd4702 # Unreachable code
|
||||
>
|
||||
>
|
||||
>
|
||||
$<$<CXX_COMPILER_ID:Clang>:
|
||||
-Weverything
|
||||
-Wno-c++98-compat
|
||||
-Wno-c++98-compat-pedantic
|
||||
-Wno-covered-switch-default # GTest
|
||||
-Wno-deprecated-declarations # Allow tests for [[deprecated]] elements
|
||||
-Wno-global-constructors # GTest
|
||||
-Wno-language-extension-token # GTest gtest-port.h
|
||||
-Wno-missing-braces
|
||||
-Wno-missing-prototypes
|
||||
-Wno-unknown-attributes
|
||||
$<$<EQUAL:${GSL_CXX_STANDARD},14>:-Wno-unused-member-function>
|
||||
-Wno-shift-sign-overflow # GTest gtest-port.h
|
||||
-Wno-undef # GTest
|
||||
-Wno-used-but-marked-unused # GTest EXPECT_DEATH
|
||||
-Wno-global-constructors
|
||||
$<$<EQUAL:${GSL_CXX_STANDARD},14>: # no support for [[maybe_unused]]
|
||||
-Wno-unused-member-function
|
||||
-Wno-unused-variable
|
||||
>
|
||||
>
|
||||
)
|
||||
else()
|
||||
@@ -61,60 +97,79 @@ else()
|
||||
-Wpedantic
|
||||
-Wshadow
|
||||
-Wsign-conversion
|
||||
-Wno-deprecated-declarations # Allow tests for [[deprecated]] elements
|
||||
$<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>>:
|
||||
-Weverything
|
||||
-Wno-c++98-compat
|
||||
-Wno-c++98-compat-pedantic
|
||||
-Wno-missing-braces
|
||||
-Wno-covered-switch-default # GTest
|
||||
-Wno-global-constructors # GTest
|
||||
-Wno-missing-prototypes
|
||||
-Wno-padded
|
||||
-Wno-unknown-attributes
|
||||
$<$<EQUAL:${GSL_CXX_STANDARD},14>:-Wno-unused-member-function>
|
||||
-Wno-used-but-marked-unused # GTest EXPECT_DEATH
|
||||
-Wno-weak-vtables
|
||||
-Wno-global-constructors
|
||||
$<$<EQUAL:${GSL_CXX_STANDARD},14>: # no support for [[maybe_unused]]
|
||||
-Wno-unused-member-function
|
||||
-Wno-unused-variable
|
||||
>
|
||||
>
|
||||
$<$<CXX_COMPILER_ID:Clang>:
|
||||
$<$<CXX_COMPILER_VERSION:5.0.2>:-Wno-undefined-func-template>
|
||||
$<$<AND:$<VERSION_GREATER:$<CXX_COMPILER_VERSION>,4.99>,$<VERSION_LESS:$<CXX_COMPILER_VERSION>,6>>:
|
||||
$<$<EQUAL:${GSL_CXX_STANDARD},17>:-Wno-undefined-func-template>
|
||||
>
|
||||
>
|
||||
$<$<CXX_COMPILER_ID:AppleClang>:
|
||||
$<$<AND:$<VERSION_GREATER:$<CXX_COMPILER_VERSION>,9.1>,$<VERSION_LESS:$<CXX_COMPILER_VERSION>,10>>:
|
||||
$<$<EQUAL:${GSL_CXX_STANDARD},17>:-Wno-undefined-func-template>
|
||||
>
|
||||
>
|
||||
$<$<CXX_COMPILER_ID:GNU>:
|
||||
-Wdouble-promotion # float implicit to double
|
||||
-Wlogical-op # suspicious uses of logical operators
|
||||
$<$<NOT:$<VERSION_LESS:$<CXX_COMPILER_VERSION>,6>>:
|
||||
-Wduplicated-cond # duplicated if-else conditions
|
||||
-Wmisleading-indentation
|
||||
-Wno-null-dereference
|
||||
-Wno-array-bounds
|
||||
$<$<EQUAL:${GSL_CXX_STANDARD},14>: # no support for [[maybe_unused]]
|
||||
-Wno-unused-variable
|
||||
>
|
||||
>
|
||||
$<$<NOT:$<VERSION_LESS:$<CXX_COMPILER_VERSION>,7>>:
|
||||
-Wduplicated-branches # identical if-else branches
|
||||
>
|
||||
>
|
||||
)
|
||||
endif(MSVC)
|
||||
|
||||
# for tests to find the catch header
|
||||
target_include_directories(gsl_tests_config INTERFACE
|
||||
${CMAKE_BINARY_DIR}/external/include
|
||||
# for tests to find the gtest header
|
||||
target_include_directories(gsl_tests_config SYSTEM INTERFACE
|
||||
googletest/googletest/include
|
||||
)
|
||||
|
||||
# set definitions for tests
|
||||
target_compile_definitions(gsl_tests_config INTERFACE
|
||||
GSL_THROW_ON_CONTRACT_VIOLATION
|
||||
)
|
||||
|
||||
# create the main executable for each test. this reduces the compile time
|
||||
# of each test by pre-compiling catch.
|
||||
add_library(test_catch STATIC test.cpp)
|
||||
target_link_libraries(test_catch
|
||||
GSL
|
||||
gsl_tests_config
|
||||
)
|
||||
add_dependencies(test_catch catch)
|
||||
set_property(TARGET test_catch PROPERTY FOLDER "GSL_tests")
|
||||
set_property(TARGET PROPERTY FOLDER "GSL_tests")
|
||||
|
||||
function(add_gsl_test name)
|
||||
add_executable(${name} ${name}.cpp)
|
||||
target_link_libraries(${name}
|
||||
GSL
|
||||
test_catch
|
||||
gsl_tests_config
|
||||
${GTestMain_LIBRARIES}
|
||||
)
|
||||
add_dependencies(${name} catch)
|
||||
add_test(
|
||||
${name}
|
||||
${name}
|
||||
${name}
|
||||
${name}
|
||||
)
|
||||
# group all tests under GSL_tests
|
||||
set_property(TARGET ${name} PROPERTY FOLDER "GSL_tests")
|
||||
endfunction()
|
||||
|
||||
add_gsl_test(span_tests)
|
||||
add_gsl_test(span_ext_tests)
|
||||
add_gsl_test(span_compatibility_tests)
|
||||
add_gsl_test(multi_span_tests)
|
||||
add_gsl_test(strided_span_tests)
|
||||
add_gsl_test(string_span_tests)
|
||||
@@ -142,7 +197,7 @@ endforeach(flag_var)
|
||||
add_library(gsl_tests_config_noexcept INTERFACE)
|
||||
if(MSVC) # MSVC or simulating MSVC
|
||||
target_compile_definitions(gsl_tests_config_noexcept INTERFACE
|
||||
_HAS_EXCEPTIONS=0
|
||||
_HAS_EXCEPTIONS=0 # disable exceptions in the Microsoft STL
|
||||
)
|
||||
target_compile_options(gsl_tests_config_noexcept INTERFACE
|
||||
${GSL_CPLUSPLUS_OPT}
|
||||
@@ -151,6 +206,8 @@ if(MSVC) # MSVC or simulating MSVC
|
||||
$<$<CXX_COMPILER_ID:MSVC>:
|
||||
/wd4577
|
||||
/wd4702
|
||||
/wd26440 # CppCoreCheck - GTest
|
||||
/wd26446 # CppCoreCheck - prefer gsl::at()
|
||||
>
|
||||
$<$<CXX_COMPILER_ID:Clang>:
|
||||
-Weverything
|
||||
@@ -158,6 +215,7 @@ if(MSVC) # MSVC or simulating MSVC
|
||||
-Wno-c++98-compat-pedantic
|
||||
-Wno-missing-prototypes
|
||||
-Wno-unknown-attributes
|
||||
-Wno-global-constructors
|
||||
>
|
||||
)
|
||||
else()
|
||||
@@ -180,20 +238,34 @@ else()
|
||||
-Wno-missing-prototypes
|
||||
-Wno-unknown-attributes
|
||||
-Wno-weak-vtables
|
||||
-Wno-global-constructors
|
||||
>
|
||||
$<$<CXX_COMPILER_ID:GNU>:
|
||||
-Wdouble-promotion # float implicit to double
|
||||
-Wlogical-op # suspicious uses of logical operators
|
||||
-Wuseless-cast # casting to its own type
|
||||
$<$<NOT:$<VERSION_LESS:$<CXX_COMPILER_VERSION>,6>>:
|
||||
-Wduplicated-cond # duplicated if-else conditions
|
||||
-Wmisleading-indentation
|
||||
-Wno-null-dereference
|
||||
-Wno-array-bounds
|
||||
>
|
||||
$<$<NOT:$<VERSION_LESS:$<CXX_COMPILER_VERSION>,7>>:
|
||||
-Wduplicated-branches # identical if-else branches
|
||||
>
|
||||
$<$<NOT:$<VERSION_LESS:$<CXX_COMPILER_VERSION>,8>>:
|
||||
-Wcast-align=strict # increase alignment (i.e. char* to int*)
|
||||
>
|
||||
>
|
||||
)
|
||||
endif(MSVC)
|
||||
|
||||
# set definitions for tests
|
||||
target_compile_definitions(gsl_tests_config_noexcept INTERFACE
|
||||
GSL_TERMINATE_ON_CONTRACT_VIOLATION
|
||||
)
|
||||
|
||||
function(add_gsl_test_noexcept name)
|
||||
add_executable(${name} ${name}.cpp)
|
||||
target_link_libraries(${name}
|
||||
GSL
|
||||
gsl_tests_config_noexcept
|
||||
${GTestMain_LIBRARIES}
|
||||
)
|
||||
add_test(
|
||||
${name}
|
||||
@@ -203,5 +275,4 @@ function(add_gsl_test_noexcept name)
|
||||
set_property(TARGET ${name} PROPERTY FOLDER "GSL_tests_noexcept")
|
||||
endfunction()
|
||||
|
||||
add_gsl_test_noexcept(no_exception_throw_tests)
|
||||
add_gsl_test_noexcept(no_exception_ensure_tests)
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
cmake_minimum_required(VERSION 3.0.2)
|
||||
project(googletest-download NONE)
|
||||
|
||||
include(ExternalProject)
|
||||
ExternalProject_Add(googletest
|
||||
GIT_REPOSITORY https://github.com/google/googletest.git
|
||||
GIT_TAG 703bd9caab50b139428cea1aaff9974ebee5742e
|
||||
SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-src"
|
||||
BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-build"
|
||||
CONFIGURE_COMMAND ""
|
||||
BUILD_COMMAND ""
|
||||
INSTALL_COMMAND ""
|
||||
TEST_COMMAND ""
|
||||
)
|
||||
+54
-48
@@ -14,30 +14,26 @@
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifdef _MSC_VER
|
||||
// blanket turn off warnings from CppCoreCheck from catch
|
||||
// so people aren't annoyed by them when running the tool.
|
||||
#pragma warning(disable : 26440 26426) // from catch
|
||||
#endif
|
||||
|
||||
#include <catch/catch.hpp> // for AssertionHandler, StringRef, CHECK, CHE...
|
||||
|
||||
#include <gsl/gsl_algorithm> // for copy
|
||||
#include <gtest/gtest.h>
|
||||
#include <gsl/algorithm> // for copy
|
||||
#include <gsl/span> // for span
|
||||
|
||||
#include <array> // for array
|
||||
#include <cstddef> // for size_t
|
||||
|
||||
namespace gsl {
|
||||
namespace
|
||||
{
|
||||
static constexpr char deathstring[] = "Expected Death";
|
||||
}
|
||||
|
||||
namespace gsl
|
||||
{
|
||||
struct fail_fast;
|
||||
} // namespace gsl
|
||||
} // namespace gsl
|
||||
|
||||
using namespace std;
|
||||
using namespace gsl;
|
||||
|
||||
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
|
||||
GSL_SUPPRESS(bounds.2) // NO-FORMAT: attribute
|
||||
TEST_CASE("same_type")
|
||||
TEST(algorithm_tests, same_type)
|
||||
{
|
||||
// dynamic source and destination span
|
||||
{
|
||||
@@ -50,9 +46,10 @@ TEST_CASE("same_type")
|
||||
copy(src_span, dst_span);
|
||||
copy(src_span, dst_span.subspan(src_span.size()));
|
||||
|
||||
for (std::size_t i = 0; i < src.size(); ++i) {
|
||||
CHECK(dst[i] == src[i]);
|
||||
CHECK(dst[i + src.size()] == src[i]);
|
||||
for (std::size_t i = 0; i < src.size(); ++i)
|
||||
{
|
||||
EXPECT_TRUE(dst[i] == src[i]);
|
||||
EXPECT_TRUE(dst[i + src.size()] == src[i]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -67,9 +64,10 @@ TEST_CASE("same_type")
|
||||
copy(src_span, dst_span);
|
||||
copy(src_span, dst_span.subspan(src_span.size()));
|
||||
|
||||
for (std::size_t i = 0; i < src.size(); ++i) {
|
||||
CHECK(dst[i] == src[i]);
|
||||
CHECK(dst[i + src.size()] == src[i]);
|
||||
for (std::size_t i = 0; i < src.size(); ++i)
|
||||
{
|
||||
EXPECT_TRUE(dst[i] == src[i]);
|
||||
EXPECT_TRUE(dst[i + src.size()] == src[i]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -84,9 +82,10 @@ TEST_CASE("same_type")
|
||||
copy(src_span, dst_span);
|
||||
copy(src_span, dst_span.subspan(src_span.size()));
|
||||
|
||||
for (std::size_t i = 0; i < src.size(); ++i) {
|
||||
CHECK(dst[i] == src[i]);
|
||||
CHECK(dst[i + src.size()] == src[i]);
|
||||
for (std::size_t i = 0; i < src.size(); ++i)
|
||||
{
|
||||
EXPECT_TRUE(dst[i] == src[i]);
|
||||
EXPECT_TRUE(dst[i + src.size()] == src[i]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -101,17 +100,15 @@ TEST_CASE("same_type")
|
||||
copy(src_span, dst_span);
|
||||
copy(src_span, dst_span.subspan(src_span.size()));
|
||||
|
||||
for (std::size_t i = 0; i < src.size(); ++i) {
|
||||
CHECK(dst[i] == src[i]);
|
||||
CHECK(dst[i + src.size()] == src[i]);
|
||||
for (std::size_t i = 0; i < src.size(); ++i)
|
||||
{
|
||||
EXPECT_TRUE(dst[i] == src[i]);
|
||||
EXPECT_TRUE(dst[i + src.size()] == src[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
|
||||
GSL_SUPPRESS(bounds.2) // NO-FORMAT: attribute
|
||||
TEST_CASE("compatible_type")
|
||||
TEST(algorithm_tests, compatible_type)
|
||||
{
|
||||
// dynamic source and destination span
|
||||
{
|
||||
@@ -124,9 +121,10 @@ TEST_CASE("compatible_type")
|
||||
copy(src_span, dst_span);
|
||||
copy(src_span, dst_span.subspan(src_span.size()));
|
||||
|
||||
for (std::size_t i = 0; i < src.size(); ++i) {
|
||||
CHECK(dst[i] == src[i]);
|
||||
CHECK(dst[i + src.size()] == src[i]);
|
||||
for (std::size_t i = 0; i < src.size(); ++i)
|
||||
{
|
||||
EXPECT_TRUE(dst[i] == src[i]);
|
||||
EXPECT_TRUE(dst[i + src.size()] == src[i]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -141,9 +139,10 @@ TEST_CASE("compatible_type")
|
||||
copy(src_span, dst_span);
|
||||
copy(src_span, dst_span.subspan(src_span.size()));
|
||||
|
||||
for (std::size_t i = 0; i < src.size(); ++i) {
|
||||
CHECK(dst[i] == src[i]);
|
||||
CHECK(dst[i + src.size()] == src[i]);
|
||||
for (std::size_t i = 0; i < src.size(); ++i)
|
||||
{
|
||||
EXPECT_TRUE(dst[i] == src[i]);
|
||||
EXPECT_TRUE(dst[i + src.size()] == src[i]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -158,9 +157,10 @@ TEST_CASE("compatible_type")
|
||||
copy(src_span, dst_span);
|
||||
copy(src_span, dst_span.subspan(src_span.size()));
|
||||
|
||||
for (std::size_t i = 0; i < src.size(); ++i) {
|
||||
CHECK(dst[i] == src[i]);
|
||||
CHECK(dst[i + src.size()] == src[i]);
|
||||
for (std::size_t i = 0; i < src.size(); ++i)
|
||||
{
|
||||
EXPECT_TRUE(dst[i] == src[i]);
|
||||
EXPECT_TRUE(dst[i + src.size()] == src[i]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -175,15 +175,16 @@ TEST_CASE("compatible_type")
|
||||
copy(src_span, dst_span);
|
||||
copy(src_span, dst_span.subspan(src_span.size()));
|
||||
|
||||
for (std::size_t i = 0; i < src.size(); ++i) {
|
||||
CHECK(dst[i] == src[i]);
|
||||
CHECK(dst[i + src.size()] == src[i]);
|
||||
for (std::size_t i = 0; i < src.size(); ++i)
|
||||
{
|
||||
EXPECT_TRUE(dst[i] == src[i]);
|
||||
EXPECT_TRUE(dst[i + src.size()] == src[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
TEST_CASE("incompatible_type")
|
||||
TEST(algorithm_tests, incompatible_type)
|
||||
{
|
||||
std::array<int, 4> src{1, 2, 3, 4};
|
||||
std::array<int*, 12> dst{};
|
||||
@@ -201,8 +202,13 @@ TEST_CASE("incompatible_type")
|
||||
}
|
||||
#endif
|
||||
|
||||
TEST_CASE("small_destination_span")
|
||||
TEST(algorithm_tests, small_destination_span)
|
||||
{
|
||||
std::set_terminate([] {
|
||||
std::cerr << "Expected Death. small_destination_span";
|
||||
std::abort();
|
||||
});
|
||||
|
||||
std::array<int, 12> src{1, 2, 3, 4};
|
||||
std::array<int, 4> dst{};
|
||||
|
||||
@@ -211,9 +217,9 @@ TEST_CASE("small_destination_span")
|
||||
const span<int> dst_span_dyn(dst);
|
||||
const span<int, 4> dst_span_static(dst);
|
||||
|
||||
CHECK_THROWS_AS(copy(src_span_dyn, dst_span_dyn), fail_fast);
|
||||
CHECK_THROWS_AS(copy(src_span_dyn, dst_span_static), fail_fast);
|
||||
CHECK_THROWS_AS(copy(src_span_static, dst_span_dyn), fail_fast);
|
||||
EXPECT_DEATH(copy(src_span_dyn, dst_span_dyn), deathstring);
|
||||
EXPECT_DEATH(copy(src_span_dyn, dst_span_static), deathstring);
|
||||
EXPECT_DEATH(copy(src_span_static, dst_span_dyn), deathstring);
|
||||
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
copy(src_span_static, dst_span_static);
|
||||
|
||||
+29
-20
@@ -14,39 +14,48 @@
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifdef _MSC_VER
|
||||
// blanket turn off warnings from CppCoreCheck from catch
|
||||
// so people aren't annoyed by them when running the tool.
|
||||
#pragma warning(disable : 26440 26426) // from catch
|
||||
#endif
|
||||
|
||||
#include <catch/catch.hpp> // for AssertionHandler, StringRef, CHECK, CHECK...
|
||||
|
||||
#include <gsl/gsl_assert> // for fail_fast (ptr only), Ensures, Expects
|
||||
#include <gtest/gtest.h>
|
||||
#include <gsl/assert> // for fail_fast (ptr only), contracts
|
||||
|
||||
using namespace gsl;
|
||||
|
||||
namespace
|
||||
{
|
||||
static constexpr char deathstring[] = "Expected Death";
|
||||
|
||||
int f(int i)
|
||||
{
|
||||
Expects(i > 0 && i < 10);
|
||||
Expects(i > 0 && i < 10, Testing);
|
||||
return i;
|
||||
}
|
||||
|
||||
TEST_CASE("expects")
|
||||
{
|
||||
CHECK(f(2) == 2);
|
||||
CHECK_THROWS_AS(f(10), fail_fast);
|
||||
}
|
||||
|
||||
int g(int i)
|
||||
{
|
||||
i++;
|
||||
Ensures(i > 0 && i < 10);
|
||||
Ensures(i > 0 && i < 10, Testing);
|
||||
return i;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
TEST_CASE("ensures")
|
||||
TEST(assertion_tests, expects)
|
||||
{
|
||||
CHECK(g(2) == 3);
|
||||
CHECK_THROWS_AS(g(9), fail_fast);
|
||||
std::set_terminate([] {
|
||||
std::cerr << "Expected Death. expects";
|
||||
std::abort();
|
||||
});
|
||||
|
||||
EXPECT_TRUE(f(2) == 2);
|
||||
EXPECT_DEATH(f(10), deathstring);
|
||||
}
|
||||
|
||||
|
||||
TEST(assertion_tests, ensures)
|
||||
{
|
||||
std::set_terminate([] {
|
||||
std::cerr << "Expected Death. ensures";
|
||||
std::abort();
|
||||
});
|
||||
|
||||
EXPECT_TRUE(g(2) == 3);
|
||||
EXPECT_DEATH(g(9), deathstring);
|
||||
}
|
||||
|
||||
+54
-54
@@ -14,103 +14,104 @@
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifdef _MSC_VER
|
||||
// blanket turn off warnings from CppCoreCheck from catch
|
||||
// so people aren't annoyed by them when running the tool.
|
||||
#pragma warning(disable : 26440 26426) // from catch
|
||||
#endif
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <catch/catch.hpp> // for AssertionHandler, StringRef, CHECK_THROW...
|
||||
|
||||
#include <gsl/gsl_util> // for at
|
||||
#include <gsl/util> // for at
|
||||
|
||||
#include <array> // for array
|
||||
#include <cstddef> // for size_t
|
||||
#include <initializer_list> // for initializer_list
|
||||
#include <vector> // for vector
|
||||
|
||||
namespace
|
||||
{
|
||||
static constexpr char deathstring[] = "Expected Death";
|
||||
}
|
||||
|
||||
namespace gsl {
|
||||
struct fail_fast;
|
||||
} // namespace gsl
|
||||
|
||||
using gsl::fail_fast;
|
||||
|
||||
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
|
||||
GSL_SUPPRESS(bounds.2) // NO-FORMAT: attribute
|
||||
TEST_CASE("static_array")
|
||||
TEST(at_tests, static_array)
|
||||
{
|
||||
int a[4] = {1, 2, 3, 4};
|
||||
const int(&c_a)[4] = a;
|
||||
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
CHECK(&gsl::at(a, i) == &a[i]);
|
||||
CHECK(&gsl::at(c_a, i) == &a[i]);
|
||||
EXPECT_TRUE(&gsl::at(a, i) == &a[i]);
|
||||
EXPECT_TRUE(&gsl::at(c_a, i) == &a[i]);
|
||||
}
|
||||
|
||||
CHECK_THROWS_AS(gsl::at(a, -1), fail_fast);
|
||||
CHECK_THROWS_AS(gsl::at(a, 4), fail_fast);
|
||||
CHECK_THROWS_AS(gsl::at(c_a, -1), fail_fast);
|
||||
CHECK_THROWS_AS(gsl::at(c_a, 4), fail_fast);
|
||||
std::set_terminate([] {
|
||||
std::cerr << "Expected Death. static_array";
|
||||
std::abort();
|
||||
});
|
||||
|
||||
EXPECT_DEATH(gsl::at(a, -1), deathstring);
|
||||
EXPECT_DEATH(gsl::at(a, 4), deathstring);
|
||||
EXPECT_DEATH(gsl::at(c_a, -1), deathstring);
|
||||
EXPECT_DEATH(gsl::at(c_a, 4), deathstring);
|
||||
}
|
||||
|
||||
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
|
||||
GSL_SUPPRESS(bounds.2) // NO-FORMAT: attribute
|
||||
TEST_CASE("std_array")
|
||||
TEST(at_tests, std_array)
|
||||
{
|
||||
std::array<int, 4> a = {1, 2, 3, 4};
|
||||
const std::array<int, 4>& c_a = a;
|
||||
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
CHECK(&gsl::at(a, i) == &a[static_cast<std::size_t>(i)]);
|
||||
CHECK(&gsl::at(c_a, i) == &a[static_cast<std::size_t>(i)]);
|
||||
EXPECT_TRUE(&gsl::at(a, i) == &a[static_cast<std::size_t>(i)]);
|
||||
EXPECT_TRUE(&gsl::at(c_a, i) == &a[static_cast<std::size_t>(i)]);
|
||||
}
|
||||
|
||||
CHECK_THROWS_AS(gsl::at(a, -1), fail_fast);
|
||||
CHECK_THROWS_AS(gsl::at(a, 4), fail_fast);
|
||||
CHECK_THROWS_AS(gsl::at(c_a, -1), fail_fast);
|
||||
CHECK_THROWS_AS(gsl::at(c_a, 4), fail_fast);
|
||||
std::set_terminate([] {
|
||||
std::cerr << "Expected Death. std_array";
|
||||
std::abort();
|
||||
});
|
||||
|
||||
EXPECT_DEATH(gsl::at(a, -1), deathstring);
|
||||
EXPECT_DEATH(gsl::at(a, 4), deathstring);
|
||||
EXPECT_DEATH(gsl::at(c_a, -1), deathstring);
|
||||
EXPECT_DEATH(gsl::at(c_a, 4), deathstring);
|
||||
}
|
||||
|
||||
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
|
||||
GSL_SUPPRESS(bounds.2) // NO-FORMAT: attribute
|
||||
TEST_CASE("StdVector")
|
||||
TEST(at_tests, std_vector)
|
||||
{
|
||||
std::vector<int> a = {1, 2, 3, 4};
|
||||
const std::vector<int>& c_a = a;
|
||||
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
CHECK(&gsl::at(a, i) == &a[static_cast<std::size_t>(i)]);
|
||||
CHECK(&gsl::at(c_a, i) == &a[static_cast<std::size_t>(i)]);
|
||||
EXPECT_TRUE(&gsl::at(a, i) == &a[static_cast<std::size_t>(i)]);
|
||||
EXPECT_TRUE(&gsl::at(c_a, i) == &a[static_cast<std::size_t>(i)]);
|
||||
}
|
||||
|
||||
CHECK_THROWS_AS(gsl::at(a, -1), fail_fast);
|
||||
CHECK_THROWS_AS(gsl::at(a, 4), fail_fast);
|
||||
CHECK_THROWS_AS(gsl::at(c_a, -1), fail_fast);
|
||||
CHECK_THROWS_AS(gsl::at(c_a, 4), fail_fast);
|
||||
std::set_terminate([] {
|
||||
std::cerr << "Expected Death. std_vector";
|
||||
std::abort();
|
||||
});
|
||||
|
||||
EXPECT_DEATH(gsl::at(a, -1), deathstring);
|
||||
EXPECT_DEATH(gsl::at(a, 4), deathstring);
|
||||
EXPECT_DEATH(gsl::at(c_a, -1), deathstring);
|
||||
EXPECT_DEATH(gsl::at(c_a, 4), deathstring);
|
||||
}
|
||||
|
||||
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
|
||||
GSL_SUPPRESS(bounds.2) // NO-FORMAT: attribute
|
||||
TEST_CASE("InitializerList")
|
||||
TEST(at_tests, InitializerList)
|
||||
{
|
||||
const std::initializer_list<int> a = {1, 2, 3, 4};
|
||||
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
CHECK(gsl::at(a, i) == i + 1);
|
||||
CHECK(gsl::at({1, 2, 3, 4}, i) == i + 1);
|
||||
EXPECT_TRUE(gsl::at(a, i) == i + 1);
|
||||
EXPECT_TRUE(gsl::at({1, 2, 3, 4}, i) == i + 1);
|
||||
}
|
||||
|
||||
CHECK_THROWS_AS(gsl::at(a, -1), fail_fast);
|
||||
CHECK_THROWS_AS(gsl::at(a, 4), fail_fast);
|
||||
CHECK_THROWS_AS(gsl::at({1, 2, 3, 4}, -1), fail_fast);
|
||||
CHECK_THROWS_AS(gsl::at({1, 2, 3, 4}, 4), fail_fast);
|
||||
std::set_terminate([] {
|
||||
std::cerr << "Expected Death. InitializerList";
|
||||
std::abort();
|
||||
});
|
||||
|
||||
EXPECT_DEATH(gsl::at(a, -1), deathstring);
|
||||
EXPECT_DEATH(gsl::at(a, 4), deathstring);
|
||||
EXPECT_DEATH(gsl::at({1, 2, 3, 4}, -1), deathstring);
|
||||
EXPECT_DEATH(gsl::at({1, 2, 3, 4}, 4), deathstring);
|
||||
}
|
||||
|
||||
#if !defined(_MSC_VER) || defined(__clang__) || _MSC_VER >= 1910
|
||||
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
|
||||
GSL_SUPPRESS(bounds.2) // NO-FORMAT: attribute
|
||||
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
|
||||
static constexpr bool test_constexpr()
|
||||
{
|
||||
int a1[4] = {1, 2, 3, 4};
|
||||
@@ -132,4 +133,3 @@ static constexpr bool test_constexpr()
|
||||
|
||||
static_assert(test_constexpr(), "FAIL");
|
||||
#endif
|
||||
|
||||
|
||||
+13
-36
@@ -14,28 +14,12 @@
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifdef _MSC_VER
|
||||
// blanket turn off warnings from CppCoreCheck from catch
|
||||
// so people aren't annoyed by them when running the tool.
|
||||
#pragma warning(disable : 26440 26426) // from catch
|
||||
#pragma warning(disable : 4996) // use of function or classes marked [[deprecated]]
|
||||
#endif
|
||||
|
||||
#if __clang__ || __GNUC__
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
||||
#endif
|
||||
|
||||
#include <catch/catch.hpp> // for AssertionHandler, StringRef, TEST_CASE
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <gsl/multi_span> // for static_bounds, static_bounds_dynamic_range_t
|
||||
|
||||
#include <cstddef> // for ptrdiff_t, size_t
|
||||
|
||||
namespace gsl {
|
||||
struct fail_fast;
|
||||
} // namespace gsl
|
||||
|
||||
using namespace std;
|
||||
using namespace gsl;
|
||||
|
||||
@@ -44,8 +28,7 @@ namespace
|
||||
void use(std::ptrdiff_t&) {}
|
||||
}
|
||||
|
||||
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
|
||||
TEST_CASE("basic_bounds")
|
||||
TEST(bounds_tests, basic_bounds)
|
||||
{
|
||||
for (auto point : static_bounds<dynamic_range, 3, 4>{2}) {
|
||||
for (decltype(point)::size_type j = 0;
|
||||
@@ -57,9 +40,7 @@ TEST_CASE("basic_bounds")
|
||||
}
|
||||
}
|
||||
|
||||
GSL_SUPPRESS(f.4) // NO-FORMAT: attribute
|
||||
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
|
||||
TEST_CASE("bounds_basic")
|
||||
TEST(bounds_tests, bounds_basic)
|
||||
{
|
||||
static_bounds<3, 4, 5> b;
|
||||
const auto a = b.slice();
|
||||
@@ -68,9 +49,7 @@ TEST_CASE("bounds_basic")
|
||||
x.slice().slice();
|
||||
}
|
||||
|
||||
GSL_SUPPRESS(f.4) // NO-FORMAT: attribute
|
||||
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
|
||||
TEST_CASE("arrayview_iterator")
|
||||
TEST(bounds_tests, arrayview_iterator)
|
||||
{
|
||||
static_bounds<4, dynamic_range, 2> bounds{3};
|
||||
|
||||
@@ -88,8 +67,7 @@ TEST_CASE("arrayview_iterator")
|
||||
#endif
|
||||
}
|
||||
|
||||
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
|
||||
TEST_CASE("bounds_convertible")
|
||||
TEST(bounds_tests, bounds_convertible)
|
||||
{
|
||||
static_bounds<7, 4, 2> b1;
|
||||
static_bounds<7, dynamic_range, 2> b2 = b1;
|
||||
@@ -102,24 +80,23 @@ TEST_CASE("bounds_convertible")
|
||||
static_bounds<7, 4, 2> b4 = b3;
|
||||
(void) b4;
|
||||
|
||||
static_bounds<dynamic_range> b11;
|
||||
|
||||
static_bounds<dynamic_range> b5;
|
||||
static_bounds<34> b6;
|
||||
|
||||
std::set_terminate([] {
|
||||
std::cerr << "Expected Death. bounds_convertible";
|
||||
std::abort();
|
||||
});
|
||||
|
||||
b5 = static_bounds<20>();
|
||||
CHECK_THROWS_AS(b6 = b5, fail_fast);
|
||||
EXPECT_DEATH(b6 = b5, ".*");
|
||||
b5 = static_bounds<34>();
|
||||
b6 = b5;
|
||||
|
||||
CHECK(b5 == b6);
|
||||
CHECK(b5.size() == b6.size());
|
||||
EXPECT_TRUE(b5 == b6);
|
||||
EXPECT_TRUE(b5.size() == b6.size());
|
||||
}
|
||||
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
copy(src_span_static, dst_span_static);
|
||||
#endif
|
||||
|
||||
#if __clang__ || __GNUC__
|
||||
#pragma GCC diagnostic pop
|
||||
#endif
|
||||
|
||||
+90
-97
@@ -14,119 +14,112 @@
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifdef _MSC_VER
|
||||
// blanket turn off warnings from CppCoreCheck from catch
|
||||
// so people aren't annoyed by them when running the tool.
|
||||
#pragma warning(disable : 26440 26426) // from catch
|
||||
#endif
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <catch/catch.hpp> // for AssertionHandler, StringRef, CHECK, TEST_...
|
||||
|
||||
#include <gsl/gsl_byte> // for to_byte, to_integer, byte, operator&, ope...
|
||||
#include <gsl/byte> // for to_byte, to_integer, byte, operator&, ope...
|
||||
|
||||
using namespace std;
|
||||
using namespace gsl;
|
||||
|
||||
namespace
|
||||
{
|
||||
TEST_CASE("construction")
|
||||
{
|
||||
{
|
||||
const byte b = static_cast<byte>(4);
|
||||
CHECK(static_cast<unsigned char>(b) == 4);
|
||||
}
|
||||
|
||||
GSL_SUPPRESS(es.49)
|
||||
{
|
||||
const byte b = byte(12);
|
||||
CHECK(static_cast<unsigned char>(b) == 12);
|
||||
}
|
||||
|
||||
{
|
||||
const byte b = to_byte<12>();
|
||||
CHECK(static_cast<unsigned char>(b) == 12);
|
||||
}
|
||||
{
|
||||
const unsigned char uc = 12;
|
||||
const byte b = to_byte(uc);
|
||||
CHECK(static_cast<unsigned char>(b) == 12);
|
||||
}
|
||||
|
||||
#if defined(__cplusplus) && (__cplusplus >= 201703L)
|
||||
{
|
||||
const byte b { 14 };
|
||||
CHECK(static_cast<unsigned char>(b) == 14);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
TEST_CASE("bitwise_operations")
|
||||
{
|
||||
const byte b = to_byte<0xFF>();
|
||||
|
||||
byte a = to_byte<0x00>();
|
||||
CHECK((b | a) == to_byte<0xFF>());
|
||||
CHECK(a == to_byte<0x00>());
|
||||
|
||||
a |= b;
|
||||
CHECK(a == to_byte<0xFF>());
|
||||
|
||||
a = to_byte<0x01>();
|
||||
CHECK((b & a) == to_byte<0x01>());
|
||||
|
||||
a &= b;
|
||||
CHECK(a == to_byte<0x01>());
|
||||
|
||||
CHECK((b ^ a) == to_byte<0xFE>());
|
||||
|
||||
CHECK(a == to_byte<0x01>());
|
||||
a ^= b;
|
||||
CHECK(a == to_byte<0xFE>());
|
||||
|
||||
a = to_byte<0x01>();
|
||||
CHECK(~a == to_byte<0xFE>());
|
||||
|
||||
a = to_byte<0xFF>();
|
||||
CHECK((a << 4) == to_byte<0xF0>());
|
||||
CHECK((a >> 4) == to_byte<0x0F>());
|
||||
|
||||
a <<= 4;
|
||||
CHECK(a == to_byte<0xF0>());
|
||||
a >>= 4;
|
||||
CHECK(a == to_byte<0x0F>());
|
||||
}
|
||||
|
||||
TEST_CASE("to_integer")
|
||||
{
|
||||
const byte b = to_byte<0x12>();
|
||||
|
||||
CHECK(0x12 == gsl::to_integer<char>(b));
|
||||
CHECK(0x12 == gsl::to_integer<short>(b));
|
||||
CHECK(0x12 == gsl::to_integer<long>(b));
|
||||
CHECK(0x12 == gsl::to_integer<long long>(b));
|
||||
|
||||
CHECK(0x12 == gsl::to_integer<unsigned char>(b));
|
||||
CHECK(0x12 == gsl::to_integer<unsigned short>(b));
|
||||
CHECK(0x12 == gsl::to_integer<unsigned long>(b));
|
||||
CHECK(0x12 == gsl::to_integer<unsigned long long>(b));
|
||||
|
||||
// CHECK(0x12 == gsl::to_integer<float>(b)); // expect compile-time error
|
||||
// CHECK(0x12 == gsl::to_integer<double>(b)); // expect compile-time error
|
||||
}
|
||||
|
||||
int modify_both(gsl::byte & b, int& i)
|
||||
int modify_both(gsl::byte& b, int& i)
|
||||
{
|
||||
i = 10;
|
||||
b = to_byte<5>();
|
||||
return i;
|
||||
}
|
||||
|
||||
GSL_SUPPRESS(type.1)
|
||||
TEST_CASE("aliasing")
|
||||
TEST(byte_tests, construction)
|
||||
{
|
||||
{
|
||||
const byte b = static_cast<byte>(4);
|
||||
EXPECT_TRUE(static_cast<unsigned char>(b) == 4);
|
||||
}
|
||||
|
||||
GSL_SUPPRESS(es.49)
|
||||
{
|
||||
const byte b = byte(12);
|
||||
EXPECT_TRUE(static_cast<unsigned char>(b) == 12);
|
||||
}
|
||||
|
||||
{
|
||||
const byte b = to_byte<12>();
|
||||
EXPECT_TRUE(static_cast<unsigned char>(b) == 12);
|
||||
}
|
||||
{
|
||||
const unsigned char uc = 12;
|
||||
const byte b = to_byte(uc);
|
||||
EXPECT_TRUE(static_cast<unsigned char>(b) == 12);
|
||||
}
|
||||
|
||||
#if defined(__cplusplus) && (__cplusplus >= 201703L)
|
||||
{
|
||||
const byte b { 14 };
|
||||
EXPECT_TRUE(static_cast<unsigned char>(b) == 14);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
TEST(byte_tests, bitwise_operations)
|
||||
{
|
||||
const byte b = to_byte<0xFF>();
|
||||
|
||||
byte a = to_byte<0x00>();
|
||||
EXPECT_TRUE((b | a) == to_byte<0xFF>());
|
||||
EXPECT_TRUE(a == to_byte<0x00>());
|
||||
|
||||
a |= b;
|
||||
EXPECT_TRUE(a == to_byte<0xFF>());
|
||||
|
||||
a = to_byte<0x01>();
|
||||
EXPECT_TRUE((b & a) == to_byte<0x01>());
|
||||
|
||||
a &= b;
|
||||
EXPECT_TRUE(a == to_byte<0x01>());
|
||||
|
||||
EXPECT_TRUE((b ^ a) == to_byte<0xFE>());
|
||||
|
||||
EXPECT_TRUE(a == to_byte<0x01>());
|
||||
a ^= b;
|
||||
EXPECT_TRUE(a == to_byte<0xFE>());
|
||||
|
||||
a = to_byte<0x01>();
|
||||
EXPECT_TRUE(~a == to_byte<0xFE>());
|
||||
|
||||
a = to_byte<0xFF>();
|
||||
EXPECT_TRUE((a << 4) == to_byte<0xF0>());
|
||||
EXPECT_TRUE((a >> 4) == to_byte<0x0F>());
|
||||
|
||||
a <<= 4;
|
||||
EXPECT_TRUE(a == to_byte<0xF0>());
|
||||
a >>= 4;
|
||||
EXPECT_TRUE(a == to_byte<0x0F>());
|
||||
}
|
||||
|
||||
TEST(byte_tests, to_integer)
|
||||
{
|
||||
const byte b = to_byte<0x12>();
|
||||
|
||||
EXPECT_TRUE(0x12 == gsl::to_integer<char>(b));
|
||||
EXPECT_TRUE(0x12 == gsl::to_integer<short>(b));
|
||||
EXPECT_TRUE(0x12 == gsl::to_integer<long>(b));
|
||||
EXPECT_TRUE(0x12 == gsl::to_integer<long long>(b));
|
||||
|
||||
EXPECT_TRUE(0x12 == gsl::to_integer<unsigned char>(b));
|
||||
EXPECT_TRUE(0x12 == gsl::to_integer<unsigned short>(b));
|
||||
EXPECT_TRUE(0x12 == gsl::to_integer<unsigned long>(b));
|
||||
EXPECT_TRUE(0x12 == gsl::to_integer<unsigned long long>(b));
|
||||
|
||||
// EXPECT_TRUE(0x12 == gsl::to_integer<float>(b)); // expect compile-time error
|
||||
// EXPECT_TRUE(0x12 == gsl::to_integer<double>(b)); // expect compile-time error
|
||||
}
|
||||
|
||||
TEST(byte_tests, aliasing)
|
||||
{
|
||||
int i{0};
|
||||
const int res = modify_both(reinterpret_cast<byte&>(i), i);
|
||||
CHECK(res == i);
|
||||
EXPECT_TRUE(res == i);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+619
-552
File diff suppressed because it is too large
Load Diff
@@ -1,48 +0,0 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
|
||||
//
|
||||
// This code is licensed under the MIT License (MIT).
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include <cstdlib> // for std::exit
|
||||
#include <gsl/gsl_assert> // for get_terminate
|
||||
#include <gsl/gsl_util> // for narrow
|
||||
|
||||
int narrow_no_throw()
|
||||
{
|
||||
const long long bigNumber = 0x0fffffffffffffff;
|
||||
return gsl::narrow<int>(bigNumber);
|
||||
}
|
||||
|
||||
[[noreturn]] void test_terminate() { std::exit(0); }
|
||||
|
||||
void setup_termination_handler() noexcept
|
||||
{
|
||||
#if defined(GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND)
|
||||
|
||||
auto& handler = gsl::details::get_terminate_handler();
|
||||
handler = &test_terminate;
|
||||
|
||||
#else
|
||||
|
||||
std::set_terminate(test_terminate);
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
setup_termination_handler();
|
||||
narrow_no_throw();
|
||||
return -1;
|
||||
}
|
||||
+132
-118
@@ -14,16 +14,7 @@
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifdef _MSC_VER
|
||||
// blanket turn off warnings from CppCoreCheck from catch
|
||||
// so people aren't annoyed by them when running the tool.
|
||||
#pragma warning(disable : 26440 26426) // from catch
|
||||
|
||||
// Fix VS2015 build breaks in Release
|
||||
#pragma warning(disable : 4702) // unreachable code
|
||||
#endif
|
||||
|
||||
#include <catch/catch.hpp> // for AssertionHandler, StringRef, CHECK, TEST_...
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <gsl/pointers> // for not_null, operator<, operator<=, operator>
|
||||
|
||||
@@ -34,13 +25,13 @@
|
||||
#include <string> // for basic_string, operator==, string, operator<<
|
||||
#include <typeinfo> // for type_info
|
||||
|
||||
namespace gsl
|
||||
{
|
||||
struct fail_fast;
|
||||
} // namespace gsl
|
||||
|
||||
using namespace gsl;
|
||||
|
||||
namespace
|
||||
{
|
||||
static constexpr char deathstring[] = "Expected Death";
|
||||
} //namespace
|
||||
|
||||
struct MyBase
|
||||
{
|
||||
};
|
||||
@@ -133,10 +124,8 @@ GSL_SUPPRESS(f.4) // NO-FORMAT: attribute
|
||||
bool helper_const(not_null<const int*> p) { return *p == 12; }
|
||||
|
||||
int* return_pointer() { return nullptr; }
|
||||
const int* return_pointer_const() { return nullptr; }
|
||||
|
||||
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
|
||||
TEST_CASE("TestNotNullConstructors")
|
||||
TEST(notnull_tests, TestNotNullConstructors)
|
||||
{
|
||||
{
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
@@ -153,18 +142,22 @@ TEST_CASE("TestNotNullConstructors")
|
||||
#endif
|
||||
}
|
||||
|
||||
std::set_terminate([] {
|
||||
std::cerr << "Expected Death. TestNotNullConstructors";
|
||||
std::abort();
|
||||
});
|
||||
{
|
||||
// from shared pointer
|
||||
int i = 12;
|
||||
auto rp = RefCounted<int>(&i);
|
||||
not_null<int*> p(rp);
|
||||
CHECK(p.get() == &i);
|
||||
EXPECT_TRUE(p.get() == &i);
|
||||
|
||||
not_null<std::shared_ptr<int>> x(
|
||||
std::make_shared<int>(10)); // shared_ptr<int> is nullptr assignable
|
||||
|
||||
int* pi = nullptr;
|
||||
CHECK_THROWS_AS(not_null<decltype(pi)>(pi), fail_fast);
|
||||
EXPECT_DEATH((not_null<decltype(pi)>(pi)), deathstring);
|
||||
}
|
||||
|
||||
{
|
||||
@@ -175,7 +168,7 @@ TEST_CASE("TestNotNullConstructors")
|
||||
helper(&t);
|
||||
helper_const(&t);
|
||||
|
||||
CHECK(*x == 42);
|
||||
EXPECT_TRUE(*x == 42);
|
||||
}
|
||||
|
||||
{
|
||||
@@ -191,7 +184,7 @@ TEST_CASE("TestNotNullConstructors")
|
||||
helper(x);
|
||||
helper_const(x);
|
||||
|
||||
CHECK(*x == 42);
|
||||
EXPECT_TRUE(*x == 42);
|
||||
}
|
||||
|
||||
{
|
||||
@@ -205,7 +198,7 @@ TEST_CASE("TestNotNullConstructors")
|
||||
helper_const(cp);
|
||||
helper_const(x);
|
||||
|
||||
CHECK(*x == 42);
|
||||
EXPECT_TRUE(*x == 42);
|
||||
}
|
||||
|
||||
{
|
||||
@@ -215,19 +208,18 @@ TEST_CASE("TestNotNullConstructors")
|
||||
|
||||
auto x = not_null<const int*>{cp};
|
||||
|
||||
CHECK(*x == 42);
|
||||
EXPECT_TRUE(*x == 42);
|
||||
}
|
||||
|
||||
{
|
||||
// from returned pointer
|
||||
|
||||
CHECK_THROWS_AS(helper(return_pointer()), fail_fast);
|
||||
CHECK_THROWS_AS(helper_const(return_pointer()), fail_fast);
|
||||
EXPECT_DEATH(helper(return_pointer()), deathstring);
|
||||
EXPECT_DEATH(helper_const(return_pointer()), deathstring);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
|
||||
void ostream_helper(T v)
|
||||
{
|
||||
not_null<T*> p(&v);
|
||||
@@ -236,18 +228,18 @@ void ostream_helper(T v)
|
||||
std::ostringstream ref;
|
||||
os << static_cast<void*>(p);
|
||||
ref << static_cast<void*>(&v);
|
||||
CHECK(os.str() == ref.str());
|
||||
EXPECT_TRUE(os.str() == ref.str());
|
||||
}
|
||||
{
|
||||
std::ostringstream os;
|
||||
std::ostringstream ref;
|
||||
os << *p;
|
||||
ref << v;
|
||||
CHECK(os.str() == ref.str());
|
||||
EXPECT_TRUE(os.str() == ref.str());
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("TestNotNullostream")
|
||||
TEST(notnull_tests, TestNotNullostream)
|
||||
{
|
||||
ostream_helper<int>(17);
|
||||
ostream_helper<float>(21.5f);
|
||||
@@ -258,9 +250,7 @@ TEST_CASE("TestNotNullostream")
|
||||
ostream_helper<std::string>("string");
|
||||
}
|
||||
|
||||
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
|
||||
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
|
||||
TEST_CASE("TestNotNullCasting")
|
||||
TEST(notnull_tests, TestNotNullCasting)
|
||||
{
|
||||
MyBase base;
|
||||
MyDerived derived;
|
||||
@@ -270,7 +260,7 @@ TEST_CASE("TestNotNullCasting")
|
||||
not_null<MyDerived*> p{&derived};
|
||||
not_null<MyBase*> q(&base);
|
||||
q = p; // allowed with heterogeneous copy ctor
|
||||
CHECK(q == p);
|
||||
EXPECT_TRUE(q == p);
|
||||
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
q = u; // no viable conversion possible between MyBase* and Unrelated*
|
||||
@@ -280,20 +270,25 @@ TEST_CASE("TestNotNullCasting")
|
||||
not_null<Unrelated*> s = reinterpret_cast<Unrelated*>(p);
|
||||
#endif
|
||||
not_null<Unrelated*> t(reinterpret_cast<Unrelated*>(p.get()));
|
||||
CHECK(reinterpret_cast<void*>(p.get()) == reinterpret_cast<void*>(t.get()));
|
||||
EXPECT_TRUE(reinterpret_cast<void*>(p.get()) == reinterpret_cast<void*>(t.get()));
|
||||
}
|
||||
|
||||
TEST_CASE("TestNotNullAssignment")
|
||||
TEST(notnull_tests, TestNotNullAssignment)
|
||||
{
|
||||
std::set_terminate([] {
|
||||
std::cerr << "Expected Death. TestNotNullAssignmentd";
|
||||
std::abort();
|
||||
});
|
||||
|
||||
int i = 12;
|
||||
not_null<int*> p(&i);
|
||||
CHECK(helper(p));
|
||||
EXPECT_TRUE(helper(p));
|
||||
|
||||
int* q = nullptr;
|
||||
CHECK_THROWS_AS(p = not_null<int*>(q), fail_fast);
|
||||
EXPECT_DEATH(p = not_null<int*>(q), deathstring);
|
||||
}
|
||||
|
||||
TEST_CASE("TestNotNullRawPointerComparison")
|
||||
TEST(notnull_tests, TestNotNullRawPointerComparison)
|
||||
{
|
||||
int ints[2] = {42, 43};
|
||||
int* p1 = &ints[0];
|
||||
@@ -302,34 +297,33 @@ TEST_CASE("TestNotNullRawPointerComparison")
|
||||
using NotNull1 = not_null<decltype(p1)>;
|
||||
using NotNull2 = not_null<decltype(p2)>;
|
||||
|
||||
CHECK((NotNull1(p1) == NotNull1(p1)) == true);
|
||||
CHECK((NotNull1(p1) == NotNull2(p2)) == false);
|
||||
EXPECT_TRUE((NotNull1(p1) == NotNull1(p1)) == true);
|
||||
EXPECT_TRUE((NotNull1(p1) == NotNull2(p2)) == false);
|
||||
|
||||
CHECK((NotNull1(p1) != NotNull1(p1)) == false);
|
||||
CHECK((NotNull1(p1) != NotNull2(p2)) == true);
|
||||
EXPECT_TRUE((NotNull1(p1) != NotNull1(p1)) == false);
|
||||
EXPECT_TRUE((NotNull1(p1) != NotNull2(p2)) == true);
|
||||
|
||||
CHECK((NotNull1(p1) < NotNull1(p1)) == false);
|
||||
CHECK((NotNull1(p1) < NotNull2(p2)) == (p1 < p2));
|
||||
CHECK((NotNull2(p2) < NotNull1(p1)) == (p2 < p1));
|
||||
EXPECT_TRUE((NotNull1(p1) < NotNull1(p1)) == false);
|
||||
EXPECT_TRUE((NotNull1(p1) < NotNull2(p2)) == (p1 < p2));
|
||||
EXPECT_TRUE((NotNull2(p2) < NotNull1(p1)) == (p2 < p1));
|
||||
|
||||
CHECK((NotNull1(p1) > NotNull1(p1)) == false);
|
||||
CHECK((NotNull1(p1) > NotNull2(p2)) == (p1 > p2));
|
||||
CHECK((NotNull2(p2) > NotNull1(p1)) == (p2 > p1));
|
||||
EXPECT_TRUE((NotNull1(p1) > NotNull1(p1)) == false);
|
||||
EXPECT_TRUE((NotNull1(p1) > NotNull2(p2)) == (p1 > p2));
|
||||
EXPECT_TRUE((NotNull2(p2) > NotNull1(p1)) == (p2 > p1));
|
||||
|
||||
CHECK((NotNull1(p1) <= NotNull1(p1)) == true);
|
||||
CHECK((NotNull1(p1) <= NotNull2(p2)) == (p1 <= p2));
|
||||
CHECK((NotNull2(p2) <= NotNull1(p1)) == (p2 <= p1));
|
||||
EXPECT_TRUE((NotNull1(p1) <= NotNull1(p1)) == true);
|
||||
EXPECT_TRUE((NotNull1(p1) <= NotNull2(p2)) == (p1 <= p2));
|
||||
EXPECT_TRUE((NotNull2(p2) <= NotNull1(p1)) == (p2 <= p1));
|
||||
}
|
||||
|
||||
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
|
||||
TEST_CASE("TestNotNullDereferenceOperator")
|
||||
TEST(notnull_tests, TestNotNullDereferenceOperator)
|
||||
{
|
||||
{
|
||||
auto sp1 = std::make_shared<NonCopyableNonMovable>();
|
||||
|
||||
using NotNullSp1 = not_null<decltype(sp1)>;
|
||||
CHECK(typeid(*sp1) == typeid(*NotNullSp1(sp1)));
|
||||
CHECK(std::addressof(*NotNullSp1(sp1)) == std::addressof(*sp1));
|
||||
EXPECT_TRUE(typeid(*sp1) == typeid(*NotNullSp1(sp1)));
|
||||
EXPECT_TRUE(std::addressof(*NotNullSp1(sp1)) == std::addressof(*sp1));
|
||||
}
|
||||
|
||||
{
|
||||
@@ -337,22 +331,22 @@ TEST_CASE("TestNotNullDereferenceOperator")
|
||||
CustomPtr<int> p1(&ints[0]);
|
||||
|
||||
using NotNull1 = not_null<decltype(p1)>;
|
||||
CHECK(typeid(*NotNull1(p1)) == typeid(*p1));
|
||||
CHECK(*NotNull1(p1) == 42);
|
||||
EXPECT_TRUE(typeid(*NotNull1(p1)) == typeid(*p1));
|
||||
EXPECT_TRUE(*NotNull1(p1) == 42);
|
||||
*NotNull1(p1) = 43;
|
||||
CHECK(ints[0] == 43);
|
||||
EXPECT_TRUE(ints[0] == 43);
|
||||
}
|
||||
|
||||
{
|
||||
int v = 42;
|
||||
gsl::not_null<int*> p(&v);
|
||||
CHECK(typeid(*p) == typeid(*(&v)));
|
||||
EXPECT_TRUE(typeid(*p) == typeid(*(&v)));
|
||||
*p = 43;
|
||||
CHECK(v == 43);
|
||||
EXPECT_TRUE(v == 43);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("TestNotNullSharedPtrComparison")
|
||||
TEST(notnull_tests, TestNotNullSharedPtrComparison)
|
||||
{
|
||||
auto sp1 = std::make_shared<int>(42);
|
||||
auto sp2 = std::make_shared<const int>(43);
|
||||
@@ -360,31 +354,30 @@ TEST_CASE("TestNotNullSharedPtrComparison")
|
||||
using NotNullSp1 = not_null<decltype(sp1)>;
|
||||
using NotNullSp2 = not_null<decltype(sp2)>;
|
||||
|
||||
CHECK((NotNullSp1(sp1) == NotNullSp1(sp1)) == true);
|
||||
CHECK((NotNullSp1(sp1) == NotNullSp2(sp2)) == false);
|
||||
EXPECT_TRUE((NotNullSp1(sp1) == NotNullSp1(sp1)) == true);
|
||||
EXPECT_TRUE((NotNullSp1(sp1) == NotNullSp2(sp2)) == false);
|
||||
|
||||
CHECK((NotNullSp1(sp1) != NotNullSp1(sp1)) == false);
|
||||
CHECK((NotNullSp1(sp1) != NotNullSp2(sp2)) == true);
|
||||
EXPECT_TRUE((NotNullSp1(sp1) != NotNullSp1(sp1)) == false);
|
||||
EXPECT_TRUE((NotNullSp1(sp1) != NotNullSp2(sp2)) == true);
|
||||
|
||||
CHECK((NotNullSp1(sp1) < NotNullSp1(sp1)) == false);
|
||||
CHECK((NotNullSp1(sp1) < NotNullSp2(sp2)) == (sp1 < sp2));
|
||||
CHECK((NotNullSp2(sp2) < NotNullSp1(sp1)) == (sp2 < sp1));
|
||||
EXPECT_TRUE((NotNullSp1(sp1) < NotNullSp1(sp1)) == false);
|
||||
EXPECT_TRUE((NotNullSp1(sp1) < NotNullSp2(sp2)) == (sp1 < sp2));
|
||||
EXPECT_TRUE((NotNullSp2(sp2) < NotNullSp1(sp1)) == (sp2 < sp1));
|
||||
|
||||
CHECK((NotNullSp1(sp1) > NotNullSp1(sp1)) == false);
|
||||
CHECK((NotNullSp1(sp1) > NotNullSp2(sp2)) == (sp1 > sp2));
|
||||
CHECK((NotNullSp2(sp2) > NotNullSp1(sp1)) == (sp2 > sp1));
|
||||
EXPECT_TRUE((NotNullSp1(sp1) > NotNullSp1(sp1)) == false);
|
||||
EXPECT_TRUE((NotNullSp1(sp1) > NotNullSp2(sp2)) == (sp1 > sp2));
|
||||
EXPECT_TRUE((NotNullSp2(sp2) > NotNullSp1(sp1)) == (sp2 > sp1));
|
||||
|
||||
CHECK((NotNullSp1(sp1) <= NotNullSp1(sp1)) == true);
|
||||
CHECK((NotNullSp1(sp1) <= NotNullSp2(sp2)) == (sp1 <= sp2));
|
||||
CHECK((NotNullSp2(sp2) <= NotNullSp1(sp1)) == (sp2 <= sp1));
|
||||
EXPECT_TRUE((NotNullSp1(sp1) <= NotNullSp1(sp1)) == true);
|
||||
EXPECT_TRUE((NotNullSp1(sp1) <= NotNullSp2(sp2)) == (sp1 <= sp2));
|
||||
EXPECT_TRUE((NotNullSp2(sp2) <= NotNullSp1(sp1)) == (sp2 <= sp1));
|
||||
|
||||
CHECK((NotNullSp1(sp1) >= NotNullSp1(sp1)) == true);
|
||||
CHECK((NotNullSp1(sp1) >= NotNullSp2(sp2)) == (sp1 >= sp2));
|
||||
CHECK((NotNullSp2(sp2) >= NotNullSp1(sp1)) == (sp2 >= sp1));
|
||||
EXPECT_TRUE((NotNullSp1(sp1) >= NotNullSp1(sp1)) == true);
|
||||
EXPECT_TRUE((NotNullSp1(sp1) >= NotNullSp2(sp2)) == (sp1 >= sp2));
|
||||
EXPECT_TRUE((NotNullSp2(sp2) >= NotNullSp1(sp1)) == (sp2 >= sp1));
|
||||
}
|
||||
|
||||
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
|
||||
TEST_CASE("TestNotNullCustomPtrComparison")
|
||||
TEST(notnull_tests, TestNotNullCustomPtrComparison)
|
||||
{
|
||||
int ints[2] = {42, 43};
|
||||
CustomPtr<int> p1(&ints[0]);
|
||||
@@ -393,33 +386,32 @@ TEST_CASE("TestNotNullCustomPtrComparison")
|
||||
using NotNull1 = not_null<decltype(p1)>;
|
||||
using NotNull2 = not_null<decltype(p2)>;
|
||||
|
||||
CHECK((NotNull1(p1) == NotNull1(p1)) == "true");
|
||||
CHECK((NotNull1(p1) == NotNull2(p2)) == "false");
|
||||
EXPECT_TRUE((NotNull1(p1) == NotNull1(p1)) == "true");
|
||||
EXPECT_TRUE((NotNull1(p1) == NotNull2(p2)) == "false");
|
||||
|
||||
CHECK((NotNull1(p1) != NotNull1(p1)) == "false");
|
||||
CHECK((NotNull1(p1) != NotNull2(p2)) == "true");
|
||||
EXPECT_TRUE((NotNull1(p1) != NotNull1(p1)) == "false");
|
||||
EXPECT_TRUE((NotNull1(p1) != NotNull2(p2)) == "true");
|
||||
|
||||
CHECK((NotNull1(p1) < NotNull1(p1)) == "false");
|
||||
CHECK((NotNull1(p1) < NotNull2(p2)) == (p1 < p2));
|
||||
CHECK((NotNull2(p2) < NotNull1(p1)) == (p2 < p1));
|
||||
EXPECT_TRUE((NotNull1(p1) < NotNull1(p1)) == "false");
|
||||
EXPECT_TRUE((NotNull1(p1) < NotNull2(p2)) == (p1 < p2));
|
||||
EXPECT_TRUE((NotNull2(p2) < NotNull1(p1)) == (p2 < p1));
|
||||
|
||||
CHECK((NotNull1(p1) > NotNull1(p1)) == "false");
|
||||
CHECK((NotNull1(p1) > NotNull2(p2)) == (p1 > p2));
|
||||
CHECK((NotNull2(p2) > NotNull1(p1)) == (p2 > p1));
|
||||
EXPECT_TRUE((NotNull1(p1) > NotNull1(p1)) == "false");
|
||||
EXPECT_TRUE((NotNull1(p1) > NotNull2(p2)) == (p1 > p2));
|
||||
EXPECT_TRUE((NotNull2(p2) > NotNull1(p1)) == (p2 > p1));
|
||||
|
||||
CHECK((NotNull1(p1) <= NotNull1(p1)) == "true");
|
||||
CHECK((NotNull1(p1) <= NotNull2(p2)) == (p1 <= p2));
|
||||
CHECK((NotNull2(p2) <= NotNull1(p1)) == (p2 <= p1));
|
||||
EXPECT_TRUE((NotNull1(p1) <= NotNull1(p1)) == "true");
|
||||
EXPECT_TRUE((NotNull1(p1) <= NotNull2(p2)) == (p1 <= p2));
|
||||
EXPECT_TRUE((NotNull2(p2) <= NotNull1(p1)) == (p2 <= p1));
|
||||
|
||||
CHECK((NotNull1(p1) >= NotNull1(p1)) == "true");
|
||||
CHECK((NotNull1(p1) >= NotNull2(p2)) == (p1 >= p2));
|
||||
CHECK((NotNull2(p2) >= NotNull1(p1)) == (p2 >= p1));
|
||||
EXPECT_TRUE((NotNull1(p1) >= NotNull1(p1)) == "true");
|
||||
EXPECT_TRUE((NotNull1(p1) >= NotNull2(p2)) == (p1 >= p2));
|
||||
EXPECT_TRUE((NotNull2(p2) >= NotNull1(p1)) == (p2 >= p1));
|
||||
}
|
||||
|
||||
#if defined(__cplusplus) && (__cplusplus >= 201703L)
|
||||
|
||||
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
|
||||
TEST_CASE("TestNotNullConstructorTypeDeduction")
|
||||
TEST(notnull_tests, TestNotNullConstructorTypeDeduction)
|
||||
{
|
||||
{
|
||||
int i = 42;
|
||||
@@ -428,7 +420,7 @@ TEST_CASE("TestNotNullConstructorTypeDeduction")
|
||||
helper(not_null{&i});
|
||||
helper_const(not_null{&i});
|
||||
|
||||
CHECK(*x == 42);
|
||||
EXPECT_TRUE(*x == 42);
|
||||
}
|
||||
|
||||
{
|
||||
@@ -439,15 +431,20 @@ TEST_CASE("TestNotNullConstructorTypeDeduction")
|
||||
helper(not_null{p});
|
||||
helper_const(not_null{p});
|
||||
|
||||
CHECK(*x == 42);
|
||||
EXPECT_TRUE(*x == 42);
|
||||
}
|
||||
|
||||
std::set_terminate([] {
|
||||
std::cerr << "Expected Death. TestNotNullConstructorTypeDeduction";
|
||||
std::abort();
|
||||
});
|
||||
|
||||
{
|
||||
auto workaround_macro = []() {
|
||||
int* p1 = nullptr;
|
||||
const not_null x{p1};
|
||||
};
|
||||
CHECK_THROWS_AS(workaround_macro(), fail_fast);
|
||||
EXPECT_DEATH(workaround_macro(), deathstring);
|
||||
}
|
||||
|
||||
{
|
||||
@@ -455,14 +452,14 @@ TEST_CASE("TestNotNullConstructorTypeDeduction")
|
||||
const int* p1 = nullptr;
|
||||
const not_null x{p1};
|
||||
};
|
||||
CHECK_THROWS_AS(workaround_macro(), fail_fast);
|
||||
EXPECT_DEATH(workaround_macro(), deathstring);
|
||||
}
|
||||
|
||||
{
|
||||
int* p = nullptr;
|
||||
|
||||
CHECK_THROWS_AS(helper(not_null{p}), fail_fast);
|
||||
CHECK_THROWS_AS(helper_const(not_null{p}), fail_fast);
|
||||
EXPECT_DEATH(helper(not_null{p}), deathstring);
|
||||
EXPECT_DEATH(helper_const(not_null{p}), deathstring);
|
||||
}
|
||||
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
@@ -475,7 +472,7 @@ TEST_CASE("TestNotNullConstructorTypeDeduction")
|
||||
}
|
||||
#endif // #if defined(__cplusplus) && (__cplusplus >= 201703L)
|
||||
|
||||
TEST_CASE("TestMakeNotNull")
|
||||
TEST(notnull_tests, TestMakeNotNull)
|
||||
{
|
||||
{
|
||||
int i = 42;
|
||||
@@ -484,7 +481,7 @@ TEST_CASE("TestMakeNotNull")
|
||||
helper(make_not_null(&i));
|
||||
helper_const(make_not_null(&i));
|
||||
|
||||
CHECK(*x == 42);
|
||||
EXPECT_TRUE(*x == 42);
|
||||
}
|
||||
|
||||
{
|
||||
@@ -495,42 +492,59 @@ TEST_CASE("TestMakeNotNull")
|
||||
helper(make_not_null(p));
|
||||
helper_const(make_not_null(p));
|
||||
|
||||
CHECK(*x == 42);
|
||||
EXPECT_TRUE(*x == 42);
|
||||
}
|
||||
|
||||
std::set_terminate([] {
|
||||
std::cerr << "Expected Death. TestMakeNotNull";
|
||||
std::abort();
|
||||
});
|
||||
|
||||
{
|
||||
const auto workaround_macro = []() {
|
||||
int* p1 = nullptr;
|
||||
const auto x = make_not_null(p1);
|
||||
CHECK(*x == 42);
|
||||
EXPECT_TRUE(*x == 42);
|
||||
};
|
||||
CHECK_THROWS_AS(workaround_macro(), fail_fast);
|
||||
EXPECT_DEATH(workaround_macro(), deathstring);
|
||||
}
|
||||
|
||||
{
|
||||
const auto workaround_macro = []() {
|
||||
const int* p1 = nullptr;
|
||||
const auto x = make_not_null(p1);
|
||||
CHECK(*x == 42);
|
||||
EXPECT_TRUE(*x == 42);
|
||||
};
|
||||
CHECK_THROWS_AS(workaround_macro(), fail_fast);
|
||||
EXPECT_DEATH(workaround_macro(), deathstring);
|
||||
}
|
||||
|
||||
{
|
||||
int* p = nullptr;
|
||||
|
||||
CHECK_THROWS_AS(helper(make_not_null(p)), fail_fast);
|
||||
CHECK_THROWS_AS(helper_const(make_not_null(p)), fail_fast);
|
||||
EXPECT_DEATH(helper(make_not_null(p)), deathstring);
|
||||
EXPECT_DEATH(helper_const(make_not_null(p)), deathstring);
|
||||
}
|
||||
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
{
|
||||
CHECK_THROWS_AS(make_not_null(nullptr), fail_fast);
|
||||
CHECK_THROWS_AS(helper(make_not_null(nullptr)), fail_fast);
|
||||
CHECK_THROWS_AS(helper_const(make_not_null(nullptr)), fail_fast);
|
||||
EXPECT_DEATH(make_not_null(nullptr), deathstring);
|
||||
EXPECT_DEATH(helper(make_not_null(nullptr)), deathstring);
|
||||
EXPECT_DEATH(helper_const(make_not_null(nullptr)), deathstring);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
static_assert(std::is_nothrow_move_constructible<not_null<void*>>::value,
|
||||
"not_null must be no-throw move constructible");
|
||||
TEST(notnull_tests, TestStdHash)
|
||||
{
|
||||
int x = 42;
|
||||
int y = 99;
|
||||
not_null<int*> nn{&x};
|
||||
const not_null<int*> cnn{&x};
|
||||
|
||||
std::hash<not_null<int*>> hash_nn;
|
||||
std::hash<int*> hash_intptr;
|
||||
|
||||
EXPECT_TRUE(hash_nn(nn) == hash_intptr(&x));
|
||||
EXPECT_FALSE(hash_nn(nn) == hash_intptr(&y));
|
||||
EXPECT_FALSE(hash_nn(nn) == hash_intptr(nullptr));
|
||||
}
|
||||
|
||||
+5
-15
@@ -14,14 +14,7 @@
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifdef _MSC_VER
|
||||
// blanket turn off warnings from CppCoreCheck from catch
|
||||
// so people aren't annoyed by them when running the tool.
|
||||
#pragma warning(disable : 26440 26426) // from catch
|
||||
|
||||
#endif
|
||||
|
||||
#include <catch/catch.hpp> // for AssertionHandler, StringRef, CHECK, TEST_...
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <gsl/pointers> // for owner
|
||||
|
||||
@@ -30,19 +23,16 @@ using namespace gsl;
|
||||
GSL_SUPPRESS(f.23) // NO-FORMAT: attribute
|
||||
void f(int* i) { *i += 1; }
|
||||
|
||||
GSL_SUPPRESS(r.11) // NO-FORMAT: attribute
|
||||
GSL_SUPPRESS(r.3) // NO-FORMAT: attribute // TODO: false positive
|
||||
GSL_SUPPRESS(r.5) // NO-FORMAT: attribute
|
||||
TEST_CASE("basic_test")
|
||||
TEST(owner_tests, basic_test)
|
||||
{
|
||||
owner<int*> p = new int(120);
|
||||
CHECK(*p == 120);
|
||||
EXPECT_TRUE(*p == 120);
|
||||
f(p);
|
||||
CHECK(*p == 121);
|
||||
EXPECT_TRUE(*p == 121);
|
||||
delete p;
|
||||
}
|
||||
|
||||
TEST_CASE("check_pointer_constraint")
|
||||
TEST(owner_tests, check_pointer_constraint)
|
||||
{
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
{
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,360 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
|
||||
//
|
||||
// This code is licensed under the MIT License (MIT).
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <gsl/util> // for narrow_cast, at
|
||||
#include <gsl/span_ext> // for operator==, operator!=, make_span
|
||||
|
||||
#include <array> // for array
|
||||
#include <iostream> // for cerr
|
||||
#include <vector> // for vector
|
||||
|
||||
using namespace std;
|
||||
using namespace gsl;
|
||||
|
||||
namespace
|
||||
{
|
||||
static constexpr char deathstring[] = "Expected Death";
|
||||
} // namespace
|
||||
|
||||
TEST(span_ext_test, make_span_from_pointer_length_constructor)
|
||||
{
|
||||
std::set_terminate([] {
|
||||
std::cerr << "Expected Death. from_pointer_length_constructor";
|
||||
std::abort();
|
||||
});
|
||||
int arr[4] = {1, 2, 3, 4};
|
||||
|
||||
{
|
||||
auto s = make_span(&arr[0], 2);
|
||||
EXPECT_TRUE(s.size() == 2);
|
||||
EXPECT_TRUE(s.data() == &arr[0]);
|
||||
EXPECT_TRUE(s[0] == 1);
|
||||
EXPECT_TRUE(s[1] == 2);
|
||||
}
|
||||
|
||||
{
|
||||
int* p = nullptr;
|
||||
auto s = make_span(p, narrow_cast<span<int>::size_type>(0));
|
||||
EXPECT_TRUE(s.size() == 0);
|
||||
EXPECT_TRUE(s.data() == nullptr);
|
||||
}
|
||||
|
||||
{
|
||||
int* p = nullptr;
|
||||
auto workaround_macro = [=]() { make_span(p, 2); };
|
||||
EXPECT_DEATH(workaround_macro(), deathstring);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(span_ext_test, make_span_from_pointer_pointer_construction)
|
||||
{
|
||||
int arr[4] = {1, 2, 3, 4};
|
||||
|
||||
{
|
||||
auto s = make_span(&arr[0], &arr[2]);
|
||||
EXPECT_TRUE(s.size() == 2);
|
||||
EXPECT_TRUE(s.data() == &arr[0]);
|
||||
EXPECT_TRUE(s[0] == 1);
|
||||
EXPECT_TRUE(s[1] == 2);
|
||||
}
|
||||
|
||||
{
|
||||
auto s = make_span(&arr[0], &arr[0]);
|
||||
EXPECT_TRUE(s.size() == 0);
|
||||
EXPECT_TRUE(s.data() == &arr[0]);
|
||||
}
|
||||
|
||||
{
|
||||
int* p = nullptr;
|
||||
auto s = make_span(p, p);
|
||||
EXPECT_TRUE(s.size() == 0);
|
||||
EXPECT_TRUE(s.data() == nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(span_ext_test, make_span_from_array_constructor)
|
||||
{
|
||||
int arr[5] = {1, 2, 3, 4, 5};
|
||||
int arr2d[2][3] = {1, 2, 3, 4, 5, 6};
|
||||
int arr3d[2][3][2] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
|
||||
|
||||
{
|
||||
const auto s = make_span(arr);
|
||||
EXPECT_TRUE(s.size() == 5);
|
||||
EXPECT_TRUE(s.data() == std::addressof(arr[0]));
|
||||
}
|
||||
|
||||
{
|
||||
const auto s = make_span(std::addressof(arr2d[0]), 1);
|
||||
EXPECT_TRUE(s.size() == 1);
|
||||
EXPECT_TRUE(s.data() == std::addressof(arr2d[0]));
|
||||
}
|
||||
|
||||
{
|
||||
const auto s = make_span(std::addressof(arr3d[0]), 1);
|
||||
EXPECT_TRUE(s.size() == 1);
|
||||
EXPECT_TRUE(s.data() == std::addressof(arr3d[0]));
|
||||
}
|
||||
}
|
||||
|
||||
TEST(span_ext_test, make_span_from_dynamic_array_constructor)
|
||||
{
|
||||
double(*arr)[3][4] = new double[100][3][4];
|
||||
|
||||
{
|
||||
auto s = make_span(&arr[0][0][0], 10);
|
||||
EXPECT_TRUE(s.size() == 10);
|
||||
EXPECT_TRUE(s.data() == &arr[0][0][0]);
|
||||
}
|
||||
|
||||
delete[] arr;
|
||||
}
|
||||
|
||||
TEST(span_ext_test, make_span_from_std_array_constructor)
|
||||
{
|
||||
std::array<int, 4> arr = {1, 2, 3, 4};
|
||||
|
||||
{
|
||||
auto s = make_span(arr);
|
||||
EXPECT_TRUE(s.size() == arr.size());
|
||||
EXPECT_TRUE(s.data() == arr.data());
|
||||
}
|
||||
|
||||
// This test checks for the bug found in gcc 6.1, 6.2, 6.3, 6.4, 6.5 7.1, 7.2, 7.3 - issue #590
|
||||
{
|
||||
span<int> s1 = make_span(arr);
|
||||
|
||||
static span<int> s2;
|
||||
s2 = s1;
|
||||
|
||||
#if defined(__GNUC__) && __GNUC__ == 6 && (__GNUC_MINOR__ == 4 || __GNUC_MINOR__ == 5) && \
|
||||
__GNUC_PATCHLEVEL__ == 0 && defined(__OPTIMIZE__)
|
||||
// Known to be broken in gcc 6.4 and 6.5 with optimizations
|
||||
// Issue in gcc: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83116
|
||||
EXPECT_TRUE(s1.size() == 4);
|
||||
EXPECT_TRUE(s2.size() == 0);
|
||||
#else
|
||||
EXPECT_TRUE(s1.size() == s2.size());
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
TEST(span_ext_test, make_span_from_const_std_array_constructor)
|
||||
{
|
||||
const std::array<int, 4> arr = {1, 2, 3, 4};
|
||||
|
||||
{
|
||||
auto s = make_span(arr);
|
||||
EXPECT_TRUE(s.size() == arr.size());
|
||||
EXPECT_TRUE(s.data() == arr.data());
|
||||
}
|
||||
}
|
||||
|
||||
TEST(span_ext_test, make_span_from_std_array_const_constructor)
|
||||
{
|
||||
std::array<const int, 4> arr = {1, 2, 3, 4};
|
||||
|
||||
{
|
||||
auto s = make_span(arr);
|
||||
EXPECT_TRUE(s.size() == arr.size());
|
||||
EXPECT_TRUE(s.data() == arr.data());
|
||||
}
|
||||
}
|
||||
|
||||
TEST(span_ext_test, make_span_from_container_constructor)
|
||||
{
|
||||
std::vector<int> v = {1, 2, 3};
|
||||
const std::vector<int> cv = v;
|
||||
|
||||
{
|
||||
auto s = make_span(v);
|
||||
EXPECT_TRUE(s.size() == v.size());
|
||||
EXPECT_TRUE(s.data() == v.data());
|
||||
|
||||
auto cs = make_span(cv);
|
||||
EXPECT_TRUE(cs.size() == cv.size());
|
||||
EXPECT_TRUE(cs.data() == cv.data());
|
||||
}
|
||||
}
|
||||
|
||||
TEST(span_test, interop_with_gsl_at)
|
||||
{
|
||||
int arr[5] = {1, 2, 3, 4, 5};
|
||||
span<int> s{arr};
|
||||
EXPECT_TRUE(at(s, 0) == 1);
|
||||
EXPECT_TRUE(at(s, 1) == 2);
|
||||
}
|
||||
|
||||
TEST(span_ext_test, iterator_free_functions)
|
||||
{
|
||||
int a[] = {1, 2, 3, 4};
|
||||
span<int> s{a};
|
||||
|
||||
EXPECT_TRUE((std::is_same<decltype(s.begin()), decltype(begin(s))>::value));
|
||||
EXPECT_TRUE((std::is_same<decltype(s.end()), decltype(end(s))>::value));
|
||||
|
||||
EXPECT_TRUE((std::is_same<decltype(std::cbegin(s)), decltype(cbegin(s))>::value));
|
||||
EXPECT_TRUE((std::is_same<decltype(std::cend(s)), decltype(cend(s))>::value));
|
||||
|
||||
EXPECT_TRUE((std::is_same<decltype(s.rbegin()), decltype(rbegin(s))>::value));
|
||||
EXPECT_TRUE((std::is_same<decltype(s.rend()), decltype(rend(s))>::value));
|
||||
|
||||
EXPECT_TRUE((std::is_same<decltype(std::crbegin(s)), decltype(crbegin(s))>::value));
|
||||
EXPECT_TRUE((std::is_same<decltype(std::crend(s)), decltype(crend(s))>::value));
|
||||
|
||||
EXPECT_TRUE(s.begin() == begin(s));
|
||||
EXPECT_TRUE(s.end() == end(s));
|
||||
|
||||
EXPECT_TRUE(s.rbegin() == rbegin(s));
|
||||
EXPECT_TRUE(s.rend() == rend(s));
|
||||
|
||||
EXPECT_TRUE(s.begin() == cbegin(s));
|
||||
EXPECT_TRUE(s.end() == cend(s));
|
||||
|
||||
EXPECT_TRUE(s.rbegin() == crbegin(s));
|
||||
EXPECT_TRUE(s.rend() == crend(s));
|
||||
}
|
||||
|
||||
TEST(span_ext_test, ssize_free_function)
|
||||
{
|
||||
int a[] = {1, 2, 3, 4};
|
||||
span<int> s{a};
|
||||
|
||||
EXPECT_FALSE((std::is_same<decltype(s.size()), decltype(ssize(s))>::value));
|
||||
EXPECT_TRUE(s.size() == static_cast<std::size_t>(ssize(s)));
|
||||
}
|
||||
|
||||
TEST(span_ext_test, comparison_operators)
|
||||
{
|
||||
{
|
||||
span<int> s1;
|
||||
span<int> s2;
|
||||
EXPECT_TRUE(s1 == s2);
|
||||
EXPECT_FALSE(s1 != s2);
|
||||
EXPECT_FALSE(s1 < s2);
|
||||
EXPECT_TRUE(s1 <= s2);
|
||||
EXPECT_FALSE(s1 > s2);
|
||||
EXPECT_TRUE(s1 >= s2);
|
||||
EXPECT_TRUE(s2 == s1);
|
||||
EXPECT_FALSE(s2 != s1);
|
||||
EXPECT_FALSE(s2 != s1);
|
||||
EXPECT_TRUE(s2 <= s1);
|
||||
EXPECT_FALSE(s2 > s1);
|
||||
EXPECT_TRUE(s2 >= s1);
|
||||
}
|
||||
|
||||
{
|
||||
int arr[] = {2, 1};
|
||||
span<int> s1 = arr;
|
||||
span<int> s2 = arr;
|
||||
|
||||
EXPECT_TRUE(s1 == s2);
|
||||
EXPECT_FALSE(s1 != s2);
|
||||
EXPECT_FALSE(s1 < s2);
|
||||
EXPECT_TRUE(s1 <= s2);
|
||||
EXPECT_FALSE(s1 > s2);
|
||||
EXPECT_TRUE(s1 >= s2);
|
||||
EXPECT_TRUE(s2 == s1);
|
||||
EXPECT_FALSE(s2 != s1);
|
||||
EXPECT_FALSE(s2 < s1);
|
||||
EXPECT_TRUE(s2 <= s1);
|
||||
EXPECT_FALSE(s2 > s1);
|
||||
EXPECT_TRUE(s2 >= s1);
|
||||
}
|
||||
|
||||
{
|
||||
int arr[] = {2, 1}; // bigger
|
||||
|
||||
span<int> s1;
|
||||
span<int> s2 = arr;
|
||||
|
||||
EXPECT_TRUE(s1 != s2);
|
||||
EXPECT_TRUE(s2 != s1);
|
||||
EXPECT_FALSE(s1 == s2);
|
||||
EXPECT_FALSE(s2 == s1);
|
||||
EXPECT_TRUE(s1 < s2);
|
||||
EXPECT_FALSE(s2 < s1);
|
||||
EXPECT_TRUE(s1 <= s2);
|
||||
EXPECT_FALSE(s2 <= s1);
|
||||
EXPECT_TRUE(s2 > s1);
|
||||
EXPECT_FALSE(s1 > s2);
|
||||
EXPECT_TRUE(s2 >= s1);
|
||||
EXPECT_FALSE(s1 >= s2);
|
||||
}
|
||||
|
||||
{
|
||||
int arr1[] = {1, 2};
|
||||
int arr2[] = {1, 2};
|
||||
span<int> s1 = arr1;
|
||||
span<int> s2 = arr2;
|
||||
|
||||
EXPECT_TRUE(s1 == s2);
|
||||
EXPECT_FALSE(s1 != s2);
|
||||
EXPECT_FALSE(s1 < s2);
|
||||
EXPECT_TRUE(s1 <= s2);
|
||||
EXPECT_FALSE(s1 > s2);
|
||||
EXPECT_TRUE(s1 >= s2);
|
||||
EXPECT_TRUE(s2 == s1);
|
||||
EXPECT_FALSE(s2 != s1);
|
||||
EXPECT_FALSE(s2 < s1);
|
||||
EXPECT_TRUE(s2 <= s1);
|
||||
EXPECT_FALSE(s2 > s1);
|
||||
EXPECT_TRUE(s2 >= s1);
|
||||
}
|
||||
|
||||
{
|
||||
int arr[] = {1, 2, 3};
|
||||
|
||||
span<int> s1 = {&arr[0], 2}; // shorter
|
||||
span<int> s2 = arr; // longer
|
||||
|
||||
EXPECT_TRUE(s1 != s2);
|
||||
EXPECT_TRUE(s2 != s1);
|
||||
EXPECT_FALSE(s1 == s2);
|
||||
EXPECT_FALSE(s2 == s1);
|
||||
EXPECT_TRUE(s1 < s2);
|
||||
EXPECT_FALSE(s2 < s1);
|
||||
EXPECT_TRUE(s1 <= s2);
|
||||
EXPECT_FALSE(s2 <= s1);
|
||||
EXPECT_TRUE(s2 > s1);
|
||||
EXPECT_FALSE(s1 > s2);
|
||||
EXPECT_TRUE(s2 >= s1);
|
||||
EXPECT_FALSE(s1 >= s2);
|
||||
}
|
||||
|
||||
{
|
||||
int arr1[] = {1, 2}; // smaller
|
||||
int arr2[] = {2, 1}; // bigger
|
||||
|
||||
span<int> s1 = arr1;
|
||||
span<int> s2 = arr2;
|
||||
|
||||
EXPECT_TRUE(s1 != s2);
|
||||
EXPECT_TRUE(s2 != s1);
|
||||
EXPECT_FALSE(s1 == s2);
|
||||
EXPECT_FALSE(s2 == s1);
|
||||
EXPECT_TRUE(s1 < s2);
|
||||
EXPECT_FALSE(s2 < s1);
|
||||
EXPECT_TRUE(s1 <= s2);
|
||||
EXPECT_FALSE(s2 <= s1);
|
||||
EXPECT_TRUE(s2 > s1);
|
||||
EXPECT_FALSE(s1 > s2);
|
||||
EXPECT_TRUE(s2 >= s1);
|
||||
EXPECT_FALSE(s1 >= s2);
|
||||
}
|
||||
}
|
||||
+1091
-1377
File diff suppressed because it is too large
Load Diff
@@ -14,16 +14,7 @@
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifdef _MSC_VER
|
||||
// blanket turn off warnings from CppCoreCheck from catch
|
||||
// so people aren't annoyed by them when running the tool.
|
||||
#pragma warning(disable : 26440 26426) // from catch
|
||||
|
||||
// Fix VS2015 build breaks in Release
|
||||
#pragma warning(disable : 4702) // unreachable code
|
||||
#endif
|
||||
|
||||
#include <catch/catch.hpp> // for AssertionHandler, StringRef, CHECK, TEST_...
|
||||
#include <gtest/gtest.h>
|
||||
#include <gsl/pointers> // for not_null, operator<, operator<=, operator>
|
||||
|
||||
namespace gsl
|
||||
@@ -35,19 +26,20 @@ using namespace gsl;
|
||||
|
||||
GSL_SUPPRESS(f.4) // NO-FORMAT: attribute
|
||||
bool helper(not_null<int*> p) { return *p == 12; }
|
||||
|
||||
GSL_SUPPRESS(f.4) // NO-FORMAT: attribute
|
||||
bool helper_const(not_null<const int*> p) { return *p == 12; }
|
||||
|
||||
GSL_SUPPRESS(f.4) // NO-FORMAT: attribute
|
||||
bool strict_helper(strict_not_null<int*> p) { return *p == 12; }
|
||||
|
||||
GSL_SUPPRESS(f.4) // NO-FORMAT: attribute
|
||||
bool strict_helper_const(strict_not_null<const int*> p) { return *p == 12; }
|
||||
|
||||
int* return_pointer() { return nullptr; }
|
||||
const int* return_pointer_const() { return nullptr; }
|
||||
|
||||
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
|
||||
TEST_CASE("TestStrictNotNull")
|
||||
TEST(strict_notnull_tests, TestStrictNotNull)
|
||||
{
|
||||
{
|
||||
// raw ptr <-> strict_not_null
|
||||
@@ -66,7 +58,7 @@ TEST_CASE("TestStrictNotNull")
|
||||
helper(snn1);
|
||||
helper_const(snn1);
|
||||
|
||||
CHECK(*snn1 == 42);
|
||||
EXPECT_TRUE(*snn1 == 42);
|
||||
}
|
||||
|
||||
{
|
||||
@@ -80,7 +72,7 @@ TEST_CASE("TestStrictNotNull")
|
||||
strict_helper_const(snn1);
|
||||
strict_helper_const(snn2);
|
||||
|
||||
CHECK(snn1 == snn2);
|
||||
EXPECT_TRUE(snn1 == snn2);
|
||||
}
|
||||
|
||||
{
|
||||
@@ -95,8 +87,8 @@ TEST_CASE("TestStrictNotNull")
|
||||
helper(snn);
|
||||
helper_const(snn);
|
||||
|
||||
CHECK(snn == nn1);
|
||||
CHECK(snn == nn2);
|
||||
EXPECT_TRUE(snn == nn1);
|
||||
EXPECT_TRUE(snn == nn2);
|
||||
}
|
||||
|
||||
{
|
||||
@@ -111,16 +103,16 @@ TEST_CASE("TestStrictNotNull")
|
||||
strict_helper(nn);
|
||||
strict_helper_const(nn);
|
||||
|
||||
CHECK(snn1 == nn);
|
||||
CHECK(snn2 == nn);
|
||||
EXPECT_TRUE(snn1 == nn);
|
||||
EXPECT_TRUE(snn2 == nn);
|
||||
|
||||
std::hash<strict_not_null<int*>> hash_snn;
|
||||
std::hash<not_null<int*>> hash_nn;
|
||||
|
||||
CHECK(hash_nn(snn1) == hash_nn(nn));
|
||||
CHECK(hash_snn(snn1) == hash_nn(nn));
|
||||
CHECK(hash_nn(snn1) == hash_nn(snn2));
|
||||
CHECK(hash_snn(snn1) == hash_snn(nn));
|
||||
EXPECT_TRUE(hash_nn(snn1) == hash_nn(nn));
|
||||
EXPECT_TRUE(hash_snn(snn1) == hash_nn(nn));
|
||||
EXPECT_TRUE(hash_nn(snn1) == hash_nn(snn2));
|
||||
EXPECT_TRUE(hash_snn(snn1) == hash_snn(nn));
|
||||
}
|
||||
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
@@ -131,10 +123,18 @@ TEST_CASE("TestStrictNotNull")
|
||||
}
|
||||
|
||||
#if defined(__cplusplus) && (__cplusplus >= 201703L)
|
||||
|
||||
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
|
||||
TEST_CASE("TestStrictNotNullConstructorTypeDeduction")
|
||||
namespace
|
||||
{
|
||||
static constexpr char deathstring[] = "Expected Death";
|
||||
}
|
||||
|
||||
TEST(strict_notnull_tests, TestStrictNotNullConstructorTypeDeduction)
|
||||
{
|
||||
std::set_terminate([] {
|
||||
std::cerr << "Expected Death. TestStrictNotNullConstructorTypeDeduction";
|
||||
std::abort();
|
||||
});
|
||||
|
||||
{
|
||||
int i = 42;
|
||||
|
||||
@@ -142,7 +142,7 @@ TEST_CASE("TestStrictNotNullConstructorTypeDeduction")
|
||||
helper(strict_not_null{&i});
|
||||
helper_const(strict_not_null{&i});
|
||||
|
||||
CHECK(*x == 42);
|
||||
EXPECT_TRUE(*x == 42);
|
||||
}
|
||||
|
||||
{
|
||||
@@ -153,7 +153,7 @@ TEST_CASE("TestStrictNotNullConstructorTypeDeduction")
|
||||
helper(strict_not_null{p});
|
||||
helper_const(strict_not_null{p});
|
||||
|
||||
CHECK(*x == 42);
|
||||
EXPECT_TRUE(*x == 42);
|
||||
}
|
||||
|
||||
{
|
||||
@@ -161,7 +161,7 @@ TEST_CASE("TestStrictNotNullConstructorTypeDeduction")
|
||||
int* p1 = nullptr;
|
||||
const strict_not_null x{p1};
|
||||
};
|
||||
CHECK_THROWS_AS(workaround_macro(), fail_fast);
|
||||
EXPECT_DEATH(workaround_macro(), deathstring);
|
||||
}
|
||||
|
||||
{
|
||||
@@ -169,14 +169,14 @@ TEST_CASE("TestStrictNotNullConstructorTypeDeduction")
|
||||
const int* p1 = nullptr;
|
||||
const strict_not_null x{p1};
|
||||
};
|
||||
CHECK_THROWS_AS(workaround_macro(), fail_fast);
|
||||
EXPECT_DEATH(workaround_macro(), deathstring);
|
||||
}
|
||||
|
||||
{
|
||||
int* p = nullptr;
|
||||
|
||||
CHECK_THROWS_AS(helper(strict_not_null{p}), fail_fast);
|
||||
CHECK_THROWS_AS(helper_const(strict_not_null{p}), fail_fast);
|
||||
EXPECT_DEATH(helper(strict_not_null{p}), deathstring);
|
||||
EXPECT_DEATH(helper_const(strict_not_null{p}), deathstring);
|
||||
}
|
||||
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
@@ -188,6 +188,3 @@ TEST_CASE("TestStrictNotNullConstructorTypeDeduction")
|
||||
#endif
|
||||
}
|
||||
#endif // #if defined(__cplusplus) && (__cplusplus >= 201703L)
|
||||
|
||||
static_assert(std::is_nothrow_move_constructible<strict_not_null<void*>>::value,
|
||||
"strict_not_null must be no-throw move constructible");
|
||||
|
||||
+264
-280
@@ -14,23 +14,9 @@
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifdef _MSC_VER
|
||||
// blanket turn off warnings from CppCoreCheck from catch
|
||||
// so people aren't annoyed by them when running the tool.
|
||||
#pragma warning(disable : 26440 26426) // from catch deprecated
|
||||
#pragma warning(disable : 4996) // strided_span is in the process of being deprecated.
|
||||
// Suppressing warnings until it is completely removed
|
||||
#endif
|
||||
|
||||
#if __clang__ || __GNUC__
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
||||
#endif
|
||||
|
||||
#include <catch/catch.hpp> // for AssertionHandler, StringRef, CHECK, CHECK...
|
||||
|
||||
#include <gsl/gsl_byte> // for byte
|
||||
#include <gsl/gsl_util> // for narrow_cast
|
||||
#include <gtest/gtest.h>
|
||||
#include <gsl/byte> // for byte
|
||||
#include <gsl/util> // for narrow_cast
|
||||
#include <gsl/multi_span> // for strided_span, index, multi_span, strided_...
|
||||
|
||||
#include <iostream> // for size_t
|
||||
@@ -39,24 +25,80 @@
|
||||
#include <type_traits> // for integral_constant<>::value, is_convertible
|
||||
#include <vector> // for vector
|
||||
|
||||
namespace gsl {
|
||||
struct fail_fast;
|
||||
} // namespace gsl
|
||||
|
||||
using namespace std;
|
||||
using namespace gsl;
|
||||
|
||||
|
||||
namespace
|
||||
{
|
||||
static constexpr char deathstring[] = "Expected Death";
|
||||
|
||||
struct BaseClass
|
||||
{
|
||||
};
|
||||
struct DerivedClass : BaseClass
|
||||
{
|
||||
};
|
||||
|
||||
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
|
||||
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
|
||||
void iterate_every_other_element(multi_span<int, dynamic_range> av)
|
||||
{
|
||||
// pick every other element
|
||||
|
||||
auto length = av.size() / 2;
|
||||
#if defined(_MSC_VER) && _MSC_VER > 1800
|
||||
auto bounds = strided_bounds<1>({length}, {2});
|
||||
#else
|
||||
auto bounds = strided_bounds<1>(multi_span_index<1>{length}, multi_span_index<1>{2});
|
||||
#endif
|
||||
strided_span<int, 1> strided(&av.data()[1], av.size() - 1, bounds);
|
||||
|
||||
EXPECT_TRUE(strided.size() == length);
|
||||
EXPECT_TRUE(strided.bounds().index_bounds()[0] == length);
|
||||
for (auto i = 0; i < strided.size(); ++i) {
|
||||
EXPECT_TRUE(strided[i] == av[2 * i + 1]);
|
||||
}
|
||||
|
||||
int idx = 0;
|
||||
for (auto num : strided) {
|
||||
EXPECT_TRUE(num == av[2 * idx + 1]);
|
||||
idx++;
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("span_section_test")
|
||||
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
|
||||
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
|
||||
GSL_SUPPRESS(bounds.2) // NO-FORMAT: attribute // TODO: does not work
|
||||
void iterate_second_slice(multi_span<int, dynamic_range, dynamic_range, dynamic_range> av)
|
||||
{
|
||||
const int expected[6] = {2, 3, 10, 11, 18, 19};
|
||||
auto section = av.section({0, 1, 0}, {3, 1, 2});
|
||||
|
||||
for (auto i = 0; i < section.extent<0>(); ++i) {
|
||||
for (auto j = 0; j < section.extent<1>(); ++j)
|
||||
for (auto k = 0; k < section.extent<2>(); ++k) {
|
||||
auto idx = multi_span_index<3>{i, j, k}; // avoid braces in the EXPECT_TRUE macro
|
||||
EXPECT_TRUE(section[idx] == expected[2 * i + 2 * j + k]);
|
||||
}
|
||||
}
|
||||
|
||||
for (auto i = 0; i < section.extent<0>(); ++i) {
|
||||
for (auto j = 0; j < section.extent<1>(); ++j)
|
||||
for (auto k = 0; k < section.extent<2>(); ++k)
|
||||
EXPECT_TRUE(section[i][j][k] == expected[2 * i + 2 * j + k]);
|
||||
}
|
||||
|
||||
int i = 0;
|
||||
for (const auto num : section) {
|
||||
EXPECT_TRUE(num == expected[i]);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
TEST(strided_span_tests, span_section_test)
|
||||
{
|
||||
int a[30][4][5];
|
||||
|
||||
@@ -66,51 +108,54 @@ TEST_CASE("span_section_test")
|
||||
(void) subsub;
|
||||
}
|
||||
|
||||
TEST_CASE("span_section")
|
||||
TEST(strided_span_tests, span_section)
|
||||
{
|
||||
std::vector<int> data(5 * 10);
|
||||
std::iota(begin(data), end(data), 0);
|
||||
const multi_span<int, 5, 10> av = as_multi_span(multi_span<int>{data}, dim<5>(), dim<10>());
|
||||
|
||||
const strided_span<int, 2> av_section_1 = av.section({1, 2}, {3, 4});
|
||||
CHECK(!av_section_1.empty());
|
||||
CHECK((av_section_1[{0, 0}] == 12));
|
||||
CHECK((av_section_1[{0, 1}] == 13));
|
||||
CHECK((av_section_1[{1, 0}] == 22));
|
||||
CHECK((av_section_1[{2, 3}] == 35));
|
||||
EXPECT_TRUE(!av_section_1.empty());
|
||||
EXPECT_TRUE((av_section_1[{0, 0}] == 12));
|
||||
EXPECT_TRUE((av_section_1[{0, 1}] == 13));
|
||||
EXPECT_TRUE((av_section_1[{1, 0}] == 22));
|
||||
EXPECT_TRUE((av_section_1[{2, 3}] == 35));
|
||||
|
||||
const strided_span<int, 2> av_section_2 = av_section_1.section({1, 2}, {2, 2});
|
||||
CHECK(!av_section_2.empty());
|
||||
CHECK((av_section_2[{0, 0}] == 24));
|
||||
CHECK((av_section_2[{0, 1}] == 25));
|
||||
CHECK((av_section_2[{1, 0}] == 34));
|
||||
EXPECT_TRUE(!av_section_2.empty());
|
||||
EXPECT_TRUE((av_section_2[{0, 0}] == 24));
|
||||
EXPECT_TRUE((av_section_2[{0, 1}] == 25));
|
||||
EXPECT_TRUE((av_section_2[{1, 0}] == 34));
|
||||
}
|
||||
|
||||
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
|
||||
TEST_CASE("strided_span_constructors")
|
||||
TEST(strided_span_tests, strided_span_constructors)
|
||||
{
|
||||
// Check stride constructor
|
||||
// EXPECT_TRUE stride constructor
|
||||
{
|
||||
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
|
||||
const int carr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
|
||||
|
||||
strided_span<int, 1> sav1{arr, {{9}, {1}}}; // T -> T
|
||||
CHECK(sav1.bounds().index_bounds() == multi_span_index<1>{9});
|
||||
CHECK(sav1.bounds().stride() == 1);
|
||||
CHECK((sav1[0] == 1 && sav1[8] == 9));
|
||||
EXPECT_TRUE(sav1.bounds().index_bounds() == multi_span_index<1>{9});
|
||||
EXPECT_TRUE(sav1.bounds().stride() == 1);
|
||||
EXPECT_TRUE(sav1[0] == 1);
|
||||
EXPECT_TRUE(sav1[8] == 9);
|
||||
|
||||
strided_span<const int, 1> sav2{carr, {{4}, {2}}}; // const T -> const T
|
||||
CHECK(sav2.bounds().index_bounds() == multi_span_index<1>{4});
|
||||
CHECK(sav2.bounds().strides() == multi_span_index<1>{2});
|
||||
CHECK((sav2[0] == 1 && sav2[3] == 7));
|
||||
EXPECT_TRUE(sav2.bounds().index_bounds() == multi_span_index<1>{4});
|
||||
EXPECT_TRUE(sav2.bounds().strides() == multi_span_index<1>{2});
|
||||
EXPECT_TRUE(sav2[0] == 1);
|
||||
EXPECT_TRUE(sav2[3] == 7);
|
||||
|
||||
strided_span<int, 2> sav3{arr, {{2, 2}, {6, 2}}}; // T -> const T
|
||||
CHECK((sav3.bounds().index_bounds() == multi_span_index<2>{2, 2}));
|
||||
CHECK((sav3.bounds().strides() == multi_span_index<2>{6, 2}));
|
||||
CHECK((sav3[{0, 0}] == 1 && sav3[{0, 1}] == 3 && sav3[{1, 0}] == 7));
|
||||
EXPECT_TRUE((sav3.bounds().index_bounds() == multi_span_index<2>{2, 2}));
|
||||
EXPECT_TRUE((sav3.bounds().strides() == multi_span_index<2>{6, 2}));
|
||||
EXPECT_TRUE((sav3[{0, 0}]) == 1);
|
||||
EXPECT_TRUE((sav3[{0, 1}]) == 3);
|
||||
EXPECT_TRUE((sav3[{1, 0}]) == 7);
|
||||
}
|
||||
|
||||
// Check multi_span constructor
|
||||
// EXPECT_TRUE multi_span constructor
|
||||
{
|
||||
int arr[] = {1, 2};
|
||||
|
||||
@@ -119,9 +164,9 @@ TEST_CASE("strided_span_constructors")
|
||||
const multi_span<int> src = arr;
|
||||
|
||||
strided_span<int, 1> sav{src, {2, 1}};
|
||||
CHECK(sav.bounds().index_bounds() == multi_span_index<1>{2});
|
||||
CHECK(sav.bounds().strides() == multi_span_index<1>{1});
|
||||
CHECK(sav[1] == 2);
|
||||
EXPECT_TRUE(sav.bounds().index_bounds() == multi_span_index<1>{2});
|
||||
EXPECT_TRUE(sav.bounds().strides() == multi_span_index<1>{1});
|
||||
EXPECT_TRUE(sav[1] == 2);
|
||||
|
||||
#if defined(_MSC_VER) && _MSC_VER > 1800
|
||||
// strided_span<const int, 1> sav_c{ {src}, {2, 1} };
|
||||
@@ -131,9 +176,9 @@ TEST_CASE("strided_span_constructors")
|
||||
strided_span<const int, 1> sav_c{multi_span<const int>{src},
|
||||
strided_bounds<1>{2, 1}};
|
||||
#endif
|
||||
CHECK(sav_c.bounds().index_bounds() == multi_span_index<1>{2});
|
||||
CHECK(sav_c.bounds().strides() == multi_span_index<1>{1});
|
||||
CHECK(sav_c[1] == 2);
|
||||
EXPECT_TRUE(sav_c.bounds().index_bounds() == multi_span_index<1>{2});
|
||||
EXPECT_TRUE(sav_c.bounds().strides() == multi_span_index<1>{1});
|
||||
EXPECT_TRUE(sav_c[1] == 2);
|
||||
|
||||
#if defined(_MSC_VER) && _MSC_VER > 1800
|
||||
strided_span<volatile int, 1> sav_v{src, {2, 1}};
|
||||
@@ -141,9 +186,9 @@ TEST_CASE("strided_span_constructors")
|
||||
strided_span<volatile int, 1> sav_v{multi_span<volatile int>{src},
|
||||
strided_bounds<1>{2, 1}};
|
||||
#endif
|
||||
CHECK(sav_v.bounds().index_bounds() == multi_span_index<1>{2});
|
||||
CHECK(sav_v.bounds().strides() == multi_span_index<1>{1});
|
||||
CHECK(sav_v[1] == 2);
|
||||
EXPECT_TRUE(sav_v.bounds().index_bounds() == multi_span_index<1>{2});
|
||||
EXPECT_TRUE(sav_v.bounds().strides() == multi_span_index<1>{1});
|
||||
EXPECT_TRUE(sav_v[1] == 2);
|
||||
|
||||
#if defined(_MSC_VER) && _MSC_VER > 1800
|
||||
strided_span<const volatile int, 1> sav_cv{src, {2, 1}};
|
||||
@@ -151,9 +196,9 @@ TEST_CASE("strided_span_constructors")
|
||||
strided_span<const volatile int, 1> sav_cv{multi_span<const volatile int>{src},
|
||||
strided_bounds<1>{2, 1}};
|
||||
#endif
|
||||
CHECK(sav_cv.bounds().index_bounds() == multi_span_index<1>{2});
|
||||
CHECK(sav_cv.bounds().strides() == multi_span_index<1>{1});
|
||||
CHECK(sav_cv[1] == 2);
|
||||
EXPECT_TRUE(sav_cv.bounds().index_bounds() == multi_span_index<1>{2});
|
||||
EXPECT_TRUE(sav_cv.bounds().strides() == multi_span_index<1>{1});
|
||||
EXPECT_TRUE(sav_cv[1] == 2);
|
||||
}
|
||||
|
||||
// From const-qualified source
|
||||
@@ -161,9 +206,9 @@ TEST_CASE("strided_span_constructors")
|
||||
const multi_span<const int> src{arr};
|
||||
|
||||
strided_span<const int, 1> sav_c{src, {2, 1}};
|
||||
CHECK(sav_c.bounds().index_bounds() == multi_span_index<1>{2});
|
||||
CHECK(sav_c.bounds().strides() == multi_span_index<1>{1});
|
||||
CHECK(sav_c[1] == 2);
|
||||
EXPECT_TRUE(sav_c.bounds().index_bounds() == multi_span_index<1>{2});
|
||||
EXPECT_TRUE(sav_c.bounds().strides() == multi_span_index<1>{1});
|
||||
EXPECT_TRUE(sav_c[1] == 2);
|
||||
|
||||
#if defined(_MSC_VER) && _MSC_VER > 1800
|
||||
strided_span<const volatile int, 1> sav_cv{src, {2, 1}};
|
||||
@@ -172,9 +217,9 @@ TEST_CASE("strided_span_constructors")
|
||||
strided_bounds<1>{2, 1}};
|
||||
#endif
|
||||
|
||||
CHECK(sav_cv.bounds().index_bounds() == multi_span_index<1>{2});
|
||||
CHECK(sav_cv.bounds().strides() == multi_span_index<1>{1});
|
||||
CHECK(sav_cv[1] == 2);
|
||||
EXPECT_TRUE(sav_cv.bounds().index_bounds() == multi_span_index<1>{2});
|
||||
EXPECT_TRUE(sav_cv.bounds().strides() == multi_span_index<1>{1});
|
||||
EXPECT_TRUE(sav_cv[1] == 2);
|
||||
}
|
||||
|
||||
// From volatile-qualified source
|
||||
@@ -182,9 +227,9 @@ TEST_CASE("strided_span_constructors")
|
||||
const multi_span<volatile int> src{arr};
|
||||
|
||||
strided_span<volatile int, 1> sav_v{src, {2, 1}};
|
||||
CHECK(sav_v.bounds().index_bounds() == multi_span_index<1>{2});
|
||||
CHECK(sav_v.bounds().strides() == multi_span_index<1>{1});
|
||||
CHECK(sav_v[1] == 2);
|
||||
EXPECT_TRUE(sav_v.bounds().index_bounds() == multi_span_index<1>{2});
|
||||
EXPECT_TRUE(sav_v.bounds().strides() == multi_span_index<1>{1});
|
||||
EXPECT_TRUE(sav_v[1] == 2);
|
||||
|
||||
#if defined(_MSC_VER) && _MSC_VER > 1800
|
||||
strided_span<const volatile int, 1> sav_cv{src, {2, 1}};
|
||||
@@ -192,9 +237,9 @@ TEST_CASE("strided_span_constructors")
|
||||
strided_span<const volatile int, 1> sav_cv{multi_span<const volatile int>{src},
|
||||
strided_bounds<1>{2, 1}};
|
||||
#endif
|
||||
CHECK(sav_cv.bounds().index_bounds() == multi_span_index<1>{2});
|
||||
CHECK(sav_cv.bounds().strides() == multi_span_index<1>{1});
|
||||
CHECK(sav_cv[1] == 2);
|
||||
EXPECT_TRUE(sav_cv.bounds().index_bounds() == multi_span_index<1>{2});
|
||||
EXPECT_TRUE(sav_cv.bounds().strides() == multi_span_index<1>{1});
|
||||
EXPECT_TRUE(sav_cv[1] == 2);
|
||||
}
|
||||
|
||||
// From cv-qualified source
|
||||
@@ -202,19 +247,19 @@ TEST_CASE("strided_span_constructors")
|
||||
const multi_span<const volatile int> src{arr};
|
||||
|
||||
strided_span<const volatile int, 1> sav_cv{src, {2, 1}};
|
||||
CHECK(sav_cv.bounds().index_bounds() == multi_span_index<1>{2});
|
||||
CHECK(sav_cv.bounds().strides() == multi_span_index<1>{1});
|
||||
CHECK(sav_cv[1] == 2);
|
||||
EXPECT_TRUE(sav_cv.bounds().index_bounds() == multi_span_index<1>{2});
|
||||
EXPECT_TRUE(sav_cv.bounds().strides() == multi_span_index<1>{1});
|
||||
EXPECT_TRUE(sav_cv[1] == 2);
|
||||
}
|
||||
}
|
||||
|
||||
// Check const-casting constructor
|
||||
// EXPECT_TRUE const-casting constructor
|
||||
{
|
||||
int arr[2] = {4, 5};
|
||||
|
||||
const multi_span<int, 2> av(arr, 2);
|
||||
multi_span<const int, 2> av2{av};
|
||||
CHECK(av2[1] == 5);
|
||||
EXPECT_TRUE(av2[1] == 5);
|
||||
|
||||
static_assert(
|
||||
std::is_convertible<const multi_span<int, 2>, multi_span<const int, 2>>::value,
|
||||
@@ -222,34 +267,35 @@ TEST_CASE("strided_span_constructors")
|
||||
|
||||
const strided_span<int, 1> src{arr, {2, 1}};
|
||||
strided_span<const int, 1> sav{src};
|
||||
CHECK(sav.bounds().index_bounds() == multi_span_index<1>{2});
|
||||
CHECK(sav.bounds().stride() == 1);
|
||||
CHECK(sav[1] == 5);
|
||||
EXPECT_TRUE(sav.bounds().index_bounds() == multi_span_index<1>{2});
|
||||
EXPECT_TRUE(sav.bounds().stride() == 1);
|
||||
EXPECT_TRUE(sav[1] == 5);
|
||||
|
||||
static_assert(
|
||||
std::is_convertible<const strided_span<int, 1>, strided_span<const int, 1>>::value,
|
||||
"ctor is not implicit!");
|
||||
}
|
||||
|
||||
// Check copy constructor
|
||||
// EXPECT_TRUE copy constructor
|
||||
{
|
||||
int arr1[2] = {3, 4};
|
||||
const strided_span<int, 1> src1{arr1, {2, 1}};
|
||||
strided_span<int, 1> sav1{src1};
|
||||
|
||||
CHECK(sav1.bounds().index_bounds() == multi_span_index<1>{2});
|
||||
CHECK(sav1.bounds().stride() == 1);
|
||||
CHECK(sav1[0] == 3);
|
||||
EXPECT_TRUE(sav1.bounds().index_bounds() == multi_span_index<1>{2});
|
||||
EXPECT_TRUE(sav1.bounds().stride() == 1);
|
||||
EXPECT_TRUE(sav1[0] == 3);
|
||||
|
||||
int arr2[6] = {1, 2, 3, 4, 5, 6};
|
||||
const strided_span<const int, 2> src2{arr2, {{3, 2}, {2, 1}}};
|
||||
strided_span<const int, 2> sav2{src2};
|
||||
CHECK((sav2.bounds().index_bounds() == multi_span_index<2>{3, 2}));
|
||||
CHECK((sav2.bounds().strides() == multi_span_index<2>{2, 1}));
|
||||
CHECK((sav2[{0, 0}] == 1 && sav2[{2, 0}] == 5));
|
||||
EXPECT_TRUE((sav2.bounds().index_bounds() == multi_span_index<2>{3, 2}));
|
||||
EXPECT_TRUE((sav2.bounds().strides() == multi_span_index<2>{2, 1}));
|
||||
EXPECT_TRUE((sav2[{0, 0}]) == 1);
|
||||
EXPECT_TRUE((sav2[{2, 0}]) == 5);
|
||||
}
|
||||
|
||||
// Check const-casting assignment operator
|
||||
// EXPECT_TRUE const-casting assignment operator
|
||||
{
|
||||
int arr1[2] = {1, 2};
|
||||
int arr2[6] = {3, 4, 5, 6, 7, 8};
|
||||
@@ -257,38 +303,38 @@ TEST_CASE("strided_span_constructors")
|
||||
const strided_span<int, 1> src{arr1, {{2}, {1}}};
|
||||
strided_span<const int, 1> sav{arr2, {{3}, {2}}};
|
||||
strided_span<const int, 1>& sav_ref = (sav = src);
|
||||
CHECK(sav.bounds().index_bounds() == multi_span_index<1>{2});
|
||||
CHECK(sav.bounds().strides() == multi_span_index<1>{1});
|
||||
CHECK(sav[0] == 1);
|
||||
CHECK(&sav_ref == &sav);
|
||||
EXPECT_TRUE(sav.bounds().index_bounds() == multi_span_index<1>{2});
|
||||
EXPECT_TRUE(sav.bounds().strides() == multi_span_index<1>{1});
|
||||
EXPECT_TRUE(sav[0] == 1);
|
||||
EXPECT_TRUE(&sav_ref == &sav);
|
||||
}
|
||||
|
||||
// Check copy assignment operator
|
||||
// EXPECT_TRUE copy assignment operator
|
||||
{
|
||||
int arr1[2] = {3, 4};
|
||||
int arr1b[1] = {0};
|
||||
const strided_span<int, 1> src1{arr1, {2, 1}};
|
||||
strided_span<int, 1> sav1{arr1b, {1, 1}};
|
||||
strided_span<int, 1>& sav1_ref = (sav1 = src1);
|
||||
CHECK(sav1.bounds().index_bounds() == multi_span_index<1>{2});
|
||||
CHECK(sav1.bounds().strides() == multi_span_index<1>{1});
|
||||
CHECK(sav1[0] == 3);
|
||||
CHECK(&sav1_ref == &sav1);
|
||||
EXPECT_TRUE(sav1.bounds().index_bounds() == multi_span_index<1>{2});
|
||||
EXPECT_TRUE(sav1.bounds().strides() == multi_span_index<1>{1});
|
||||
EXPECT_TRUE(sav1[0] == 3);
|
||||
EXPECT_TRUE(&sav1_ref == &sav1);
|
||||
|
||||
const int arr2[6] = {1, 2, 3, 4, 5, 6};
|
||||
const int arr2b[1] = {0};
|
||||
const strided_span<const int, 2> src2{arr2, {{3, 2}, {2, 1}}};
|
||||
strided_span<const int, 2> sav2{arr2b, {{1, 1}, {1, 1}}};
|
||||
strided_span<const int, 2>& sav2_ref = (sav2 = src2);
|
||||
CHECK((sav2.bounds().index_bounds() == multi_span_index<2>{3, 2}));
|
||||
CHECK((sav2.bounds().strides() == multi_span_index<2>{2, 1}));
|
||||
CHECK((sav2[{0, 0}] == 1 && sav2[{2, 0}] == 5));
|
||||
CHECK(&sav2_ref == &sav2);
|
||||
EXPECT_TRUE((sav2.bounds().index_bounds() == multi_span_index<2>{3, 2}));
|
||||
EXPECT_TRUE((sav2.bounds().strides() == multi_span_index<2>{2, 1}));
|
||||
EXPECT_TRUE((sav2[{0, 0}] == 1));
|
||||
EXPECT_TRUE((sav2[{2, 0}] == 5));
|
||||
EXPECT_TRUE(&sav2_ref == &sav2);
|
||||
}
|
||||
}
|
||||
|
||||
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
|
||||
TEST_CASE("strided_span_slice")
|
||||
TEST(strided_span_tests, strided_span_slice)
|
||||
{
|
||||
std::vector<int> data(5 * 10);
|
||||
std::iota(begin(data), end(data), 0);
|
||||
@@ -303,19 +349,18 @@ TEST_CASE("strided_span_slice")
|
||||
{{5, 10}, {10, 1}}};
|
||||
|
||||
strided_span<int, 1> sav_sl = sav[2];
|
||||
CHECK(sav_sl[0] == 20);
|
||||
CHECK(sav_sl[9] == 29);
|
||||
EXPECT_TRUE(sav_sl[0] == 20);
|
||||
EXPECT_TRUE(sav_sl[9] == 29);
|
||||
|
||||
strided_span<const int, 1> csav_sl = sav[3];
|
||||
CHECK(csav_sl[0] == 30);
|
||||
CHECK(csav_sl[9] == 39);
|
||||
EXPECT_TRUE(csav_sl[0] == 30);
|
||||
EXPECT_TRUE(csav_sl[9] == 39);
|
||||
|
||||
CHECK(sav[4][0] == 40);
|
||||
CHECK(sav[4][9] == 49);
|
||||
EXPECT_TRUE(sav[4][0] == 40);
|
||||
EXPECT_TRUE(sav[4][9] == 49);
|
||||
}
|
||||
|
||||
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
|
||||
TEST_CASE("strided_span_column_major")
|
||||
TEST(strided_span_tests, strided_span_column_major)
|
||||
{
|
||||
// strided_span may be used to accommodate more peculiar
|
||||
// use cases, such as column-major multidimensional array
|
||||
@@ -325,119 +370,123 @@ TEST_CASE("strided_span_column_major")
|
||||
strided_span<int, 2> cm_sav{cm_array, {{5, 3}, {1, 5}}};
|
||||
|
||||
// Accessing elements
|
||||
CHECK((cm_sav[{0, 0}] == 1));
|
||||
CHECK((cm_sav[{0, 1}] == 2));
|
||||
CHECK((cm_sav[{1, 0}] == 4));
|
||||
CHECK((cm_sav[{4, 2}] == 15));
|
||||
EXPECT_TRUE((cm_sav[{0, 0}] == 1));
|
||||
EXPECT_TRUE((cm_sav[{0, 1}] == 2));
|
||||
EXPECT_TRUE((cm_sav[{1, 0}] == 4));
|
||||
EXPECT_TRUE((cm_sav[{4, 2}] == 15));
|
||||
|
||||
// Slice
|
||||
strided_span<int, 1> cm_sl = cm_sav[3];
|
||||
|
||||
CHECK(cm_sl[0] == 10);
|
||||
CHECK(cm_sl[1] == 11);
|
||||
CHECK(cm_sl[2] == 12);
|
||||
EXPECT_TRUE(cm_sl[0] == 10);
|
||||
EXPECT_TRUE(cm_sl[1] == 11);
|
||||
EXPECT_TRUE(cm_sl[2] == 12);
|
||||
|
||||
// Section
|
||||
strided_span<int, 2> cm_sec = cm_sav.section({2, 1}, {3, 2});
|
||||
|
||||
CHECK((cm_sec.bounds().index_bounds() == multi_span_index<2>{3, 2}));
|
||||
CHECK((cm_sec[{0, 0}] == 8));
|
||||
CHECK((cm_sec[{0, 1}] == 9));
|
||||
CHECK((cm_sec[{1, 0}] == 11));
|
||||
CHECK((cm_sec[{2, 1}] == 15));
|
||||
EXPECT_TRUE((cm_sec.bounds().index_bounds() == multi_span_index<2>{3, 2}));
|
||||
EXPECT_TRUE((cm_sec[{0, 0}] == 8));
|
||||
EXPECT_TRUE((cm_sec[{0, 1}] == 9));
|
||||
EXPECT_TRUE((cm_sec[{1, 0}] == 11));
|
||||
EXPECT_TRUE((cm_sec[{2, 1}] == 15));
|
||||
}
|
||||
|
||||
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
|
||||
TEST_CASE("strided_span_bounds")
|
||||
TEST(strided_span_tests, strided_span_bounds)
|
||||
{
|
||||
int arr[] = {0, 1, 2, 3};
|
||||
multi_span<int> av(arr);
|
||||
|
||||
std::set_terminate([] {
|
||||
std::cerr << "Expected Death. strided_span_bounds";
|
||||
std::abort();
|
||||
});
|
||||
|
||||
{
|
||||
// incorrect sections
|
||||
|
||||
CHECK_THROWS_AS(av.section(0, 0)[0], fail_fast);
|
||||
CHECK_THROWS_AS(av.section(1, 0)[0], fail_fast);
|
||||
CHECK_THROWS_AS(av.section(1, 1)[1], fail_fast);
|
||||
EXPECT_DEATH(av.section(0, 0)[0], deathstring);
|
||||
EXPECT_DEATH(av.section(1, 0)[0], deathstring);
|
||||
EXPECT_DEATH(av.section(1, 1)[1], deathstring);
|
||||
|
||||
CHECK_THROWS_AS(av.section(2, 5), fail_fast);
|
||||
CHECK_THROWS_AS(av.section(5, 2), fail_fast);
|
||||
CHECK_THROWS_AS(av.section(5, 0), fail_fast);
|
||||
CHECK_THROWS_AS(av.section(0, 5), fail_fast);
|
||||
CHECK_THROWS_AS(av.section(5, 5), fail_fast);
|
||||
EXPECT_DEATH(av.section(2, 5), deathstring);
|
||||
EXPECT_DEATH(av.section(5, 2), deathstring);
|
||||
EXPECT_DEATH(av.section(5, 0), deathstring);
|
||||
EXPECT_DEATH(av.section(0, 5), deathstring);
|
||||
EXPECT_DEATH(av.section(5, 5), deathstring);
|
||||
}
|
||||
|
||||
{
|
||||
// zero stride
|
||||
strided_span<int, 1> sav{av, {{4}, {}}};
|
||||
CHECK(sav[0] == 0);
|
||||
CHECK(sav[3] == 0);
|
||||
CHECK_THROWS_AS(sav[4], fail_fast);
|
||||
EXPECT_TRUE(sav[0] == 0);
|
||||
EXPECT_TRUE(sav[3] == 0);
|
||||
EXPECT_DEATH(sav[4], deathstring);
|
||||
}
|
||||
|
||||
{
|
||||
// zero extent
|
||||
strided_span<int, 1> sav{av, {{}, {1}}};
|
||||
CHECK_THROWS_AS(sav[0], fail_fast);
|
||||
EXPECT_DEATH(sav[0], deathstring);
|
||||
}
|
||||
|
||||
{
|
||||
// zero extent and stride
|
||||
strided_span<int, 1> sav{av, {{}, {}}};
|
||||
CHECK_THROWS_AS(sav[0], fail_fast);
|
||||
EXPECT_DEATH(sav[0], deathstring);
|
||||
}
|
||||
|
||||
{
|
||||
// strided array ctor with matching strided bounds
|
||||
strided_span<int, 1> sav{arr, {4, 1}};
|
||||
CHECK(sav.bounds().index_bounds() == multi_span_index<1>{4});
|
||||
CHECK(sav[3] == 3);
|
||||
CHECK_THROWS_AS(sav[4], fail_fast);
|
||||
EXPECT_TRUE(sav.bounds().index_bounds() == multi_span_index<1>{4});
|
||||
EXPECT_TRUE(sav[3] == 3);
|
||||
EXPECT_DEATH(sav[4], deathstring);
|
||||
}
|
||||
|
||||
{
|
||||
// strided array ctor with smaller strided bounds
|
||||
strided_span<int, 1> sav{arr, {2, 1}};
|
||||
CHECK(sav.bounds().index_bounds() == multi_span_index<1>{2});
|
||||
CHECK(sav[1] == 1);
|
||||
CHECK_THROWS_AS(sav[2], fail_fast);
|
||||
EXPECT_TRUE(sav.bounds().index_bounds() == multi_span_index<1>{2});
|
||||
EXPECT_TRUE(sav[1] == 1);
|
||||
EXPECT_DEATH(sav[2], deathstring);
|
||||
}
|
||||
|
||||
{
|
||||
// strided array ctor with fitting irregular bounds
|
||||
strided_span<int, 1> sav{arr, {2, 3}};
|
||||
CHECK(sav.bounds().index_bounds() == multi_span_index<1>{2});
|
||||
CHECK(sav[0] == 0);
|
||||
CHECK(sav[1] == 3);
|
||||
CHECK_THROWS_AS(sav[2], fail_fast);
|
||||
EXPECT_TRUE(sav.bounds().index_bounds() == multi_span_index<1>{2});
|
||||
EXPECT_TRUE(sav[0] == 0);
|
||||
EXPECT_TRUE(sav[1] == 3);
|
||||
EXPECT_DEATH(sav[2], deathstring);
|
||||
}
|
||||
|
||||
{
|
||||
// bounds cross data boundaries - from static arrays
|
||||
CHECK_THROWS_AS((strided_span<int, 1>{arr, {3, 2}}), fail_fast);
|
||||
CHECK_THROWS_AS((strided_span<int, 1>{arr, {3, 3}}), fail_fast);
|
||||
CHECK_THROWS_AS((strided_span<int, 1>{arr, {4, 5}}), fail_fast);
|
||||
CHECK_THROWS_AS((strided_span<int, 1>{arr, {5, 1}}), fail_fast);
|
||||
CHECK_THROWS_AS((strided_span<int, 1>{arr, {5, 5}}), fail_fast);
|
||||
EXPECT_DEATH((strided_span<int, 1>{arr, {3, 2}}), deathstring);
|
||||
EXPECT_DEATH((strided_span<int, 1>{arr, {3, 3}}), deathstring);
|
||||
EXPECT_DEATH((strided_span<int, 1>{arr, {4, 5}}), deathstring);
|
||||
EXPECT_DEATH((strided_span<int, 1>{arr, {5, 1}}), deathstring);
|
||||
EXPECT_DEATH((strided_span<int, 1>{arr, {5, 5}}), deathstring);
|
||||
}
|
||||
|
||||
{
|
||||
// bounds cross data boundaries - from array view
|
||||
CHECK_THROWS_AS((strided_span<int, 1>{av, {3, 2}}), fail_fast);
|
||||
CHECK_THROWS_AS((strided_span<int, 1>{av, {3, 3}}), fail_fast);
|
||||
CHECK_THROWS_AS((strided_span<int, 1>{av, {4, 5}}), fail_fast);
|
||||
CHECK_THROWS_AS((strided_span<int, 1>{av, {5, 1}}), fail_fast);
|
||||
CHECK_THROWS_AS((strided_span<int, 1>{av, {5, 5}}), fail_fast);
|
||||
EXPECT_DEATH((strided_span<int, 1>{av, {3, 2}}), deathstring);
|
||||
EXPECT_DEATH((strided_span<int, 1>{av, {3, 3}}), deathstring);
|
||||
EXPECT_DEATH((strided_span<int, 1>{av, {4, 5}}), deathstring);
|
||||
EXPECT_DEATH((strided_span<int, 1>{av, {5, 1}}), deathstring);
|
||||
EXPECT_DEATH((strided_span<int, 1>{av, {5, 5}}), deathstring);
|
||||
}
|
||||
|
||||
{
|
||||
// bounds cross data boundaries - from dynamic arrays
|
||||
CHECK_THROWS_AS((strided_span<int, 1>{av.data(), 4, {3, 2}}), fail_fast);
|
||||
CHECK_THROWS_AS((strided_span<int, 1>{av.data(), 4, {3, 3}}), fail_fast);
|
||||
CHECK_THROWS_AS((strided_span<int, 1>{av.data(), 4, {4, 5}}), fail_fast);
|
||||
CHECK_THROWS_AS((strided_span<int, 1>{av.data(), 4, {5, 1}}), fail_fast);
|
||||
CHECK_THROWS_AS((strided_span<int, 1>{av.data(), 4, {5, 5}}), fail_fast);
|
||||
CHECK_THROWS_AS((strided_span<int, 1>{av.data(), 2, {2, 2}}), fail_fast);
|
||||
EXPECT_DEATH((strided_span<int, 1>{av.data(), 4, {3, 2}}), deathstring);
|
||||
EXPECT_DEATH((strided_span<int, 1>{av.data(), 4, {3, 3}}), deathstring);
|
||||
EXPECT_DEATH((strided_span<int, 1>{av.data(), 4, {4, 5}}), deathstring);
|
||||
EXPECT_DEATH((strided_span<int, 1>{av.data(), 4, {5, 1}}), deathstring);
|
||||
EXPECT_DEATH((strided_span<int, 1>{av.data(), 4, {5, 5}}), deathstring);
|
||||
EXPECT_DEATH((strided_span<int, 1>{av.data(), 2, {2, 2}}), deathstring);
|
||||
}
|
||||
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
@@ -464,12 +513,16 @@ TEST_CASE("strided_span_bounds")
|
||||
#endif
|
||||
}
|
||||
|
||||
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
|
||||
TEST_CASE("strided_span_type_conversion")
|
||||
TEST(strided_span_tests, strided_span_type_conversion)
|
||||
{
|
||||
int arr[] = {0, 1, 2, 3};
|
||||
multi_span<int> av(arr);
|
||||
|
||||
std::set_terminate([] {
|
||||
std::cerr << "Expected Death. strided_span_type_conversion";
|
||||
std::abort();
|
||||
});
|
||||
|
||||
{
|
||||
strided_span<int, 1> sav{av.data(), av.size(), {av.size() / 2, 2}};
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
@@ -490,10 +543,10 @@ TEST_CASE("strided_span_type_conversion")
|
||||
strided_bounds<2> bounds{{2, bytes.size() / 4}, {bytes.size() / 2, 1}};
|
||||
strided_span<const byte, 2> sav2{bytes.data(), bytes.size(), bounds};
|
||||
strided_span<const int, 2> sav3 = sav2.as_strided_span<const int>();
|
||||
CHECK(sav3[0][0] == 0);
|
||||
CHECK(sav3[1][0] == 2);
|
||||
CHECK_THROWS_AS(sav3[1][1], fail_fast);
|
||||
CHECK_THROWS_AS(sav3[0][1], fail_fast);
|
||||
EXPECT_TRUE(sav3[0][0] == 0);
|
||||
EXPECT_TRUE(sav3[1][0] == 2);
|
||||
EXPECT_DEATH(sav3[1][1], deathstring);
|
||||
EXPECT_DEATH(sav3[0][1], deathstring);
|
||||
}
|
||||
|
||||
// retype strided array with regular strides - from multi_span
|
||||
@@ -503,10 +556,10 @@ TEST_CASE("strided_span_type_conversion")
|
||||
as_multi_span(bytes, dim<2>(), dim(bytes.size() / 2));
|
||||
strided_span<const byte, 2> sav2{bytes2, bounds};
|
||||
strided_span<int, 2> sav3 = sav2.as_strided_span<int>();
|
||||
CHECK(sav3[0][0] == 0);
|
||||
CHECK(sav3[1][0] == 2);
|
||||
CHECK_THROWS_AS(sav3[1][1], fail_fast);
|
||||
CHECK_THROWS_AS(sav3[0][1], fail_fast);
|
||||
EXPECT_TRUE(sav3[0][0] == 0);
|
||||
EXPECT_TRUE(sav3[1][0] == 2);
|
||||
EXPECT_DEATH(sav3[1][1], deathstring);
|
||||
EXPECT_DEATH(sav3[0][1], deathstring);
|
||||
}
|
||||
|
||||
// retype strided array with not enough elements - last dimension of the array is too small
|
||||
@@ -515,7 +568,7 @@ TEST_CASE("strided_span_type_conversion")
|
||||
multi_span<const byte, 2, dynamic_range> bytes2 =
|
||||
as_multi_span(bytes, dim<2>(), dim(bytes.size() / 2));
|
||||
strided_span<const byte, 2> sav2{bytes2, bounds};
|
||||
CHECK_THROWS_AS(sav2.as_strided_span<int>(), fail_fast);
|
||||
EXPECT_DEATH(sav2.as_strided_span<int>(), deathstring);
|
||||
}
|
||||
|
||||
// retype strided array with not enough elements - strides are too small
|
||||
@@ -524,7 +577,7 @@ TEST_CASE("strided_span_type_conversion")
|
||||
multi_span<const byte, 2, dynamic_range> bytes2 =
|
||||
as_multi_span(bytes, dim<2>(), dim(bytes.size() / 2));
|
||||
strided_span<const byte, 2> sav2{bytes2, bounds};
|
||||
CHECK_THROWS_AS(sav2.as_strided_span<int>(), fail_fast);
|
||||
EXPECT_DEATH(sav2.as_strided_span<int>(), deathstring);
|
||||
}
|
||||
|
||||
// retype strided array with not enough elements - last dimension does not divide by the new
|
||||
@@ -534,7 +587,7 @@ TEST_CASE("strided_span_type_conversion")
|
||||
multi_span<const byte, 2, dynamic_range> bytes2 =
|
||||
as_multi_span(bytes, dim<2>(), dim(bytes.size() / 2));
|
||||
strided_span<const byte, 2> sav2{bytes2, bounds};
|
||||
CHECK_THROWS_AS(sav2.as_strided_span<int>(), fail_fast);
|
||||
EXPECT_DEATH(sav2.as_strided_span<int>(), deathstring);
|
||||
}
|
||||
|
||||
// retype strided array with not enough elements - strides does not divide by the new
|
||||
@@ -544,88 +597,63 @@ TEST_CASE("strided_span_type_conversion")
|
||||
multi_span<const byte, 2, dynamic_range> bytes2 =
|
||||
as_multi_span(bytes, dim<2>(), dim(bytes.size() / 2));
|
||||
strided_span<const byte, 2> sav2{bytes2, bounds};
|
||||
CHECK_THROWS_AS(sav2.as_strided_span<int>(), fail_fast);
|
||||
EXPECT_DEATH(sav2.as_strided_span<int>(), deathstring);
|
||||
}
|
||||
|
||||
// retype strided array with irregular strides - from raw data
|
||||
{
|
||||
strided_bounds<1> bounds{bytes.size() / 2, 2};
|
||||
strided_span<const byte, 1> sav2{bytes.data(), bytes.size(), bounds};
|
||||
CHECK_THROWS_AS(sav2.as_strided_span<int>(), fail_fast);
|
||||
EXPECT_DEATH(sav2.as_strided_span<int>(), deathstring);
|
||||
}
|
||||
|
||||
// retype strided array with irregular strides - from multi_span
|
||||
{
|
||||
strided_bounds<1> bounds{bytes.size() / 2, 2};
|
||||
strided_span<const byte, 1> sav2{bytes, bounds};
|
||||
CHECK_THROWS_AS(sav2.as_strided_span<int>(), fail_fast);
|
||||
EXPECT_DEATH(sav2.as_strided_span<int>(), deathstring);
|
||||
}
|
||||
}
|
||||
|
||||
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
|
||||
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
|
||||
TEST_CASE("empty_strided_spans")
|
||||
TEST(strided_span_tests, empty_strided_spans)
|
||||
{
|
||||
std::set_terminate([] {
|
||||
std::cerr << "Expected Death. empty_strided_spans";
|
||||
std::abort();
|
||||
});
|
||||
|
||||
{
|
||||
multi_span<int, 0> empty_av(nullptr);
|
||||
strided_span<int, 1> empty_sav{empty_av, {0, 1}};
|
||||
|
||||
CHECK(empty_sav.bounds().index_bounds() == multi_span_index<1>{0});
|
||||
CHECK(empty_sav.empty());
|
||||
CHECK_THROWS_AS(empty_sav[0], fail_fast);
|
||||
CHECK_THROWS_AS(empty_sav.begin()[0], fail_fast);
|
||||
CHECK_THROWS_AS(empty_sav.cbegin()[0], fail_fast);
|
||||
EXPECT_TRUE(empty_sav.bounds().index_bounds() == multi_span_index<1>{0});
|
||||
EXPECT_TRUE(empty_sav.empty());
|
||||
EXPECT_DEATH(empty_sav[0], deathstring);
|
||||
EXPECT_DEATH(empty_sav.begin()[0], deathstring);
|
||||
EXPECT_DEATH(empty_sav.cbegin()[0], deathstring);
|
||||
|
||||
for (const auto& v : empty_sav) {
|
||||
(void) v;
|
||||
CHECK(false);
|
||||
EXPECT_TRUE(false);
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
strided_span<int, 1> empty_sav{nullptr, 0, {0, 1}};
|
||||
|
||||
CHECK(empty_sav.bounds().index_bounds() == multi_span_index<1>{0});
|
||||
CHECK_THROWS_AS(empty_sav[0], fail_fast);
|
||||
CHECK_THROWS_AS(empty_sav.begin()[0], fail_fast);
|
||||
CHECK_THROWS_AS(empty_sav.cbegin()[0], fail_fast);
|
||||
EXPECT_TRUE(empty_sav.bounds().index_bounds() == multi_span_index<1>{0});
|
||||
EXPECT_DEATH(empty_sav[0], deathstring);
|
||||
EXPECT_DEATH(empty_sav.begin()[0], deathstring);
|
||||
EXPECT_DEATH(empty_sav.cbegin()[0], deathstring);
|
||||
|
||||
for (const auto& v : empty_sav) {
|
||||
(void) v;
|
||||
CHECK(false);
|
||||
EXPECT_TRUE(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
|
||||
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
|
||||
void iterate_every_other_element(multi_span<int, dynamic_range> av)
|
||||
{
|
||||
// pick every other element
|
||||
|
||||
auto length = av.size() / 2;
|
||||
#if defined(_MSC_VER) && _MSC_VER > 1800
|
||||
auto bounds = strided_bounds<1>({length}, {2});
|
||||
#else
|
||||
auto bounds = strided_bounds<1>(multi_span_index<1>{length}, multi_span_index<1>{2});
|
||||
#endif
|
||||
strided_span<int, 1> strided(&av.data()[1], av.size() - 1, bounds);
|
||||
|
||||
CHECK(strided.size() == length);
|
||||
CHECK(strided.bounds().index_bounds()[0] == length);
|
||||
for (auto i = 0; i < strided.size(); ++i) {
|
||||
CHECK(strided[i] == av[2 * i + 1]);
|
||||
}
|
||||
|
||||
int idx = 0;
|
||||
for (auto num : strided) {
|
||||
CHECK(num == av[2 * idx + 1]);
|
||||
idx++;
|
||||
}
|
||||
}
|
||||
|
||||
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
|
||||
TEST_CASE("strided_span_section_iteration")
|
||||
TEST(strided_span_tests, strided_span_section_iteration)
|
||||
{
|
||||
int arr[8] = {4, 0, 5, 1, 6, 2, 7, 3};
|
||||
|
||||
@@ -642,12 +670,7 @@ TEST_CASE("strided_span_section_iteration")
|
||||
}
|
||||
}
|
||||
|
||||
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
|
||||
GSL_SUPPRESS(r.11) // NO-FORMAT: attribute
|
||||
GSL_SUPPRESS(r.3) // NO-FORMAT: attribute
|
||||
GSL_SUPPRESS(r.5) // NO-FORMAT: attribute
|
||||
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
|
||||
TEST_CASE("dynamic_strided_span_section_iteration")
|
||||
TEST(strided_span_tests, dynamic_strided_span_section_iteration)
|
||||
{
|
||||
auto arr = new int[8];
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
@@ -661,39 +684,7 @@ TEST_CASE("dynamic_strided_span_section_iteration")
|
||||
delete[] arr;
|
||||
}
|
||||
|
||||
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
|
||||
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
|
||||
GSL_SUPPRESS(bounds.2) // NO-FORMAT: attribute // TODO: does not work
|
||||
void iterate_second_slice(multi_span<int, dynamic_range, dynamic_range, dynamic_range> av)
|
||||
{
|
||||
const int expected[6] = {2, 3, 10, 11, 18, 19};
|
||||
auto section = av.section({0, 1, 0}, {3, 1, 2});
|
||||
|
||||
for (auto i = 0; i < section.extent<0>(); ++i) {
|
||||
for (auto j = 0; j < section.extent<1>(); ++j)
|
||||
for (auto k = 0; k < section.extent<2>(); ++k) {
|
||||
auto idx = multi_span_index<3>{i, j, k}; // avoid braces in the CHECK macro
|
||||
CHECK(section[idx] == expected[2 * i + 2 * j + k]);
|
||||
}
|
||||
}
|
||||
|
||||
for (auto i = 0; i < section.extent<0>(); ++i) {
|
||||
for (auto j = 0; j < section.extent<1>(); ++j)
|
||||
for (auto k = 0; k < section.extent<2>(); ++k)
|
||||
CHECK(section[i][j][k] == expected[2 * i + 2 * j + k]);
|
||||
}
|
||||
|
||||
int i = 0;
|
||||
for (const auto num : section) {
|
||||
CHECK(num == expected[i]);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
|
||||
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
|
||||
GSL_SUPPRESS(bounds.2) // NO-FORMAT: attribute
|
||||
TEST_CASE("strided_span_section_iteration_3d")
|
||||
TEST(strided_span_tests, strided_span_section_iteration_3d)
|
||||
{
|
||||
int arr[3][4][2]{};
|
||||
for (auto i = 0; i < 3; ++i) {
|
||||
@@ -707,12 +698,7 @@ TEST_CASE("strided_span_section_iteration_3d")
|
||||
}
|
||||
}
|
||||
|
||||
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
|
||||
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
|
||||
GSL_SUPPRESS(r.3) // NO-FORMAT: attribute
|
||||
GSL_SUPPRESS(r.5) // NO-FORMAT: attribute
|
||||
GSL_SUPPRESS(r.11) // NO-FORMAT: attribute
|
||||
TEST_CASE("dynamic_strided_span_section_iteration_3d")
|
||||
TEST(strided_span_tests, dynamic_strided_span_section_iteration_3d)
|
||||
{
|
||||
const auto height = 12, width = 2;
|
||||
const auto size = height * width;
|
||||
@@ -744,11 +730,13 @@ TEST_CASE("dynamic_strided_span_section_iteration_3d")
|
||||
delete[] arr;
|
||||
}
|
||||
|
||||
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
|
||||
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
|
||||
GSL_SUPPRESS(bounds.2) // NO-FORMAT: attribute
|
||||
TEST_CASE("strided_span_conversion")
|
||||
TEST(strided_span_tests, strided_span_conversion)
|
||||
{
|
||||
std::set_terminate([] {
|
||||
std::cerr << "Expected Death. strided_span_conversion";
|
||||
std::abort();
|
||||
});
|
||||
|
||||
// get an multi_span of 'c' values from the list of X's
|
||||
|
||||
struct X
|
||||
@@ -767,8 +755,8 @@ TEST_CASE("strided_span_conversion")
|
||||
// convert to 4x12 array of bytes
|
||||
auto av = as_multi_span(as_bytes(as_multi_span(&arr[0], 4)), dim(d1), dim(d2));
|
||||
|
||||
CHECK(av.bounds().index_bounds()[0] == 4);
|
||||
CHECK(av.bounds().index_bounds()[1] == 12);
|
||||
EXPECT_TRUE(av.bounds().index_bounds()[0] == 4);
|
||||
EXPECT_TRUE(av.bounds().index_bounds()[1] == 12);
|
||||
|
||||
// get the last 4 columns
|
||||
auto section = av.section({0, 2 * s}, {4, s}); // { { arr[0].c[0], arr[0].c[1], arr[0].c[2],
|
||||
@@ -778,8 +766,8 @@ TEST_CASE("strided_span_conversion")
|
||||
// convert to array 4x1 array of integers
|
||||
auto cs = section.as_strided_span<int>(); // { { arr[0].c }, {arr[1].c } , ... }
|
||||
|
||||
CHECK(cs.bounds().index_bounds()[0] == 4);
|
||||
CHECK(cs.bounds().index_bounds()[1] == 1);
|
||||
EXPECT_TRUE(cs.bounds().index_bounds()[0] == 4);
|
||||
EXPECT_TRUE(cs.bounds().index_bounds()[1] == 1);
|
||||
|
||||
// transpose to 1x4 array
|
||||
strided_bounds<2> reverse_bounds{
|
||||
@@ -791,16 +779,12 @@ TEST_CASE("strided_span_conversion")
|
||||
// slice to get a one-dimensional array of c's
|
||||
strided_span<int, 1> result = transposed[0];
|
||||
|
||||
CHECK(result.bounds().index_bounds()[0] == 4);
|
||||
CHECK_THROWS_AS(result.bounds().index_bounds()[1], fail_fast);
|
||||
EXPECT_TRUE(result.bounds().index_bounds()[0] == 4);
|
||||
EXPECT_DEATH(result.bounds().index_bounds()[1], deathstring);
|
||||
|
||||
int i = 0;
|
||||
for (auto& num : result) {
|
||||
CHECK(num == arr[i].c);
|
||||
EXPECT_TRUE(num == arr[i].c);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
#if __clang__ || __GNUC__
|
||||
#pragma GCC diagnostic pop
|
||||
#endif
|
||||
|
||||
+359
-375
File diff suppressed because it is too large
Load Diff
@@ -1,27 +0,0 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
|
||||
//
|
||||
// This code is licensed under the MIT License (MIT).
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#define CATCH_CONFIG_MAIN
|
||||
|
||||
#ifdef _MSC_VER
|
||||
|
||||
// blanket turn off warnings from CppCoreCheck from catch
|
||||
// so people aren't annoyed by them when running the tool.
|
||||
#include <CodeAnalysis/Warnings.h>
|
||||
#pragma warning(disable : ALL_CODE_ANALYSIS_WARNINGS) // from catch
|
||||
#endif // _MSC_VER
|
||||
|
||||
#include <catch/catch.hpp>
|
||||
+62
-48
@@ -14,120 +14,134 @@
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifdef _MSC_VER
|
||||
// blanket turn off warnings from CppCoreCheck from catch
|
||||
// so people aren't annoyed by them when running the tool.
|
||||
#pragma warning(disable : 26440 26426) // from catch
|
||||
|
||||
#endif
|
||||
|
||||
#include <catch/catch.hpp> // for AssertionHandler, StringRef, CHECK, TEST_...
|
||||
|
||||
#include <gsl/gsl_util> // for narrow, finally, narrow_cast, narrowing_e...
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <gsl/util> // finally, narrow_cast
|
||||
#include <gsl/narrow> // for narrow, narrowing_error
|
||||
#include <algorithm> // for move
|
||||
#include <functional> // for reference_wrapper, _Bind_helper<>::type
|
||||
#include <limits> // for numeric_limits
|
||||
#include <stdint.h> // for uint32_t, int32_t
|
||||
#include <type_traits> // for is_same
|
||||
#include <cstddef> // for std::ptrdiff_t
|
||||
|
||||
using namespace gsl;
|
||||
|
||||
TEST_CASE("sanity check for gsl::index typedef")
|
||||
namespace
|
||||
{
|
||||
void f(int& i) { i += 1; }
|
||||
static int j = 0;
|
||||
void g() { j += 1; }
|
||||
}
|
||||
|
||||
|
||||
TEST(utils_tests, sanity_check_for_gsl_index_typedef)
|
||||
{
|
||||
static_assert(std::is_same<gsl::index, std::ptrdiff_t>::value,
|
||||
"gsl::index represents wrong arithmetic type");
|
||||
}
|
||||
|
||||
void f(int& i) { i += 1; }
|
||||
|
||||
TEST_CASE("finally_lambda")
|
||||
TEST(utils_tests, finally_lambda)
|
||||
{
|
||||
int i = 0;
|
||||
{
|
||||
auto _ = finally([&]() { f(i); });
|
||||
CHECK(i == 0);
|
||||
EXPECT_TRUE(i == 0);
|
||||
}
|
||||
CHECK(i == 1);
|
||||
EXPECT_TRUE(i == 1);
|
||||
}
|
||||
|
||||
TEST_CASE("finally_lambda_move")
|
||||
TEST(utils_tests, finally_lambda_move)
|
||||
{
|
||||
int i = 0;
|
||||
{
|
||||
auto _1 = finally([&]() { f(i); });
|
||||
{
|
||||
auto _2 = std::move(_1);
|
||||
CHECK(i == 0);
|
||||
EXPECT_TRUE(i == 0);
|
||||
}
|
||||
CHECK(i == 1);
|
||||
EXPECT_TRUE(i == 1);
|
||||
{
|
||||
auto _2 = std::move(_1);
|
||||
CHECK(i == 1);
|
||||
EXPECT_TRUE(i == 1);
|
||||
}
|
||||
CHECK(i == 1);
|
||||
EXPECT_TRUE(i == 1);
|
||||
}
|
||||
CHECK(i == 1);
|
||||
EXPECT_TRUE(i == 1);
|
||||
}
|
||||
|
||||
TEST_CASE("finally_function_with_bind")
|
||||
TEST(utils_tests, finally_const_lvalue_lambda)
|
||||
{
|
||||
int i = 0;
|
||||
{
|
||||
const auto const_lvalue_lambda = [&]() { f(i); };
|
||||
auto _ = finally(const_lvalue_lambda);
|
||||
EXPECT_TRUE(i == 0);
|
||||
}
|
||||
EXPECT_TRUE(i == 1);
|
||||
}
|
||||
|
||||
TEST(utils_tests, finally_mutable_lvalue_lambda)
|
||||
{
|
||||
int i = 0;
|
||||
{
|
||||
auto mutable_lvalue_lambda = [&]() { f(i); };
|
||||
auto _ = finally(mutable_lvalue_lambda);
|
||||
EXPECT_TRUE(i == 0);
|
||||
}
|
||||
EXPECT_TRUE(i == 1);
|
||||
}
|
||||
|
||||
TEST(utils_tests, finally_function_with_bind)
|
||||
{
|
||||
int i = 0;
|
||||
{
|
||||
auto _ = finally(std::bind(&f, std::ref(i)));
|
||||
CHECK(i == 0);
|
||||
EXPECT_TRUE(i == 0);
|
||||
}
|
||||
CHECK(i == 1);
|
||||
EXPECT_TRUE(i == 1);
|
||||
}
|
||||
|
||||
static int j = 0;
|
||||
void g() { j += 1; }
|
||||
TEST_CASE("finally_function_ptr")
|
||||
TEST(utils_tests, finally_function_ptr)
|
||||
{
|
||||
j = 0;
|
||||
{
|
||||
auto _ = finally(&g);
|
||||
CHECK(j == 0);
|
||||
EXPECT_TRUE(j == 0);
|
||||
}
|
||||
CHECK(j == 1);
|
||||
EXPECT_TRUE(j == 1);
|
||||
}
|
||||
|
||||
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
|
||||
TEST_CASE("narrow_cast")
|
||||
TEST(utils_tests, narrow_cast)
|
||||
{
|
||||
int n = 120;
|
||||
char c = narrow_cast<char>(n);
|
||||
CHECK(c == 120);
|
||||
EXPECT_TRUE(c == 120);
|
||||
|
||||
n = 300;
|
||||
unsigned char uc = narrow_cast<unsigned char>(n);
|
||||
CHECK(uc == 44);
|
||||
EXPECT_TRUE(uc == 44);
|
||||
}
|
||||
|
||||
GSL_SUPPRESS(con.5) // NO-FORMAT: attribute
|
||||
TEST_CASE("narrow")
|
||||
TEST(utils_tests, narrow)
|
||||
{
|
||||
int n = 120;
|
||||
const char c = narrow<char>(n);
|
||||
CHECK(c == 120);
|
||||
EXPECT_TRUE(c == 120);
|
||||
|
||||
n = 300;
|
||||
CHECK_THROWS_AS(narrow<char>(n), narrowing_error);
|
||||
EXPECT_THROW(narrow<char>(n), narrowing_error);
|
||||
|
||||
const auto int32_max = std::numeric_limits<int32_t>::max();
|
||||
const auto int32_min = std::numeric_limits<int32_t>::min();
|
||||
|
||||
CHECK(narrow<uint32_t>(int32_t(0)) == 0);
|
||||
CHECK(narrow<uint32_t>(int32_t(1)) == 1);
|
||||
CHECK(narrow<uint32_t>(int32_max) == static_cast<uint32_t>(int32_max));
|
||||
EXPECT_TRUE(narrow<uint32_t>(int32_t(0)) == 0);
|
||||
EXPECT_TRUE(narrow<uint32_t>(int32_t(1)) == 1);
|
||||
EXPECT_TRUE(narrow<uint32_t>(int32_max) == static_cast<uint32_t>(int32_max));
|
||||
|
||||
CHECK_THROWS_AS(narrow<uint32_t>(int32_t(-1)), narrowing_error);
|
||||
CHECK_THROWS_AS(narrow<uint32_t>(int32_min), narrowing_error);
|
||||
EXPECT_THROW(narrow<uint32_t>(int32_t(-1)), narrowing_error);
|
||||
EXPECT_THROW(narrow<uint32_t>(int32_min), narrowing_error);
|
||||
|
||||
n = -42;
|
||||
CHECK_THROWS_AS(narrow<unsigned>(n), narrowing_error);
|
||||
|
||||
#if GSL_CONSTEXPR_NARROW
|
||||
static_assert(narrow<char>(120) == 120, "Fix GSL_CONSTEXPR_NARROW");
|
||||
#endif
|
||||
EXPECT_THROW(narrow<unsigned>(n), narrowing_error);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user