mirror of
https://github.com/boostorg/conversion.git
synced 2025-07-29 20:17:25 +02:00
Compare commits
418 Commits
boost-1.45
...
esp-idf-co
Author | SHA1 | Date | |
---|---|---|---|
d6316b5ba5 | |||
4ae8b4d495 | |||
71b6a864a4 | |||
b2a49138cf | |||
00f06d3549 | |||
968dfd47f1 | |||
f0fe02f7d6 | |||
19b5a1cf29 | |||
1a2e4fd8ef | |||
8d1721f312 | |||
c6ab56e983 | |||
1c62347706 | |||
56becb8fee | |||
67ae26b0f0 | |||
d4e593120d | |||
76ed70b0b1 | |||
4b7d551907 | |||
9e3216d22f | |||
4e557d31d1 | |||
46695ffb86 | |||
c1c098fac1 | |||
b171fe1ff6 | |||
a178d1a691 | |||
5176e189b8 | |||
49e6421271 | |||
a0bfc39077 | |||
2bf2bba4be | |||
f882ab890d | |||
e0bb75e096 | |||
80b9240f2e | |||
df86cb59c4 | |||
4fec53f3db | |||
db398fc1c8 | |||
1576ff7be6 | |||
9540be42b4 | |||
7624b70a6d | |||
78fc22c66a | |||
f1d5059c75 | |||
f2e83915c3 | |||
e0a40b7067 | |||
1dd1c98b79 | |||
a1788bc31b | |||
58c33270a2 | |||
e130cd860c | |||
9d4a518e74 | |||
1525157560 | |||
3421136ef0 | |||
441d4e4e1b | |||
b3f8e9f014 | |||
24f2769eb7 | |||
6806d99529 | |||
e38c0753ef | |||
6b3395e57d | |||
83e57ed513 | |||
56393150bb | |||
e8d104f34d | |||
a770ff87d7 | |||
29997a9fe1 | |||
2a0a2a59f5 | |||
51d544e6b8 | |||
ee92f4f98b | |||
d46e12c921 | |||
2648b144be | |||
32130aa427 | |||
c704762482 | |||
ae1f2a5cd1 | |||
993685b7d0 | |||
eafe2d352e | |||
3b9769567e | |||
610f9fe674 | |||
cbe18465d7 | |||
181eb0e6fc | |||
46694fb293 | |||
4d814efd3f | |||
da8b2fe695 | |||
a7b9f42b9a | |||
b6e9403ef0 | |||
58e8e78899 | |||
8f4b6ec985 | |||
a6100358f4 | |||
8f1126f6b2 | |||
b2019fa2df | |||
dc25edb4dd | |||
43ee183296 | |||
84ab59227d | |||
0dc31621c8 | |||
00b6a53f7e | |||
6199649ae2 | |||
a8253878c8 | |||
582fc91adb | |||
6a224fa5e9 | |||
5d66fea7d5 | |||
4e9021cac5 | |||
ff7ea09a73 | |||
6943537150 | |||
cf31a2ec00 | |||
800c10a4eb | |||
df57f25807 | |||
f0caf56d0b | |||
24d6ac1e87 | |||
8a03282736 | |||
40c42eb740 | |||
36945c35bd | |||
a0922ab598 | |||
77a4a55af4 | |||
5c3878f015 | |||
5e93f4086c | |||
e2a68fdec1 | |||
ee91295ac4 | |||
e3e4d3836a | |||
d1a51b897d | |||
9e6aa219d3 | |||
1e65265b8f | |||
90b4286753 | |||
5a757dcece | |||
36bc5aea44 | |||
ac7da550b4 | |||
e8954f3868 | |||
1707131ac3 | |||
2f2c1a3443 | |||
91b77e7373 | |||
e3dec04dd5 | |||
b40bc02f94 | |||
07082420b3 | |||
57e4e87e3b | |||
e913b7cb1c | |||
c0c02e560e | |||
bbd746d2ac | |||
bf75439403 | |||
edde5f442e | |||
d78d334dbb | |||
39d1abe754 | |||
524232ebab | |||
472ebeff30 | |||
13f14db751 | |||
8a49514506 | |||
c8bd860de6 | |||
a98a74d3dd | |||
f2e0225b1b | |||
701963d3c4 | |||
ce7a54af7f | |||
c2c4b884fe | |||
6f656adea3 | |||
c359ed53b2 | |||
47041de63a | |||
c58a55a512 | |||
dd165b4b0f | |||
b5552e351c | |||
297b115e6c | |||
a2299ec1f9 | |||
bf551ad8c9 | |||
fac805ddf6 | |||
3ce36a2848 | |||
4ba64ea889 | |||
d723c18fff | |||
6bd8edfa02 | |||
7e484c17f6 | |||
5f44bdcae8 | |||
a65fcf571f | |||
13d98f02d0 | |||
91aa2ce64d | |||
f7bd5e24c2 | |||
3af0af9405 | |||
c3c44c208a | |||
93a01eba32 | |||
b97f2542ec | |||
12fd711cc8 | |||
32d70a0764 | |||
1a0ffcadbf | |||
ab0727fdac | |||
633b852b81 | |||
9fc1a66d27 | |||
a4bd056b8e | |||
61938bc8d3 | |||
694f7b5033 | |||
335c868e9b | |||
a15dbed640 | |||
5c8c897d30 | |||
3e4a1669f1 | |||
cb9de2c55f | |||
89556c8a0f | |||
47bef9517f | |||
518203671c | |||
63b680c082 | |||
16890bf041 | |||
565e2882d3 | |||
24bc6ca6f2 | |||
4ad6f1bd4b | |||
5a6309db7a | |||
2333ee53f8 | |||
dbda7689e0 | |||
e15f179bfc | |||
82969ebb20 | |||
17f4b6fd4c | |||
2ea7487806 | |||
cd5a55aab5 | |||
f682bff3a7 | |||
1c37887f2b | |||
5c3159cd1c | |||
c484325310 | |||
0920c53e23 | |||
42e7585dd4 | |||
7b6c4eed7d | |||
824fbd9b06 | |||
8d373a0c99 | |||
77eff6b47d | |||
8705038cd2 | |||
bb224866a7 | |||
733438e072 | |||
c43b86d7f1 | |||
c290a26625 | |||
d08477900d | |||
63acf4f174 | |||
ce95319f03 | |||
f67347e308 | |||
d6a72e9ea5 | |||
46c6cc911b | |||
3c94791b1e | |||
80d480ca0b | |||
e81d4dfdbb | |||
675d0bb451 | |||
fe79553bf8 | |||
093e298c9d | |||
d2361fe05a | |||
b037563bf9 | |||
88244b57d4 | |||
e3cd0ca7fa | |||
79d6468aeb | |||
c6318e3819 | |||
e691b15b77 | |||
ca9c8d30bd | |||
bb46dbc46c | |||
d30dd9e3c8 | |||
b1b5305984 | |||
15ab0d6385 | |||
eb4ad73caf | |||
d963842d70 | |||
9ff79f4df9 | |||
746d466e38 | |||
674e71fb7d | |||
35d592fc21 | |||
7881eb5666 | |||
8562b480f8 | |||
6ff6958204 | |||
1e58331baf | |||
2af8c47558 | |||
deef894ae2 | |||
4a617adce1 | |||
969a913153 | |||
41a673db61 | |||
acb746274f | |||
dfb518997f | |||
93ee01ab69 | |||
4265f454e1 | |||
76b9f29c12 | |||
a065884c59 | |||
676bde6e1f | |||
7e7f494dd1 | |||
83639bd9ae | |||
0dded6d3a7 | |||
25d880f5eb | |||
b71fb9aada | |||
d9baa71302 | |||
80e858b182 | |||
646b958a92 | |||
04f441e989 | |||
e51cf081b3 | |||
7a48fea517 | |||
c9863cd254 | |||
132663404b | |||
a0cbfa8af6 | |||
efe20fcf76 | |||
84d9706182 | |||
5ed7bfd280 | |||
51a6aacb57 | |||
7bdc45327a | |||
a2e4606e6c | |||
9a6d983ee5 | |||
d754143308 | |||
82abf7b54f | |||
085bd6f93c | |||
5d288580ee | |||
7112ded1b9 | |||
8627f8bb8a | |||
f32fb4b5e5 | |||
a9697b88fd | |||
2312691bde | |||
7dd63d08d6 | |||
1eda87448e | |||
4ddb0bfb95 | |||
c07cbcee5c | |||
8c7c7b9237 | |||
3d60698843 | |||
3a62368d0e | |||
d2ae7fdbd7 | |||
e1caac418c | |||
2acef7aa44 | |||
8480a6d083 | |||
00f1246faf | |||
2f5d0c847b | |||
eb66b40e67 | |||
5ebb13bbad | |||
4a4150d0be | |||
9955dcce51 | |||
e8108e5066 | |||
a9778ad460 | |||
aba423d425 | |||
30e4efd062 | |||
19f316f768 | |||
9ac4e2bef1 | |||
2b76f558c1 | |||
0ee6400d5e | |||
fd264b0583 | |||
8a756eae7b | |||
c1c8485cb4 | |||
c372f482a2 | |||
fa7f4ba7a7 | |||
272e92ba7f | |||
c6c501c769 | |||
de3df54f80 | |||
27a7fac22e | |||
fff0066392 | |||
0e2235dc75 | |||
6a8c22d5c3 | |||
6c5f31e7a5 | |||
cd0167d6b8 | |||
1f7147d24b | |||
ccaa3059e8 | |||
b0b1ce9453 | |||
98d24e04f4 | |||
a385c3ec99 | |||
920e36f6ba | |||
dc9b364d6f | |||
95360b5df6 | |||
701d5f0bf0 | |||
266ebbcb88 | |||
e41ef929d2 | |||
c28efea0b3 | |||
00b609b498 | |||
2cfa78ab40 | |||
5c1dc5388c | |||
fce9ad4e1b | |||
febafb7cb8 | |||
f5d825e77f | |||
79a141ce9d | |||
78adbeaab1 | |||
0aaa89042a | |||
b37f56f478 | |||
c22d671409 | |||
8582e25571 | |||
a77fc4c0bb | |||
7b37688ca8 | |||
fa4a95c1dd | |||
5fa1edeee0 | |||
b72cb10ae6 | |||
4217a354f5 | |||
6c5fd3e86d | |||
2296357eaf | |||
33981b9400 | |||
2708853971 | |||
ae273a3e7b | |||
0d258ff7b1 | |||
ae5cbbbec8 | |||
e1dbc6ef04 | |||
0ad888867f | |||
408cbf0dcb | |||
808f210c0d | |||
69cf3b2766 | |||
ec8665d114 | |||
05036d3ae1 | |||
159b045ad7 | |||
2cc7aedd28 | |||
340f9b9e45 | |||
726f188aa4 | |||
e70cf014ad | |||
0d6a924f94 | |||
b9a7ee6deb | |||
9c1f3650f2 | |||
da5151abf6 | |||
f986e8e588 | |||
959b7ab0b4 | |||
42cb6d66cf | |||
209644c177 | |||
cf477316ed | |||
986ec16ecc | |||
2c41ed0473 | |||
fcc28133c5 | |||
de6746d67a | |||
67968d9b33 | |||
c004fb5fc6 | |||
ef439017ca | |||
fb63ebd4cd | |||
0f46451c1d | |||
663635d124 | |||
4d69e9f2af | |||
d23f722228 | |||
e9767dc4e8 | |||
2b069eb3a3 | |||
36db66bd12 | |||
4b1ca7bdf6 | |||
0a605d1bb3 | |||
04de824ead | |||
115ad05af5 | |||
ee7ea0011c | |||
a2c7ddf5d9 | |||
a7436ebacb | |||
e797b09590 | |||
2fca4d02a1 | |||
c17002aa73 | |||
ae431bacf8 | |||
425e7cb367 | |||
453555619e | |||
0743b5982c | |||
795b7e1809 | |||
c5bc634015 | |||
e6c83d0665 | |||
aac8fcb4b6 | |||
d436099477 |
171
.github/workflows/ci.yml
vendored
Normal file
171
.github/workflows/ci.yml
vendored
Normal file
@ -0,0 +1,171 @@
|
||||
name: CI
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
push:
|
||||
branches:
|
||||
- master
|
||||
- develop
|
||||
- feature/**
|
||||
|
||||
env:
|
||||
UBSAN_OPTIONS: print_stacktrace=1
|
||||
|
||||
jobs:
|
||||
posix:
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
include:
|
||||
- toolset: gcc-7
|
||||
cxxstd: "03,11,14,17"
|
||||
os: ubuntu-18.04
|
||||
- toolset: gcc-9
|
||||
cxxstd: "03,11,14,17,2a"
|
||||
os: ubuntu-18.04
|
||||
- toolset: gcc-10
|
||||
cxxstd: "03,11,14,17,2a"
|
||||
os: ubuntu-18.04
|
||||
cxxflags: "cxxflags=--coverage -fsanitize=address,leak,undefined -fno-sanitize-recover=undefined"
|
||||
linkflags: "linkflags=--coverage -lasan -lubsan"
|
||||
gcov_tool: "gcov-10"
|
||||
- toolset: clang
|
||||
compiler: clang++-10
|
||||
cxxstd: "03,11,14,17,2a"
|
||||
os: ubuntu-20.04
|
||||
cxxflags: "cxxflags=-fsanitize=address,undefined,integer -fno-sanitize-recover=undefined"
|
||||
linkflags: "linkflags=-fsanitize=address,undefined,integer"
|
||||
- toolset: clang
|
||||
cxxstd: "03,11,14,17,2a"
|
||||
os: macos-10.15
|
||||
cxxflags: "cxxflags=-fsanitize=address,undefined -fno-sanitize-recover=undefined"
|
||||
linkflags: "linkflags=-fsanitize=address,undefined"
|
||||
|
||||
runs-on: ${{matrix.os}}
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
|
||||
- name: Install packages
|
||||
if: matrix.install
|
||||
run: sudo apt install ${{matrix.install}}
|
||||
|
||||
- name: Setup Boost
|
||||
run: |
|
||||
echo GITHUB_REPOSITORY: $GITHUB_REPOSITORY
|
||||
LIBRARY=${GITHUB_REPOSITORY#*/}
|
||||
echo LIBRARY: $LIBRARY
|
||||
echo "LIBRARY=$LIBRARY" >> $GITHUB_ENV
|
||||
echo GITHUB_BASE_REF: $GITHUB_BASE_REF
|
||||
echo GITHUB_REF: $GITHUB_REF
|
||||
REF=${GITHUB_BASE_REF:-$GITHUB_REF}
|
||||
REF=${REF#refs/heads/}
|
||||
echo REF: $REF
|
||||
BOOST_BRANCH=develop && [ "$REF" == "master" ] && BOOST_BRANCH=master || true
|
||||
echo BOOST_BRANCH: $BOOST_BRANCH
|
||||
cd ..
|
||||
git clone -b $BOOST_BRANCH --depth 10 https://github.com/boostorg/boost.git boost-root
|
||||
cd boost-root
|
||||
git submodule update --init --depth 10 --jobs 2 tools/boostdep tools/inspect libs/filesystem
|
||||
python tools/boostdep/depinst/depinst.py --git_args "--depth 10 --jobs 3" filesystem
|
||||
rm -rf libs/$LIBRARY/*
|
||||
cp -r $GITHUB_WORKSPACE/* libs/$LIBRARY
|
||||
python tools/boostdep/depinst/depinst.py --include benchmark --include example --include examples --include tools --git_args "--depth 10 --jobs 3" $LIBRARY
|
||||
./bootstrap.sh
|
||||
./b2 -d0 headers
|
||||
./b2 -j4 variant=debug tools/inspect/build
|
||||
|
||||
- name: Run tests
|
||||
run: |
|
||||
cd ../boost-root
|
||||
./b2 -j3 libs/$LIBRARY/test toolset=${{matrix.toolset}} cxxstd=${{matrix.cxxstd}} variant=debug,release "${{matrix.cxxflags}}" "${{matrix.linkflags}}" "${{matrix.launcher}}"
|
||||
dist/bin/inspect libs/$LIBRARY
|
||||
|
||||
- name: Prepare coverage data
|
||||
if: matrix.gcov_tool
|
||||
run: |
|
||||
mkdir -p $GITHUB_WORKSPACE/coveralls
|
||||
|
||||
echo -e "#!/bin/bash\nexec ${{matrix.gcov_tool}} \"\$@\"" > $GITHUB_WORKSPACE/coveralls/gcov_wrapper.sh
|
||||
chmod +x $GITHUB_WORKSPACE/coveralls/gcov_wrapper.sh
|
||||
wget https://github.com/linux-test-project/lcov/archive/v1.15.zip
|
||||
unzip v1.15.zip
|
||||
LCOV="`pwd`/lcov-1.15/bin/lcov --gcov-tool $GITHUB_WORKSPACE/coveralls/gcov_wrapper.sh"
|
||||
|
||||
echo "$LCOV --directory ../boost-root/bin.v2/libs/$LIBRARY/ --base-directory `pwd`/libs/$LIBRARY/test --capture --output-file $GITHUB_WORKSPACE/coveralls/coverage.info"
|
||||
$LCOV --directory ../boost-root/bin.v2/libs/$LIBRARY/ --base-directory ../boost-root/ --capture --output-file $GITHUB_WORKSPACE/coveralls/coverage.info
|
||||
$LCOV --remove $GITHUB_WORKSPACE/coveralls/coverage.info "/usr*" "*/$LIBRARY/test/*" ${{matrix.ignore_coverage}} "*/$LIBRARY/tests/*" "*/$LIBRARY/examples/*" "*/$LIBRARY/example/*" -o $GITHUB_WORKSPACE/coveralls/coverage.info
|
||||
|
||||
cd ../boost-root
|
||||
OTHER_LIBS=`grep "submodule .*" .gitmodules | sed 's/\[submodule\ "\(.*\)"\]/"\*\/boost\/\1\.hpp" "\*\/boost\/\1\/\*"/g'| sed "/\"\*\/boost\/$LIBRARY\/\*\"/d" | sed ':a;N;$!ba;s/\n/ /g'`
|
||||
echo $OTHER_LIBS
|
||||
eval "$LCOV --remove $GITHUB_WORKSPACE/coveralls/coverage.info $OTHER_LIBS -o $GITHUB_WORKSPACE/coveralls/coverage.info"
|
||||
|
||||
- name: Coveralls
|
||||
uses: coverallsapp/github-action@master
|
||||
if: matrix.gcov_tool
|
||||
with:
|
||||
github-token: ${{ secrets.GITHUB_TOKEN }}
|
||||
path-to-lcov: ./coveralls/coverage.info
|
||||
parallel: true
|
||||
|
||||
windows:
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
include:
|
||||
- toolset: msvc-14.1
|
||||
cxxstd: "14,17,latest"
|
||||
addrmd: 32,64
|
||||
os: windows-2016
|
||||
- toolset: msvc-14.2
|
||||
cxxstd: "14,17,latest"
|
||||
addrmd: 32,64
|
||||
os: windows-2019
|
||||
- toolset: gcc
|
||||
cxxstd: "03,11,14,17,2a"
|
||||
addrmd: 64
|
||||
os: windows-2019
|
||||
|
||||
runs-on: ${{matrix.os}}
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
|
||||
- name: Setup Boost
|
||||
shell: cmd
|
||||
run: |
|
||||
echo GITHUB_REPOSITORY: %GITHUB_REPOSITORY%
|
||||
for /f %%i in ("%GITHUB_REPOSITORY%") do set LIBRARY=%%~nxi
|
||||
echo LIBRARY: %LIBRARY%
|
||||
echo LIBRARY=%LIBRARY%>>%GITHUB_ENV%
|
||||
echo GITHUB_BASE_REF: %GITHUB_BASE_REF%
|
||||
echo GITHUB_REF: %GITHUB_REF%
|
||||
if "%GITHUB_BASE_REF%" == "" set GITHUB_BASE_REF=%GITHUB_REF%
|
||||
set BOOST_BRANCH=develop
|
||||
for /f %%i in ("%GITHUB_BASE_REF%") do if "%%~nxi" == "master" set BOOST_BRANCH=master
|
||||
echo BOOST_BRANCH: %BOOST_BRANCH%
|
||||
cd ..
|
||||
git clone -b %BOOST_BRANCH% --depth 10 https://github.com/boostorg/boost.git boost-root
|
||||
cd boost-root
|
||||
xcopy /s /e /q %GITHUB_WORKSPACE% libs\%LIBRARY%\
|
||||
git submodule update --init tools/boostdep
|
||||
python tools/boostdep/depinst/depinst.py --include benchmark --include example --include examples --include tools --git_args "--jobs 3" %LIBRARY%
|
||||
cmd /c bootstrap
|
||||
b2 -d0 headers
|
||||
|
||||
- name: Run tests
|
||||
shell: cmd
|
||||
run: |
|
||||
cd ../boost-root
|
||||
b2 -j3 libs/%LIBRARY%/test toolset=${{matrix.toolset}} cxxstd=${{matrix.cxxstd}} address-model=${{matrix.addrmd}} variant=debug,release
|
||||
|
||||
finish:
|
||||
needs: posix
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Coveralls Finished
|
||||
uses: coverallsapp/github-action@master
|
||||
with:
|
||||
github-token: ${{ secrets.github_token }}
|
||||
parallel-finished: true
|
45
CMakeLists.txt
Normal file
45
CMakeLists.txt
Normal file
@ -0,0 +1,45 @@
|
||||
# Copyright 2019 Mike Dev
|
||||
# Distributed under the Boost Software License, Version 1.0.
|
||||
# See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
|
||||
|
||||
if(NOT DEFINED IDF_TARGET)
|
||||
|
||||
cmake_minimum_required( VERSION 3.5...3.20 )
|
||||
project( boost_conversion VERSION "${BOOST_SUPERPROJECT_VERSION}" LANGUAGES CXX )
|
||||
|
||||
add_library( boost_conversion INTERFACE )
|
||||
add_library( Boost::conversion ALIAS boost_conversion )
|
||||
|
||||
target_include_directories( boost_conversion INTERFACE include )
|
||||
|
||||
target_link_libraries( boost_conversion
|
||||
INTERFACE
|
||||
Boost::assert
|
||||
Boost::config
|
||||
Boost::core
|
||||
Boost::smart_ptr
|
||||
Boost::throw_exception
|
||||
Boost::type_traits
|
||||
Boost::typeof
|
||||
)
|
||||
|
||||
else()
|
||||
|
||||
FILE(GLOB_RECURSE headers include/*.h include/*.hpp)
|
||||
|
||||
idf_component_register(
|
||||
SRCS
|
||||
${headers}
|
||||
INCLUDE_DIRS
|
||||
include
|
||||
REQUIRES
|
||||
boost_assert
|
||||
boost_config
|
||||
boost_core
|
||||
boost_smart_ptr
|
||||
boost_throw_exception
|
||||
boost_type_traits
|
||||
boost_typeof
|
||||
)
|
||||
|
||||
endif()
|
15
README.md
Normal file
15
README.md
Normal file
@ -0,0 +1,15 @@
|
||||
# [Boost.Conversion](https://boost.org/libs/conversion)
|
||||
Boost.Conversion is one of the [Boost C++ Libraries](https://github.com/boostorg). This library improves program safety and clarity by performing otherwise messy conversions.
|
||||
|
||||
### Test results
|
||||
|
||||
@ | Build | Tests coverage | More info
|
||||
----------------|-------------- | -------------- |-----------
|
||||
Develop branch: | [](https://github.com/boostorg/conversion/actions/workflows/ci.yml) [](https://ci.appveyor.com/project/apolukhin/conversion/branch/develop) | [](https://coveralls.io/github/boostorg/conversion?branch=develop) | [details...](https://www.boost.org/development/tests/develop/developer/conversion.html)
|
||||
Master branch: | [](https://github.com/boostorg/conversion/actions/workflows/ci.yml) [](https://ci.appveyor.com/project/apolukhin/conversion/branch/master) | [](https://coveralls.io/github/boostorg/conversion?branch=master) | [details...](https://www.boost.org/development/tests/master/developer/conversion.html)
|
||||
|
||||
[Latest developer documentation](https://www.boost.org/doc/libs/develop/doc/html/conversion.html)
|
||||
|
||||
### License
|
||||
|
||||
Distributed under the [Boost Software License, Version 1.0](https://boost.org/LICENSE_1_0.txt).
|
140
cast.htm
140
cast.htm
@ -1,140 +0,0 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||
|
||||
<html>
|
||||
<head>
|
||||
<meta name="generator" content=
|
||||
"Microsoft FrontPage 5.0">
|
||||
<meta http-equiv="Content-Type" content=
|
||||
"text/html; charset=windows-1252">
|
||||
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
|
||||
<meta name="ProgId" content="FrontPage.Editor.Document">
|
||||
|
||||
<title>Header boost/cast.hpp Documentation</title>
|
||||
</head>
|
||||
|
||||
<body bgcolor="#FFFFFF" text="#000000">
|
||||
<h1><img src="../../boost.png" alt="boost.png (6897 bytes)" align=
|
||||
"middle" width="277" height="86">Header <a href=
|
||||
"../../boost/cast.hpp">boost/cast.hpp</a></h1>
|
||||
|
||||
<h2><a name="Cast Functions">Cast Functions</a></h2>
|
||||
|
||||
<p>The header <a href="../../boost/cast.hpp">boost/cast.hpp</a> provides <code>
|
||||
<a href="#Polymorphic_cast">polymorphic_cast</a> and</code> <a href=
|
||||
"#Polymorphic_cast"><code>polymorphic_downcast</code></a> function templates designed to
|
||||
complement the C++ built-in casts.</p>
|
||||
|
||||
<p>The program <a href="cast_test.cpp">cast_test.cpp</a> can be used to
|
||||
verify these function templates work as expected.</p>
|
||||
|
||||
<h3><a name="Polymorphic_cast">Polymorphic casts</a></h3>
|
||||
|
||||
<p>Pointers to polymorphic objects (objects of classes which define at
|
||||
least one virtual function) are sometimes downcast or crosscast.
|
||||
Downcasting means casting from a base class to a derived class.
|
||||
Crosscasting means casting across an inheritance hierarchy diagram, such
|
||||
as from one base to the other in a <code>Y</code> diagram hierarchy.</p>
|
||||
|
||||
<p>Such casts can be done with old-style casts, but this approach is
|
||||
never to be recommended. Old-style casts are sorely lacking in type
|
||||
safety, suffer poor readability, and are difficult to locate with search
|
||||
tools.</p>
|
||||
|
||||
<p>The C++ built-in <code>static_cast</code> can be used for efficiently
|
||||
downcasting pointers to polymorphic objects, but provides no error
|
||||
detection for the case where the pointer being cast actually points to
|
||||
the wrong derived class. The <code>polymorphic_downcast</code> template retains
|
||||
the efficiency of <code>static_cast</code> for non-debug compilations, but for
|
||||
debug compilations adds safety via an assert() that a <code>dynamic_cast</code>
|
||||
succeeds.</p>
|
||||
|
||||
<p>The C++ built-in <code>dynamic_cast</code> can be used for downcasts and
|
||||
crosscasts of pointers to polymorphic objects, but error notification in
|
||||
the form of a returned value of 0 is inconvenient to test, or worse yet,
|
||||
easy to forget to test. The throwing form of <code>dynamic_cast</code>, which
|
||||
works on references, can be used on pointers through the ugly expression
|
||||
&<code>dynamic_cast<T&>(*p)</code>, which causes undefined
|
||||
behavior if <code>p</code> is <code>0</code>. The <code>polymorphic_cast</code>
|
||||
template performs a <code>dynamic_cast</code> on a pointer, and throws an
|
||||
exception if the <code>dynamic_cast</code> returns 0.</p>
|
||||
|
||||
<p>A <code>polymorphic_downcast</code> should be used for
|
||||
downcasts that you are certain should succeed. Error checking is
|
||||
only performed in translation units where <code>NDEBUG</code> is
|
||||
not defined, via
|
||||
<pre> assert( dynamic_cast<Derived>(x) == x )
|
||||
</pre> where <code>x</code> is the source pointer. This approach
|
||||
ensures that not only is a non-zero pointer returned, but also
|
||||
that it is correct in the presence of multiple inheritance.
|
||||
Attempts to crosscast using <code>polymorphic_downcast</code> will
|
||||
fail to compile.
|
||||
<b>Warning:</b> Because <code>polymorphic_downcast</code> uses assert(), it
|
||||
violates the One Definition Rule (ODR) if NDEBUG is inconsistently
|
||||
defined across translation units. [See ISO Std 3.2]
|
||||
</p><p>
|
||||
For crosscasts, or when the success of a cast can only be known at
|
||||
runtime, or when efficiency is not important,
|
||||
<code>polymorphic_cast</code> is preferred. </p>
|
||||
|
||||
<p>The C++ built-in <code>dynamic_cast</code> must be used to cast references
|
||||
rather than pointers. It is also the only cast that can be used to check
|
||||
whether a given interface is supported; in that case a return of 0 isn't
|
||||
an error condition.</p>
|
||||
|
||||
<h3>polymorphic_cast and polymorphic_downcast synopsis</h3>
|
||||
|
||||
<blockquote>
|
||||
<pre>namespace boost {
|
||||
|
||||
template <class Derived, class Base>
|
||||
inline Derived polymorphic_cast(Base* x);
|
||||
// Throws: std::bad_cast if ( dynamic_cast<Derived>(x) == 0 )
|
||||
// Returns: dynamic_cast<Derived>(x)
|
||||
|
||||
template <class Derived, class Base>
|
||||
inline Derived polymorphic_downcast(Base* x);
|
||||
// Effects: assert( dynamic_cast<Derived>(x) == x );
|
||||
// Returns: static_cast<Derived>(x)
|
||||
|
||||
}
|
||||
</pre>
|
||||
</blockquote>
|
||||
|
||||
<h3>polymorphic_downcast example</h3>
|
||||
|
||||
<blockquote>
|
||||
<pre>#include <boost/cast.hpp>
|
||||
...
|
||||
class Fruit { public: virtual ~Fruit(){}; ... };
|
||||
class Banana : public Fruit { ... };
|
||||
...
|
||||
void f( Fruit * fruit ) {
|
||||
// ... logic which leads us to believe it is a Banana
|
||||
Banana * banana = boost::polymorphic_downcast<Banana*>(fruit);
|
||||
...
|
||||
</pre>
|
||||
</blockquote>
|
||||
|
||||
<h3>History</h3>
|
||||
|
||||
<p><code>polymorphic_cast</code> was suggested by Bjarne Stroustrup in "The C++
|
||||
Programming Language".<br>
|
||||
<code>polymorphic_downcast</code> was contributed by <a href=
|
||||
"http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams</a>.<code><br>
|
||||
An old
|
||||
numeric_cast</code> that was contributed by <a href=
|
||||
"http://www.boost.org/people/kevlin_henney.htm">Kevlin Henney</a> is now superseeded by the <a href="../numeric/conversion/doc/html/index.html">Boost Numeric Conversion Library</a></p>
|
||||
<hr>
|
||||
|
||||
<p>Revised
|
||||
<!--webbot bot="Timestamp" s-type="EDITED" s-format="%d %B, %Y" startspan
|
||||
-->June 23, 2005<!--webbot bot="Timestamp" endspan i-checksum="30348"
|
||||
--></p>
|
||||
|
||||
<p>© Copyright boost.org 1999. Permission to copy, use, modify, sell
|
||||
and distribute this document is granted provided this copyright notice
|
||||
appears in all copies. This document is provided "as is" without express
|
||||
or implied warranty, and with no claim as to its suitability for any
|
||||
purpose.</p>
|
||||
</body>
|
||||
</html>
|
38
doc/Jamfile.v2
Normal file
38
doc/Jamfile.v2
Normal file
@ -0,0 +1,38 @@
|
||||
# Copyright (c) 2016 Mikhail Maximov <vigorous.activity -at- gmail.com>
|
||||
|
||||
# Distributed under the Boost Software License, Version 1.0.
|
||||
# (See accompanying file LICENSE_1_0.txt or copy at
|
||||
# http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
project doc/conversion ;
|
||||
|
||||
using quickbook ;
|
||||
import boostbook ;
|
||||
|
||||
xml conversion : conversion.qbk ;
|
||||
boostbook standalone
|
||||
:
|
||||
conversion
|
||||
:
|
||||
<xsl:param>boost.root=../../../..
|
||||
# File name of HTML output:
|
||||
<xsl:param>root.filename=conversion
|
||||
# How far down we chunk nested sections, basically all of them:
|
||||
<xsl:param>chunk.section.depth=0
|
||||
# Don't put the first section on the same page as the TOC:
|
||||
<xsl:param>chunk.first.sections=0
|
||||
# How far down sections get TOC's
|
||||
<xsl:param>toc.section.depth=2
|
||||
# Max depth in each TOC:
|
||||
<xsl:param>toc.max.depth=2
|
||||
# How far down we go with TOC's
|
||||
<xsl:param>generate.section.toc.level=0
|
||||
|
||||
<xsl:param>generate.manifest=0
|
||||
;
|
||||
|
||||
###############################################################################
|
||||
alias boostdoc : conversion ;
|
||||
explicit boostdoc ;
|
||||
alias boostrelease ;
|
||||
explicit boostrelease ;
|
218
doc/conversion.qbk
Normal file
218
doc/conversion.qbk
Normal file
@ -0,0 +1,218 @@
|
||||
[/
|
||||
Copyright 2016 Mikhail Maximov.
|
||||
Copyright Antony Polukhin, 2020-2022.
|
||||
|
||||
Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</a>)
|
||||
]
|
||||
|
||||
[article The Conversion Library
|
||||
[quickbook 1.6]
|
||||
[compatibility-mode 1.5]
|
||||
[id conversion]
|
||||
[version 1.7]
|
||||
[authors [Stroustrup, Bjarne], [Abrahams, Dave], [Rasin, Boris], [Polukhin, Antony]]
|
||||
[copyright 2001 Beman Dawes, 2014-2022 Antony Polukhin]
|
||||
[license
|
||||
Distributed under the Boost Software License, Version 1.0.
|
||||
(See accompanying file LICENSE_1_0.txt or copy at
|
||||
[@http://www.boost.org/LICENSE_1_0.txt])
|
||||
]
|
||||
[source-mode c++]
|
||||
]
|
||||
|
||||
[/ QuickBook Document version 1.5 ]
|
||||
[/ Dec, 2016 ]
|
||||
|
||||
[section Description]
|
||||
|
||||
The Conversion Library improves program safety and clarity by performing
|
||||
otherwise messy conversions. It includes cast-style function templates designed
|
||||
to complement the C++ Standard's built-in casts.
|
||||
|
||||
To reduce coupling, particularly to standard library IOStreams,
|
||||
the Boost Conversion Library is supplied by several headers:
|
||||
|
||||
# The [@boost:boost/polymorphic_cast.hpp boost/polymorphic_cast.hpp] header
|
||||
provides [link polymorphic_cast `polymorphic_cast<>`] and
|
||||
[link polymorphic_downcast `polymorphic_downcast<>`]
|
||||
to perform safe casting between polymorphic types.
|
||||
# The [@boost:boost/polymorphic_pointer_cast.hpp boost/polymorphic_pointer_cast.hpp] header
|
||||
provides [link polymorphic_pointer_cast `polymorphic_pointer_cast<>`] and
|
||||
[link polymorphic_pointer_cast `polymorphic_pointer_downcast<>`]
|
||||
# The [@boost:boost/implicit_cast.hpp boost/implicit_cast.hpp] header provides `implicit_cast<>`
|
||||
to perform implicit casts only (no down-cast, no void*->T*, no U->T if T has only explicit constructors for U).
|
||||
# The [@boost:boost/lexical_cast.hpp boost/lexical_cast.hpp] header
|
||||
provides [@boost:doc/libs/release/libs/lexical_cast/ `lexical_cast<>`] general literal text conversions, such as an `int` represented as a `string`, or vice-versa.
|
||||
|
||||
[endsect]
|
||||
|
||||
[section Polymorphic casts]
|
||||
Pointers to polymorphic objects (objects of classes which define at
|
||||
least one virtual function) are sometimes downcast or crosscast.
|
||||
Downcasting means casting from a base class to a derived class.
|
||||
Crosscasting means casting across an inheritance hierarchy diagram, such
|
||||
as from one base to the other in a [^Y] diagram hierarchy.
|
||||
|
||||
Such casts can be done with old-style casts, but this approach is
|
||||
never to be recommended. Old-style casts are sorely lacking in type
|
||||
safety, suffer poor readability, and are difficult to locate with search
|
||||
tools.
|
||||
|
||||
[#polymorphic_downcast]
|
||||
[section polymorphic_downcast]
|
||||
|
||||
The C++ built-in `static_cast` can be used for efficiently
|
||||
downcasting pointers to polymorphic objects, but provides no error
|
||||
detection for the case where the pointer being cast actually points to
|
||||
the wrong derived class. The `polymorphic_downcast` template retains
|
||||
the efficiency of `static_cast` for non-debug compilations, but for
|
||||
debug compilations adds safety via an `assert()` that a `dynamic_cast`
|
||||
succeeds.
|
||||
|
||||
A `polymorphic_downcast` should be used for
|
||||
downcasts that you are certain should succeed. Error checking is
|
||||
only performed in translation units where `NDEBUG` is
|
||||
not defined, via
|
||||
```
|
||||
assert( dynamic_cast<Derived>(x) == x )
|
||||
```
|
||||
where `x` is the source pointer. This approach
|
||||
ensures that not only is a non-zero pointer returned, but also
|
||||
that it is correct in the presence of multiple inheritance.
|
||||
Attempts to crosscast using `polymorphic_downcast` will
|
||||
fail to compile.
|
||||
|
||||
[warning Because `polymorphic_downcast` uses `assert()`, it
|
||||
violates the One Definition Rule (ODR) if `NDEBUG` is inconsistently
|
||||
defined across translation units. See ISO Std 3.2]
|
||||
|
||||
[h4 Example:]
|
||||
```
|
||||
#include <boost/polymorphic_cast.hpp>
|
||||
...
|
||||
class Fruit { public: virtual ~Fruit(){}; ... };
|
||||
class Banana : public Fruit { ... };
|
||||
...
|
||||
void f( Fruit * fruit ) {
|
||||
// ... logic which leads us to believe it is a Banana
|
||||
Banana * banana = boost::polymorphic_downcast<Banana*>(fruit);
|
||||
...
|
||||
}
|
||||
```
|
||||
[endsect]
|
||||
|
||||
[#polymorphic_cast]
|
||||
[section polymorphic_cast]
|
||||
|
||||
The C++ built-in `dynamic_cast` can be used for downcasts and
|
||||
crosscasts of pointers to polymorphic objects, but error notification in
|
||||
the form of a returned value of 0 is inconvenient to test, or worse yet,
|
||||
easy to forget to test. The throwing form of `dynamic_cast`, which
|
||||
works on references, can be used on pointers through the ugly expression
|
||||
`&dynamic_cast<T&>(*p)`, which causes undefined
|
||||
behavior if `p` is `0`. The `polymorphic_cast`
|
||||
template performs a `dynamic_cast` on a pointer, and throws an
|
||||
exception if the `dynamic_cast` returns 0.
|
||||
|
||||
For crosscasts, or when the success of a cast can only be known at runtime,
|
||||
or when efficiency is not important, `polymorphic_cast` is preferred.
|
||||
|
||||
The C++ built-in `dynamic_cast` must be used to cast references rather than pointers.
|
||||
It is also the only cast that can be used to check whether a given interface is supported; in that case a return of 0 isn't an error condition.
|
||||
|
||||
[endsect]
|
||||
|
||||
|
||||
[#polymorphic_pointer_cast]
|
||||
[section polymorphic_pointer_cast]
|
||||
While `polymorphic_downcast` and `polymorphic_cast` work with built-in pointer types only,
|
||||
`polymorphic_pointer_downcast` and `polymorphic_pointer_cast` are more generic versions
|
||||
with support for any pointer type for which the following expressions would be valid:
|
||||
|
||||
For `polymorphic_pointer_downcast`:
|
||||
```
|
||||
static_pointer_cast<Derived>(p);
|
||||
dynamic_pointer_cast<Derived>(p);
|
||||
```
|
||||
For `polymorphic_pointer_cast`:
|
||||
```
|
||||
dynamic_pointer_cast<Derived>(p);
|
||||
!p; // conversion to bool with negation
|
||||
```
|
||||
|
||||
This includes C++ built-in pointers, `std::shared_ptr`,
|
||||
`boost::shared_ptr`, `boost::intrusive_ptr`, etc.
|
||||
|
||||
|
||||
[h4 Example:]
|
||||
```
|
||||
#include <boost/polymorphic_pointer_cast.hpp>
|
||||
|
||||
class Fruit { public: virtual ~Fruit(){} };
|
||||
class Banana : public Fruit {};
|
||||
|
||||
// Use one of these:
|
||||
typedef Fruit* FruitPtr;
|
||||
typedef std::shared_ptr<Fruit> FruitPtr;
|
||||
typedef boost::shared_ptr<Fruit> FruitPtr;
|
||||
typedef boost::intrusive_ptr<Fruit> FruitPtr;
|
||||
|
||||
void f(FruitPtr fruit) {
|
||||
// ... logic which leads us to believe it is a banana
|
||||
auto banana = boost::polymorphic_pointer_downcast<Banana>(fruit);
|
||||
...
|
||||
}
|
||||
```
|
||||
|
||||
[endsect]
|
||||
|
||||
[endsect]
|
||||
|
||||
[section Synopsis]
|
||||
```
|
||||
namespace boost {
|
||||
|
||||
// Throws: std::bad_cast if ( dynamic_cast<Derived>(x) == 0 )
|
||||
// Returns: dynamic_cast<Derived>(x)
|
||||
template <class Derived, class Base>
|
||||
inline Derived polymorphic_cast(Base* x);
|
||||
|
||||
// Effects: assert( dynamic_cast<Derived>(x) == x );
|
||||
// Returns: static_cast<Derived>(x)
|
||||
template <class Derived, class Base>
|
||||
inline Derived polymorphic_downcast(Base* x);
|
||||
|
||||
// Effects: assert( dynamic_cast<Derived>(&x) == &x );
|
||||
// Returns: static_cast<Derived>(x)
|
||||
template <class Derived, class Base>
|
||||
inline Derived polymorphic_downcast(Base& x);
|
||||
|
||||
// Throws: std::bad_cast if ( dynamic_pointer_cast<Derived>(x) == 0 )
|
||||
// Returns: dynamic_pointer_cast<Derived>(x)
|
||||
template <class Derived, class Base>
|
||||
inline auto polymorphic_pointer_cast(Base x);
|
||||
|
||||
// Effects: assert( dynamic_pointer_cast<Derived>(x) == x );
|
||||
// Returns: static_pointer_cast<Derived>(x)
|
||||
template <class Derived, class Base>
|
||||
inline auto polymorphic_pointer_downcast(Base x);
|
||||
|
||||
}
|
||||
```
|
||||
[endsect]
|
||||
|
||||
|
||||
[section History]
|
||||
`polymorphic_cast` was suggested by Bjarne Stroustrup in "The C++ Programming Language".
|
||||
|
||||
`polymorphic_downcast` was contributed by [@http://www.boost.org/people/dave_abrahams.htm Dave Abrahams].
|
||||
|
||||
`polymorphic_pointer_downcast` was contributed by [@http://www.boost.org/people/boris_rasin.htm Boris Rasin]
|
||||
and `polymorphic_pointer_cast` by Antony Polukhin.
|
||||
|
||||
`polymorphic_downcast` overload for references was contributed by Julien Delacroix.
|
||||
|
||||
An old `numeric_cast` that was contributed by [@http://www.boost.org/people/kevlin_henney.htm Kevlin Henney]
|
||||
is now superseded by the [@boost:numeric_conversion/doc/html/html/boost_numericconversion/improved_numeric_cast__.html Boost Numeric Conversion Library]
|
||||
[endsect]
|
@ -1,184 +0,0 @@
|
||||
// Copyright Alexander Nasonov & Paul A. Bristow 2006.
|
||||
|
||||
// Use, modification and distribution are subject to the
|
||||
// Boost Software License, Version 1.0.
|
||||
// (See accompanying file LICENSE_1_0.txt
|
||||
// or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_DETAIL_LCAST_PRECISION_HPP_INCLUDED
|
||||
#define BOOST_DETAIL_LCAST_PRECISION_HPP_INCLUDED
|
||||
|
||||
#include <climits>
|
||||
#include <ios>
|
||||
#include <limits>
|
||||
|
||||
#include <boost/config.hpp>
|
||||
#include <boost/integer_traits.hpp>
|
||||
|
||||
#ifndef BOOST_NO_IS_ABSTRACT
|
||||
// Fix for SF:1358600 - lexical_cast & pure virtual functions & VC 8 STL
|
||||
#include <boost/mpl/if.hpp>
|
||||
#include <boost/type_traits/is_abstract.hpp>
|
||||
#endif
|
||||
|
||||
#if defined(BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS) || \
|
||||
(defined(BOOST_MSVC) && (BOOST_MSVC<1310))
|
||||
|
||||
#define BOOST_LCAST_NO_COMPILE_TIME_PRECISION
|
||||
#endif
|
||||
|
||||
#ifdef BOOST_LCAST_NO_COMPILE_TIME_PRECISION
|
||||
#include <boost/assert.hpp>
|
||||
#else
|
||||
#include <boost/static_assert.hpp>
|
||||
#endif
|
||||
|
||||
namespace boost { namespace detail {
|
||||
|
||||
class lcast_abstract_stub {};
|
||||
|
||||
#ifndef BOOST_LCAST_NO_COMPILE_TIME_PRECISION
|
||||
// Calculate an argument to pass to std::ios_base::precision from
|
||||
// lexical_cast. See alternative implementation for broken standard
|
||||
// libraries in lcast_get_precision below. Keep them in sync, please.
|
||||
template<class T>
|
||||
struct lcast_precision
|
||||
{
|
||||
#ifdef BOOST_NO_IS_ABSTRACT
|
||||
typedef std::numeric_limits<T> limits; // No fix for SF:1358600.
|
||||
#else
|
||||
typedef BOOST_DEDUCED_TYPENAME boost::mpl::if_<
|
||||
boost::is_abstract<T>
|
||||
, std::numeric_limits<lcast_abstract_stub>
|
||||
, std::numeric_limits<T>
|
||||
>::type limits;
|
||||
#endif
|
||||
|
||||
BOOST_STATIC_CONSTANT(bool, use_default_precision =
|
||||
!limits::is_specialized || limits::is_exact
|
||||
);
|
||||
|
||||
BOOST_STATIC_CONSTANT(bool, is_specialized_bin =
|
||||
!use_default_precision &&
|
||||
limits::radix == 2 && limits::digits > 0
|
||||
);
|
||||
|
||||
BOOST_STATIC_CONSTANT(bool, is_specialized_dec =
|
||||
!use_default_precision &&
|
||||
limits::radix == 10 && limits::digits10 > 0
|
||||
);
|
||||
|
||||
BOOST_STATIC_CONSTANT(std::streamsize, streamsize_max =
|
||||
boost::integer_traits<std::streamsize>::const_max
|
||||
);
|
||||
|
||||
BOOST_STATIC_CONSTANT(unsigned int, precision_dec = limits::digits10 + 1U);
|
||||
|
||||
BOOST_STATIC_ASSERT(!is_specialized_dec ||
|
||||
precision_dec <= streamsize_max + 0UL
|
||||
);
|
||||
|
||||
BOOST_STATIC_CONSTANT(unsigned long, precision_bin =
|
||||
2UL + limits::digits * 30103UL / 100000UL
|
||||
);
|
||||
|
||||
BOOST_STATIC_ASSERT(!is_specialized_bin ||
|
||||
(limits::digits + 0UL < ULONG_MAX / 30103UL &&
|
||||
precision_bin > limits::digits10 + 0UL &&
|
||||
precision_bin <= streamsize_max + 0UL)
|
||||
);
|
||||
|
||||
BOOST_STATIC_CONSTANT(std::streamsize, value =
|
||||
is_specialized_bin ? precision_bin
|
||||
: is_specialized_dec ? precision_dec : 6
|
||||
);
|
||||
};
|
||||
#endif
|
||||
|
||||
template<class T>
|
||||
inline std::streamsize lcast_get_precision(T* = 0)
|
||||
{
|
||||
#ifndef BOOST_LCAST_NO_COMPILE_TIME_PRECISION
|
||||
return lcast_precision<T>::value;
|
||||
#else // Follow lcast_precision algorithm at run-time:
|
||||
|
||||
#ifdef BOOST_NO_IS_ABSTRACT
|
||||
typedef std::numeric_limits<T> limits; // No fix for SF:1358600.
|
||||
#else
|
||||
typedef BOOST_DEDUCED_TYPENAME boost::mpl::if_<
|
||||
boost::is_abstract<T>
|
||||
, std::numeric_limits<lcast_abstract_stub>
|
||||
, std::numeric_limits<T>
|
||||
>::type limits;
|
||||
#endif
|
||||
|
||||
bool const use_default_precision =
|
||||
!limits::is_specialized || limits::is_exact;
|
||||
|
||||
if(!use_default_precision)
|
||||
{ // Includes all built-in floating-point types, float, double ...
|
||||
// and UDT types for which digits (significand bits) is defined (not zero)
|
||||
|
||||
bool const is_specialized_bin =
|
||||
limits::radix == 2 && limits::digits > 0;
|
||||
bool const is_specialized_dec =
|
||||
limits::radix == 10 && limits::digits10 > 0;
|
||||
std::streamsize const streamsize_max =
|
||||
(boost::integer_traits<std::streamsize>::max)();
|
||||
|
||||
if(is_specialized_bin)
|
||||
{ // Floating-point types with
|
||||
// limits::digits defined by the specialization.
|
||||
|
||||
unsigned long const digits = limits::digits;
|
||||
unsigned long const precision = 2UL + digits * 30103UL / 100000UL;
|
||||
// unsigned long is selected because it is at least 32-bits
|
||||
// and thus ULONG_MAX / 30103UL is big enough for all types.
|
||||
BOOST_ASSERT(
|
||||
digits < ULONG_MAX / 30103UL &&
|
||||
precision > limits::digits10 + 0UL &&
|
||||
precision <= streamsize_max + 0UL
|
||||
);
|
||||
return precision;
|
||||
}
|
||||
else if(is_specialized_dec)
|
||||
{ // Decimal Floating-point type, most likely a User Defined Type
|
||||
// rather than a real floating-point hardware type.
|
||||
unsigned int const precision = limits::digits10 + 1U;
|
||||
BOOST_ASSERT(precision <= streamsize_max + 0UL);
|
||||
return precision;
|
||||
}
|
||||
}
|
||||
|
||||
// Integral type (for which precision has no effect)
|
||||
// or type T for which limits is NOT specialized,
|
||||
// so assume stream precision remains the default 6 decimal digits.
|
||||
// Warning: if your User-defined Floating-point type T is NOT specialized,
|
||||
// then you may lose accuracy by only using 6 decimal digits.
|
||||
// To avoid this, you need to specialize T with either
|
||||
// radix == 2 and digits == the number of significand bits,
|
||||
// OR
|
||||
// radix = 10 and digits10 == the number of decimal digits.
|
||||
|
||||
return 6;
|
||||
#endif
|
||||
}
|
||||
|
||||
template<class T>
|
||||
inline void lcast_set_precision(std::ios_base& stream, T*)
|
||||
{
|
||||
stream.precision(lcast_get_precision<T>());
|
||||
}
|
||||
|
||||
template<class Source, class Target>
|
||||
inline void lcast_set_precision(std::ios_base& stream, Source*, Target*)
|
||||
{
|
||||
std::streamsize const s = lcast_get_precision(static_cast<Source*>(0));
|
||||
std::streamsize const t = lcast_get_precision(static_cast<Target*>(0));
|
||||
stream.precision(s > t ? s : t);
|
||||
}
|
||||
|
||||
}}
|
||||
|
||||
#endif // BOOST_DETAIL_LCAST_PRECISION_HPP_INCLUDED
|
||||
|
27
include/boost/implicit_cast.hpp
Executable file → Normal file
27
include/boost/implicit_cast.hpp
Executable file → Normal file
@ -2,28 +2,37 @@
|
||||
// Distributed under the Boost Software License, Version 1.0. (See
|
||||
// accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
#ifndef IMPLICIT_CAST_DWA200356_HPP
|
||||
# define IMPLICIT_CAST_DWA200356_HPP
|
||||
|
||||
# include <boost/mpl/identity.hpp>
|
||||
#ifndef BOOST_IMPLICIT_CAST_DWA200356_HPP
|
||||
#define BOOST_IMPLICIT_CAST_DWA200356_HPP
|
||||
|
||||
#include <boost/config.hpp>
|
||||
#ifdef BOOST_HAS_PRAGMA_ONCE
|
||||
# pragma once
|
||||
#endif
|
||||
|
||||
namespace boost {
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<class T> struct icast_identity
|
||||
{
|
||||
typedef T type;
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
// implementation originally suggested by C. Green in
|
||||
// http://lists.boost.org/MailArchives/boost/msg00886.php
|
||||
|
||||
// The use of identity creates a non-deduced form, so that the
|
||||
// explicit template argument must be supplied
|
||||
template <typename T>
|
||||
inline T implicit_cast (typename mpl::identity<T>::type x) {
|
||||
BOOST_CONSTEXPR inline T implicit_cast (typename boost::detail::icast_identity<T>::type x) {
|
||||
return x;
|
||||
}
|
||||
|
||||
// incomplete return type now is here
|
||||
//template <typename T>
|
||||
//void implicit_cast (...);
|
||||
|
||||
} // namespace boost
|
||||
|
||||
|
||||
#endif // IMPLICIT_CAST_DWA200356_HPP
|
||||
#endif // BOOST_IMPLICIT_CAST_DWA200356_HPP
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -1,6 +1,7 @@
|
||||
// boost cast.hpp header file ----------------------------------------------//
|
||||
// boost polymorphic_cast.hpp header file ----------------------------------------------//
|
||||
|
||||
// (C) Copyright Kevlin Henney and Dave Abrahams 1999.
|
||||
// (C) Copyright Boris Rasin 2014.
|
||||
// Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
@ -8,7 +9,12 @@
|
||||
// See http://www.boost.org/libs/conversion for Documentation.
|
||||
|
||||
// Revision History
|
||||
// 23 JUn 05 numeric_cast removed and redirected to the new verion (Fernando Cacciola)
|
||||
// 10 Nov 14 polymorphic_pointer_downcast moved to a separate header,
|
||||
// minor improvements to stisfy latest Boost coding style
|
||||
// 08 Nov 14 Add polymorphic_pointer_downcast (Boris Rasin)
|
||||
// 09 Jun 14 "cast.hpp" was renamed to "polymorphic_cast.hpp" and
|
||||
// inclusion of numeric_cast was removed (Antony Polukhin)
|
||||
// 23 Jun 05 numeric_cast removed and redirected to the new verion (Fernando Cacciola)
|
||||
// 02 Apr 01 Removed BOOST_NO_LIMITS workarounds and included
|
||||
// <boost/limits.hpp> instead (the workaround did not
|
||||
// actually compile when BOOST_NO_LIMITS was defined in
|
||||
@ -40,26 +46,23 @@
|
||||
// place in nested namespace.
|
||||
// 3 Aug 99 Initial version
|
||||
|
||||
#ifndef BOOST_CAST_HPP
|
||||
#define BOOST_CAST_HPP
|
||||
#ifndef BOOST_POLYMORPHIC_CAST_HPP
|
||||
#define BOOST_POLYMORPHIC_CAST_HPP
|
||||
|
||||
# include <boost/config.hpp>
|
||||
# include <boost/assert.hpp>
|
||||
# include <typeinfo>
|
||||
# include <boost/type.hpp>
|
||||
# include <boost/limits.hpp>
|
||||
# include <boost/detail/select_type.hpp>
|
||||
|
||||
// It has been demonstrated numerous times that MSVC 6.0 fails silently at link
|
||||
// time if you use a template function which has template parameters that don't
|
||||
// appear in the function's argument list.
|
||||
//
|
||||
// TODO: Add this to config.hpp?
|
||||
# if defined(BOOST_MSVC) && BOOST_MSVC < 1300
|
||||
# define BOOST_EXPLICIT_DEFAULT_TARGET , ::boost::type<Target>* = 0
|
||||
# else
|
||||
# define BOOST_EXPLICIT_DEFAULT_TARGET
|
||||
# endif
|
||||
#ifdef BOOST_HAS_PRAGMA_ONCE
|
||||
# pragma once
|
||||
#endif
|
||||
|
||||
# include <boost/assert.hpp>
|
||||
# include <boost/core/addressof.hpp>
|
||||
# include <boost/core/enable_if.hpp>
|
||||
# include <boost/throw_exception.hpp>
|
||||
# include <boost/type_traits/is_reference.hpp>
|
||||
# include <boost/type_traits/remove_reference.hpp>
|
||||
|
||||
# include <typeinfo>
|
||||
|
||||
namespace boost
|
||||
{
|
||||
@ -73,16 +76,16 @@ namespace boost
|
||||
// section 15.8 exercise 1, page 425.
|
||||
|
||||
template <class Target, class Source>
|
||||
inline Target polymorphic_cast(Source* x BOOST_EXPLICIT_DEFAULT_TARGET)
|
||||
inline Target polymorphic_cast(Source* x)
|
||||
{
|
||||
Target tmp = dynamic_cast<Target>(x);
|
||||
if ( tmp == 0 ) throw std::bad_cast();
|
||||
if ( tmp == 0 ) boost::throw_exception( std::bad_cast() );
|
||||
return tmp;
|
||||
}
|
||||
|
||||
// polymorphic_downcast ----------------------------------------------------//
|
||||
|
||||
// BOOST_ASSERT() checked polymorphic downcast. Crosscasts prohibited.
|
||||
// BOOST_ASSERT() checked raw pointer polymorphic downcast. Crosscasts prohibited.
|
||||
|
||||
// WARNING: Because this cast uses BOOST_ASSERT(), it violates
|
||||
// the One Definition Rule if used in multiple translation units
|
||||
@ -92,16 +95,32 @@ namespace boost
|
||||
// Contributed by Dave Abrahams
|
||||
|
||||
template <class Target, class Source>
|
||||
inline Target polymorphic_downcast(Source* x BOOST_EXPLICIT_DEFAULT_TARGET)
|
||||
inline Target polymorphic_downcast(Source* x)
|
||||
{
|
||||
BOOST_ASSERT( dynamic_cast<Target>(x) == x ); // detect logic error
|
||||
return static_cast<Target>(x);
|
||||
}
|
||||
|
||||
# undef BOOST_EXPLICIT_DEFAULT_TARGET
|
||||
// BOOST_ASSERT() checked reference polymorphic downcast. Crosscasts prohibited.
|
||||
|
||||
// WARNING: Because this cast uses BOOST_ASSERT(), it violates
|
||||
// the One Definition Rule if used in multiple translation units
|
||||
// where BOOST_DISABLE_ASSERTS, BOOST_ENABLE_ASSERT_HANDLER
|
||||
// NDEBUG are defined inconsistently.
|
||||
|
||||
// Contributed by Julien Delacroix
|
||||
|
||||
template <class Target, class Source>
|
||||
inline typename boost::enable_if_c<
|
||||
boost::is_reference<Target>::value, Target
|
||||
>::type polymorphic_downcast(Source& x)
|
||||
{
|
||||
typedef typename boost::remove_reference<Target>::type* target_pointer_type;
|
||||
return *boost::polymorphic_downcast<target_pointer_type>(
|
||||
boost::addressof(x)
|
||||
);
|
||||
}
|
||||
|
||||
} // namespace boost
|
||||
|
||||
# include <boost/numeric/conversion/cast.hpp>
|
||||
|
||||
#endif // BOOST_CAST_HPP
|
||||
#endif // BOOST_POLYMORPHIC_CAST_HPP
|
79
include/boost/polymorphic_pointer_cast.hpp
Normal file
79
include/boost/polymorphic_pointer_cast.hpp
Normal file
@ -0,0 +1,79 @@
|
||||
// boost polymorphic_pointer_cast.hpp header file ----------------------------------------------//
|
||||
// (C) Copyright Boris Rasin, 2014-2021.
|
||||
// (C) Copyright Antony Polukhin, 2014-2022.
|
||||
// Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
// See http://www.boost.org/libs/conversion for Documentation.
|
||||
|
||||
#ifndef BOOST_CONVERSION_POLYMORPHIC_POINTER_CAST_HPP
|
||||
#define BOOST_CONVERSION_POLYMORPHIC_POINTER_CAST_HPP
|
||||
|
||||
# include <boost/config.hpp>
|
||||
# include <boost/assert.hpp>
|
||||
# include <boost/pointer_cast.hpp>
|
||||
# include <boost/throw_exception.hpp>
|
||||
# include <boost/utility/declval.hpp>
|
||||
# ifdef BOOST_NO_CXX11_DECLTYPE
|
||||
# include <boost/typeof/typeof.hpp>
|
||||
# endif
|
||||
|
||||
#ifdef BOOST_HAS_PRAGMA_ONCE
|
||||
# pragma once
|
||||
#endif
|
||||
|
||||
namespace boost
|
||||
{
|
||||
// See the documentation for descriptions of how to choose between
|
||||
// static_pointer_cast<>, dynamic_pointer_cast<>, polymorphic_pointer_cast<> and polymorphic_pointer_downcast<>
|
||||
|
||||
// polymorphic_pointer_downcast --------------------------------------------//
|
||||
|
||||
// BOOST_ASSERT() checked polymorphic downcast. Crosscasts prohibited.
|
||||
// Supports any type with static_pointer_cast/dynamic_pointer_cast functions:
|
||||
// built-in pointers, std::shared_ptr, boost::shared_ptr, boost::intrusive_ptr, etc.
|
||||
|
||||
// WARNING: Because this cast uses BOOST_ASSERT(), it violates
|
||||
// the One Definition Rule if used in multiple translation units
|
||||
// where BOOST_DISABLE_ASSERTS, BOOST_ENABLE_ASSERT_HANDLER
|
||||
// NDEBUG are defined inconsistently.
|
||||
|
||||
// Contributed by Boris Rasin
|
||||
|
||||
namespace detail
|
||||
{
|
||||
template <typename Target, typename Source>
|
||||
struct dynamic_pointer_cast_result
|
||||
{
|
||||
#ifdef BOOST_NO_CXX11_DECLTYPE
|
||||
BOOST_TYPEOF_NESTED_TYPEDEF_TPL(nested, dynamic_pointer_cast<Target>(boost::declval<Source>()))
|
||||
typedef typename nested::type type;
|
||||
#else
|
||||
typedef decltype(dynamic_pointer_cast<Target>(boost::declval<Source>())) type;
|
||||
#endif
|
||||
};
|
||||
}
|
||||
|
||||
template <typename Target, typename Source>
|
||||
inline typename detail::dynamic_pointer_cast_result<Target, Source>::type
|
||||
polymorphic_pointer_downcast (const Source& x)
|
||||
{
|
||||
BOOST_ASSERT(dynamic_pointer_cast<Target> (x) == x);
|
||||
return static_pointer_cast<Target> (x);
|
||||
}
|
||||
|
||||
template <typename Target, typename Source>
|
||||
inline typename detail::dynamic_pointer_cast_result<Target, Source>::type
|
||||
polymorphic_pointer_cast (const Source& x)
|
||||
{
|
||||
typename detail::dynamic_pointer_cast_result<Target, Source>::type tmp
|
||||
= dynamic_pointer_cast<Target> (x);
|
||||
if ( !tmp ) boost::throw_exception( std::bad_cast() );
|
||||
|
||||
return tmp;
|
||||
}
|
||||
|
||||
} // namespace boost
|
||||
|
||||
#endif // BOOST_CONVERSION_POLYMORPHIC_POINTER_CAST_HPP
|
67
index.html
67
index.html
@ -1,38 +1,37 @@
|
||||
<!DOCTYPE html>
|
||||
<!--
|
||||
Copyright (c) 2016 Mikhail Maximov
|
||||
vigorous.activity at gmail dot com
|
||||
|
||||
Copyright (c) Antony Polukhin, 2021-2022
|
||||
|
||||
Distributed under the Boost Software License,
|
||||
Version 1.0. (See accompanying file LICENSE_1_0.txt
|
||||
or copy at http://boost.org/LICENSE_1_0.txt)
|
||||
-->
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<meta http-equiv="Content-Language" content="en-us">
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
|
||||
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
|
||||
<meta name="ProgId" content="FrontPage.Editor.Document">
|
||||
<title>Boost Conversion Library</title>
|
||||
<meta charset="utf-8">
|
||||
<meta http-equiv="refresh" content="0; url=https://www.boost.org/doc/libs/master/doc/html/conversion.html">
|
||||
<title>Boost.Conversion</title>
|
||||
<style>
|
||||
body {
|
||||
background: #fff;
|
||||
color: #000;
|
||||
}
|
||||
a {
|
||||
color: #00f;
|
||||
text-decoration: none;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<body bgcolor="#FFFFFF" text="#000000">
|
||||
|
||||
<h1><img border="0" src="../../boost.png" align="center" width="277" height="86">Boost
|
||||
Conversion Library</h1>
|
||||
|
||||
<p>The Conversion Library improves program safety and clarity by performing
|
||||
otherwise messy conversions. It includes cast-style function templates designed to complement the C++
|
||||
Standard's built-in casts.</p>
|
||||
<p>To reduce coupling, particularly to standard library IOStreams, the Boost
|
||||
Conversion Library is
|
||||
supplied by several headers:</p>
|
||||
<ul>
|
||||
<li>The <a href="cast.htm">boost/cast</a> header provides <b>polymorphic_cast<></b>
|
||||
and <b>polymorphic_downcast<></b> to perform safe casting between
|
||||
polymorphic types.<br>
|
||||
</li>
|
||||
<li>The <a href="lexical_cast.htm">boost/lexical_cast</a> header provides <b>lexical_cast<></b>
|
||||
general literal text conversions, such as an <code>int</code> represented as
|
||||
a <code>string</code>, or vice-versa.</li>
|
||||
</ul>
|
||||
<hr>
|
||||
<p>Revised <!--webbot bot="Timestamp" S-Type="EDITED"
|
||||
S-Format="%d %B, %Y" startspan -->June 23, 2005<!--webbot bot="Timestamp" endspan i-checksum="30348" -->
|
||||
</p>
|
||||
|
||||
<body>
|
||||
<p>
|
||||
Automatic redirection failed, please go to
|
||||
<a href="https://www.boost.org/doc/libs/master/doc/html/conversion.html">https://www.boost.org/doc/libs/master/doc/html/conversion.html</a>
|
||||
</p>
|
||||
<p>
|
||||
© Antony Polukhin, 2014-2022
|
||||
</p>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
</html>
|
||||
|
270
lexical_cast.htm
270
lexical_cast.htm
@ -1,270 +0,0 @@
|
||||
<!-- saved from url=(0022)http://internet.e-mail -->
|
||||
<!doctype html public "-//W3C//DTD HTML Transitional 4.0//EN">
|
||||
<html>
|
||||
<head>
|
||||
<title>lexical_cast</title>
|
||||
<meta name="author" content="Kevlin Henney, mailto:kevlin@curbralan.com">
|
||||
<meta name="generator" content="Microsoft FrontPage 5.0">
|
||||
</head>
|
||||
<body bgcolor="#FFFFFF" text="#000000">
|
||||
<h1><img src="../../boost.png" alt="boost.png (6897 bytes)" align="center" width="277" height="86">Header
|
||||
<a href="../../boost/lexical_cast.hpp">boost/lexical_cast.hpp</a></h1>
|
||||
<ul type="square">
|
||||
<li>
|
||||
<a href="#motivation">Motivation</a></li>
|
||||
<li>
|
||||
<a href="#examples">Examples</a></li>
|
||||
<li>
|
||||
<a href="#synopsis">Synopsis</a></li>
|
||||
<li>
|
||||
<a href="#lexical_cast"><code>lexical_cast</code></a></li>
|
||||
<li>
|
||||
<a href="#bad_lexical_cast"><code>bad_lexical_cast</code></a></li>
|
||||
<li>
|
||||
<a href="#faq">Frequently Asked Questions</a></li>
|
||||
<li>
|
||||
<a href="#references">References</a></li>
|
||||
<li>
|
||||
<a href="#changes">Changes</a></li>
|
||||
</ul>
|
||||
<hr>
|
||||
<h2><a name="motivation">Motivation</a></h2>
|
||||
Sometimes a value must be converted to a literal text form, such as an <code>int</code>
|
||||
represented as a <code>string</code>, or vice-versa, when a <code>string</code>
|
||||
is interpreted as an <code>int</code>. Such examples are common when converting
|
||||
between data types internal to a program and representation external to a
|
||||
program, such as windows and configuration files.
|
||||
<p>
|
||||
The standard C and C++ libraries offer a number of facilities for performing
|
||||
such conversions. However, they vary with their ease of use, extensibility, and
|
||||
safety.
|
||||
<p>
|
||||
For instance, there are a number of limitations with the family of standard C
|
||||
functions typified by <code>atoi</code>:
|
||||
<ul type="square">
|
||||
<li>
|
||||
Conversion is supported in one direction only: from text to internal data type.
|
||||
Converting the other way using the C library requires either the inconvenience
|
||||
and compromised safety of the <code>sprintf</code> function, or the loss of
|
||||
portability associated with non-standard functions such as <code>itoa</code>.
|
||||
</li>
|
||||
<li>
|
||||
The range of types supported is only a subset of the built-in numeric types,
|
||||
namely <code>int</code>, <code>long</code>, and <code>double</code>.
|
||||
</li>
|
||||
<li>
|
||||
The range of types cannot be extended in a uniform manner. For instance,
|
||||
conversion from string representation to <code>complex</code> or <code>rational</code>.
|
||||
</li>
|
||||
</ul>
|
||||
The standard C functions typified by <code>strtol</code> have the same basic
|
||||
limitations, but offer finer control over the conversion process. However, for
|
||||
the common case such control is often either not required or not used. The <code>scanf</code>
|
||||
family of functions offer even greater control, but also lack safety and ease
|
||||
of use.
|
||||
<p>
|
||||
The standard C++ library offers <code>stringstream</code> for the kind of
|
||||
in-core formatting being discussed. It offers a great deal of control over the
|
||||
formatting and conversion of I/O to and from arbitrary types through text.
|
||||
However, for simple conversions direct use of <code>stringstream</code> can be
|
||||
either clumsy (with the introduction of extra local variables and the loss of
|
||||
infix-expression convenience) or obscure (where <code>stringstream</code>
|
||||
objects are created as temporary objects in an expression). Facets provide a
|
||||
comprehensive concept and facility for controlling textual representation, but
|
||||
their perceived complexity and high entry level requires an extreme degree of
|
||||
involvement for simple conversions, and excludes all but a few programmers.
|
||||
<p>
|
||||
The <code>lexical_cast</code> function template offers a convenient and
|
||||
consistent form for supporting common conversions to and from arbitrary types
|
||||
when they are represented as text. The simplification it offers is in
|
||||
expression-level convenience for such conversions. For more involved
|
||||
conversions, such as where precision or formatting need tighter control than is
|
||||
offered by the default behavior of <code>lexical_cast</code>, the conventional <code>
|
||||
stringstream</code> approach is recommended. Where the conversions are
|
||||
numeric to numeric, <code><a href="../numeric/conversion/doc/html/boost_numericconversion/improved_numeric_cast__.html">numeric_cast</a></code>
|
||||
may offer more reasonable behavior than <code>lexical_cast</code>.
|
||||
<p>
|
||||
For a good discussion of the options and issues involved in string-based
|
||||
formatting, including comparison of <code>stringstream</code>, <code>lexical_cast</code>,
|
||||
and others, see Herb Sutter's article, <a href="http://www.gotw.ca/publications/mill19.htm">
|
||||
<i>The String Formatters of Manor Farm</i></a>.
|
||||
<p>
|
||||
<hr>
|
||||
<h2><a name="examples">Examples</a></h2>
|
||||
The following example treats command line arguments as a sequence of numeric
|
||||
data: <blockquote>
|
||||
<pre>int main(int argc, char * argv[])
|
||||
{
|
||||
using boost::lexical_cast;
|
||||
using boost::bad_lexical_cast;
|
||||
|
||||
std::vector<short> args;
|
||||
|
||||
while(*++argv)
|
||||
{
|
||||
try
|
||||
{
|
||||
args.push_back(lexical_cast<short>(*argv));
|
||||
}
|
||||
catch(bad_lexical_cast &)
|
||||
{
|
||||
args.push_back(0);
|
||||
}
|
||||
}
|
||||
...
|
||||
}
|
||||
</pre>
|
||||
</blockquote>The following example uses numeric data in a string expression: <blockquote>
|
||||
<pre>void log_message(const std::string &);
|
||||
|
||||
void log_errno(int yoko)
|
||||
{
|
||||
log_message("Error " + boost::lexical_cast<std::string>(yoko) + ": " + strerror(yoko));
|
||||
}
|
||||
</pre>
|
||||
</blockquote>
|
||||
<hr>
|
||||
<h2><a name="synopsis">Synopsis</a></h2>
|
||||
Library features defined in <a href="../../boost/lexical_cast.hpp"><code>"boost/lexical_cast.hpp"</code></a>:
|
||||
<blockquote>
|
||||
<pre>namespace boost
|
||||
{
|
||||
class <a href="#bad_lexical_cast">bad_lexical_cast</a>;
|
||||
template<typename Target, typename Source>
|
||||
Target <a href="#lexical_cast">lexical_cast</a>(const Source& arg);
|
||||
}
|
||||
</pre>
|
||||
</blockquote>Unit test defined in <a href="lexical_cast_test.cpp"><code>"lexical_cast_test.cpp"</code></a>.
|
||||
<p>
|
||||
<hr>
|
||||
<h2><a name="lexical_cast"><code>lexical_cast</code></a></h2>
|
||||
<blockquote>
|
||||
<pre>template<typename Target, typename Source>
|
||||
Target lexical_cast(const Source& arg);
|
||||
</pre>
|
||||
</blockquote>Returns the result of streaming <code>arg</code> into a
|
||||
standard library string-based stream and then out as a <code>Target</code> object.
|
||||
Where <code>Target</code> is either <code>std::string</code>
|
||||
or <code>std::wstring</code>, stream extraction takes the whole content
|
||||
of the string, including spaces, rather than relying on the default
|
||||
<code>operator>></code> behavior.
|
||||
If the conversion is unsuccessful, a <a href="#bad_lexical_cast">
|
||||
<code>bad_lexical_cast</code></a> exception is thrown.
|
||||
<p>
|
||||
The requirements on the argument and result types are:
|
||||
<ul type="square">
|
||||
<li>
|
||||
<code>Source</code> is <i>OutputStreamable</i>, meaning that an <code>operator<<</code>
|
||||
is defined that takes a <code>std::ostream</code> or <code>std::wostream</code> object on the
|
||||
left hand side and an instance of the argument type on the right.
|
||||
</li>
|
||||
<li>
|
||||
<code>Target</code> is <i>InputStreamable</i>, meaning that an <code>operator>></code>
|
||||
is defined that takes a <code>std::istream</code> or <code>std::wistream</code> object on the left hand side
|
||||
and an instance of the result type on the right.
|
||||
</li>
|
||||
<li>
|
||||
<code>Target</code> is <i>CopyConstructible</i> [20.1.3].
|
||||
</li>
|
||||
<li>
|
||||
<code>Target</code> is <i>DefaultConstructible</i>, meaning that it is possible
|
||||
to <i>default-initialize</i> an object of that type [8.5, 20.1.4].
|
||||
</li>
|
||||
</ul>
|
||||
The character type of the underlying stream is assumed to be <code>char</code> unless
|
||||
either the <code>Source</code> or the <code>Target</code> requires wide-character
|
||||
streaming, in which case the underlying stream uses <code>wchar_t</code>.
|
||||
<code>Source</code> types that require wide-character streaming are <code>wchar_t</code>,
|
||||
<code>wchar_t *</code>, and <code>std::wstring</code>. <code>Target</code> types that
|
||||
require wide-character streaming are <code>wchar_t</code> and <code>std::wstring</code>.
|
||||
<p>
|
||||
Where a higher degree of control is required over conversions, <code>std::stringstream</code>
|
||||
and <code>std::wstringstream</code> offer a more appropriate path. Where non-stream-based conversions are
|
||||
required, <code>lexical_cast</code>
|
||||
is the wrong tool for the job and is not special-cased for such scenarios.
|
||||
<p>
|
||||
<hr>
|
||||
<h2><a name="bad_lexical_cast"><code>bad_lexical_cast</code></a></h2>
|
||||
<blockquote>
|
||||
<pre>class bad_lexical_cast : public std::bad_cast
|
||||
{
|
||||
public:
|
||||
... // <i>same member function interface as</i> std::exception
|
||||
};
|
||||
</pre>
|
||||
</blockquote>Exception used to indicate runtime <a href="#lexical_cast"><code>lexical_cast</code></a>
|
||||
failure.
|
||||
<hr>
|
||||
|
||||
<h2><a name="faq">Frequently Asked Questions</h2>
|
||||
<p> Q: Why does <code>lexical_cast<int8_t>("127")</code> throw <code>bad_lexical_cast</code>?
|
||||
<br> A: The type <code>int8_t</code> is a typedef to <code>char</code> or <code>signed char</code>.
|
||||
Lexical conversion to these types is simply reading a byte from source but since the source has
|
||||
more than one byte, the exception is thrown.
|
||||
<p>Please use other integer types such as <code>int</code> or <code>short int</code>. If bounds checking
|
||||
is important, you can also call <a href="../../libs/numeric/conversion/doc/html/boost_numericconversion/improved_numeric_cast__.html">numeric_cast</a>:
|
||||
|
||||
<pre><a href="../../libs/numeric/conversion/doc/html/boost_numericconversion/improved_numeric_cast__.html">numeric_cast</a><int8_t>(lexical_cast<int>("127"));</pre>
|
||||
|
||||
<p> Q: What does <code>lexical_cast<std::string></code> of an <code>int8_t</code> or <code>uint8_t</code> not do what I expect?
|
||||
<br> A: As above, note that <code>int8_t</code> and <code>uint8_t</code> are actually chars and are formatted as such. To avoid this, cast to an integer type first:
|
||||
|
||||
<pre>lexical_cast<std::string>(static_cast<int>(n));</pre>
|
||||
|
||||
<p> Q: The implementation always resets the <code>ios_base::skipws</code> flag of an underlying stream object. It breaks my <code>operator>></code> that works only in presence of this flag. Can you remove code that resets the flag?
|
||||
<br> A: May be in a future version. There is no requirement in <a href="#n1973">[N1973]</a> to reset the flag but remember that <a href="#n1973">[N1973]</a> is not yet accepted by the committee. By the way, it's a great opportunity to make your <code>operator>></code> conform to the standard. Read a good C++ book, study <code>std::sentry</code> and <a href="../../libs/io/doc/ios_state.html">ios_state_saver</a>.
|
||||
</ul>
|
||||
<h2><a name="references">References</h2>
|
||||
<ul type="square">
|
||||
<a name="n1973"></a><li> [N1973] Kevlin Henney, Beman Dawes, Lexical Conversion Library Proposal for TR2,
|
||||
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n1973.html">N1973</a>.
|
||||
<a name="tuning"></a><li> [Tuning] Alexander Nasonov, Fine Tuning for lexical_cast,
|
||||
<a href="http://www.accu.org/var/uploads/journals/overload74.pdf">Overload #74</a>,
|
||||
August 2006.</li>
|
||||
</ul>
|
||||
<h2><a name="changes">Changes</a></h2>
|
||||
<h3>August, October 2006:</h3>
|
||||
<ul type="square">
|
||||
<li>Better performance for many combinations of <code>Source</code> and <code>Target</code>
|
||||
types. Refer to <a href="#tuning">[Tuning]</a> for more details.
|
||||
</li>
|
||||
</ul>
|
||||
<h3>June 2005:</h3>
|
||||
<ul type="square">
|
||||
<li>Call-by-const reference for the parameters. This requires partial specialization
|
||||
of class templates, so it doesn't work for MSVC 6, and it uses the original
|
||||
pass by value there.<br>
|
||||
</li>
|
||||
<li>The MSVC 6 support is deprecated, and will be removed in a future Boost
|
||||
version. </li>
|
||||
</ul>
|
||||
<h3>Earlier:</h3>
|
||||
|
||||
<ul type="square">
|
||||
<li>The previous version of <code>lexical_cast</code> used the default stream
|
||||
precision for reading and writing floating-point numbers. For numerics that
|
||||
have a corresponding specialization of <code>std::numeric_limits</code>, the
|
||||
current version now chooses a precision to match. <br>
|
||||
<li>The previous version of <code>lexical_cast</code> did not support conversion
|
||||
to or from any wide-character-based types. For compilers with full language
|
||||
and library support for wide characters, <code>lexical_cast</code> now supports
|
||||
conversions from <code>wchar_t</code>, <code>wchar_t *</code>, and <code>std::wstring</code>
|
||||
and to <code>wchar_t</code> and <code>std::wstring</code>. <br>
|
||||
<li>The previous version of <code>lexical_cast</code> assumed that the conventional
|
||||
stream extractor operators were sufficient for reading values. However, string
|
||||
I/O is asymmetric, with the result that spaces play the role of I/O separators
|
||||
rather than string content. The current version fixes this error for <code>std::string</code>
|
||||
and, where supported, <code>std::wstring</code>: <code>lexical_cast<std::string>("Hello,
|
||||
World")</code> succeeds instead of failing with a <code>bad_lexical_cast</code>
|
||||
exception. <br>
|
||||
<li>The previous version of <code>lexical_cast</code> allowed unsafe and meaningless
|
||||
conversions to pointers. The current version now throws a <code>bad_lexical_cast</code>
|
||||
for conversions to pointers: <code>lexical_cast<char *>("Goodbye, World")</code>
|
||||
now throws an exception instead of causing undefined behavior.
|
||||
</ul>
|
||||
<p>
|
||||
<hr>
|
||||
|
||||
<div align="right"><small><i>© Copyright Kevlin Henney, 2000–2005</i></small></div>
|
||||
</body>
|
||||
</html>
|
@ -1,783 +0,0 @@
|
||||
// Unit test for boost::lexical_cast.
|
||||
//
|
||||
// See http://www.boost.org for most recent version, including documentation.
|
||||
//
|
||||
// Copyright Terje Sletteb<65> and Kevlin Henney, 2005.
|
||||
// Copyright Alexander Nasonov, 2006.
|
||||
//
|
||||
// Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt).
|
||||
//
|
||||
// Note: The unit test no longer compile on MSVC 6, but lexical_cast itself works for it.
|
||||
|
||||
#include <boost/config.hpp>
|
||||
|
||||
#if defined(__INTEL_COMPILER)
|
||||
#pragma warning(disable: 193 383 488 981 1418 1419)
|
||||
#elif defined(BOOST_MSVC)
|
||||
#pragma warning(disable: 4097 4100 4121 4127 4146 4244 4245 4511 4512 4701 4800)
|
||||
#endif
|
||||
|
||||
#include <boost/lexical_cast.hpp>
|
||||
|
||||
#include <boost/cstdint.hpp>
|
||||
#include <boost/test/unit_test.hpp>
|
||||
#include <boost/test/floating_point_comparison.hpp>
|
||||
|
||||
#include <string>
|
||||
#include <memory>
|
||||
|
||||
#if (defined(BOOST_HAS_LONG_LONG) || defined(BOOST_HAS_MS_INT64)) \
|
||||
&& !(defined(BOOST_MSVC) && BOOST_MSVC < 1300)
|
||||
#define LCAST_TEST_LONGLONG
|
||||
#endif
|
||||
|
||||
template<class CharT>
|
||||
struct my_traits : std::char_traits<CharT>
|
||||
{
|
||||
};
|
||||
|
||||
template<class CharT>
|
||||
struct my_allocator : std::allocator<CharT>
|
||||
{
|
||||
};
|
||||
|
||||
// Test all 65536 values if true:
|
||||
bool const lcast_test_small_integral_types_completely = false;
|
||||
|
||||
// lcast_integral_test_counter: use when testing all values of an integral
|
||||
// types is not possible. Max. portable value is 32767.
|
||||
int const lcast_integral_test_counter=1000;
|
||||
|
||||
using namespace boost;
|
||||
|
||||
void test_conversion_to_char();
|
||||
void test_conversion_to_int();
|
||||
void test_conversion_to_double();
|
||||
void test_conversion_to_bool();
|
||||
void test_conversion_to_string();
|
||||
void test_conversion_from_to_wchar_t_alias();
|
||||
void test_conversion_to_pointer();
|
||||
void test_conversion_from_wchar_t();
|
||||
void test_conversion_to_wchar_t();
|
||||
void test_conversion_from_wstring();
|
||||
void test_conversion_to_wstring();
|
||||
void test_bad_lexical_cast();
|
||||
void test_no_whitespace_stripping();
|
||||
void test_conversion_from_to_short();
|
||||
void test_conversion_from_to_ushort();
|
||||
void test_conversion_from_to_int();
|
||||
void test_conversion_from_to_uint();
|
||||
void test_conversion_from_to_long();
|
||||
void test_conversion_from_to_ulong();
|
||||
void test_conversion_from_to_intmax_t();
|
||||
void test_conversion_from_to_uintmax_t();
|
||||
#ifdef LCAST_TEST_LONGLONG
|
||||
void test_conversion_from_to_longlong();
|
||||
void test_conversion_from_to_ulonglong();
|
||||
#endif
|
||||
#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
|
||||
void test_traits();
|
||||
void test_wtraits();
|
||||
void test_allocator();
|
||||
void test_wallocator();
|
||||
#endif
|
||||
|
||||
unit_test::test_suite *init_unit_test_suite(int, char *[])
|
||||
{
|
||||
unit_test_framework::test_suite *suite =
|
||||
BOOST_TEST_SUITE("lexical_cast unit test");
|
||||
suite->add(BOOST_TEST_CASE(test_conversion_to_char));
|
||||
suite->add(BOOST_TEST_CASE(test_conversion_to_int));
|
||||
suite->add(BOOST_TEST_CASE(test_conversion_to_double));
|
||||
suite->add(BOOST_TEST_CASE(test_conversion_to_bool));
|
||||
suite->add(BOOST_TEST_CASE(test_conversion_from_to_wchar_t_alias));
|
||||
suite->add(BOOST_TEST_CASE(test_conversion_to_pointer));
|
||||
suite->add(BOOST_TEST_CASE(test_conversion_to_string));
|
||||
#ifndef BOOST_LCAST_NO_WCHAR_T
|
||||
suite->add(BOOST_TEST_CASE(test_conversion_from_wchar_t));
|
||||
suite->add(BOOST_TEST_CASE(test_conversion_to_wchar_t));
|
||||
suite->add(BOOST_TEST_CASE(test_conversion_from_wstring));
|
||||
suite->add(BOOST_TEST_CASE(test_conversion_to_wstring));
|
||||
#endif
|
||||
suite->add(BOOST_TEST_CASE(test_bad_lexical_cast));
|
||||
suite->add(BOOST_TEST_CASE(test_no_whitespace_stripping));
|
||||
suite->add(BOOST_TEST_CASE(&test_conversion_from_to_short));
|
||||
suite->add(BOOST_TEST_CASE(&test_conversion_from_to_ushort));
|
||||
suite->add(BOOST_TEST_CASE(&test_conversion_from_to_int));
|
||||
suite->add(BOOST_TEST_CASE(&test_conversion_from_to_uint));
|
||||
suite->add(BOOST_TEST_CASE(&test_conversion_from_to_long));
|
||||
suite->add(BOOST_TEST_CASE(&test_conversion_from_to_ulong));
|
||||
suite->add(BOOST_TEST_CASE(&test_conversion_from_to_intmax_t));
|
||||
suite->add(BOOST_TEST_CASE(&test_conversion_from_to_uintmax_t));
|
||||
#ifdef LCAST_TEST_LONGLONG
|
||||
suite->add(BOOST_TEST_CASE(&test_conversion_from_to_longlong));
|
||||
suite->add(BOOST_TEST_CASE(&test_conversion_from_to_ulonglong));
|
||||
#endif
|
||||
#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
|
||||
suite->add(BOOST_TEST_CASE(&test_traits));
|
||||
suite->add(BOOST_TEST_CASE(&test_wtraits));
|
||||
suite->add(BOOST_TEST_CASE(&test_allocator));
|
||||
suite->add(BOOST_TEST_CASE(&test_wallocator));
|
||||
#endif
|
||||
|
||||
return suite;
|
||||
}
|
||||
|
||||
void test_conversion_to_char()
|
||||
{
|
||||
BOOST_CHECK_EQUAL('A', lexical_cast<char>('A'));
|
||||
BOOST_CHECK_EQUAL(' ', lexical_cast<char>(' '));
|
||||
BOOST_CHECK_EQUAL('1', lexical_cast<char>(1));
|
||||
BOOST_CHECK_EQUAL('0', lexical_cast<char>(0));
|
||||
BOOST_CHECK_THROW(lexical_cast<char>(123), bad_lexical_cast);
|
||||
BOOST_CHECK_EQUAL('1', lexical_cast<char>(1.0));
|
||||
BOOST_CHECK_EQUAL('1', lexical_cast<char>(true));
|
||||
BOOST_CHECK_EQUAL('0', lexical_cast<char>(false));
|
||||
BOOST_CHECK_EQUAL('A', lexical_cast<char>("A"));
|
||||
BOOST_CHECK_EQUAL(' ', lexical_cast<char>(" "));
|
||||
BOOST_CHECK_THROW(lexical_cast<char>(""), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<char>("Test"), bad_lexical_cast);
|
||||
BOOST_CHECK_EQUAL('A', lexical_cast<char>(std::string("A")));
|
||||
BOOST_CHECK_EQUAL(' ', lexical_cast<char>(std::string(" ")));
|
||||
BOOST_CHECK_THROW(
|
||||
lexical_cast<char>(std::string("")), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(
|
||||
lexical_cast<char>(std::string("Test")), bad_lexical_cast);
|
||||
}
|
||||
|
||||
void test_conversion_to_int()
|
||||
{
|
||||
BOOST_CHECK_EQUAL(1, lexical_cast<int>('1'));
|
||||
BOOST_CHECK_EQUAL(0, lexical_cast<int>('0'));
|
||||
BOOST_CHECK_THROW(lexical_cast<int>('A'), bad_lexical_cast);
|
||||
BOOST_CHECK_EQUAL(1, lexical_cast<int>(1));
|
||||
BOOST_CHECK_EQUAL(1, lexical_cast<int>(1.0));
|
||||
|
||||
BOOST_CHECK_EQUAL(
|
||||
(std::numeric_limits<int>::max)(),
|
||||
lexical_cast<int>((std::numeric_limits<int>::max)()));
|
||||
|
||||
BOOST_CHECK_EQUAL(
|
||||
(std::numeric_limits<int>::min)(),
|
||||
lexical_cast<int>((std::numeric_limits<int>::min)()));
|
||||
|
||||
BOOST_CHECK_THROW(lexical_cast<int>(1.23), bad_lexical_cast);
|
||||
|
||||
BOOST_CHECK_THROW(lexical_cast<int>(1e20), bad_lexical_cast);
|
||||
BOOST_CHECK_EQUAL(1, lexical_cast<int>(true));
|
||||
BOOST_CHECK_EQUAL(0, lexical_cast<int>(false));
|
||||
BOOST_CHECK_EQUAL(123, lexical_cast<int>("123"));
|
||||
BOOST_CHECK_THROW(
|
||||
lexical_cast<int>(" 123"), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<int>(""), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<int>("Test"), bad_lexical_cast);
|
||||
BOOST_CHECK_EQUAL(123, lexical_cast<int>("123"));
|
||||
BOOST_CHECK_EQUAL(123, lexical_cast<int>(std::string("123")));
|
||||
BOOST_CHECK_THROW(
|
||||
lexical_cast<int>(std::string(" 123")), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(
|
||||
lexical_cast<int>(std::string("")), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(
|
||||
lexical_cast<int>(std::string("Test")), bad_lexical_cast);
|
||||
}
|
||||
|
||||
void test_conversion_to_double()
|
||||
{
|
||||
BOOST_CHECK_CLOSE(1.0, lexical_cast<double>('1'), (std::numeric_limits<double>::epsilon()));
|
||||
BOOST_CHECK_THROW(lexical_cast<double>('A'), bad_lexical_cast);
|
||||
BOOST_CHECK_CLOSE(1.0, lexical_cast<double>(1), (std::numeric_limits<double>::epsilon()));
|
||||
BOOST_CHECK_CLOSE(1.23, lexical_cast<double>(1.23), (std::numeric_limits<double>::epsilon()));
|
||||
BOOST_CHECK_CLOSE(1.234567890, 1.234567890, std::numeric_limits<double>::epsilon());
|
||||
BOOST_CHECK_CLOSE(1.0, lexical_cast<double>(true), (std::numeric_limits<double>::epsilon()));
|
||||
BOOST_CHECK_CLOSE(0.0, lexical_cast<double>(false), (std::numeric_limits<double>::epsilon()));
|
||||
BOOST_CHECK_CLOSE(1.23, lexical_cast<double>("1.23"), (std::numeric_limits<double>::epsilon()));
|
||||
BOOST_CHECK_THROW(lexical_cast<double>(""), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<double>("Test"), bad_lexical_cast);
|
||||
BOOST_CHECK_CLOSE(1.23, lexical_cast<double>(std::string("1.23")), (std::numeric_limits<double>::epsilon()));
|
||||
BOOST_CHECK_THROW(
|
||||
lexical_cast<double>(std::string("")), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(
|
||||
lexical_cast<double>(std::string("Test")), bad_lexical_cast);
|
||||
}
|
||||
|
||||
void test_conversion_to_bool()
|
||||
{
|
||||
BOOST_CHECK_EQUAL(true, lexical_cast<bool>('1'));
|
||||
BOOST_CHECK_EQUAL(false, lexical_cast<bool>('0'));
|
||||
BOOST_CHECK_THROW(lexical_cast<bool>('A'), bad_lexical_cast);
|
||||
BOOST_CHECK_EQUAL(true, lexical_cast<bool>(1));
|
||||
BOOST_CHECK_EQUAL(false, lexical_cast<bool>(0));
|
||||
BOOST_CHECK_THROW(lexical_cast<bool>(123), bad_lexical_cast);
|
||||
BOOST_CHECK_EQUAL(true, lexical_cast<bool>(1.0));
|
||||
BOOST_CHECK_EQUAL(false, lexical_cast<bool>(0.0));
|
||||
BOOST_CHECK_EQUAL(true, lexical_cast<bool>(true));
|
||||
BOOST_CHECK_EQUAL(false, lexical_cast<bool>(false));
|
||||
BOOST_CHECK_EQUAL(true, lexical_cast<bool>("1"));
|
||||
BOOST_CHECK_EQUAL(false, lexical_cast<bool>("0"));
|
||||
BOOST_CHECK_THROW(lexical_cast<bool>(""), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<bool>("Test"), bad_lexical_cast);
|
||||
BOOST_CHECK_EQUAL(true, lexical_cast<bool>("1"));
|
||||
BOOST_CHECK_EQUAL(false, lexical_cast<bool>("0"));
|
||||
BOOST_CHECK_EQUAL(true, lexical_cast<bool>(std::string("1")));
|
||||
BOOST_CHECK_EQUAL(false, lexical_cast<bool>(std::string("0")));
|
||||
BOOST_CHECK_THROW(
|
||||
lexical_cast<bool>(std::string("")), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(
|
||||
lexical_cast<bool>(std::string("Test")), bad_lexical_cast);
|
||||
}
|
||||
|
||||
void test_conversion_to_string()
|
||||
{
|
||||
char buf[] = "hello";
|
||||
char* str = buf;
|
||||
BOOST_CHECK_EQUAL(str, lexical_cast<std::string>(str));
|
||||
BOOST_CHECK_EQUAL("A", lexical_cast<std::string>('A'));
|
||||
BOOST_CHECK_EQUAL(" ", lexical_cast<std::string>(' '));
|
||||
BOOST_CHECK_EQUAL("123", lexical_cast<std::string>(123));
|
||||
BOOST_CHECK_EQUAL("1.23", lexical_cast<std::string>(1.23));
|
||||
BOOST_CHECK_EQUAL("1.111111111", lexical_cast<std::string>(1.111111111));
|
||||
BOOST_CHECK_EQUAL("1", lexical_cast<std::string>(true));
|
||||
BOOST_CHECK_EQUAL("0", lexical_cast<std::string>(false));
|
||||
BOOST_CHECK_EQUAL("Test", lexical_cast<std::string>("Test"));
|
||||
BOOST_CHECK_EQUAL(" ", lexical_cast<std::string>(" "));
|
||||
BOOST_CHECK_EQUAL("", lexical_cast<std::string>(""));
|
||||
BOOST_CHECK_EQUAL("Test", lexical_cast<std::string>(std::string("Test")));
|
||||
BOOST_CHECK_EQUAL(" ", lexical_cast<std::string>(std::string(" ")));
|
||||
BOOST_CHECK_EQUAL("", lexical_cast<std::string>(std::string("")));
|
||||
}
|
||||
|
||||
void test_conversion_from_to_wchar_t_alias()
|
||||
{
|
||||
BOOST_CHECK_EQUAL(123u, lexical_cast<unsigned short>("123"));
|
||||
BOOST_CHECK_EQUAL(123u, lexical_cast<unsigned int>("123"));
|
||||
BOOST_CHECK_EQUAL(123u, lexical_cast<unsigned long>("123"));
|
||||
BOOST_CHECK_EQUAL(std::string("123"),
|
||||
lexical_cast<std::string>(static_cast<unsigned short>(123)));
|
||||
BOOST_CHECK_EQUAL(std::string("123"), lexical_cast<std::string>(123u));
|
||||
BOOST_CHECK_EQUAL(std::string("123"), lexical_cast<std::string>(123ul));
|
||||
}
|
||||
|
||||
void test_conversion_to_pointer()
|
||||
{
|
||||
BOOST_CHECK_THROW(lexical_cast<char *>("Test"), bad_lexical_cast);
|
||||
#ifndef BOOST_LCAST_NO_WCHAR_T
|
||||
BOOST_CHECK_THROW(lexical_cast<wchar_t *>("Test"), bad_lexical_cast);
|
||||
#endif
|
||||
}
|
||||
|
||||
void test_conversion_from_wchar_t()
|
||||
{
|
||||
#ifndef BOOST_LCAST_NO_WCHAR_T
|
||||
#if !defined(BOOST_NO_INTRINSIC_WCHAR_T)
|
||||
BOOST_CHECK_EQUAL(1, lexical_cast<int>(L'1'));
|
||||
BOOST_CHECK_THROW(lexical_cast<int>(L'A'), bad_lexical_cast);
|
||||
#endif
|
||||
|
||||
BOOST_CHECK_EQUAL(123, lexical_cast<int>(L"123"));
|
||||
BOOST_CHECK_THROW(lexical_cast<int>(L""), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<int>(L"Test"), bad_lexical_cast);
|
||||
|
||||
#if !defined(BOOST_NO_INTRINSIC_WCHAR_T)
|
||||
BOOST_CHECK_EQUAL(1.0, lexical_cast<double>(L'1'));
|
||||
BOOST_CHECK_THROW(lexical_cast<double>(L'A'), bad_lexical_cast);
|
||||
#endif
|
||||
|
||||
BOOST_CHECK_EQUAL(1.23, lexical_cast<double>(L"1.23"));
|
||||
BOOST_CHECK_THROW(lexical_cast<double>(L""), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<double>(L"Test"), bad_lexical_cast);
|
||||
|
||||
#if !defined(BOOST_NO_INTRINSIC_WCHAR_T)
|
||||
BOOST_CHECK_EQUAL(true, lexical_cast<bool>(L'1'));
|
||||
BOOST_CHECK_EQUAL(false, lexical_cast<bool>(L'0'));
|
||||
BOOST_CHECK_THROW(lexical_cast<bool>(L'A'), bad_lexical_cast);
|
||||
#endif
|
||||
BOOST_CHECK_EQUAL(true, lexical_cast<bool>(L"1"));
|
||||
BOOST_CHECK_EQUAL(false, lexical_cast<bool>(L"0"));
|
||||
BOOST_CHECK_THROW(lexical_cast<bool>(L""), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<bool>(L"Test"), bad_lexical_cast);
|
||||
#endif
|
||||
}
|
||||
|
||||
void test_conversion_to_wchar_t()
|
||||
{
|
||||
#if !defined(BOOST_LCAST_NO_WCHAR_T) && !defined(BOOST_NO_INTRINSIC_WCHAR_T)
|
||||
BOOST_CHECK_EQUAL(L'1', lexical_cast<wchar_t>(1));
|
||||
BOOST_CHECK_EQUAL(L'0', lexical_cast<wchar_t>(0));
|
||||
BOOST_CHECK_EQUAL(L'1', lexical_cast<wchar_t>('1'));
|
||||
BOOST_CHECK_EQUAL(L'0', lexical_cast<wchar_t>('0'));
|
||||
BOOST_CHECK_THROW(lexical_cast<wchar_t>(123), bad_lexical_cast);
|
||||
BOOST_CHECK_EQUAL(L'1', lexical_cast<wchar_t>(1.0));
|
||||
BOOST_CHECK_EQUAL(L'0', lexical_cast<wchar_t>(0.0));
|
||||
BOOST_CHECK_EQUAL(L'1', lexical_cast<wchar_t>(true));
|
||||
BOOST_CHECK_EQUAL(L'0', lexical_cast<wchar_t>(false));
|
||||
BOOST_CHECK_EQUAL(L'A', lexical_cast<wchar_t>(L'A'));
|
||||
BOOST_CHECK_EQUAL(L' ', lexical_cast<wchar_t>(L' '));
|
||||
BOOST_CHECK_EQUAL(L'A', lexical_cast<wchar_t>(L"A"));
|
||||
BOOST_CHECK_EQUAL(L' ', lexical_cast<wchar_t>(L" "));
|
||||
BOOST_CHECK_THROW(lexical_cast<wchar_t>(L""), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<wchar_t>(L"Test"), bad_lexical_cast);
|
||||
BOOST_CHECK_EQUAL(L'A', lexical_cast<wchar_t>(std::wstring(L"A")));
|
||||
BOOST_CHECK_EQUAL(L' ', lexical_cast<wchar_t>(std::wstring(L" ")));
|
||||
BOOST_CHECK_THROW(
|
||||
lexical_cast<wchar_t>(std::wstring(L"")), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(
|
||||
lexical_cast<wchar_t>(std::wstring(L"Test")), bad_lexical_cast);
|
||||
#endif
|
||||
}
|
||||
|
||||
void test_conversion_from_wstring()
|
||||
{
|
||||
#ifndef BOOST_LCAST_NO_WCHAR_T
|
||||
BOOST_CHECK_EQUAL(123, lexical_cast<int>(std::wstring(L"123")));
|
||||
BOOST_CHECK_THROW(
|
||||
lexical_cast<int>(std::wstring(L"")), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(
|
||||
lexical_cast<int>(std::wstring(L"Test")), bad_lexical_cast);
|
||||
|
||||
BOOST_CHECK_EQUAL(true, lexical_cast<bool>(std::wstring(L"1")));
|
||||
BOOST_CHECK_EQUAL(false, lexical_cast<bool>(std::wstring(L"0")));
|
||||
BOOST_CHECK_THROW(
|
||||
lexical_cast<bool>(std::wstring(L"")), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(
|
||||
lexical_cast<bool>(std::wstring(L"Test")), bad_lexical_cast);
|
||||
#endif
|
||||
}
|
||||
|
||||
void test_conversion_to_wstring()
|
||||
{
|
||||
#ifndef BOOST_LCAST_NO_WCHAR_T
|
||||
wchar_t buf[] = L"hello";
|
||||
wchar_t* str = buf;
|
||||
BOOST_CHECK(str == lexical_cast<std::wstring>(str));
|
||||
BOOST_CHECK(L"123" == lexical_cast<std::wstring>(123));
|
||||
BOOST_CHECK(L"1.23" == lexical_cast<std::wstring>(1.23));
|
||||
BOOST_CHECK(L"1.111111111" == lexical_cast<std::wstring>(1.111111111));
|
||||
BOOST_CHECK(L"1" == lexical_cast<std::wstring>(true));
|
||||
BOOST_CHECK(L"0" == lexical_cast<std::wstring>(false));
|
||||
#if !defined(BOOST_NO_INTRINSIC_WCHAR_T)
|
||||
BOOST_CHECK(L"A" == lexical_cast<std::wstring>(L'A'));
|
||||
BOOST_CHECK(L" " == lexical_cast<std::wstring>(L' '));
|
||||
BOOST_CHECK(L"A" == lexical_cast<std::wstring>('A'));
|
||||
#endif
|
||||
BOOST_CHECK(L"Test" == lexical_cast<std::wstring>(L"Test"));
|
||||
BOOST_CHECK(L" " == lexical_cast<std::wstring>(L" "));
|
||||
BOOST_CHECK(L"" == lexical_cast<std::wstring>(L""));
|
||||
BOOST_CHECK(L"Test" == lexical_cast<std::wstring>(std::wstring(L"Test")));
|
||||
BOOST_CHECK(L" " == lexical_cast<std::wstring>(std::wstring(L" ")));
|
||||
BOOST_CHECK(L"" == lexical_cast<std::wstring>(std::wstring(L"")));
|
||||
#endif
|
||||
}
|
||||
|
||||
void test_bad_lexical_cast()
|
||||
{
|
||||
try
|
||||
{
|
||||
lexical_cast<int>(std::string("Test"));
|
||||
|
||||
BOOST_CHECK(false); // Exception expected
|
||||
}
|
||||
catch(const bad_lexical_cast &e)
|
||||
{
|
||||
BOOST_CHECK(e.source_type() == typeid(std::string));
|
||||
BOOST_CHECK(e.target_type() == typeid(int));
|
||||
}
|
||||
}
|
||||
|
||||
void test_no_whitespace_stripping()
|
||||
{
|
||||
BOOST_CHECK_THROW(lexical_cast<int>(" 123"), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<int>("123 "), bad_lexical_cast);
|
||||
}
|
||||
|
||||
// Replace "-,999" with "-999".
|
||||
template<class CharT>
|
||||
std::basic_string<CharT> to_str_gcc_workaround(std::basic_string<CharT> str)
|
||||
{
|
||||
std::locale loc;
|
||||
std::numpunct<CharT> const& np = BOOST_USE_FACET(std::numpunct<CharT>, loc);
|
||||
std::ctype<CharT> const& ct = BOOST_USE_FACET(std::ctype<CharT>, loc);
|
||||
|
||||
if(np.grouping().empty())
|
||||
return str;
|
||||
|
||||
CharT prefix[3] = { ct.widen('-'), np.thousands_sep(), CharT() };
|
||||
|
||||
if(str.find(prefix) != 0)
|
||||
return str;
|
||||
|
||||
prefix[1] = CharT();
|
||||
str.replace(0, 2, prefix);
|
||||
return str;
|
||||
}
|
||||
|
||||
template<class CharT, class T>
|
||||
std::basic_string<CharT> to_str(T t)
|
||||
{
|
||||
std::basic_ostringstream<CharT> o;
|
||||
o << t;
|
||||
return to_str_gcc_workaround(o.str());
|
||||
}
|
||||
|
||||
template<class T, class CharT>
|
||||
void test_conversion_from_integral_to_char(CharT zero)
|
||||
{
|
||||
BOOST_CHECK(lexical_cast<CharT>(static_cast<T>(0)) == zero + 0);
|
||||
BOOST_CHECK(lexical_cast<CharT>(static_cast<T>(1)) == zero + 1);
|
||||
BOOST_CHECK(lexical_cast<CharT>(static_cast<T>(2)) == zero + 2);
|
||||
BOOST_CHECK(lexical_cast<CharT>(static_cast<T>(3)) == zero + 3);
|
||||
BOOST_CHECK(lexical_cast<CharT>(static_cast<T>(4)) == zero + 4);
|
||||
BOOST_CHECK(lexical_cast<CharT>(static_cast<T>(5)) == zero + 5);
|
||||
BOOST_CHECK(lexical_cast<CharT>(static_cast<T>(6)) == zero + 6);
|
||||
BOOST_CHECK(lexical_cast<CharT>(static_cast<T>(7)) == zero + 7);
|
||||
BOOST_CHECK(lexical_cast<CharT>(static_cast<T>(8)) == zero + 8);
|
||||
BOOST_CHECK(lexical_cast<CharT>(static_cast<T>(9)) == zero + 9);
|
||||
|
||||
BOOST_CHECK_THROW(lexical_cast<CharT>(static_cast<T>(10)), bad_lexical_cast);
|
||||
|
||||
T t = (std::numeric_limits<T>::max)();
|
||||
BOOST_CHECK_THROW(lexical_cast<CharT>(t), bad_lexical_cast);
|
||||
}
|
||||
|
||||
template<class T>
|
||||
void test_conversion_from_integral_to_integral()
|
||||
{
|
||||
T t = 0;
|
||||
BOOST_CHECK(lexical_cast<T>(t) == t);
|
||||
|
||||
// Next two variables are used to supress warnings.
|
||||
int st = 32767; unsigned int ut = st;
|
||||
t = st;
|
||||
BOOST_CHECK(lexical_cast<short>(t) == st);
|
||||
BOOST_CHECK(lexical_cast<unsigned short>(t) == ut);
|
||||
BOOST_CHECK(lexical_cast<int>(t) == st);
|
||||
BOOST_CHECK(lexical_cast<unsigned int>(t) == ut);
|
||||
BOOST_CHECK(lexical_cast<long>(t) == st);
|
||||
BOOST_CHECK(lexical_cast<unsigned long>(t) == ut);
|
||||
|
||||
t = (std::numeric_limits<T>::max)();
|
||||
BOOST_CHECK(lexical_cast<T>(t) == t);
|
||||
|
||||
t = (std::numeric_limits<T>::min)();
|
||||
BOOST_CHECK(lexical_cast<T>(t) == t);
|
||||
}
|
||||
|
||||
template<class T, class CharT>
|
||||
void test_conversion_from_integral_to_string(CharT)
|
||||
{
|
||||
typedef std::numeric_limits<T> limits;
|
||||
typedef std::basic_string<CharT> string_type;
|
||||
|
||||
T t;
|
||||
|
||||
t = (limits::min)();
|
||||
BOOST_CHECK(lexical_cast<string_type>(t) == to_str<CharT>(t));
|
||||
|
||||
t = (limits::max)();
|
||||
BOOST_CHECK(lexical_cast<string_type>(t) == to_str<CharT>(t));
|
||||
|
||||
if(limits::digits <= 16 && lcast_test_small_integral_types_completely)
|
||||
// min and max have already been tested.
|
||||
for(t = 1 + (limits::min)(); t != (limits::max)(); ++t)
|
||||
BOOST_CHECK(lexical_cast<string_type>(t) == to_str<CharT>(t));
|
||||
else
|
||||
{
|
||||
T const min_val = (limits::min)();
|
||||
T const max_val = (limits::max)();
|
||||
T const half_max_val = max_val / 2;
|
||||
T const cnt = lcast_integral_test_counter; // to supress warnings
|
||||
unsigned int const counter = cnt < half_max_val ? cnt : half_max_val;
|
||||
|
||||
unsigned int i;
|
||||
|
||||
// Test values around min:
|
||||
t = min_val;
|
||||
for(i = 0; i < counter; ++i, ++t)
|
||||
BOOST_CHECK(lexical_cast<string_type>(t) == to_str<CharT>(t));
|
||||
|
||||
// Test values around max:
|
||||
t = max_val;
|
||||
for(i = 0; i < counter; ++i, --t)
|
||||
BOOST_CHECK(lexical_cast<string_type>(t) == to_str<CharT>(t));
|
||||
|
||||
// Test values around zero:
|
||||
if(limits::is_signed)
|
||||
for(t = -counter; t < static_cast<T>(counter); ++t)
|
||||
BOOST_CHECK(lexical_cast<string_type>(t) == to_str<CharT>(t));
|
||||
|
||||
// Test values around 100, 1000, 10000, ...
|
||||
T ten_power = 100;
|
||||
for(int e = 2; e <= limits::digits10; ++e, ten_power *= 10)
|
||||
{
|
||||
// ten_power + 100 probably never overflows
|
||||
for(t = ten_power - 100; t != ten_power + 100; ++t)
|
||||
BOOST_CHECK(lexical_cast<string_type>(t) == to_str<CharT>(t));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template<class T, class CharT>
|
||||
void test_conversion_from_string_to_integral(CharT)
|
||||
{
|
||||
typedef std::numeric_limits<T> limits;
|
||||
typedef std::basic_string<CharT> string_type;
|
||||
|
||||
string_type s;
|
||||
string_type const zero = to_str<CharT>(0);
|
||||
string_type const nine = to_str<CharT>(9);
|
||||
T const min_val = (limits::min)();
|
||||
T const max_val = (limits::max)();
|
||||
|
||||
s = to_str<CharT>(min_val);
|
||||
BOOST_CHECK_EQUAL(lexical_cast<T>(s), min_val);
|
||||
if(limits::is_signed)
|
||||
{
|
||||
#if defined(BOOST_MSVC) && BOOST_MSVC == 1400
|
||||
// VC++ 8.0 bug, see libs/conversion/test/lexical_cast_vc8_bug_test.cpp
|
||||
if(sizeof(T) < sizeof(boost::intmax_t))
|
||||
#endif
|
||||
{
|
||||
BOOST_CHECK_THROW(lexical_cast<T>(s + zero), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<T>(s + nine), bad_lexical_cast);
|
||||
}
|
||||
}
|
||||
|
||||
s = to_str<CharT>(max_val);
|
||||
BOOST_CHECK_EQUAL(lexical_cast<T>(s), max_val);
|
||||
#if defined(BOOST_MSVC) && BOOST_MSVC == 1400
|
||||
// VC++ 8.0 bug, see libs/conversion/test/lexical_cast_vc8_bug_test.cpp
|
||||
if(sizeof(T) != sizeof(boost::intmax_t))
|
||||
#endif
|
||||
{
|
||||
BOOST_CHECK_THROW(lexical_cast<T>(s + zero), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<T>(s + nine), bad_lexical_cast);
|
||||
}
|
||||
|
||||
if(limits::digits <= 16 && lcast_test_small_integral_types_completely)
|
||||
// min and max have already been tested.
|
||||
for(T t = 1 + min_val; t != max_val; ++t)
|
||||
BOOST_CHECK(lexical_cast<T>(to_str<CharT>(t)) == t);
|
||||
else
|
||||
{
|
||||
T const half_max_val = max_val / 2;
|
||||
T const cnt = lcast_integral_test_counter; // to supress warnings
|
||||
unsigned int const counter = cnt < half_max_val ? cnt : half_max_val;
|
||||
|
||||
T t;
|
||||
unsigned int i;
|
||||
|
||||
// Test values around min:
|
||||
t = min_val;
|
||||
for(i = 0; i < counter; ++i, ++t)
|
||||
BOOST_CHECK(lexical_cast<T>(to_str<CharT>(t)) == t);
|
||||
|
||||
// Test values around max:
|
||||
t = max_val;
|
||||
for(i = 0; i < counter; ++i, --t)
|
||||
BOOST_CHECK(lexical_cast<T>(to_str<CharT>(t)) == t);
|
||||
|
||||
// Test values around zero:
|
||||
if(limits::is_signed)
|
||||
for(t = -counter; t < static_cast<T>(counter); ++t)
|
||||
BOOST_CHECK(lexical_cast<T>(to_str<CharT>(t)) == t);
|
||||
|
||||
// Test values around 100, 1000, 10000, ...
|
||||
T ten_power = 100;
|
||||
for(int e = 2; e <= limits::digits10; ++e, ten_power *= 10)
|
||||
{
|
||||
// ten_power + 100 probably never overflows
|
||||
for(t = ten_power - 100; t != ten_power + 100; ++t)
|
||||
BOOST_CHECK(lexical_cast<T>(to_str<CharT>(t)) == t);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template<class T>
|
||||
void test_conversion_from_to_integral_for_locale()
|
||||
{
|
||||
test_conversion_from_integral_to_integral<T>();
|
||||
test_conversion_from_integral_to_string<T>('0');
|
||||
test_conversion_from_string_to_integral<T>('0');
|
||||
#if !defined(BOOST_LCAST_NO_WCHAR_T)
|
||||
test_conversion_from_integral_to_string<T>(L'0');
|
||||
test_conversion_from_string_to_integral<T>(L'0');
|
||||
#endif
|
||||
}
|
||||
|
||||
struct restore_oldloc
|
||||
{
|
||||
std::locale oldloc;
|
||||
~restore_oldloc() { std::locale::global(oldloc); }
|
||||
};
|
||||
|
||||
template<class T>
|
||||
void test_conversion_from_to_integral()
|
||||
{
|
||||
char const zero = '0';
|
||||
signed char const szero = '0';
|
||||
unsigned char const uzero = '0';
|
||||
test_conversion_from_integral_to_char<T>(zero);
|
||||
test_conversion_from_integral_to_char<T>(szero);
|
||||
test_conversion_from_integral_to_char<T>(uzero);
|
||||
#if !defined(BOOST_LCAST_NO_WCHAR_T) && !defined(BOOST_NO_INTRINSIC_WCHAR_T)
|
||||
wchar_t const wzero = L'0';
|
||||
test_conversion_from_integral_to_char<T>(wzero);
|
||||
#endif
|
||||
|
||||
// test_conversion_from_to_integral_for_locale
|
||||
|
||||
typedef std::numpunct<char> numpunct;
|
||||
|
||||
restore_oldloc guard;
|
||||
std::locale const& oldloc = guard.oldloc;
|
||||
|
||||
std::string grouping1 = BOOST_USE_FACET(numpunct, oldloc).grouping();
|
||||
std::string grouping2(grouping1);
|
||||
|
||||
test_conversion_from_to_integral_for_locale<T>();
|
||||
|
||||
try
|
||||
{
|
||||
std::locale newloc("");
|
||||
std::locale::global(newloc);
|
||||
|
||||
grouping2 = BOOST_USE_FACET(numpunct, newloc).grouping();
|
||||
}
|
||||
catch(std::exception const& ex)
|
||||
{
|
||||
std::string msg("Failed to set system locale: ");
|
||||
msg += ex.what();
|
||||
BOOST_TEST_MESSAGE(msg);
|
||||
}
|
||||
|
||||
if(grouping1 != grouping2)
|
||||
test_conversion_from_to_integral_for_locale<T>();
|
||||
|
||||
if(grouping1.empty() && grouping2.empty())
|
||||
BOOST_TEST_MESSAGE("Formatting with thousands_sep has not been tested");
|
||||
}
|
||||
|
||||
void test_conversion_from_to_short()
|
||||
{
|
||||
test_conversion_from_to_integral<short>();
|
||||
}
|
||||
|
||||
void test_conversion_from_to_ushort()
|
||||
{
|
||||
test_conversion_from_to_integral<unsigned short>();
|
||||
}
|
||||
|
||||
void test_conversion_from_to_int()
|
||||
{
|
||||
test_conversion_from_to_integral<int>();
|
||||
}
|
||||
|
||||
void test_conversion_from_to_uint()
|
||||
{
|
||||
test_conversion_from_to_integral<unsigned int>();
|
||||
}
|
||||
|
||||
void test_conversion_from_to_long()
|
||||
{
|
||||
test_conversion_from_to_integral<long>();
|
||||
}
|
||||
|
||||
void test_conversion_from_to_ulong()
|
||||
{
|
||||
test_conversion_from_to_integral<unsigned long>();
|
||||
}
|
||||
|
||||
void test_conversion_from_to_intmax_t()
|
||||
{
|
||||
test_conversion_from_to_integral<boost::intmax_t>();
|
||||
}
|
||||
|
||||
void test_conversion_from_to_uintmax_t()
|
||||
{
|
||||
test_conversion_from_to_integral<boost::uintmax_t>();
|
||||
}
|
||||
|
||||
#if defined(BOOST_HAS_LONG_LONG)
|
||||
|
||||
void test_conversion_from_to_longlong()
|
||||
{
|
||||
test_conversion_from_to_integral<boost::long_long_type>();
|
||||
}
|
||||
|
||||
void test_conversion_from_to_ulonglong()
|
||||
{
|
||||
test_conversion_from_to_integral<boost::ulong_long_type>();
|
||||
}
|
||||
|
||||
#elif defined(LCAST_TEST_LONGLONG)
|
||||
|
||||
void test_conversion_from_to_longlong()
|
||||
{
|
||||
test_conversion_from_to_integral<__int64>();
|
||||
}
|
||||
|
||||
void test_conversion_from_to_ulonglong()
|
||||
{
|
||||
test_conversion_from_to_integral<unsigned __int64>();
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
|
||||
void test_traits()
|
||||
{
|
||||
typedef std::basic_string<char, my_traits<char> > my_string;
|
||||
|
||||
my_string const s("s");
|
||||
BOOST_CHECK(boost::lexical_cast<char>(s) == s[0]);
|
||||
BOOST_CHECK(boost::lexical_cast<my_string>(s) == s);
|
||||
BOOST_CHECK(boost::lexical_cast<my_string>(-1) == "-1");
|
||||
}
|
||||
|
||||
void test_wtraits()
|
||||
{
|
||||
typedef std::basic_string<wchar_t, my_traits<wchar_t> > my_string;
|
||||
|
||||
my_string const s(L"s");
|
||||
BOOST_CHECK(boost::lexical_cast<wchar_t>(s) == s[0]);
|
||||
BOOST_CHECK(boost::lexical_cast<my_string>(s) == s);
|
||||
//BOOST_CHECK(boost::lexical_cast<my_string>(-1) == L"-1");
|
||||
// Commented out because gcc 3.3 doesn't support this:
|
||||
// basic_ostream<wchar_t, my_traits<wchar_t> > o; o << -1;
|
||||
}
|
||||
|
||||
void test_allocator()
|
||||
{
|
||||
typedef std::basic_string< char
|
||||
, std::char_traits<char>
|
||||
, my_allocator<char>
|
||||
> my_string;
|
||||
|
||||
my_string s("s");
|
||||
BOOST_CHECK(boost::lexical_cast<char>(s) == s[0]);
|
||||
BOOST_CHECK(boost::lexical_cast<std::string>(s) == "s");
|
||||
BOOST_CHECK(boost::lexical_cast<my_string>(s) == s);
|
||||
BOOST_CHECK(boost::lexical_cast<my_string>(1) == "1");
|
||||
BOOST_CHECK(boost::lexical_cast<my_string>("s") == s);
|
||||
BOOST_CHECK(boost::lexical_cast<my_string>(std::string("s")) == s);
|
||||
}
|
||||
|
||||
void test_wallocator()
|
||||
{
|
||||
typedef std::basic_string< wchar_t
|
||||
, std::char_traits<wchar_t>
|
||||
, my_allocator<wchar_t>
|
||||
> my_string;
|
||||
|
||||
my_string s(L"s");
|
||||
BOOST_CHECK(boost::lexical_cast<wchar_t>(s) == s[0]);
|
||||
BOOST_CHECK(boost::lexical_cast<std::wstring>(s) == L"s");
|
||||
BOOST_CHECK(boost::lexical_cast<my_string>(s) == s);
|
||||
BOOST_CHECK(boost::lexical_cast<my_string>(1) == L"1");
|
||||
BOOST_CHECK(boost::lexical_cast<my_string>(L"s") == s);
|
||||
BOOST_CHECK(boost::lexical_cast<my_string>(std::wstring(L"s")) == s);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
16
meta/libraries.json
Normal file
16
meta/libraries.json
Normal file
@ -0,0 +1,16 @@
|
||||
{
|
||||
"key": "conversion",
|
||||
"name": "Conversion",
|
||||
"authors": [
|
||||
"Dave Abrahams",
|
||||
"Kevlin Henney"
|
||||
],
|
||||
"description": "Polymorphic casts.",
|
||||
"category": [
|
||||
"Miscellaneous"
|
||||
],
|
||||
"maintainers": [
|
||||
"Antony Polukhin <antoshkka -at- gmail.com>"
|
||||
],
|
||||
"cxxstd": "03"
|
||||
}
|
@ -1,100 +0,0 @@
|
||||
// boost utility cast test program -----------------------------------------//
|
||||
|
||||
// (C) Copyright Beman Dawes, Dave Abrahams 1999. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
// See http://www.boost.org for most recent version including documentation.
|
||||
|
||||
// Revision History
|
||||
// 28 Set 04 factored out numeric_cast<> test (Fernando Cacciola)
|
||||
// 20 Jan 01 removed use of <limits> for portability to raw GCC (David Abrahams)
|
||||
// 28 Jun 00 implicit_cast removed (Beman Dawes)
|
||||
// 30 Aug 99 value_cast replaced by numeric_cast
|
||||
// 3 Aug 99 Initial Version
|
||||
|
||||
#include <iostream>
|
||||
#include <climits>
|
||||
#include <cfloat> // for DBL_MAX (Peter Schmid)
|
||||
#include <boost/cast.hpp>
|
||||
|
||||
#include "boost/test/minimal.hpp"
|
||||
|
||||
# if SCHAR_MAX == LONG_MAX
|
||||
# error "This test program doesn't work if SCHAR_MAX == LONG_MAX"
|
||||
# endif
|
||||
|
||||
using namespace boost;
|
||||
using std::cout;
|
||||
|
||||
|
||||
int test_main( int argc, char * argv[] )
|
||||
{
|
||||
|
||||
# ifdef NDEBUG
|
||||
cout << "NDEBUG is defined\n";
|
||||
# else
|
||||
cout << "NDEBUG is not defined\n";
|
||||
# endif
|
||||
|
||||
cout << "\nBeginning tests...\n";
|
||||
|
||||
// test implicit_cast and numeric_cast -------------------------------------//
|
||||
|
||||
// tests which should succeed
|
||||
long small_value = 1;
|
||||
long small_negative_value = -1;
|
||||
long large_value = LONG_MAX;
|
||||
long large_negative_value = LONG_MIN;
|
||||
signed char c = 0;
|
||||
|
||||
c = large_value; // see if compiler generates warning
|
||||
|
||||
c = numeric_cast<signed char>( small_value );
|
||||
BOOST_CHECK( c == 1 );
|
||||
c = 0;
|
||||
c = numeric_cast<signed char>( small_value );
|
||||
BOOST_CHECK( c == 1 );
|
||||
c = 0;
|
||||
c = numeric_cast<signed char>( small_negative_value );
|
||||
BOOST_CHECK( c == -1 );
|
||||
|
||||
// These tests courtesy of Joe R NWP Swatosh<joe.r.swatosh@usace.army.mil>
|
||||
BOOST_CHECK( 0.0f == numeric_cast<float>( 0.0 ) );
|
||||
BOOST_CHECK( 0.0 == numeric_cast<double>( 0.0 ) );
|
||||
|
||||
// tests which should result in errors being detected
|
||||
|
||||
bool caught_exception = false;
|
||||
try { c = numeric_cast<signed char>( large_value ); }
|
||||
catch (bad_numeric_cast)
|
||||
{ cout<<"caught bad_numeric_cast #1\n"; caught_exception = true; }
|
||||
BOOST_CHECK ( caught_exception );
|
||||
|
||||
caught_exception = false;
|
||||
try { c = numeric_cast<signed char>( large_negative_value ); }
|
||||
catch (bad_numeric_cast)
|
||||
{ cout<<"caught bad_numeric_cast #2\n"; caught_exception = true; }
|
||||
BOOST_CHECK ( caught_exception );
|
||||
|
||||
unsigned long ul;
|
||||
caught_exception = false;
|
||||
try { ul = numeric_cast<unsigned long>( large_negative_value ); }
|
||||
catch (bad_numeric_cast)
|
||||
{ cout<<"caught bad_numeric_cast #3\n"; caught_exception = true; }
|
||||
BOOST_CHECK ( caught_exception );
|
||||
|
||||
caught_exception = false;
|
||||
try { ul = numeric_cast<unsigned long>( small_negative_value ); }
|
||||
catch (bad_numeric_cast)
|
||||
{ cout<<"caught bad_numeric_cast #4\n"; caught_exception = true; }
|
||||
BOOST_CHECK ( caught_exception );
|
||||
|
||||
caught_exception = false;
|
||||
try { numeric_cast<int>( DBL_MAX ); }
|
||||
catch (bad_numeric_cast)
|
||||
{ cout<<"caught bad_numeric_cast #5\n"; caught_exception = true; }
|
||||
BOOST_CHECK ( caught_exception );
|
||||
|
||||
return 0 ;
|
||||
}
|
308
test.hpp
308
test.hpp
@ -1,308 +0,0 @@
|
||||
// what: simple unit test framework
|
||||
// who: developed by Kevlin Henney
|
||||
// when: November 2000
|
||||
// where: tested with BCC 5.5, MSVC 6.0, and g++ 2.91
|
||||
//
|
||||
// ChangeLog:
|
||||
// 20 Jan 2001 - Fixed a warning for MSVC (Dave Abrahams)
|
||||
|
||||
#ifndef TEST_INCLUDED
|
||||
#define TEST_INCLUDED
|
||||
|
||||
#include <exception>
|
||||
#include <iostream>
|
||||
#include <strstream> // for out-of-the-box g++
|
||||
#include <string>
|
||||
|
||||
namespace test // test tuple comprises name and nullary function (object)
|
||||
{
|
||||
template<typename string_type, typename function_type>
|
||||
struct test
|
||||
{
|
||||
string_type name;
|
||||
function_type action;
|
||||
|
||||
static test make(string_type name, function_type action)
|
||||
{
|
||||
test result; // MSVC aggreggate initializer bugs
|
||||
result.name = name;
|
||||
result.action = action;
|
||||
return result;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
namespace test // failure exception used to indicate checked test failures
|
||||
{
|
||||
class failure : public std::exception
|
||||
{
|
||||
public: // struction (default cases are OK)
|
||||
|
||||
failure(const std::string & why)
|
||||
: reason(why)
|
||||
{
|
||||
}
|
||||
|
||||
// std::~string has no exception-specification (could throw anything),
|
||||
// but we need to be compatible with std::~exception's empty one
|
||||
// see std::15.4p13 and std::15.4p3
|
||||
~failure() throw()
|
||||
{
|
||||
}
|
||||
|
||||
public: // usage
|
||||
|
||||
virtual const char * what() const throw()
|
||||
{
|
||||
return reason.c_str();
|
||||
}
|
||||
|
||||
private: // representation
|
||||
|
||||
std::string reason;
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
namespace test // not_implemented exception used to mark unimplemented tests
|
||||
{
|
||||
class not_implemented : public std::exception
|
||||
{
|
||||
public: // usage (default ctor and dtor are OK)
|
||||
|
||||
virtual const char * what() const throw()
|
||||
{
|
||||
return "not implemented";
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
namespace test // test utilities
|
||||
{
|
||||
inline void check(bool condition, const std::string & description)
|
||||
{
|
||||
if(!condition)
|
||||
{
|
||||
throw failure(description);
|
||||
}
|
||||
}
|
||||
|
||||
inline void check_true(bool value, const std::string & description)
|
||||
{
|
||||
check(value, "expected true: " + description);
|
||||
}
|
||||
|
||||
inline void check_false(bool value, const std::string & description)
|
||||
{
|
||||
check(!value, "expected false: " + description);
|
||||
}
|
||||
|
||||
template<typename lhs_type, typename rhs_type>
|
||||
void check_equal(
|
||||
const lhs_type & lhs, const rhs_type & rhs,
|
||||
const std::string & description)
|
||||
{
|
||||
check(lhs == rhs, "expected equal values: " + description);
|
||||
}
|
||||
|
||||
template<typename lhs_type, typename rhs_type>
|
||||
void check_unequal(
|
||||
const lhs_type & lhs, const rhs_type & rhs,
|
||||
const std::string & description)
|
||||
{
|
||||
check(lhs != rhs, "expected unequal values: " + description);
|
||||
}
|
||||
|
||||
inline void check_null(const void* ptr, const std::string & description)
|
||||
{
|
||||
check(!ptr, "expected null pointer: " + description);
|
||||
}
|
||||
|
||||
inline void check_non_null(const void* ptr, const std::string & description)
|
||||
{
|
||||
check(ptr != 0, "expected non-null pointer: " + description);
|
||||
}
|
||||
}
|
||||
|
||||
#define TEST_CHECK_THROW(expression, exception, description) \
|
||||
try \
|
||||
{ \
|
||||
expression; \
|
||||
throw ::test::failure(description); \
|
||||
} \
|
||||
catch(exception &) \
|
||||
{ \
|
||||
}
|
||||
|
||||
namespace test // memory tracking (enabled if test new and delete linked in)
|
||||
{
|
||||
class allocations
|
||||
{
|
||||
public: // singleton access
|
||||
|
||||
static allocations & instance()
|
||||
{
|
||||
static allocations singleton;
|
||||
return singleton;
|
||||
}
|
||||
|
||||
public: // logging
|
||||
|
||||
void clear()
|
||||
{
|
||||
alloc_count = dealloc_count = 0;
|
||||
}
|
||||
|
||||
void allocation()
|
||||
{
|
||||
++alloc_count;
|
||||
}
|
||||
|
||||
void deallocation()
|
||||
{
|
||||
++dealloc_count;
|
||||
}
|
||||
|
||||
public: // reporting
|
||||
|
||||
unsigned long allocated() const
|
||||
{
|
||||
return alloc_count;
|
||||
}
|
||||
|
||||
unsigned long deallocated() const
|
||||
{
|
||||
return dealloc_count;
|
||||
}
|
||||
|
||||
bool balanced() const
|
||||
{
|
||||
return alloc_count == dealloc_count;
|
||||
}
|
||||
|
||||
private: // structors (default dtor is fine)
|
||||
|
||||
allocations()
|
||||
: alloc_count(0), dealloc_count(0)
|
||||
{
|
||||
}
|
||||
|
||||
private: // prevention
|
||||
|
||||
allocations(const allocations &);
|
||||
allocations & operator=(const allocations &);
|
||||
|
||||
private: // state
|
||||
|
||||
unsigned long alloc_count, dealloc_count;
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
namespace test // tester is the driver class for a sequence of tests
|
||||
{
|
||||
template<typename test_iterator>
|
||||
class tester
|
||||
{
|
||||
public: // structors (default destructor is OK)
|
||||
|
||||
tester(test_iterator first_test, test_iterator after_last_test)
|
||||
: begin(first_test), end(after_last_test)
|
||||
{
|
||||
}
|
||||
|
||||
public: // usage
|
||||
|
||||
bool operator()(); // returns true if all tests passed
|
||||
|
||||
private: // representation
|
||||
|
||||
test_iterator begin, end;
|
||||
|
||||
private: // prevention
|
||||
|
||||
tester(const tester &);
|
||||
tester &operator=(const tester &);
|
||||
|
||||
};
|
||||
|
||||
template<typename test_iterator>
|
||||
bool tester<test_iterator>::operator()()
|
||||
{
|
||||
using namespace std;
|
||||
|
||||
unsigned long passed = 0, failed = 0, unimplemented = 0;
|
||||
|
||||
for(test_iterator current = begin; current != end; ++current)
|
||||
{
|
||||
cerr << "[" << current->name << "] " << flush;
|
||||
string result = "passed"; // optimistic
|
||||
|
||||
try
|
||||
{
|
||||
allocations::instance().clear();
|
||||
current->action();
|
||||
|
||||
if(!allocations::instance().balanced())
|
||||
{
|
||||
unsigned long allocated = allocations::instance().allocated();
|
||||
unsigned long deallocated = allocations::instance().deallocated();
|
||||
ostrstream report;
|
||||
report << "new/delete ("
|
||||
<< allocated << " allocated, "
|
||||
<< deallocated << " deallocated)"
|
||||
<< ends;
|
||||
const char * text = report.str();
|
||||
report.freeze(false);
|
||||
throw failure(text);
|
||||
}
|
||||
|
||||
++passed;
|
||||
}
|
||||
catch(const failure & caught)
|
||||
{
|
||||
(result = "failed: ") += caught.what();
|
||||
++failed;
|
||||
}
|
||||
catch(const not_implemented &)
|
||||
{
|
||||
result = "not implemented";
|
||||
++unimplemented;
|
||||
}
|
||||
catch(const exception & caught)
|
||||
{
|
||||
(result = "exception: ") += caught.what();
|
||||
++failed;
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
result = "failed with unknown exception";
|
||||
++failed;
|
||||
}
|
||||
|
||||
cerr << result << endl;
|
||||
}
|
||||
|
||||
cerr << passed + failed << " tests: "
|
||||
<< passed << " passed, "
|
||||
<< failed << " failed";
|
||||
|
||||
if(unimplemented)
|
||||
{
|
||||
cerr << " (" << unimplemented << " not implemented)";
|
||||
}
|
||||
|
||||
cerr << endl;
|
||||
|
||||
return failed == 0;
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
// Copyright Kevlin Henney, 2000. All rights reserved.
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See
|
||||
// accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
@ -1,30 +1,29 @@
|
||||
# Signals library
|
||||
|
||||
# Copyright (C) 2001-2003 Douglas Gregor
|
||||
# Copyright (C) Antony Polukhin, 2011-2022
|
||||
#
|
||||
# Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
#
|
||||
|
||||
# Permission to copy, use, sell and distribute this software is granted
|
||||
# provided this copyright notice appears in all copies. Permission to modify
|
||||
# the code and to distribute modified code is granted provided this copyright
|
||||
# notice appears in all copies, and a notice that the code was modified is
|
||||
# included with the copyright notice. This software is provided "as is"
|
||||
# without express or implied warranty, and with no claim as to its suitability
|
||||
# for any purpose.
|
||||
|
||||
# For more information, see http://www.boost.org/
|
||||
|
||||
# bring in rules for testing
|
||||
import testing ;
|
||||
import feature ;
|
||||
|
||||
project
|
||||
: requirements
|
||||
<toolset>gcc-4.7:<cxxflags>-ftrapv
|
||||
<toolset>gcc-4.6:<cxxflags>-ftrapv
|
||||
<toolset>clang:<cxxflags>-ftrapv
|
||||
# default to all warnings on:
|
||||
<warnings>all
|
||||
# set warnings as errors for those compilers we know we get warning free:
|
||||
<toolset>gcc:<cxxflags>-Wextra
|
||||
<toolset>gcc:<cxxflags>-Wno-uninitialized
|
||||
;
|
||||
|
||||
test-suite conversion
|
||||
: [ run implicit_cast.cpp ]
|
||||
[ compile-fail implicit_cast_fail.cpp ]
|
||||
[ run ../cast_test.cpp ]
|
||||
[ run ../numeric_cast_test.cpp ]
|
||||
[ run ../lexical_cast_test.cpp ../../test/build//boost_unit_test_framework/<link>static ]
|
||||
[ run lexical_cast_loopback_test.cpp ../../test/build//boost_unit_test_framework/<link>static ]
|
||||
[ run lexical_cast_abstract_test.cpp ../../test/build//boost_unit_test_framework/<link>static ]
|
||||
[ run lexical_cast_noncopyable_test.cpp ../../test/build//boost_unit_test_framework/<link>static ]
|
||||
[ run lexical_cast_vc8_bug_test.cpp ../../test/build//boost_unit_test_framework/<link>static ]
|
||||
[ run cast_test.cpp ]
|
||||
[ run polymorphic_cast_test.cpp ]
|
||||
[ compile-fail implicit_cast_fail2.cpp ]
|
||||
;
|
||||
|
||||
|
||||
|
90
test/appveyor.yml
Normal file
90
test/appveyor.yml
Normal file
@ -0,0 +1,90 @@
|
||||
# Use, modification, and distribution are
|
||||
# subject to the Boost Software License, Version 1.0. (See accompanying
|
||||
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
#
|
||||
# Copyright Antony Polukhin, 2016-2022.
|
||||
|
||||
#
|
||||
# See https://svn.boost.org/trac/boost/wiki/TravisCoverals for description of this file
|
||||
# and how it can be used with Boost libraries.
|
||||
#
|
||||
# File revision #6
|
||||
|
||||
init:
|
||||
# boost-local/libs/ folder to put this library into. This may be useful, if you're for example running Travis
|
||||
# from `Boost.DLL` repo while Boost already has `dll` and with to replace `dll` with content of`Boost.DLL`.
|
||||
#
|
||||
# Otherwise just leave the default value - set BOOST_LIBS_FOLDER=%APPVEYOR_PROJECT_NAME%
|
||||
- set BOOST_LIBS_FOLDER=%APPVEYOR_PROJECT_NAME%
|
||||
|
||||
###############################################################################################################
|
||||
# From this point and below code is same for all the Boost libs
|
||||
###############################################################################################################
|
||||
|
||||
version: 1.71.{build}-{branch}
|
||||
|
||||
# branches to build
|
||||
branches:
|
||||
except:
|
||||
- gh-pages
|
||||
|
||||
skip_tags: true
|
||||
|
||||
environment:
|
||||
matrix:
|
||||
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015
|
||||
TOOLSET: msvc-9.0,msvc-10.0,msvc-11.0,msvc-12.0
|
||||
ADDRMD: 32
|
||||
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017
|
||||
TOOLSET: msvc-14.1,clang-win
|
||||
CXXSTD: 14,17
|
||||
ADDRMD: 32,64
|
||||
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015
|
||||
ADDPATH: C:\cygwin\bin;
|
||||
TOOLSET: gcc
|
||||
CXXSTD: 03,11,14,1z
|
||||
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015
|
||||
ADDPATH: C:\cygwin64\bin;
|
||||
TOOLSET: gcc
|
||||
CXXSTD: 03,11,14,1z
|
||||
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015
|
||||
ADDPATH: C:\mingw\bin;
|
||||
TOOLSET: gcc
|
||||
CXXSTD: 03,11,14,1z
|
||||
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015
|
||||
ADDPATH: C:\mingw-w64\x86_64-7.2.0-posix-seh-rt_v5-rev1\mingw64\bin;
|
||||
TOOLSET: gcc
|
||||
CXXSTD: 03,11,14,1z
|
||||
|
||||
before_build:
|
||||
- set BOOST_BRANCH=develop
|
||||
- if "%APPVEYOR_REPO_BRANCH%" == "master" set BOOST_BRANCH=master
|
||||
- echo "Testing %APPVEYOR_PROJECT_NAME%"
|
||||
# Cloning Boost libraries (fast nondeep cloning)
|
||||
- set BOOST=C:/boost-local
|
||||
- git clone -b %BOOST_BRANCH% --depth 10 https://github.com/boostorg/boost.git %BOOST%
|
||||
- cd %BOOST%
|
||||
- git submodule update --init --depth 10 tools/build tools/boostdep
|
||||
|
||||
- rm -rf %BOOST%/libs/%BOOST_LIBS_FOLDER%
|
||||
- mv -f %APPVEYOR_BUILD_FOLDER% %BOOST%/libs/%BOOST_LIBS_FOLDER%
|
||||
- python tools/boostdep/depinst/depinst.py --git_args "--depth 10 --jobs 2" %BOOST_LIBS_FOLDER%
|
||||
|
||||
build_script:
|
||||
- cmd /c bootstrap
|
||||
- b2.exe headers
|
||||
- cd %BOOST%/libs/%BOOST_LIBS_FOLDER%/test
|
||||
|
||||
after_build:
|
||||
before_test:
|
||||
test_script:
|
||||
- PATH=%ADDPATH%%PATH%
|
||||
- if not "%CXXSTD%" == "" set CXXSTD=cxxstd=%CXXSTD%
|
||||
- if not "%ADDRMD%" == "" set ADDRMD=address-model=%ADDRMD%
|
||||
- echo "Running command ..\..\..\b2 -j3 toolset=%TOOLSET% %CXXSTD% %ADDRMD% variant=debug,release"
|
||||
- ..\..\..\b2.exe -j3 toolset=%TOOLSET% %CXXSTD% %ADDRMD% variant=debug,release cxxflags="-DBOOST_TRAVISCI_BUILD"
|
||||
|
||||
after_test:
|
||||
on_success:
|
||||
on_failure:
|
||||
on_finish:
|
@ -14,13 +14,8 @@
|
||||
// 3 Aug 99 Initial Version
|
||||
|
||||
#include <iostream>
|
||||
#include <climits>
|
||||
#include <cfloat> // for DBL_MAX (Peter Schmid)
|
||||
#include <boost/cast.hpp>
|
||||
|
||||
# if SCHAR_MAX == LONG_MAX
|
||||
# error "This test program doesn't work if SCHAR_MAX == LONG_MAX"
|
||||
# endif
|
||||
#include <boost/polymorphic_cast.hpp>
|
||||
#include <boost/core/lightweight_test.hpp>
|
||||
|
||||
using namespace boost;
|
||||
using std::cout;
|
||||
@ -57,35 +52,31 @@ int main( int argc, char * argv[] )
|
||||
// test polymorphic_cast ---------------------------------------------------//
|
||||
|
||||
// tests which should succeed
|
||||
Base * base = new Derived;
|
||||
Base2 * base2 = 0;
|
||||
Derived * derived = 0;
|
||||
derived = polymorphic_downcast<Derived*>( base ); // downcast
|
||||
assert( derived->kind() == 'D' );
|
||||
Derived derived_instance;
|
||||
Base * base = &derived_instance;
|
||||
Derived * derived = polymorphic_downcast<Derived*>( base ); // downcast
|
||||
BOOST_TEST( derived->kind() == 'D' );
|
||||
|
||||
derived = 0;
|
||||
derived = polymorphic_cast<Derived*>( base ); // downcast, throw on error
|
||||
assert( derived->kind() == 'D' );
|
||||
BOOST_TEST( derived->kind() == 'D' );
|
||||
|
||||
base2 = polymorphic_cast<Base2*>( base ); // crosscast
|
||||
assert( base2->kind2() == '2' );
|
||||
Base2 * base2 = polymorphic_cast<Base2*>( base ); // crosscast
|
||||
BOOST_TEST( base2->kind2() == '2' );
|
||||
|
||||
// tests which should result in errors being detected
|
||||
int err_count = 0;
|
||||
base = new Base;
|
||||
Base base_instance;
|
||||
base = &base_instance;
|
||||
|
||||
if ( argc > 1 && *argv[1] == '1' )
|
||||
{ derived = polymorphic_downcast<Derived*>( base ); } // #1 assert failure
|
||||
|
||||
bool caught_exception = false;
|
||||
try { derived = polymorphic_cast<Derived*>( base ); }
|
||||
catch (std::bad_cast)
|
||||
catch (const std::bad_cast&)
|
||||
{ cout<<"caught bad_cast\n"; caught_exception = true; }
|
||||
if ( !caught_exception ) ++err_count;
|
||||
BOOST_TEST( caught_exception );
|
||||
// the following is just so generated code can be inspected
|
||||
if ( derived->kind() == 'B' ) ++err_count;
|
||||
BOOST_TEST( derived->kind() != 'B' );
|
||||
|
||||
cout << err_count << " errors detected\nTest "
|
||||
<< (err_count==0 ? "passed\n" : "failed\n");
|
||||
return err_count;
|
||||
return boost::report_errors();
|
||||
} // main
|
@ -28,5 +28,15 @@ int main()
|
||||
|
||||
type<foo> f = check_return(boost::implicit_cast<foo>("hello"));
|
||||
type<long> z = check_return(boost::implicit_cast<long>(foo("hello")));
|
||||
|
||||
// warning suppression:
|
||||
(void)x;
|
||||
(void)f;
|
||||
(void)z;
|
||||
|
||||
|
||||
BOOST_CONSTEXPR long value = boost::implicit_cast<long>(42);
|
||||
BOOST_TEST(value == 42L);
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
||||
|
@ -4,10 +4,6 @@
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#include <boost/implicit_cast.hpp>
|
||||
#include <boost/type.hpp>
|
||||
|
||||
#define BOOST_INCLUDE_MAIN
|
||||
#include <boost/test/test_tools.hpp>
|
||||
|
||||
using boost::implicit_cast;
|
||||
|
||||
@ -16,7 +12,9 @@ struct foo
|
||||
explicit foo(char const*) {}
|
||||
};
|
||||
|
||||
int test_main(int, char*[])
|
||||
int main()
|
||||
{
|
||||
foo x = implicit_cast<foo>("foobar");
|
||||
(void)x; // warning suppression.
|
||||
return 0;
|
||||
}
|
||||
|
19
test/implicit_cast_fail2.cpp
Normal file
19
test/implicit_cast_fail2.cpp
Normal file
@ -0,0 +1,19 @@
|
||||
//
|
||||
// Test that implicit_cast requires a template argument
|
||||
//
|
||||
// Copyright 2014 Peter Dimov
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
//
|
||||
// See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt
|
||||
//
|
||||
|
||||
#include <boost/implicit_cast.hpp>
|
||||
|
||||
int main()
|
||||
{
|
||||
int x = boost::implicit_cast( 1 );
|
||||
(void)x;
|
||||
|
||||
return 0;
|
||||
}
|
@ -1,61 +0,0 @@
|
||||
// Unit test for boost::lexical_cast.
|
||||
//
|
||||
// See http://www.boost.org for most recent version, including documentation.
|
||||
//
|
||||
// Copyright Sergey Shandar 2005, Alexander Nasonov, 2007.
|
||||
//
|
||||
// Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt).
|
||||
//
|
||||
// Test abstract class. Bug 1358600:
|
||||
// http://sf.net/tracker/?func=detail&aid=1358600&group_id=7586&atid=107586
|
||||
|
||||
#include <boost/config.hpp>
|
||||
|
||||
#if defined(__INTEL_COMPILER)
|
||||
#pragma warning(disable: 193 383 488 981 1418 1419)
|
||||
#elif defined(BOOST_MSVC)
|
||||
#pragma warning(disable: 4097 4100 4121 4127 4146 4244 4245 4511 4512 4701 4800)
|
||||
#endif
|
||||
|
||||
#include <boost/lexical_cast.hpp>
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
using namespace boost;
|
||||
|
||||
void test_abstract();
|
||||
|
||||
unit_test::test_suite *init_unit_test_suite(int, char *[])
|
||||
{
|
||||
unit_test_framework::test_suite *suite =
|
||||
BOOST_TEST_SUITE("lexical_cast unit test");
|
||||
suite->add(BOOST_TEST_CASE(&test_abstract));
|
||||
|
||||
return suite;
|
||||
}
|
||||
|
||||
class A
|
||||
{
|
||||
public:
|
||||
virtual void out(std::ostream &) const = 0;
|
||||
};
|
||||
|
||||
class B: public A
|
||||
{
|
||||
public:
|
||||
virtual void out(std::ostream &O) const { O << "B"; }
|
||||
};
|
||||
|
||||
std::ostream &operator<<(std::ostream &O, const A &a)
|
||||
{
|
||||
a.out(O);
|
||||
return O;
|
||||
};
|
||||
|
||||
void test_abstract()
|
||||
{
|
||||
const A &a = B();
|
||||
BOOST_CHECK(boost::lexical_cast<std::string>(a) == "B");
|
||||
}
|
||||
|
@ -1,98 +0,0 @@
|
||||
// Unit test for boost::lexical_cast.
|
||||
//
|
||||
// See http://www.boost.org for most recent version, including documentation.
|
||||
//
|
||||
// Copyright Alexander Nasonov, 2006.
|
||||
//
|
||||
// Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt).
|
||||
//
|
||||
// Test round-tripping conversion FPT -> string -> FPT,
|
||||
// where FPT is Floating Point Type.
|
||||
|
||||
#include <boost/config.hpp>
|
||||
|
||||
#if defined(__INTEL_COMPILER)
|
||||
#pragma warning(disable: 193 383 488 981 1418 1419)
|
||||
#elif defined(BOOST_MSVC)
|
||||
#pragma warning(disable: 4097 4100 4121 4127 4146 4244 4245 4511 4512 4701 4800)
|
||||
#endif
|
||||
|
||||
#include <boost/lexical_cast.hpp>
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
using namespace boost;
|
||||
|
||||
void test_round_conversion_float();
|
||||
void test_round_conversion_double();
|
||||
void test_round_conversion_long_double();
|
||||
|
||||
unit_test::test_suite *init_unit_test_suite(int, char *[])
|
||||
{
|
||||
unit_test_framework::test_suite *suite =
|
||||
BOOST_TEST_SUITE("lexical_cast unit test");
|
||||
suite->add(BOOST_TEST_CASE(&test_round_conversion_float));
|
||||
suite->add(BOOST_TEST_CASE(&test_round_conversion_double));
|
||||
suite->add(BOOST_TEST_CASE(&test_round_conversion_long_double));
|
||||
|
||||
return suite;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
void test_round_conversion()
|
||||
{
|
||||
T epsilon = std::numeric_limits<T>::epsilon();
|
||||
std::string const epsilon_s = boost::lexical_cast<std::string>(epsilon);
|
||||
BOOST_CHECK(epsilon == lexical_cast<T>(epsilon_s));
|
||||
|
||||
T max_ = (std::numeric_limits<T>::max)();
|
||||
std::string const max_s = boost::lexical_cast<std::string>(max_);
|
||||
BOOST_CHECK(max_ == lexical_cast<T>(max_s));
|
||||
|
||||
T min_ = (std::numeric_limits<T>::min)();
|
||||
std::string const min_s = boost::lexical_cast<std::string>(min_);
|
||||
BOOST_CHECK(min_ == lexical_cast<T>(min_s));
|
||||
|
||||
T max_div137 = max_ / 137;
|
||||
std::string max_div137_s = boost::lexical_cast<std::string>(max_div137);
|
||||
BOOST_CHECK(max_div137 == lexical_cast<T>(max_div137_s));
|
||||
|
||||
T epsilon_mult137 = epsilon * 137;
|
||||
std::string epsilon_mult137_s(lexical_cast<std::string>(epsilon_mult137));
|
||||
BOOST_CHECK(epsilon_mult137 == lexical_cast<T>(epsilon_mult137_s));
|
||||
|
||||
}
|
||||
|
||||
#if defined(BOOST_MSVC)
|
||||
// See bug http://tinyurl.com/vhpvo
|
||||
template<class T>
|
||||
void test_msvc_magic_values()
|
||||
{
|
||||
T magic_msvc = 0.00010000433948393407;
|
||||
std::string magic_msvc_s = boost::lexical_cast<std::string>(magic_msvc);
|
||||
BOOST_CHECK(magic_msvc == lexical_cast<T>(magic_msvc_s));
|
||||
}
|
||||
#endif
|
||||
|
||||
void test_round_conversion_float()
|
||||
{
|
||||
test_round_conversion<float>();
|
||||
}
|
||||
|
||||
void test_round_conversion_double()
|
||||
{
|
||||
test_round_conversion<double>();
|
||||
#if defined(BOOST_MSVC)
|
||||
test_msvc_magic_values<double>();
|
||||
#endif
|
||||
}
|
||||
|
||||
void test_round_conversion_long_double()
|
||||
{
|
||||
test_round_conversion<long double>();
|
||||
#if defined(BOOST_MSVC)
|
||||
test_msvc_magic_values<long double>();
|
||||
#endif
|
||||
}
|
||||
|
@ -1,54 +0,0 @@
|
||||
// Unit test for boost::lexical_cast.
|
||||
//
|
||||
// See http://www.boost.org for most recent version, including documentation.
|
||||
//
|
||||
// Copyright Alexander Nasonov, 2007.
|
||||
//
|
||||
// Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt).
|
||||
//
|
||||
// Test that Source can be non-copyable.
|
||||
|
||||
#include <boost/config.hpp>
|
||||
|
||||
#if defined(__INTEL_COMPILER)
|
||||
#pragma warning(disable: 193 383 488 981 1418 1419)
|
||||
#elif defined(BOOST_MSVC)
|
||||
#pragma warning(disable: 4097 4100 4121 4127 4146 4244 4245 4511 4512 4701 4800)
|
||||
#endif
|
||||
|
||||
#include <boost/lexical_cast.hpp>
|
||||
#include <boost/noncopyable.hpp>
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
using namespace boost;
|
||||
|
||||
void test_noncopyable();
|
||||
|
||||
unit_test::test_suite *init_unit_test_suite(int, char *[])
|
||||
{
|
||||
unit_test_framework::test_suite *suite =
|
||||
BOOST_TEST_SUITE("lexical_cast unit test");
|
||||
suite->add(BOOST_TEST_CASE(&test_noncopyable));
|
||||
|
||||
return suite;
|
||||
}
|
||||
|
||||
class Noncopyable : private boost::noncopyable
|
||||
{
|
||||
public:
|
||||
Noncopyable() {}
|
||||
};
|
||||
|
||||
inline std::ostream &operator<<(std::ostream &out, const Noncopyable&)
|
||||
{
|
||||
return out << "Noncopyable";
|
||||
}
|
||||
|
||||
void test_noncopyable()
|
||||
{
|
||||
Noncopyable x;
|
||||
BOOST_CHECK(boost::lexical_cast<std::string>(x) == "Noncopyable");
|
||||
}
|
||||
|
@ -1,53 +0,0 @@
|
||||
#include <boost/config.hpp>
|
||||
#include <boost/lexical_cast.hpp>
|
||||
#include <boost/cstdint.hpp>
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
#include <string>
|
||||
|
||||
using namespace boost;
|
||||
|
||||
// See also test_conversion_from_string_to_integral(CharT)
|
||||
// in libs/conversion/lexical_cast_test.cpp
|
||||
template<class T, class CharT>
|
||||
void test_too_long_number(CharT zero)
|
||||
{
|
||||
typedef std::numeric_limits<T> limits;
|
||||
|
||||
std::basic_string<CharT> s;
|
||||
|
||||
std::basic_ostringstream<CharT> o;
|
||||
o << (limits::max)() << zero;
|
||||
s = o.str();
|
||||
BOOST_CHECK_THROW(lexical_cast<T>(s), bad_lexical_cast);
|
||||
s[s.size()-1] += 9; // '0' -> '9'
|
||||
BOOST_CHECK_THROW(lexical_cast<T>(s), bad_lexical_cast);
|
||||
|
||||
if(limits::is_signed)
|
||||
{
|
||||
std::basic_ostringstream<CharT> o;
|
||||
o << (limits::min)() << zero;
|
||||
s = o.str();
|
||||
BOOST_CHECK_THROW(lexical_cast<T>(s), bad_lexical_cast);
|
||||
s[s.size()-1] += 9; // '0' -> '9'
|
||||
BOOST_CHECK_THROW(lexical_cast<T>(s), bad_lexical_cast);
|
||||
}
|
||||
}
|
||||
|
||||
void test_vc8_bug()
|
||||
{
|
||||
test_too_long_number<boost::intmax_t>('0');
|
||||
test_too_long_number<boost::uintmax_t>('0');
|
||||
#if !defined(BOOST_LCAST_NO_WCHAR_T)
|
||||
test_too_long_number<boost::intmax_t>(L'0');
|
||||
test_too_long_number<boost::uintmax_t>(L'0');
|
||||
#endif
|
||||
}
|
||||
|
||||
unit_test::test_suite *init_unit_test_suite(int, char *[])
|
||||
{
|
||||
unit_test_framework::test_suite *suite =
|
||||
BOOST_TEST_SUITE("lexical_cast vc8 bug unit test");
|
||||
suite->add(BOOST_TEST_CASE(test_vc8_bug));
|
||||
return suite;
|
||||
}
|
391
test/polymorphic_cast_test.cpp
Normal file
391
test/polymorphic_cast_test.cpp
Normal file
@ -0,0 +1,391 @@
|
||||
//
|
||||
// Test boost::polymorphic_cast, boost::polymorphic_downcast and
|
||||
// boost::polymorphic_pointer_cast, boost::polymorphic_pointer_downcast
|
||||
//
|
||||
// Copyright 1999 Beman Dawes
|
||||
// Copyright 1999 Dave Abrahams
|
||||
// Copyright 2014 Peter Dimov
|
||||
// Copyright 2014 Boris Rasin, Antony Polukhin
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
//
|
||||
// See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt
|
||||
//
|
||||
|
||||
#define BOOST_ENABLE_ASSERT_HANDLER
|
||||
#include <boost/polymorphic_cast.hpp>
|
||||
#include <boost/polymorphic_pointer_cast.hpp>
|
||||
#include <boost/smart_ptr/shared_ptr.hpp>
|
||||
#include <boost/smart_ptr/intrusive_ptr.hpp>
|
||||
#include <boost/smart_ptr/intrusive_ref_counter.hpp>
|
||||
#include <boost/core/lightweight_test.hpp>
|
||||
#include <string>
|
||||
#include <memory>
|
||||
|
||||
static bool expect_assertion = false;
|
||||
static int assertion_failed_count = 0;
|
||||
|
||||
//assertion handler throws it to exit like assert, but to be able to catch it and stop
|
||||
//usage: BOOST_TEST_THROWS( function_with_assert(), expected_assertion );
|
||||
struct expected_assertion {};
|
||||
|
||||
// BOOST_ASSERT custom handler
|
||||
void boost::assertion_failed( char const * expr, char const * function, char const * file, long line )
|
||||
{
|
||||
if( expect_assertion )
|
||||
{
|
||||
++assertion_failed_count;
|
||||
throw expected_assertion();
|
||||
}
|
||||
else
|
||||
{
|
||||
BOOST_ERROR( "unexpected assertion" );
|
||||
|
||||
BOOST_LIGHTWEIGHT_TEST_OSTREAM
|
||||
<< file << "(" << line << "): assertion '" << expr << "' failed in function '"
|
||||
<< function << "'" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
struct Base : boost::intrusive_ref_counter<Base>
|
||||
{
|
||||
virtual ~Base() {}
|
||||
virtual std::string kind() { return "Base"; }
|
||||
};
|
||||
|
||||
struct Base2
|
||||
{
|
||||
virtual ~Base2() {}
|
||||
virtual std::string kind2() { return "Base2"; }
|
||||
};
|
||||
|
||||
struct Derived : public Base, Base2
|
||||
{
|
||||
virtual std::string kind() { return "Derived"; }
|
||||
};
|
||||
|
||||
static void test_polymorphic_cast()
|
||||
{
|
||||
Base * base = new Derived;
|
||||
|
||||
Derived * derived;
|
||||
|
||||
try
|
||||
{
|
||||
derived = boost::polymorphic_cast<Derived*>( base );
|
||||
|
||||
BOOST_TEST( derived != 0 );
|
||||
|
||||
if( derived != 0 )
|
||||
{
|
||||
BOOST_TEST_EQ( derived->kind(), "Derived" );
|
||||
}
|
||||
}
|
||||
catch( std::bad_cast const& )
|
||||
{
|
||||
BOOST_ERROR( "boost::polymorphic_cast<Derived*>( base ) threw std::bad_cast" );
|
||||
}
|
||||
|
||||
Base2 * base2;
|
||||
|
||||
try
|
||||
{
|
||||
base2 = boost::polymorphic_cast<Base2*>( base ); // crosscast
|
||||
|
||||
BOOST_TEST( base2 != 0 );
|
||||
|
||||
if( base2 != 0 )
|
||||
{
|
||||
BOOST_TEST_EQ( base2->kind2(), "Base2" );
|
||||
}
|
||||
}
|
||||
catch( std::bad_cast const& )
|
||||
{
|
||||
BOOST_ERROR( "boost::polymorphic_cast<Base2*>( base ) threw std::bad_cast" );
|
||||
}
|
||||
|
||||
delete base;
|
||||
}
|
||||
|
||||
static void test_polymorphic_pointer_cast()
|
||||
{
|
||||
Base * base = new Derived;
|
||||
|
||||
Derived * derived;
|
||||
|
||||
try
|
||||
{
|
||||
derived = boost::polymorphic_pointer_cast<Derived>( base );
|
||||
|
||||
BOOST_TEST( derived != 0 );
|
||||
|
||||
if( derived != 0 )
|
||||
{
|
||||
BOOST_TEST_EQ( derived->kind(), "Derived" );
|
||||
}
|
||||
}
|
||||
catch( std::bad_cast const& )
|
||||
{
|
||||
BOOST_ERROR( "boost::polymorphic_pointer_cast<Derived>( base ) threw std::bad_cast" );
|
||||
}
|
||||
|
||||
Base2 * base2;
|
||||
|
||||
try
|
||||
{
|
||||
base2 = boost::polymorphic_pointer_cast<Base2>( base ); // crosscast
|
||||
|
||||
BOOST_TEST( base2 != 0 );
|
||||
|
||||
if( base2 != 0 )
|
||||
{
|
||||
BOOST_TEST_EQ( base2->kind2(), "Base2" );
|
||||
}
|
||||
}
|
||||
catch( std::bad_cast const& )
|
||||
{
|
||||
BOOST_ERROR( "boost::polymorphic_pointer_cast<Base2>( base ) threw std::bad_cast" );
|
||||
}
|
||||
|
||||
boost::shared_ptr<Base> sp_base( base );
|
||||
boost::shared_ptr<Base2> sp_base2;
|
||||
try
|
||||
{
|
||||
sp_base2 = boost::polymorphic_pointer_cast<Base2>( sp_base ); // crosscast
|
||||
|
||||
BOOST_TEST( sp_base2 != 0 );
|
||||
|
||||
if( sp_base2 != 0 )
|
||||
{
|
||||
BOOST_TEST_EQ( sp_base2->kind2(), "Base2" );
|
||||
}
|
||||
}
|
||||
catch( std::bad_cast const& )
|
||||
{
|
||||
BOOST_ERROR( "boost::polymorphic_pointer_cast<Base2>( sp_base ) threw std::bad_cast" );
|
||||
}
|
||||
|
||||
// we do not `delete base;` because sahred_ptr is holding base
|
||||
}
|
||||
|
||||
static void test_polymorphic_downcast()
|
||||
{
|
||||
Base *base_pointer = new Derived;
|
||||
|
||||
// test raw pointer cast
|
||||
Derived *derived_pointer = boost::polymorphic_downcast<Derived *>(base_pointer);
|
||||
|
||||
BOOST_TEST(derived_pointer != 0);
|
||||
|
||||
if (derived_pointer != 0)
|
||||
{
|
||||
BOOST_TEST_EQ(derived_pointer->kind(), "Derived");
|
||||
}
|
||||
|
||||
// test reference cast
|
||||
Derived& derived_ref = boost::polymorphic_downcast<Derived&>(*base_pointer);
|
||||
BOOST_TEST_EQ(derived_ref.kind(), "Derived");
|
||||
|
||||
delete base_pointer;
|
||||
}
|
||||
|
||||
static void test_polymorphic_pointer_downcast_builtin()
|
||||
{
|
||||
Base * base = new Derived;
|
||||
|
||||
Derived * derived = boost::polymorphic_pointer_downcast<Derived>( base );
|
||||
|
||||
BOOST_TEST( derived != 0 );
|
||||
|
||||
if( derived != 0 )
|
||||
{
|
||||
BOOST_TEST_EQ( derived->kind(), "Derived" );
|
||||
}
|
||||
|
||||
// polymorphic_pointer_downcast can't do crosscasts
|
||||
|
||||
delete base;
|
||||
}
|
||||
|
||||
static void test_polymorphic_pointer_downcast_boost_shared()
|
||||
{
|
||||
boost::shared_ptr<Base> base (new Derived);
|
||||
|
||||
boost::shared_ptr<Derived> derived = boost::polymorphic_pointer_downcast<Derived>( base );
|
||||
|
||||
BOOST_TEST( derived != 0 );
|
||||
|
||||
if( derived != 0 )
|
||||
{
|
||||
BOOST_TEST_EQ( derived->kind(), "Derived" );
|
||||
}
|
||||
}
|
||||
|
||||
static void test_polymorphic_pointer_downcast_intrusive()
|
||||
{
|
||||
boost::intrusive_ptr<Base> base (new Derived);
|
||||
|
||||
boost::intrusive_ptr<Derived> derived = boost::polymorphic_pointer_downcast<Derived>( base );
|
||||
|
||||
BOOST_TEST( derived != 0 );
|
||||
|
||||
if( derived != 0 )
|
||||
{
|
||||
BOOST_TEST_EQ( derived->kind(), "Derived" );
|
||||
}
|
||||
}
|
||||
|
||||
#ifndef BOOST_NO_CXX11_SMART_PTR
|
||||
|
||||
static void test_polymorphic_pointer_downcast_std_shared()
|
||||
{
|
||||
std::shared_ptr<Base> base (new Derived);
|
||||
|
||||
std::shared_ptr<Derived> derived = boost::polymorphic_pointer_downcast<Derived>( base );
|
||||
|
||||
BOOST_TEST( derived != 0 );
|
||||
|
||||
if( derived != 0 )
|
||||
{
|
||||
BOOST_TEST_EQ( derived->kind(), "Derived" );
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
static void test_polymorphic_cast_fail()
|
||||
{
|
||||
Base * base = new Base;
|
||||
|
||||
BOOST_TEST_THROWS( boost::polymorphic_cast<Derived*>( base ), std::bad_cast );
|
||||
|
||||
delete base;
|
||||
}
|
||||
|
||||
static void test_polymorphic_pointer_cast_fail()
|
||||
{
|
||||
Base * base = new Base;
|
||||
BOOST_TEST_THROWS( boost::polymorphic_pointer_cast<Derived>( base ), std::bad_cast );
|
||||
delete base;
|
||||
|
||||
BOOST_TEST_THROWS( boost::polymorphic_pointer_cast<Derived>( boost::shared_ptr<Base>(new Base) ), std::bad_cast );
|
||||
|
||||
#ifndef BOOST_NO_CXX11_SMART_PTR
|
||||
BOOST_TEST_THROWS( boost::polymorphic_pointer_cast<Derived>( std::shared_ptr<Base>(new Base) ), std::bad_cast );
|
||||
#endif
|
||||
|
||||
BOOST_TEST_THROWS( boost::polymorphic_pointer_cast<Derived>( boost::intrusive_ptr<Base>(new Base) ), std::bad_cast );
|
||||
}
|
||||
|
||||
static void test_polymorphic_downcast_fail()
|
||||
{
|
||||
Base * base_pointer = new Base;
|
||||
|
||||
{
|
||||
// test raw pointer cast
|
||||
|
||||
int old_count = assertion_failed_count;
|
||||
expect_assertion = true;
|
||||
|
||||
BOOST_TEST_THROWS(boost::polymorphic_downcast<Derived *>(base_pointer), expected_assertion); // should assert
|
||||
|
||||
BOOST_TEST_EQ(assertion_failed_count, old_count + 1);
|
||||
expect_assertion = false;
|
||||
}
|
||||
{
|
||||
// test reference cast
|
||||
|
||||
int old_count = assertion_failed_count;
|
||||
expect_assertion = true;
|
||||
|
||||
BOOST_TEST_THROWS(boost::polymorphic_downcast<Derived &>(*base_pointer), expected_assertion); // should assert
|
||||
|
||||
BOOST_TEST_EQ(assertion_failed_count, old_count + 1);
|
||||
expect_assertion = false;
|
||||
}
|
||||
|
||||
delete base_pointer;
|
||||
}
|
||||
|
||||
static void test_polymorphic_pointer_downcast_builtin_fail()
|
||||
{
|
||||
Base * base = new Base;
|
||||
|
||||
int old_count = assertion_failed_count;
|
||||
expect_assertion = true;
|
||||
|
||||
BOOST_TEST_THROWS( boost::polymorphic_pointer_downcast<Derived>( base ), expected_assertion ); // should assert
|
||||
|
||||
BOOST_TEST_EQ( assertion_failed_count, old_count + 1 );
|
||||
expect_assertion = false;
|
||||
|
||||
delete base;
|
||||
}
|
||||
|
||||
static void test_polymorphic_pointer_downcast_boost_shared_fail()
|
||||
{
|
||||
boost::shared_ptr<Base> base (new Base);
|
||||
|
||||
int old_count = assertion_failed_count;
|
||||
expect_assertion = true;
|
||||
|
||||
BOOST_TEST_THROWS( boost::polymorphic_pointer_downcast<Derived>( base ), expected_assertion ); // should assert
|
||||
|
||||
BOOST_TEST_EQ( assertion_failed_count, old_count + 1 );
|
||||
expect_assertion = false;
|
||||
}
|
||||
|
||||
#ifndef BOOST_NO_CXX11_SMART_PTR
|
||||
|
||||
static void test_polymorphic_pointer_downcast_std_shared_fail()
|
||||
{
|
||||
std::shared_ptr<Base> base (new Base);
|
||||
|
||||
int old_count = assertion_failed_count;
|
||||
expect_assertion = true;
|
||||
|
||||
BOOST_TEST_THROWS( boost::polymorphic_pointer_downcast<Derived>( base ), expected_assertion ); // should assert
|
||||
|
||||
BOOST_TEST_EQ( assertion_failed_count, old_count + 1 );
|
||||
expect_assertion = false;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
static void test_polymorphic_pointer_downcast_intrusive_fail()
|
||||
{
|
||||
boost::intrusive_ptr<Base> base (new Base);
|
||||
|
||||
int old_count = assertion_failed_count;
|
||||
expect_assertion = true;
|
||||
|
||||
BOOST_TEST_THROWS( boost::polymorphic_pointer_downcast<Derived>( base ), expected_assertion); // should assert
|
||||
|
||||
BOOST_TEST_EQ( assertion_failed_count, old_count + 1 );
|
||||
expect_assertion = false;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
test_polymorphic_cast();
|
||||
test_polymorphic_pointer_cast();
|
||||
test_polymorphic_downcast();
|
||||
test_polymorphic_pointer_downcast_builtin();
|
||||
test_polymorphic_pointer_downcast_boost_shared();
|
||||
test_polymorphic_pointer_downcast_intrusive();
|
||||
test_polymorphic_cast_fail();
|
||||
test_polymorphic_pointer_cast_fail();
|
||||
test_polymorphic_downcast_fail();
|
||||
test_polymorphic_pointer_downcast_builtin_fail();
|
||||
test_polymorphic_pointer_downcast_boost_shared_fail();
|
||||
test_polymorphic_pointer_downcast_intrusive_fail();
|
||||
|
||||
#ifndef BOOST_NO_CXX11_SMART_PTR
|
||||
test_polymorphic_pointer_downcast_std_shared();
|
||||
test_polymorphic_pointer_downcast_std_shared_fail();
|
||||
#endif
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
Reference in New Issue
Block a user