├── .gitattributes ├── .travis.yml ├── CMakeLists.txt ├── appveyor.yml ├── build.jam ├── doc ├── circular_buffer.idx ├── circular_buffer.qbk ├── images │ ├── circular_buffer.png │ └── space_optimized.png └── jamfile.v2 ├── example ├── bounded_buffer_comparison.cpp ├── circular_buffer_bound_example.cpp ├── circular_buffer_example.cpp ├── circular_buffer_examples.bat ├── circular_buffer_iter_example.cpp ├── circular_buffer_sum_example.cpp └── jamfile.v2 ├── include └── boost │ ├── circular_buffer.hpp │ ├── circular_buffer │ ├── base.hpp │ ├── debug.hpp │ ├── details.hpp │ └── space_optimized.hpp │ └── circular_buffer_fwd.hpp ├── index.html ├── meta └── libraries.json └── test ├── Jamfile.v2 ├── base_test.cpp ├── bounded_buffer_comparison.cpp ├── common.ipp ├── constant_erase_test.cpp ├── soft_iterator_invalidation.cpp ├── space_optimized_test.cpp └── test.hpp /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto !eol svneol=native#text/plain 2 | *.gitattributes text svneol=native#text/plain 3 | 4 | # Scriptish formats 5 | *.bat text svneol=native#text/plain 6 | *.bsh text svneol=native#text/x-beanshell 7 | *.cgi text svneol=native#text/plain 8 | *.cmd text svneol=native#text/plain 9 | *.js text svneol=native#text/javascript 10 | *.php text svneol=native#text/x-php 11 | *.pl text svneol=native#text/x-perl 12 | *.pm text svneol=native#text/x-perl 13 | *.py text svneol=native#text/x-python 14 | *.sh eol=lf svneol=LF#text/x-sh 15 | configure eol=lf svneol=LF#text/x-sh 16 | 17 | # Image formats 18 | *.bmp binary svneol=unset#image/bmp 19 | *.gif binary svneol=unset#image/gif 20 | *.ico binary svneol=unset#image/ico 21 | *.jpeg binary svneol=unset#image/jpeg 22 | *.jpg binary svneol=unset#image/jpeg 23 | *.png binary svneol=unset#image/png 24 | *.tif binary svneol=unset#image/tiff 25 | *.tiff binary svneol=unset#image/tiff 26 | *.svg text svneol=native#image/svg%2Bxml 27 | 28 | # Data formats 29 | *.pdf binary svneol=unset#application/pdf 30 | *.avi binary svneol=unset#video/avi 31 | *.doc binary svneol=unset#application/msword 32 | *.dsp text svneol=crlf#text/plain 33 | *.dsw text svneol=crlf#text/plain 34 | *.eps binary svneol=unset#application/postscript 35 | *.gz binary svneol=unset#application/gzip 36 | *.mov binary svneol=unset#video/quicktime 37 | *.mp3 binary svneol=unset#audio/mpeg 38 | *.ppt binary svneol=unset#application/vnd.ms-powerpoint 39 | *.ps binary svneol=unset#application/postscript 40 | *.psd binary svneol=unset#application/photoshop 41 | *.rdf binary svneol=unset#text/rdf 42 | *.rss text svneol=unset#text/xml 43 | *.rtf binary svneol=unset#text/rtf 44 | *.sln text svneol=native#text/plain 45 | *.swf binary svneol=unset#application/x-shockwave-flash 46 | *.tgz binary svneol=unset#application/gzip 47 | *.vcproj text svneol=native#text/xml 48 | *.vcxproj text svneol=native#text/xml 49 | *.vsprops text svneol=native#text/xml 50 | *.wav binary svneol=unset#audio/wav 51 | *.xls binary svneol=unset#application/vnd.ms-excel 52 | *.zip binary svneol=unset#application/zip 53 | 54 | # Text formats 55 | .htaccess text svneol=native#text/plain 56 | *.bbk text svneol=native#text/xml 57 | *.cmake text svneol=native#text/plain 58 | *.css text svneol=native#text/css 59 | *.dtd text svneol=native#text/xml 60 | *.htm text svneol=native#text/html 61 | *.html text svneol=native#text/html 62 | *.ini text svneol=native#text/plain 63 | *.log text svneol=native#text/plain 64 | *.mak text svneol=native#text/plain 65 | *.qbk text svneol=native#text/plain 66 | *.rst text svneol=native#text/plain 67 | *.sql text svneol=native#text/x-sql 68 | *.txt text svneol=native#text/plain 69 | *.xhtml text svneol=native#text/xhtml%2Bxml 70 | *.xml text svneol=native#text/xml 71 | *.xsd text svneol=native#text/xml 72 | *.xsl text svneol=native#text/xml 73 | *.xslt text svneol=native#text/xml 74 | *.xul text svneol=native#text/xul 75 | *.yml text svneol=native#text/plain 76 | boost-no-inspect text svneol=native#text/plain 77 | CHANGES text svneol=native#text/plain 78 | COPYING text svneol=native#text/plain 79 | INSTALL text svneol=native#text/plain 80 | Jamfile text svneol=native#text/plain 81 | Jamroot text svneol=native#text/plain 82 | Jamfile.v2 text svneol=native#text/plain 83 | Jamrules text svneol=native#text/plain 84 | Makefile* text svneol=native#text/plain 85 | README text svneol=native#text/plain 86 | TODO text svneol=native#text/plain 87 | 88 | # Code formats 89 | *.c text svneol=native#text/plain 90 | *.cpp text svneol=native#text/plain 91 | *.h text svneol=native#text/plain 92 | *.hpp text svneol=native#text/plain 93 | *.ipp text svneol=native#text/plain 94 | *.tpp text svneol=native#text/plain 95 | *.jam text svneol=native#text/plain 96 | *.java text svneol=native#text/plain 97 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | # Copyright 2018 Peter Dimov 2 | # Distributed under the Boost Software License, Version 1.0. 3 | 4 | language: cpp 5 | 6 | sudo: false 7 | 8 | dist: trusty 9 | 10 | python: "2.7" 11 | 12 | branches: 13 | only: 14 | - master 15 | - develop 16 | 17 | env: 18 | matrix: 19 | - BOGUS_JOB=true 20 | 21 | matrix: 22 | 23 | exclude: 24 | - env: BOGUS_JOB=true 25 | 26 | include: 27 | - os: linux 28 | compiler: g++ 29 | env: TOOLSET=gcc COMPILER=g++ CXXSTD=03,11 30 | 31 | - os: linux 32 | compiler: g++-4.4 33 | env: TOOLSET=gcc COMPILER=g++-4.4 CXXSTD=98,0x 34 | addons: 35 | apt: 36 | packages: 37 | - g++-4.4 38 | sources: 39 | - ubuntu-toolchain-r-test 40 | 41 | - os: linux 42 | compiler: g++-4.6 43 | env: TOOLSET=gcc COMPILER=g++-4.6 CXXSTD=03,0x 44 | addons: 45 | apt: 46 | packages: 47 | - g++-4.6 48 | sources: 49 | - ubuntu-toolchain-r-test 50 | 51 | - os: linux 52 | compiler: g++-4.7 53 | env: TOOLSET=gcc COMPILER=g++-4.7 CXXSTD=03,11 54 | addons: 55 | apt: 56 | packages: 57 | - g++-4.7 58 | sources: 59 | - ubuntu-toolchain-r-test 60 | 61 | - os: linux 62 | compiler: g++-4.8 63 | env: TOOLSET=gcc COMPILER=g++-4.8 CXXSTD=03,11 64 | addons: 65 | apt: 66 | packages: 67 | - g++-4.8 68 | sources: 69 | - ubuntu-toolchain-r-test 70 | 71 | - os: linux 72 | compiler: g++-4.9 73 | env: TOOLSET=gcc COMPILER=g++-4.9 CXXSTD=03,11 74 | addons: 75 | apt: 76 | packages: 77 | - g++-4.9 78 | sources: 79 | - ubuntu-toolchain-r-test 80 | 81 | - os: linux 82 | compiler: g++-5 83 | env: TOOLSET=gcc COMPILER=g++-5 CXXSTD=03,11,14,1z 84 | addons: 85 | apt: 86 | packages: 87 | - g++-5 88 | sources: 89 | - ubuntu-toolchain-r-test 90 | 91 | - os: linux 92 | compiler: g++-6 93 | env: TOOLSET=gcc COMPILER=g++-6 CXXSTD=03,11,14,1z 94 | addons: 95 | apt: 96 | packages: 97 | - g++-6 98 | sources: 99 | - ubuntu-toolchain-r-test 100 | 101 | - os: linux 102 | compiler: g++-7 103 | env: TOOLSET=gcc COMPILER=g++-7 CXXSTD=03,11,14,17 104 | addons: 105 | apt: 106 | packages: 107 | - g++-7 108 | sources: 109 | - ubuntu-toolchain-r-test 110 | 111 | - os: linux 112 | compiler: g++-8 113 | env: TOOLSET=gcc COMPILER=g++-8 CXXSTD=03,11,14 114 | addons: 115 | apt: 116 | packages: 117 | - g++-8 118 | sources: 119 | - ubuntu-toolchain-r-test 120 | 121 | - os: linux 122 | compiler: g++-8 123 | env: TOOLSET=gcc COMPILER=g++-8 CXXSTD=17,2a 124 | addons: 125 | apt: 126 | packages: 127 | - g++-8 128 | sources: 129 | - ubuntu-toolchain-r-test 130 | 131 | - os: linux 132 | compiler: clang++ 133 | env: TOOLSET=clang COMPILER=clang++ CXXSTD=03,11 134 | 135 | - os: linux 136 | compiler: /usr/bin/clang++ 137 | env: TOOLSET=clang COMPILER=/usr/bin/clang++ CXXSTD=03,11 138 | addons: 139 | apt: 140 | packages: 141 | - clang-3.3 142 | 143 | - os: linux 144 | compiler: /usr/bin/clang++ 145 | env: TOOLSET=clang COMPILER=/usr/bin/clang++ CXXSTD=03,11 146 | addons: 147 | apt: 148 | packages: 149 | - clang-3.4 150 | 151 | - os: linux 152 | compiler: clang++-3.5 153 | env: TOOLSET=clang COMPILER=clang++-3.5 CXXSTD=03,11,14,1z 154 | addons: 155 | apt: 156 | packages: 157 | - clang-3.5 158 | - libstdc++-4.9-dev 159 | sources: 160 | - ubuntu-toolchain-r-test 161 | - llvm-toolchain-precise-3.5 162 | 163 | - os: linux 164 | compiler: clang++-3.6 165 | env: TOOLSET=clang COMPILER=clang++-3.6 CXXSTD=03,11,14,1z 166 | addons: 167 | apt: 168 | packages: 169 | - clang-3.6 170 | sources: 171 | - ubuntu-toolchain-r-test 172 | - llvm-toolchain-precise-3.6 173 | 174 | - os: linux 175 | compiler: clang++-3.7 176 | env: TOOLSET=clang COMPILER=clang++-3.7 CXXSTD=03,11,14,1z 177 | addons: 178 | apt: 179 | packages: 180 | - clang-3.7 181 | sources: 182 | - ubuntu-toolchain-r-test 183 | - llvm-toolchain-precise-3.7 184 | 185 | - os: linux 186 | compiler: clang++-3.8 187 | env: TOOLSET=clang COMPILER=clang++-3.8 CXXSTD=03,11,14,1z 188 | addons: 189 | apt: 190 | packages: 191 | - clang-3.8 192 | - libstdc++-4.9-dev 193 | sources: 194 | - ubuntu-toolchain-r-test 195 | - llvm-toolchain-precise-3.8 196 | 197 | - os: linux 198 | compiler: clang++-3.9 199 | env: TOOLSET=clang COMPILER=clang++-3.9 CXXSTD=03,11,14,1z 200 | addons: 201 | apt: 202 | packages: 203 | - clang-3.9 204 | - libstdc++-4.9-dev 205 | sources: 206 | - ubuntu-toolchain-r-test 207 | - llvm-toolchain-precise-3.9 208 | 209 | - os: linux 210 | compiler: clang++-4.0 211 | env: TOOLSET=clang COMPILER=clang++-4.0 CXXSTD=03,11,14,1z 212 | addons: 213 | apt: 214 | packages: 215 | - clang-4.0 216 | sources: 217 | - ubuntu-toolchain-r-test 218 | - llvm-toolchain-trusty-4.0 219 | 220 | - os: linux 221 | compiler: clang++-5.0 222 | env: TOOLSET=clang COMPILER=clang++-5.0 CXXSTD=03,11,14,1z 223 | addons: 224 | apt: 225 | packages: 226 | - clang-5.0 227 | sources: 228 | - ubuntu-toolchain-r-test 229 | - llvm-toolchain-trusty-5.0 230 | 231 | - os: linux 232 | compiler: clang++-6.0 233 | env: TOOLSET=clang COMPILER=clang++-6.0 CXXSTD=03,11,14,17,2a 234 | addons: 235 | apt: 236 | packages: 237 | - clang-6.0 238 | sources: 239 | - ubuntu-toolchain-r-test 240 | - llvm-toolchain-trusty-6.0 241 | 242 | - os: linux 243 | compiler: clang++-7 244 | env: TOOLSET=clang COMPILER=clang++-7 CXXSTD=03,11,14,17,2a 245 | addons: 246 | apt: 247 | packages: 248 | - clang-7 249 | sources: 250 | - ubuntu-toolchain-r-test 251 | - llvm-toolchain-trusty-7 252 | 253 | - os: linux 254 | compiler: clang++-libc++ 255 | env: TOOLSET=clang COMPILER=clang++-libc++ CXXSTD=03,11,14,1z 256 | addons: 257 | apt: 258 | packages: 259 | - libc++-dev 260 | 261 | - os: osx 262 | compiler: clang++ 263 | env: TOOLSET=clang COMPILER=clang++ CXXSTD=03,11,14,1z 264 | 265 | install: 266 | - BOOST_BRANCH=develop && [ "$TRAVIS_BRANCH" == "master" ] && BOOST_BRANCH=master || true 267 | - cd .. 268 | - git clone -b $BOOST_BRANCH https://github.com/boostorg/boost.git boost 269 | - cd boost 270 | - git submodule update --init tools/build 271 | - git submodule update --init libs/config 272 | - git submodule update --init tools/boostdep 273 | - mkdir -p libs/circular_buffer 274 | - cp -r $TRAVIS_BUILD_DIR/* libs/circular_buffer 275 | - python tools/boostdep/depinst/depinst.py circular_buffer 276 | - ./bootstrap.sh 277 | - ./b2 headers 278 | 279 | script: 280 | - |- 281 | echo "using $TOOLSET : : $COMPILER ;" > ~/user-config.jam 282 | - ./b2 -j 3 libs/circular_buffer/test toolset=$TOOLSET cxxstd=$CXXSTD 283 | 284 | notifications: 285 | email: 286 | on_success: always 287 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright 2018 Glen Joseph Fernandes 2 | # (glenjofe@gmail.com) 3 | # 4 | # Distributed under the Boost Software License, Version 1.0. 5 | # (http://www.boost.org/LICENSE_1_0.txt) 6 | 7 | cmake_minimum_required(VERSION 3.5...3.20) 8 | 9 | project(boost_circular_buffer VERSION "${BOOST_SUPERPROJECT_VERSION}" LANGUAGES CXX) 10 | 11 | add_library(boost_circular_buffer INTERFACE) 12 | 13 | add_library(Boost::circular_buffer ALIAS boost_circular_buffer) 14 | 15 | target_include_directories(boost_circular_buffer INTERFACE include) 16 | 17 | target_link_libraries(boost_circular_buffer INTERFACE 18 | Boost::assert 19 | Boost::concept_check 20 | Boost::config 21 | Boost::core 22 | Boost::move 23 | Boost::static_assert 24 | Boost::throw_exception 25 | Boost::type_traits 26 | ) 27 | -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | # Copyright 2018 Peter Dimov 2 | # Distributed under the Boost Software License, Version 1.0. 3 | 4 | version: 1.0.{build}-{branch} 5 | 6 | shallow_clone: true 7 | 8 | branches: 9 | only: 10 | - master 11 | - develop 12 | 13 | environment: 14 | matrix: 15 | - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015 16 | TOOLSET: msvc-9.0 17 | - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015 18 | TOOLSET: msvc-10.0 19 | - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015 20 | TOOLSET: msvc-11.0 21 | - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015 22 | TOOLSET: msvc-12.0 23 | - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015 24 | TOOLSET: msvc-14.0 25 | - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017 26 | TOOLSET: msvc-14.1 27 | CXXSTD: 14,17 28 | 29 | install: 30 | - set BOOST_BRANCH=develop 31 | - if "%APPVEYOR_REPO_BRANCH%" == "master" set BOOST_BRANCH=master 32 | - cd .. 33 | - git clone -b %BOOST_BRANCH% https://github.com/boostorg/boost.git boost 34 | - cd boost 35 | - git submodule update --init tools/build 36 | - git submodule update --init libs/config 37 | - git submodule update --init tools/boostdep 38 | - xcopy /s /e /q %APPVEYOR_BUILD_FOLDER% libs\circular_buffer\ 39 | - python tools/boostdep/depinst/depinst.py circular_buffer 40 | - cmd /c bootstrap 41 | - b2 headers 42 | 43 | build: off 44 | 45 | test_script: 46 | - PATH=%ADDPATH%%PATH% 47 | - if not "%CXXSTD%" == "" set CXXSTD=cxxstd=%CXXSTD% 48 | - b2 -j 3 libs/circular_buffer/test toolset=%TOOLSET% %CXXSTD% 49 | -------------------------------------------------------------------------------- /build.jam: -------------------------------------------------------------------------------- 1 | # Copyright René Ferdinand Rivera Morell 2023-2024 2 | # Distributed under the Boost Software License, Version 1.0. 3 | # (See accompanying file LICENSE_1_0.txt or copy at 4 | # http://www.boost.org/LICENSE_1_0.txt) 5 | 6 | require-b2 5.2 ; 7 | 8 | constant boost_dependencies : 9 | /boost/assert//boost_assert 10 | /boost/concept_check//boost_concept_check 11 | /boost/config//boost_config 12 | /boost/core//boost_core 13 | /boost/move//boost_move 14 | /boost/static_assert//boost_static_assert 15 | /boost/throw_exception//boost_throw_exception 16 | /boost/type_traits//boost_type_traits ; 17 | 18 | project /boost/circular_buffer 19 | : common-requirements 20 | 21 | ; 22 | 23 | explicit 24 | [ alias boost_circular_buffer : : : 25 | : include $(boost_dependencies) ] 26 | [ alias all : boost_circular_buffer example test ] 27 | ; 28 | 29 | call-if : boost-library circular_buffer 30 | ; 31 | 32 | -------------------------------------------------------------------------------- /doc/circular_buffer.idx: -------------------------------------------------------------------------------- 1 | # circular_buffer.idx index script file 2 | # for Boost.circular_buffer Quickbook Doxygen documentation Auto-indexing forcircular_buffer library. 3 | 4 | # Copyright (c) 2011 Paul A. Bristow 5 | # Copyright (c) 2003 - 2008 Jan Gaspar 6 | 7 | # boost-no-inspect 8 | 9 | # Use, modification and distribution is subject to the Boost Software License, Version 1.0. 10 | # (See accompanying file LICENSE_1_0.txt 11 | # or copy at http://www.boost.org/LICENSE_1_0.txt) 12 | 13 | # All header files, recursing down to include sub-folders. 14 | !scan-path "boost/circular_buffer" ".*\.hpp" true 15 | 16 | # All example source files, assuming no sub-folders. 17 | !scan-path "libs/circular_buffer/example" ".*\.cpp" 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /doc/circular_buffer.qbk: -------------------------------------------------------------------------------- 1 | [article Boost.Circular Buffer 2 | [quickbook 1.6] 3 | [id circular_buffer] 4 | [copyright 2003-2013 Jan Gaspar] 5 | [license 6 | Distributed under the Boost Software License, Version 1.0. 7 | (See accompanying file LICENSE_1_0.txt or copy at 8 | [@http://www.boost.org/LICENSE_1_0.txt]) 9 | ] 10 | [authors [Gaspar, Jan]] 11 | [source-mode c++] 12 | ] 13 | 14 | [/ Links - by (most common) convention, prefixed with double underscore so not confused with other names.] 15 | [def __alert [$./images/alert.png]] [/ Examples of your own images (in doc/html/images/ .] 16 | [def __tip [$./images/tip.png]] 17 | [/ If you provide a file type like .png, you will probably find that the file is missing in the pdf version.] 18 | [/ This is because the default file type specified is .png in html, but .svg for pdf version.] 19 | 20 | [/ Some links to external sources.] 21 | [/ You often want to link more than once, so using a def ensures you always refer to the same location.] 22 | [def __boost [@http://www.boost.org/ Boost]] [/Boost.org] 23 | [def __boostroot [@boost: Boost root]] [/ Your boost root] 24 | [/Note the custom boost root url schema for linking to files within the Boost distribution.] 25 | [/Note It can't be used for images, nor for pdf, so not so useful.] 26 | [/def __boostlicense [@http://www.boost.org/LICENSE_1_0.txt Boost License]] 27 | [/ Or refer to your most recent version of Boost.] 28 | [def __boostlicense [@boost:/LICENSE_1_0.txt Boost License]] 29 | [def __boostbook [@http://www.boost.org/doc/html/boostbook.html BoostBook]] 30 | [def __boostbook_docs [@http://www.boost.org/doc/libs/1_53_0/doc/html/boostbook.html BoostBook documentation]] 31 | [def __quickbook [@http://www.boost.org/doc/tools/quickbook/index.html Quickbook]] 32 | [def __quickbook_syntax [@http://www.boost.org/doc/libs/1_53_0/doc/html/quickbook/ref.html Quickbook Syntax Compendium]] 33 | [def __docbook [@http://www.docbook.org/ DocBook]] 34 | [def __doxygen [@http://www.doxygen.org/ Doxygen]] 35 | [def __autoindex [@boost:/tools/auto_index/doc/html/index.html AutoIndex]] 36 | [def __pdf [@http://www.adobe.com/products/acrobat/adobepdf.html PDF]] 37 | [def __textpad [@http://www.textpad.com Textpad]] 38 | [def __emacs [@http://www.gnu.org/software/emacs/ GNU emacs]] 39 | [def __css [@http://en.wikipedia.org/wiki/Cascading_Style_Sheets Cascading Style Sheet]] 40 | [def __intro [link circular_buffer.intro Introduction]] [/Link to a Quickbook section (see below).] 41 | 42 | [def __docbook_params [@http://docbook.sourceforge.net/release/xsl/current/doc/ Docbook xsl:param format options]] 43 | 44 | [def __cb [classref boost::circular_buffer circular_buffer]] 45 | [def __cbso [classref boost::circular_buffer_space_optimized circular_buffer_space_optimized]] 46 | [def __min_capacity [memberref boost::circular_buffer_space_optimized::min_capacity() min_capacity]] 47 | [def __capacity_control [memberref boost::circular_buffer_space_optimized::capacity_control () capacity_control ]] 48 | 49 | [def __debug_support [link circular_buffer.implementation.debug_support debug support]] 50 | 51 | [include ../../../tools/auto_index/include/auto_index_helpers.qbk] 52 | [/ Must be first included file!] 53 | 54 | [note A printer-friendly PDF version of this manual is also available.] 55 | 56 | [section:intro Introduction] 57 | 58 | A Circular Buffer. 59 | 60 | [h2 Description] 61 | 62 | The term [@http://en.wikipedia.org/wiki/Circular_buffer circular buffer] 63 | (also called a ['ring] or ['cyclic buffer]) 64 | refers to an area in memory which is used to store incoming data. 65 | When the buffer is filled, 66 | new data is written starting at the beginning of the buffer and overwriting the old. 67 | 68 | [classref boost::circular_buffer] is a STL compliant container. 69 | 70 | It is a kind of sequence similar to [@https://www.boost.org/sgi/stl/List.html std::list] 71 | or [@https://www.boost.org/sgi/stl/Deque.html std::deque]. 72 | It supports random access iterators, constant time insert and erase operations 73 | at the beginning or the end of the buffer and interoperability with std algorithms. 74 | 75 | The __cb is especially designed to provide [*fixed capacity] storage. 76 | When its capacity is exhausted, newly inserted elements will cause elements 77 | to be overwritten, either at the beginning or end of the buffer 78 | (depending on what insert operation is used). 79 | 80 | The __cb only allocates memory when created, 81 | when the capacity is adjusted explicitly, 82 | or as necessary to accommodate resizing or assign operations. 83 | 84 | [$../../libs/circular_buffer/doc/images/circular_buffer.png] 85 | 86 | There is also a __cbso version available. 87 | 88 | [$../../libs/circular_buffer/doc/images/space_optimized.png] 89 | 90 | __cbso is an adaptation of the __cb 91 | which [*does not allocate memory all at once when created], 92 | instead it allocates memory as needed. 93 | 94 | The predictive memory allocation is similar to typical `std::vector` implementation. 95 | Memory is automatically freed as the size of the container decreases. 96 | 97 | The memory allocation process of the space-optimized circular buffer. 98 | The __min_capacity of the capacity controller represents 99 | the minimal guaranteed amount of allocated memory. 100 | The allocated memory will never drop under this value. 101 | The default value of the `min_capacity` is set to 0. 102 | The `min_capacity` can be set using the constructor parameter __capacity_control 103 | or the function `set_capacity`. 104 | 105 | The space-optimized version is, of course, a little slower. 106 | 107 | [endsect] [/section:intro Introduction] 108 | 109 | [section:example Circular_buffer example] 110 | 111 | Here is a simple example to introduce the class __cb. 112 | 113 | [import ../example/circular_buffer_example.cpp] 114 | 115 | [circular_buffer_example_1] 116 | 117 | This example shows construction, inserting elements, overwriting and popping. 118 | 119 | [circular_buffer_example_2] 120 | 121 | [/circular_buffer_example_output - there is no output for this example] 122 | 123 | You can see the full example code at [@boost:libs/circular_buffer/example/circular_buffer_example.cpp circular_buffer_example.cpp]. 124 | 125 | The full annotated description is in the C++ Reference section. 126 | 127 | [endsect] [/section:example circular_buffer example] 128 | 129 | [section:rationale Rationale] 130 | 131 | The basic motivation behind the __cb was to create a container which would [*work seamlessly with STL]. 132 | 133 | Additionally, the design of the __cb was guided by the following principles: 134 | 135 | * Maximum ['efficiency] for envisaged applications. 136 | * Suitable for ['general purpose use]. 137 | * The behaviour of the buffer as ['intuitive] as possible. 138 | * Suitable for ['specialization] by means of adaptors. (The __cbso is such an example of the adaptor.) 139 | * Easy to ['debug]. (See Debug Support for details.) 140 | 141 | In order to achieve maximum efficiency, the __cb and __cbso store their elements in a 142 | [*contiguous region of memory], which then enables: 143 | 144 | * Use of fixed memory and no implicit or unexpected memory allocation. 145 | * Fast constant-time insertion and removal of elements from the front and back. 146 | * Fast constant-time random access of elements. 147 | * Suitability for real-time and performance critical applications. 148 | 149 | Possible applications of the circular buffer include: 150 | 151 | * Storage of the ['most recently received samples], overwriting the oldest as new samples arrive. 152 | * As an underlying container for a ['bounded buffer] 153 | (see the Bounded Buffer example, code at [@boost:libs/circular_buffer/example/circular_buffer_bound_example.cpp circular_buffer_bound_example.cpp]). 154 | * A kind of ['cache] storing a specified number of last inserted elements. 155 | * Efficient fixed capacity ['FIFO (First In, First Out)], 156 | * Efficient fixed capacity ['LIFO (Last In, First Out)] queue which removes the oldest (inserted as first) elements when full. 157 | 158 | [endsect] [/section:rationale Rationale] 159 | 160 | [section:implementation Implementation ] 161 | 162 | The following paragraphs describe issues that had to be considered during the implementation of the circular_buffer: 163 | 164 | [h3 Thread-Safety] 165 | 166 | The thread-safety of the __cb is the same as the thread-safety of containers in most STL implementations. 167 | This means the __cb is not fully thread-safe. 168 | The thread-safety is guaranteed only in the sense that simultaneous accesses 169 | to distinct instances of the __cb are safe, 170 | and simultaneous read accesses to a shared __cb are safe. 171 | 172 | If multiple threads access a single __cb, 173 | and at least one of the threads may potentially write, 174 | then the user is responsible for ensuring mutual exclusion between the threads during the container accesses. 175 | The mutual exclusion between the threads can be achieved by wrapping 176 | operations of the underlying __cb with a lock acquisition and release. 177 | (See the Bounded Buffer example code at [@boost:libs/circular_buffer/example/circular_buffer_bound_example.cpp circular_buffer_bound_example.cpp]) 178 | 179 | [h3 Overwrite Operation] 180 | 181 | Overwrite operation occurs when an element is inserted into a full __cb - 182 | the old element is being overwritten by the new one. 183 | There was a discussion what exactly "overwriting of an element" means during the formal review. 184 | It may be either a destruction of the original element and 185 | a consequent inplace construction of a new element 186 | or it may be an assignment of a new element into an old one. 187 | The __cb implements assignment because it is more effective. 188 | 189 | From the point of business logic of a stored element, 190 | the destruction/construction operation and assignment usually mean the same. 191 | However, in very rare cases (if in any) they may differ. 192 | If there is a requirement for elements to be destructed/constructed instead of being assigned, 193 | consider implementing a wrapper of the element which would implement the assign operator, 194 | and store the wrappers instead. 195 | It is necessary to note that storing such wrappers has a drawback. 196 | The destruction/construction will be invoked on every assignment of the wrapper - 197 | not only when a wrapper is being overwritten (when the buffer is full) 198 | but also when the stored wrappers are being shifted 199 | (e.g. as a result of insertion into the middle of container). 200 | 201 | [h3 Writing to a Full Buffer] 202 | 203 | There are several options how to cope if a data source produces more data than can fit in the fixed-sized buffer: 204 | 205 | * Inform the data source to wait until there is room in the buffer (e.g. by throwing an overflow exception). 206 | * If the oldest data is the most important, ignore new data from the source until there is room in the buffer again. 207 | * If the latest data is the most important, write over the oldest data. 208 | * Let the producer to be responsible for checking the size of the buffer prior writing into it. 209 | 210 | It is apparent that the __cb implements the third option. 211 | But it may be less apparent it does not implement any other option - 212 | especially the first two. 213 | One can get an impression that the __cb should implement first three options 214 | and offer a mechanism of choosing among them. This impression is wrong. 215 | 216 | The __cb was designed and optimized to be circular 217 | (which means overwriting the oldest data when full). 218 | If such a controlling mechanism had been enabled, 219 | it would just complicate the matters 220 | and the usage of the __cb would be probably less straightforward. 221 | 222 | Moreover, the first two options (and the fourth option as well) 223 | do not require the buffer to be circular at all. 224 | If there is a need for the first or second option, consider implementing an adaptor of e.g. std::vector. 225 | In this case the __cb is not suitable for adapting, because, 226 | contrary to std::vector, it bears an overhead for its circular behaviour. 227 | 228 | [h3 Reading/Removing from an Empty Buffer] 229 | 230 | When reading or removing an element from an empty buffer, 231 | the buffer should be able to notify the data consumer 232 | (e.g. by throwing underflow exception) that there are no elements stored in it. 233 | The __cb does not implement such a behaviour for two reasons: 234 | 235 | * It would introduce a performance overhead. 236 | * No other std container implements it this way. 237 | 238 | It is considered to be a bug to read or remove an element 239 | (e.g. by calling [memberref boost::circular_buffer::front() front()] 240 | or [memberref boost::circular_buffer::pop_back() pop_back()]) 241 | from an empty std container and from an empty __cb as well. 242 | The data consumer has to test if the container is not empty before reading/removing from it by testing 243 | [memberref boost::circular_buffer::empty empty()]. 244 | However, when reading from the __cb, 245 | there is an option to rely on the [memberref boost::circular_buffer::at() at()] 246 | method which throws an exception when the index is out of range. 247 | 248 | [h3 Iterator Invalidation] 249 | 250 | An iterator is usually considered to be invalidated if an element, 251 | the iterator pointed to, had been removed or overwritten by an another element. 252 | This definition is enforced by the Debug Support and is documented for every method. 253 | However, some applications utilizing __cb may require less strict definition: 254 | an iterator is invalid only if it points to an uninitialized memory. 255 | 256 | Consider following example: 257 | 258 | [import ../example/circular_buffer_iter_example.cpp] 259 | 260 | [circular_buffer_iter_example_1] 261 | 262 | The iterator does not point to the original element any more 263 | (and is considered to be invalid from the "strict" point of view) 264 | but it still points to the same valid place in the memory. 265 | This "soft" definition of iterator invalidation is supported by the __cb 266 | but should be considered as an implementation detail rather than a full-fledged feature. 267 | The rules when the iterator is still valid can be inferred from the code in 268 | [@boost:libs/circular_buffer/test/soft_iterator_invalidation.cpp soft_iterator_invalidation.cpp]. 269 | 270 | 271 | [h3 Move emulation and rvalues] 272 | 273 | Since Boost 1.54.0 support for move semantics was implemented using 274 | the [@boost:libs/move/index.html Boost.Move] library. 275 | If rvalue references are available __cb will use them, but if not it uses a close, 276 | but imperfect emulation. On such compilers: 277 | 278 | * Non-copyable objects can be stored in the containers. 279 | They can be constructed in place using `emplace`, or if they support 280 | Boost.Move, moved into place. 281 | * The containers themselves are not movable. 282 | * Argument forwarding is not perfect. 283 | 284 | __cb will use rvalues and move emulations for value types only if move constructor and move assignment operator of the value type do not throw; 285 | or if the value type has no copy constructor. 286 | 287 | Some methods won't use move constructor for the value type at all, if the constructor throws. This is 288 | required for data consistency and avoidance of situations, when aftrer an exception __cb 289 | contains moved away objects along with the good ones. 290 | 291 | See documentation for [@boost:libs/type_traits/doc/html/boost_typetraits/reference/is_copy_constructible.html `is_copy_constructible`], [@boost:libs/type_traits/doc/html/boost_typetraits/reference/is_nothrow_move_assignable.html `is_nothrow_move_assignable`] and [@boost:libs/type_traits/doc/html/boost_typetraits/reference/is_nothrow_move_constructible.html `is_nothrow_move_constructible`] type triats. 292 | There you'll find information about how to make constructor of class noexcept and how to make a non-copyable 293 | class in C++03 and C++98. 294 | 295 | Performance of __cb will *greatly improve* if value type has noexcept move constructor and noexcept move assignment. 296 | 297 | [h3 Exceptions of move_if_noexcept(T&)] 298 | 299 | Reference documentation of the __cb contains notes like "Throws: See Exceptions of `move_if_noexcept(T&)`". 300 | That note means the following: `move_if_noexcept(T& value)` does not throws exceptions at all, but it returns 301 | `value` as rvalue reference only if class `T` have noexcept move constructor and noexcept move assignment operator; 302 | or if it has no copy constructor. Otherwise `move_if_noexcept(T& value)` returns `value` as const reference. 303 | 304 | This leads us to the following situation: 305 | 306 | * If `value` has a noexcept move constructor and noexcept move assignment operator, then no exceptions will be thrown at all. 307 | * If `value` has a throwing move constructor and some copy constructor, then method may throw exceptions of copy constructor. 308 | * If `value` has no copy constructor, then method may throw exceptions of move constructor. 309 | 310 | `move_if_noexcept(T&)` uses [@boost:libs/move/index.html Boost.Move], [@boost:libs/type_traits/doc/html/boost_typetraits/reference/is_copy_constructible.html `is_copy_constructible`], [@boost:libs/type_traits/doc/html/boost_typetraits/reference/is_nothrow_move_assignable.html `is_nothrow_move_assignable`] and [@boost:libs/type_traits/doc/html/boost_typetraits/reference/is_nothrow_move_constructible.html `is_nothrow_move_constructible`] type triats. 311 | 312 | 313 | [h3 Caveats] 314 | 315 | The __cb should not be used for storing pointers to dynamically allocated objects. 316 | When a circular buffer becomes full, further insertion will overwrite the stored pointers 317 | - resulting in a [*memory leak]. One recommend alternative is the use of smart pointers, for example 318 | [@http://www.boost.org/doc/libs/1_53_0/libs/smart_ptr/smart_ptr.htm Boost Smart pointers]. 319 | 320 | [@http://en.wikipedia.org/wiki/Std::auto_ptr std::auto_ptr] 321 | 322 | [caution Any container of `std::auto_ptr` is considered particularly hazardous.] 323 | 324 | [tip Never create a circular buffer of `std::auto_ptr`. 325 | Refer to Scott Meyers' excellent book Effective STL for a detailed discussion. 326 | (Meyers S., Effective STL: 50 Specific Ways to Improve Your Use of the Standard Template Library. 327 | Addison-Wesley, 2001.) 328 | ] 329 | 330 | While internals of a __cb are circular, [*iterators are not]. 331 | Iterators of a __cb are only valid for the range `\[begin(), end()\]`, 332 | so for example: iterators `(begin() - 1)` and `(end() + 1)` are both invalid. 333 | 334 | [h3 Debug Support] 335 | 336 | In order to help a programmer to avoid and find common bugs, 337 | the __cb can be enabled to provide a kind of debug support. 338 | 339 | When the debugging functionality is enabled, the __cb maintains a list of valid iterators. 340 | As soon as any element gets destroyed all iterators pointing to this element 341 | are removed from this list and explicitly invalidated (an invalidation flag is set). 342 | The debug support also consists of many assertions (`BOOST_ASSERT` macros) 343 | which ensure the __cb and its iterators are used in the correct manner at runtime. 344 | In case an invalid iterator is used, the assertion will report an error. 345 | The connection of explicit iterator invalidation and assertions 346 | makes a very robust debug technique which catches most of the errors. 347 | 348 | Moreover, the uninitialized memory allocated by __cb is filled with the value `0xcc` in the debug mode. 349 | When debugging the code, this can help the programmer to recognize the initialized memory from the uninitialized. 350 | For details refer the source code [@boost:boost/circular_buffer/debug.hpp circular_buffer/debug.hpp]. 351 | 352 | [caution Since the debugging code makes __cb and its iterators more interconnected, thread safety guarantees of __cb 353 | are different when debug support is enabled. In addition to the container itself, all iterators tracked by the container 354 | (including any copies thereof) must be protected from concurrent access. In particular, this includes copying, destroying or 355 | obtaining iterators from the container, even if for read-only access.] 356 | 357 | The debug support is disabled by default. To enable it, one has to define `BOOST_CB_ENABLE_DEBUG` macro with the value of 1 358 | while compiling the code using __cb. 359 | 360 | [h3 Compatibility with Interprocess library] 361 | 362 | The __cb is compatible with the [@boost:libs/interprocess/index.html Boost.Interprocess] 363 | [/ This should be in @boost:libs/interprocess/doc/index.html ] 364 | library used for interprocess communication. 365 | Considering that the circular_buffer's debug support relies on 'raw' pointers 366 | (which is not permitted by the Interprocess library) 367 | the code has to compiled with debug support disabled (i.e. with `BOOST_CB_ENABLE_DEBUG` macro not defined or defined to 0). 368 | Not doing that will cause the compilation to fail. 369 | 370 | [endsect] [/section:implementation Implementation ] 371 | 372 | [section:examples More Examples] 373 | 374 | [h3 Summing all the values in a circular buffer] 375 | 376 | [import ../example/circular_buffer_sum_example.cpp] 377 | 378 | [circular_buffer_sum_example_1] 379 | 380 | [/circular_buffer_example_output - there is no output for this example] 381 | 382 | The __cb has a capacity of three `int`. 383 | Therefore, the size of the buffer will never exceed three. 384 | The `std::accumulate` algorithm evaluates the sum of the stored elements. 385 | The semantics of the __cb can be inferred from the assertions. 386 | 387 | You can see the full example code at [@boost:libs/circular_buffer/example/circular_buffer_sum_example.cpp circular_buffer_sum_example.cpp]. 388 | 389 | [h3 Bounded Buffer Example] 390 | 391 | The bounded buffer is normally used in a producer-consumer mode: 392 | producer threads produce items and store them in the container 393 | and consumer threads remove these items and process them. 394 | The bounded buffer has to guarantee that 395 | 396 | * producers do not insert items into the container when the container is full, 397 | * consumers do not try to remove items when the container is empty, 398 | * each produced item is consumed by exactly one consumer. 399 | 400 | [import ../example/circular_buffer_bound_example.cpp] 401 | 402 | [circular_buffer_bound_example_1] 403 | 404 | [/ there is no output for this example] 405 | 406 | The bounded_buffer relies on [@boost:/doc/html/thread.html Boost.Thread] 407 | and [@boost:libs/bind/index.html Boost.Bind] libraries 408 | and [@boost:libs/utility/call_traits.htm Boost.call_traits utility]. 409 | 410 | The [memberref boost::circular_buffer::push_front() push_front()] 411 | method is called by the producer thread in order to insert a new item into the buffer. 412 | The method locks the mutex and waits until there is a space for the new item. 413 | (The mutex is unlocked during the waiting stage and has to be regained when the condition is met.) 414 | If there is a space in the buffer available, 415 | the execution continues and the method inserts the item at the end of the __cb. 416 | Then it increments the number of unread items and unlocks the mutex 417 | (in case an exception is thrown before the mutex is unlocked, 418 | the mutex is unlocked automatically by the destructor of the scoped_lock). 419 | At last the method notifies one of the consumer threads 420 | waiting for a new item to be inserted into the buffer. 421 | 422 | The [memberref boost::circular_buffer::pop_back() pop_back()] 423 | method is called by the consumer thread in order to read the next item from the buffer. 424 | The method locks the mutex and waits until there is an unread item in the buffer. 425 | If there is at least one unread item, 426 | the method decrements the number of unread items and reads the next item from the __cb. 427 | Then it unlocks the mutex and notifies one of the producer threads 428 | waiting for the buffer to free a space for the next item. 429 | 430 | The `bounded buffer::pop_back()` 431 | method [*does not remove the item] but the item is left 432 | in the circular_buffer which then [*replaces it with a new one] 433 | (inserted by a producer) when the circular_buffer is full. 434 | This technique is more effective than removing the item 435 | explicitly by calling the [memberref boost::circular_buffer::pop_back() circular_buffer::pop_back()] 436 | method of the __cb. 437 | 438 | This claim is based on the assumption that an assignment (replacement) 439 | of a new item into an old one is more effective than a destruction 440 | (removal) of an old item and a consequent inplace construction (insertion) of a new item. 441 | 442 | For comparison of bounded buffers based on different containers compile and 443 | run [@boost:libs/circular_buffer/test/bounded_buffer_comparison.cpp bounded_buffer_comparison.cpp]. 444 | The test should reveal the bounded buffer based on the __cb is most effective 445 | closely followed by the `std::deque` based bounded buffer. 446 | (In reality, the result may differ sometimes because the test 447 | is always affected by external factors such as immediate CPU load.) 448 | 449 | [import ../test/bounded_buffer_comparison.cpp] 450 | 451 | You can see the full test code at [@boost:libs/circular_buffer/test/bounded_buffer_comparison.cpp bounded_buffer_comparison.cpp], 452 | and an example of output is [bounded_buffer_comparison_output]. 453 | 454 | [endsect] [/section:examples More examples] 455 | 456 | [section:headers Header Files] 457 | 458 | The circular buffer library is defined in the file [@boost:boost/circular_buffer.hpp circular_buffer.hpp]. 459 | 460 | #include 461 | 462 | (There is also a forward declaration for the __cb 463 | in the header file [@boost:boost/circular_buffer_fwd.hpp circular_buffer_fwd.hpp]). 464 | 465 | The __cb is defined in the file [@boost:boost/circular_buffer/base.hpp base.hpp]. 466 | 467 | The __cbso is defined in the file [@boost:boost/circular_buffer/space_optimized.hpp space_optimized.hpp]. 468 | 469 | [endsect] [/section:headers Header Files] 470 | 471 | [section:concepts Modelled Concepts] 472 | 473 | [@https://www.boost.org/sgi/stl/RandomAccessContainer.html Random Access Container], 474 | [@https://www.boost.org/sgi/stl/FrontInsertionSequence.html Front Insertion Sequence], and 475 | [@https://www.boost.org/sgi/stl/BackInsertionSequence.html Back Insertion sequence] 476 | 477 | [endsect] [/section:concepts Modelled Concepts] 478 | 479 | [section:template_params Template Parameters] 480 | 481 | [table:templ Template parameter requirements 482 | [[parameter] [Requirements]] 483 | [[T] [The type of the elements stored in the circular_buffer. 484 | The T has to be [@boost:libs/utility/Assignable.html Assignable] 485 | and [@boost:libs/utility/CopyConstructible.html CopyConstructible]. 486 | Moreover T has to be [@https://www.boost.org/sgi/stl/DefaultConstructible.html DefaultConstructible] 487 | if supplied as a default parameter when invoking some of the circular_buffer's methods, 488 | e.g. `insert(iterator pos, const value_type& item = value_type())`. 489 | And [@https://www.boost.org/sgi/stl/EqualityComparable.html EqualityComparable] 490 | and/or [@boost:libs/utility/LessThanComparable.html LessThanComparable] 491 | if the circular_buffer will be compared with another container.]] 492 | [[Alloc] [The allocator type used for all internal memory management. 493 | The Alloc has to meet the allocator requirements imposed by STL.]] 494 | ] 495 | 496 | [endsect] [/section:template_params Template Parameters] 497 | 498 | [section:tickets Trac Tickets] 499 | 500 | Report and view bugs and features by adding a ticket at [@https://svn.boost.org/trac/boost Boost.Trac]. 501 | 502 | Existing open tickets for this library alone can be viewed 503 | [@https://svn.boost.org/trac/boost/query?status=assigned&status=new&status=reopened&component=circular_buffer&col=id&col=summary&col=status&col=owner&col=type&col=milestone&order=priority here]. 504 | Existing tickets for this library - including closed ones - can be viewed 505 | [@https://svn.boost.org/trac/boost/query?status=assigned&status=closed&status=new&status=reopened&component=circular_buffer&col=id&col=summary&col=status&col=owner&col=type&col=milestone&order=priority here]. 506 | 507 | Type: Bugs 508 | 509 | [@https://svn.boost.org/trac/boost/ticket/4100 #4100] Some boost classes have sizeof that depends on NDEBUG. 510 | 511 | [@https://svn.boost.org/trac/boost/ticket/5362 #5362] circular_buffer does not compile with BOOST_NO_EXCEPTIONS. 512 | 513 | [@https://svn.boost.org/trac/boost/ticket/6277 #6277] Checked iterators are not threadsafe. 514 | 515 | [@https://svn.boost.org/trac/boost/ticket/6747 #6747] Circular_Buffer / Bounded_Buffer inside Template class problem. 516 | 517 | [@https://svn.boost.org/trac/boost/ticket/7025 #7025] circular buffer reports warning: " type qualifiers ignored on function return type" while compile. 518 | 519 | [@https://svn.boost.org/trac/boost/ticket/7950 #7950] Eliminate W4-warnings under VS2005. 520 | 521 | [@https://svn.boost.org/trac/boost/ticket/8012 #8012] Inconsistency in `linearize()`. 522 | 523 | [@https://svn.boost.org/trac/boost/ticket/8438 #8438] `vector` & __cb storage misbehave when using compiler optimizations. 524 | 525 | Type: Feature Requests 526 | 527 | [@https://svn.boost.org/trac/boost/ticket/5511 #5511] Documentation needs some improvement. 528 | 529 | [@https://svn.boost.org/trac/boost/ticket/7888 #7888] circular_buffer should support move semantics. 530 | 531 | Type: Patches 532 | 533 | [@https://svn.boost.org/trac/boost/ticket/8032 #8032] Warning fixes in circular_buffer. 534 | 535 | [endsect] [/section:tickets Trac Tickets] 536 | 537 | [section:release Release Notes] 538 | 539 | [h4 Boost 1.56] 540 | 541 | * C++11 allocator model support implemented by Glen Fernandes. 542 | 543 | [h4 Boost 1.55] 544 | 545 | * Documentation refactored by Paul A. Bristow using Quickbook, Doxygen and Autoindexing. 546 | * Rvalue references emulation added by Antony Polukhin using Boost.Move. 547 | 548 | [h4 Boost 1.42] 549 | 550 | * Added methods erase_begin(size_type) and erase_end(size_type) with constant complexity for such types of stored elements which do not need an explicit destruction e.g. int or double. 551 | * Similarly changed implementation of the clear() method and the destructor so their complexity is now constant for such types of stored elements which do not require an explicit destruction (the complexity for other types remains linear). 552 | 553 | [h4 Boost 1.37] 554 | 555 | *Added new methods is_linearized() and rotate(const_iterator). 556 | * Fixed bugs: 557 | [@https://svn.boost.org/trac/boost/ticket/1987 #1987] Patch to make circular_buffer.hpp #includes absolute. 558 | [@https://svn.boost.org/trac/boost/ticket/1852 #1852] Copy constructor does not copy capacity. 559 | 560 | [h4 Boost 1.36] 561 | 562 | * Changed behaviour of the circular_buffer(const allocator_type&) constructor. 563 | Since this version the constructor does not allocate any memory and both capacity and size are set to zero. 564 | * Fixed bug: 565 | [@https://svn.boost.org/trac/boost/ticket/191 #1919] Default constructed circular buffer throws std::bad_alloc. 566 | 567 | [h4 Boost 1.35] 568 | * Initial release. 569 | 570 | [endsect] [/section:release Release Notes] 571 | 572 | [section:acknowledgements Acknowledgements] 573 | 574 | Thomas Witt in 2002 produced a prototype called cyclic buffer. 575 | 576 | The circular_buffer has a short history. Its first version was a std::deque adaptor. 577 | This container was not very effective because of many reallocations when inserting/removing an element. 578 | Thomas Wenish did a review of this version and 579 | motivated me to create a circular buffer which allocates memory at once when created. 580 | 581 | The second version adapted `std::vector` but it has been abandoned soon 582 | because of limited control over iterator invalidation. 583 | The current version is a full-fledged STL compliant container. 584 | 585 | Pavel Vozenilek did a thorough review of this version and came with many good ideas and improvements. 586 | 587 | The idea of the space optimized circular buffer has been introduced by Pavel Vozenilek. 588 | 589 | Also, I would like to thank Howard Hinnant, Nigel Stewart and everyone 590 | who participated at the formal review for valuable comments and ideas. 591 | 592 | Paul A. Bristow refactored the documentation in 2013 to use the full power of Quickbook, Doxygen and Autoindexing. 593 | 594 | [endsect] [/section:acknowledgements Acknowledgements] 595 | 596 | 597 | [section:version_id Documentation Version Info] 598 | 599 | Last edit to Quickbook file __FILENAME__ was at __TIME__ on __DATE__. 600 | 601 | [tip This should appear on the pdf version 602 | (but may be redundant on a html version where the last edit date is on the first (home) page).] 603 | 604 | [warning Home page "Last revised" is GMT, not local time. Last edit date is local time.] 605 | [/See also Adobe Reader pdf File Properties for creation date, and PDF producer, version and page count.] 606 | 607 | [endsect] [/section:version_id Version Info] 608 | 609 | [xinclude autodoc.xml] [/ Using Doxygen reference documentation.] 610 | [/ The position of this in the Quickbook determines the location of the Doxygen references section.] 611 | [/ Index(es) should be invoked in the main module, not within a section.] 612 | 613 | ''' 614 | 615 | ''' 616 | 617 | [/ circular_buffer.qbk 618 | Copyright 2013 Paul A. Bristow. 619 | Copyright 2003-2008 Jan Gaspar. 620 | 621 | Distributed under the Boost Software License, Version 1.0. 622 | (See accompanying file LICENSE_1_0.txt or copy at 623 | http://www.boost.org/LICENSE_1_0.txt). 624 | ] 625 | 626 | 627 | -------------------------------------------------------------------------------- /doc/images/circular_buffer.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/boostorg/circular_buffer/0320ba365cf959d3f9c982eb05b78452571b35e5/doc/images/circular_buffer.png -------------------------------------------------------------------------------- /doc/images/space_optimized.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/boostorg/circular_buffer/0320ba365cf959d3f9c982eb05b78452571b35e5/doc/images/space_optimized.png -------------------------------------------------------------------------------- /doc/jamfile.v2: -------------------------------------------------------------------------------- 1 | 2 | # Boost.circular_buffer library documentation Jamfile.v2 3 | # 4 | # Copyright Paul A. Bristow 2013. 5 | # Copyright Jan Gaspar 2003-2008. 6 | 7 | # Use, modification and distribution is subject to 8 | # the Boost Software License, Version 1.0. 9 | # (See accompanying file LICENSE_1_0.txt 10 | # or copy at http://www.boost.org/LICENSE_1_0.txt) 11 | 12 | path-constant nav_images : html/images/ ; # png and svg images for home, next, note, tip... 13 | path-constant images_location : html/images ; # location of my SVG and PNG images referenced by Quickbook. 14 | path-constant pdf_images_location : .. ; # location of SVG and PNG images referenced by pdf. 15 | path-constant here : . ; # location of /doc folder. 16 | 17 | # echo "nav_images = " $(nav_images) ; # "nav_images = I:\boost-trunk\libs\circular_buffer\doc\html\images 18 | # echo "images_location = " $(images_location) ; # images_location = I:\boost-trunk\libs\circular_buffer\doc\html\images 19 | # echo "pdf_images_location = " $(pdf_images_location) # 20 | import modules ; 21 | using auto-index ; 22 | using doxygen ; # Required if you want to use Doxygen. 23 | using quickbook ; 24 | 25 | 26 | doxygen autodoc 27 | : 28 | # List all the files individually (RECURSIVE=NO ). 29 | [ glob ../include/boost/circular_buffer.hpp ] 30 | [ glob ../include/boost/circular_buffer/base.hpp ] 31 | [ glob ../include/boost/circular_buffer/space_optimized.hpp ] 32 | 33 | : 34 | # Pass some setting parameters to Doxygen. 35 | WARNINGS=YES # Default NO, but useful to see warnings, especially in a logfile. 36 | # It is also wise to to set a warnings logfile like this: 37 | WARN_LOGFILE=AutoDoxywarnings.log # This may not be empty (usually not a good sign!), depending on options chosen. 38 | # Much better to send message to a logfile than the default stderr. 39 | # and make sure that there are no Doxygen errors or significant warnings in the log file. 40 | RECURSIVE=NO # Search recursively down .hpp and .cpp subdirectories. 41 | EXTRACT_ALL=NO 42 | EXTRACT_PRIVATE=NO # NO means do not extract info about private member functions and data. 43 | HIDE_UNDOC_MEMBERS=YES # Only show members that have some documentation like \param, \return ... 44 | MACRO_EXPANSION=YES # YES will expand all macro names in the source code (default = NO). 45 | EXPAND_ONLY_PREDEF=YES # If the EXPAND_ONLY_PREDEF and MACRO_EXPANSION tags are both set to YES 46 | # then the macro expansion is limited to the macros specified with the PREDEFINED and EXPAND_AS_DEFINED tags. 47 | # If EXPAND_ONLY_PREDEF tag can be used to specify a list of macro names that should be expanded (as defined). 48 | # The PREDEFINED tag can be used to specify one or more macro names that are defined 49 | # before the preprocessor is started (similar to the -D option of gcc). 50 | # The argument of the tag is a list of macros of the form: 51 | # name or name=definition (no spaces). 52 | # If the definition and the "=" are omitted, "=1" is assumed. 53 | # To prevent a macro definition from being undefined via #undef or 54 | # recursively expanded use the := operator instead of the = operator. 55 | # See http://www.stack.nl/~dimitri/doxygen/config.html#cfg_predefined. 56 | # static char *malloc BOOST_PREVENT_MACRO_SUBSTITUTION(const size_type bytes); 57 | # will not produce a helpful Doxygen output, so 58 | # replace some with more helpful text, or none, for example: 59 | "PREDEFINED= \\ 60 | \"BOOST_PREVENT_MACRO_SUBSTITUTION\" \\ 61 | \"BOOST_STATIC_CONSTANT(T,V)=static x const y\" \\ 62 | \"BOOST_UNITS_AUTO_STATIC_CONSTANT(a,b)=static const auto a = b\" \\ 63 | \"BOOST_DEDUCED_TYPENAME=typename\" \\ 64 | \"BOOST_CONSTEXPR=constexpr\" \\ 65 | \"BOOST_CONTAINER_NOEXCEPT=noexcept\" \\ 66 | \"BOOST_CONTAINER_NOEXCEPT_IF(T)=noexcept(T)\" \\ 67 | \"BOOST_UNITS_TYPEOF(a)=typeof(a)\" \\ 68 | \"BOOST_UNITS_HAS_TYPEOF=1\" \\ 69 | \"BOOST_MPL_ASSERT(expr)=\" \\ 70 | \"BOOST_ASSERT(expr)=\" \\ 71 | \"BOOST_RV_REF(T)=T &&\" \\ 72 | \"ASSERT(x)=assert(x)\" \\ 73 | \"__cplusplus \"" 74 | # BOOST_PREVENT_MACRO_SUBSTITUTION, will not be replaced by , 75 | # BOOST_STATIC_CONSTANT will be replaced by "static x const y", 76 | # BOOST_DEDUCED_TYPENAME will be replaced by "typename", 77 | # BOOST_CONSTEXPR will be replaced by "constexpr". 78 | EXCLUDE_SYMBOLS=*_throws 79 | # IMAGE_PATH="../images" # for circular_buffer.png 80 | # See autodoxywarnings.log to check this is correct. 81 | 82 | # The syntax hoops to jump through are 'interesting' for more than one PREDEFINED, 83 | # and to permit spaces within definitions (use double quotes). 84 | # Don't forget that every double quote " needs a preceding \trip character! 85 | # and that each trailing continuation \ needs a preceding \trip character too! 86 | # And finally that if more than one item is included (as here) the whole is 87 | # enclosed in "PREDEFINED=... ", but without a leading \. Go figure... 88 | 89 | # A grep for PREDEFINED= in jamfiles will reveal even more complex examples. 90 | # Boost Libraries with useful examples are: Accumulators, Interprocess, MPI, Random, Units, Expressive. 91 | 92 | # Optionally, you can provide a Reference section name specific for your library, for example: 93 | "boost.doxygen.reftitle=Boost.Circular_buffer C++ Reference" 94 | ; 95 | 96 | xml circular_buffer : circular_buffer.qbk ; 97 | 98 | using boostbook ; 99 | 100 | boostbook standalone 101 | : 102 | circular_buffer 103 | : 104 | 105 | # General settings 106 | # ================= 107 | html:boost.root=../../../.. 108 | html:img.src.path=../../../../doc/html/ 109 | docbook:boost.root=boost: 110 | 111 | # Options for html and pdf 112 | # ======================== 113 | # No indent on body text: 114 | body.start.indent=0pt 115 | # Margin size: 116 | page.margin.inner=0.5in 117 | # Margin size: 118 | page.margin.outer=0.5in 119 | # Yes, we want graphics for admonishments: 120 | admon.graphics=1 121 | 122 | # HTML options: 123 | # ============= 124 | # Use graphics icons not text for navigation: 125 | navig.graphics=1 126 | # How far down we chunk nested sections, basically all of them: 127 | chunk.section.depth=2 128 | # Don't put the first section on the same page as the TOC itself: 129 | chunk.first.sections=1 130 | # How far down sections get TOC's 131 | toc.section.depth=4 132 | # Max depth in each TOC: 133 | toc.max.depth=2 134 | # How far down we go with TOC's 135 | generate.section.toc.level=10 136 | # Horizontal ? spacing in table cells. 137 | html:html.cellspacing=3 # pixels 138 | # Vertical spacing in table cells. 139 | html:html.cellpadding=5 # pixels 140 | # Not sure if these are right way round? 141 | 142 | on # Turns on index (or off). 143 | # Turns on (or off) index-verbose for diagnostic info (using /bin auto-index-verbose folders). 144 | on 145 | 146 | pdf:off # on (or off) to use internally generated indexes. 147 | 148 | html:index.on.type=1 # = 1 For the native stylesheets to generate multiple different indexes. 149 | 150 | circular_buffer.idx # Specifies the name of the script to load for circular_buffer. 151 | ../../.. # Will get you back up to /circular_buffer, so !scan-path "boost/circular_buffer/" is where *.hpp will be, 152 | # and /libs/circular_buffer for other files. 153 | # Without this would need !scan-path "../../../boost/circular_buffer" 154 | 155 | # Used by Quickbook to invoke indexing. 156 | # Required by boost-trunk/doc/ see jamfile.v2 to use auto-index. 157 | # Choose indexing method for html: 158 | html:on 159 | docbook:on 160 | 161 | # PDF Options: 162 | # ============ 163 | # TOC Generation: this is needed for FOP-0.9 and later: 164 | pdf:fop1.extensions=0 165 | # Or enable this if you're using XEP: 166 | pdf:xep.extensions=1 167 | # TOC generation: this is needed for FOP 0.2, but must not be set to zero for FOP-0.9! 168 | pdf:fop.extensions=0 169 | # No indent on body text: 170 | body.start.indent=0pt 171 | # Margin size: 172 | page.margin.inner=0.5in 173 | # Margin size: 174 | page.margin.outer=0.5in 175 | 176 | # Yes, we want graphics for admonishments: 177 | admon.graphics=1 178 | 179 | # Set these one for PDF generation *only*: 180 | # default png graphics are awful in PDF form, 181 | # better use SVG instead: 182 | pdf:admon.graphics.extension=".svg" 183 | #pdf:admon.graphics.extension=".png" # Only png images are available. 184 | # Don't need this, default path works OK: 185 | #pdf:admon.graphics.path=$(nav_images)/ # next, prev, note, tip ... for pdf. 186 | pdf:use.role.for.mediaobject=1 187 | pdf:preferred.mediaobject.role=print 188 | pdf:img.src.path=$(pdf_images_location)/ # graphics (diagrams) for pdf. 189 | pdf:draft.mode="no" 190 | pdf:boost.url.prefix=../../../.. 191 | 192 | autodoc # 193 | png_install 194 | ; 195 | 196 | # Install (copy) the 'master' copies of all icon images (both PNG and SVG) 197 | # and the Boost logo from your current Boost-root 198 | # to the local /doc/html/images folder so that html is complete and standalone. 199 | install png_install : [ glob $(here)/*.png ] : $(here)/../../../doc/html/images ; 200 | 201 | # install pdf-install : standalone : PDF . ; 202 | # Effectively copies the file from \bin folder to the \doc folder, 203 | # but will not work as expected if doxygen and/or autoindex is used 204 | # because a modified pdf file is created, so this command 205 | # will rename the file to the expected filename, here circular_buffer.pdf. 206 | 207 | install pdfinstall : standalone : PDF . circular_buffer.pdf ; 208 | 209 | ############################################################################### 210 | alias boostdoc 211 | : standalone/docbook 212 | : 213 | : 214 | : ; 215 | explicit boostdoc ; 216 | alias boostrelease ; 217 | explicit boostrelease ; 218 | -------------------------------------------------------------------------------- /example/bounded_buffer_comparison.cpp: -------------------------------------------------------------------------------- 1 | // Comparison of bounded buffers based on different containers. 2 | 3 | // Copyright (c) 2003-2008 Jan Gaspar 4 | // Copyright 2013 Paul A. Bristow. Added some Quickbook snippet markers. 5 | 6 | // Use, modification, and distribution is subject to the Boost Software 7 | // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at 8 | // http://www.boost.org/LICENSE_1_0.txt) 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | const unsigned long QUEUE_SIZE = 1000L; 23 | const unsigned long TOTAL_ELEMENTS = QUEUE_SIZE * 1000L; 24 | 25 | template 26 | class bounded_buffer { 27 | public: 28 | 29 | typedef boost::circular_buffer container_type; 30 | typedef typename container_type::size_type size_type; 31 | typedef typename container_type::value_type value_type; 32 | typedef typename boost::call_traits::param_type param_type; 33 | 34 | explicit bounded_buffer(size_type capacity) : m_unread(0), m_container(capacity) {} 35 | 36 | void push_front(param_type item) { 37 | boost::mutex::scoped_lock lock(m_mutex); 38 | m_not_full.wait(lock, boost::bind(&bounded_buffer::is_not_full, this)); 39 | m_container.push_front(item); 40 | ++m_unread; 41 | lock.unlock(); 42 | m_not_empty.notify_one(); 43 | } 44 | 45 | void pop_back(value_type* pItem) { 46 | boost::mutex::scoped_lock lock(m_mutex); 47 | m_not_empty.wait(lock, boost::bind(&bounded_buffer::is_not_empty, this)); 48 | *pItem = m_container[--m_unread]; 49 | lock.unlock(); 50 | m_not_full.notify_one(); 51 | } 52 | 53 | private: 54 | bounded_buffer(const bounded_buffer&); // Disabled copy constructor 55 | bounded_buffer& operator = (const bounded_buffer&); // Disabled assign operator 56 | 57 | bool is_not_empty() const { return m_unread > 0; } 58 | bool is_not_full() const { return m_unread < m_container.capacity(); } 59 | 60 | size_type m_unread; 61 | container_type m_container; 62 | boost::mutex m_mutex; 63 | boost::condition m_not_empty; 64 | boost::condition m_not_full; 65 | }; 66 | 67 | template 68 | class bounded_buffer_space_optimized { 69 | public: 70 | 71 | typedef boost::circular_buffer_space_optimized container_type; 72 | typedef typename container_type::size_type size_type; 73 | typedef typename container_type::value_type value_type; 74 | typedef typename boost::call_traits::param_type param_type; 75 | 76 | explicit bounded_buffer_space_optimized(size_type capacity) : m_container(capacity) {} 77 | 78 | void push_front(param_type item) { 79 | boost::mutex::scoped_lock lock(m_mutex); 80 | m_not_full.wait(lock, boost::bind(&bounded_buffer_space_optimized::is_not_full, this)); 81 | m_container.push_front(item); 82 | lock.unlock(); 83 | m_not_empty.notify_one(); 84 | } 85 | 86 | void pop_back(value_type* pItem) { 87 | boost::mutex::scoped_lock lock(m_mutex); 88 | m_not_empty.wait(lock, boost::bind(&bounded_buffer_space_optimized::is_not_empty, this)); 89 | *pItem = m_container.back(); 90 | m_container.pop_back(); 91 | lock.unlock(); 92 | m_not_full.notify_one(); 93 | } 94 | 95 | private: 96 | 97 | bounded_buffer_space_optimized(const bounded_buffer_space_optimized&); // Disabled copy constructor 98 | bounded_buffer_space_optimized& operator = (const bounded_buffer_space_optimized&); // Disabled assign operator 99 | 100 | bool is_not_empty() const { return m_container.size() > 0; } 101 | bool is_not_full() const { return m_container.size() < m_container.capacity(); } 102 | 103 | container_type m_container; 104 | boost::mutex m_mutex; 105 | boost::condition m_not_empty; 106 | boost::condition m_not_full; 107 | }; 108 | 109 | template 110 | class bounded_buffer_deque_based { 111 | public: 112 | 113 | typedef std::deque container_type; 114 | typedef typename container_type::size_type size_type; 115 | typedef typename container_type::value_type value_type; 116 | typedef typename boost::call_traits::param_type param_type; 117 | 118 | explicit bounded_buffer_deque_based(size_type capacity) : m_capacity(capacity) {} 119 | 120 | void push_front(param_type item) { 121 | boost::mutex::scoped_lock lock(m_mutex); 122 | m_not_full.wait(lock, boost::bind(&bounded_buffer_deque_based::is_not_full, this)); 123 | m_container.push_front(item); 124 | lock.unlock(); 125 | m_not_empty.notify_one(); 126 | } 127 | 128 | void pop_back(value_type* pItem) { 129 | boost::mutex::scoped_lock lock(m_mutex); 130 | m_not_empty.wait(lock, boost::bind(&bounded_buffer_deque_based::is_not_empty, this)); 131 | *pItem = m_container.back(); 132 | m_container.pop_back(); 133 | lock.unlock(); 134 | m_not_full.notify_one(); 135 | } 136 | 137 | private: 138 | 139 | bounded_buffer_deque_based(const bounded_buffer_deque_based&); // Disabled copy constructor 140 | bounded_buffer_deque_based& operator = (const bounded_buffer_deque_based&); // Disabled assign operator 141 | 142 | bool is_not_empty() const { return m_container.size() > 0; } 143 | bool is_not_full() const { return m_container.size() < m_capacity; } 144 | 145 | const size_type m_capacity; 146 | container_type m_container; 147 | boost::mutex m_mutex; 148 | boost::condition m_not_empty; 149 | boost::condition m_not_full; 150 | }; 151 | 152 | template 153 | class bounded_buffer_list_based { 154 | public: 155 | 156 | typedef std::list container_type; 157 | typedef typename container_type::size_type size_type; 158 | typedef typename container_type::value_type value_type; 159 | typedef typename boost::call_traits::param_type param_type; 160 | 161 | explicit bounded_buffer_list_based(size_type capacity) : m_capacity(capacity) {} 162 | 163 | void push_front(param_type item) { 164 | boost::mutex::scoped_lock lock(m_mutex); 165 | m_not_full.wait(lock, boost::bind(&bounded_buffer_list_based::is_not_full, this)); 166 | m_container.push_front(item); 167 | lock.unlock(); 168 | m_not_empty.notify_one(); 169 | } 170 | 171 | void pop_back(value_type* pItem) { 172 | boost::mutex::scoped_lock lock(m_mutex); 173 | m_not_empty.wait(lock, boost::bind(&bounded_buffer_list_based::is_not_empty, this)); 174 | *pItem = m_container.back(); 175 | m_container.pop_back(); 176 | lock.unlock(); 177 | m_not_full.notify_one(); 178 | } 179 | 180 | private: 181 | 182 | bounded_buffer_list_based(const bounded_buffer_list_based&); // Disabled copy constructor 183 | bounded_buffer_list_based& operator = (const bounded_buffer_list_based&); // Disabled assign operator 184 | 185 | bool is_not_empty() const { return m_container.size() > 0; } 186 | bool is_not_full() const { return m_container.size() < m_capacity; } 187 | 188 | const size_type m_capacity; 189 | container_type m_container; 190 | boost::mutex m_mutex; 191 | boost::condition m_not_empty; 192 | boost::condition m_not_full; 193 | }; 194 | 195 | template 196 | class Consumer { 197 | 198 | typedef typename Buffer::value_type value_type; 199 | Buffer* m_container; 200 | value_type m_item; 201 | 202 | public: 203 | Consumer(Buffer* buffer) : m_container(buffer) {} 204 | 205 | void operator()() { 206 | for (unsigned long i = 0L; i < TOTAL_ELEMENTS; ++i) { 207 | m_container->pop_back(&m_item); 208 | } 209 | } 210 | }; 211 | 212 | template 213 | class Producer { 214 | 215 | typedef typename Buffer::value_type value_type; 216 | Buffer* m_container; 217 | 218 | public: 219 | Producer(Buffer* buffer) : m_container(buffer) {} 220 | 221 | void operator()() { 222 | for (unsigned long i = 0L; i < TOTAL_ELEMENTS; ++i) { 223 | m_container->push_front(value_type()); 224 | } 225 | } 226 | }; 227 | 228 | template 229 | void fifo_test(Buffer* buffer) { 230 | 231 | // Start of measurement 232 | boost::timer::auto_cpu_timer progress; 233 | 234 | // Initialize the buffer with some values before launching producer and consumer threads. 235 | for (unsigned long i = QUEUE_SIZE / 2L; i > 0; --i) { 236 | #if BOOST_WORKAROUND(BOOST_BORLANDC, BOOST_TESTED_AT(0x581)) 237 | buffer->push_front(Buffer::value_type()); 238 | #else 239 | buffer->push_front(BOOST_DEDUCED_TYPENAME Buffer::value_type()); 240 | #endif 241 | } 242 | 243 | Consumer consumer(buffer); 244 | Producer producer(buffer); 245 | 246 | // Start the threads. 247 | boost::thread consume(consumer); 248 | boost::thread produce(producer); 249 | 250 | // Wait for completion. 251 | consume.join(); 252 | produce.join(); 253 | 254 | // End of measurement 255 | } 256 | 257 | int main(int /*argc*/, char* /*argv*/[]) { 258 | 259 | bounded_buffer bb_int(QUEUE_SIZE); 260 | std::cout << "bounded_buffer "; 261 | fifo_test(&bb_int); 262 | 263 | bounded_buffer_space_optimized bb_space_optimized_int(QUEUE_SIZE); 264 | std::cout << "bounded_buffer_space_optimized "; 265 | fifo_test(&bb_space_optimized_int); 266 | 267 | bounded_buffer_deque_based bb_deque_based_int(QUEUE_SIZE); 268 | std::cout << "bounded_buffer_deque_based "; 269 | fifo_test(&bb_deque_based_int); 270 | 271 | bounded_buffer_list_based bb_list_based_int(QUEUE_SIZE); 272 | std::cout << "bounded_buffer_list_based "; 273 | fifo_test(&bb_list_based_int); 274 | 275 | bounded_buffer bb_string(QUEUE_SIZE); 276 | std::cout << "bounded_buffer "; 277 | fifo_test(&bb_string); 278 | 279 | bounded_buffer_space_optimized bb_space_optimized_string(QUEUE_SIZE); 280 | std::cout << "bounded_buffer_space_optimized "; 281 | fifo_test(&bb_space_optimized_string); 282 | 283 | bounded_buffer_deque_based bb_deque_based_string(QUEUE_SIZE); 284 | std::cout << "bounded_buffer_deque_based "; 285 | fifo_test(&bb_deque_based_string); 286 | 287 | bounded_buffer_list_based bb_list_based_string(QUEUE_SIZE); 288 | std::cout << "bounded_buffer_list_based "; 289 | fifo_test(&bb_list_based_string); 290 | 291 | return 0; 292 | } 293 | /* 294 | 295 | //[bounded_buffer_comparison_output 296 | 297 | Description: Autorun "J:\Cpp\Misc\Debug\bounded_buffer_comparison.exe" 298 | bounded_buffer 5.15 s 299 | 300 | bounded_buffer_space_optimized 5.71 s 301 | 302 | bounded_buffer_deque_based 15.57 s 303 | 304 | bounded_buffer_list_based 17.33 s 305 | 306 | bounded_buffer 24.49 s 307 | 308 | bounded_buffer_space_optimized 28.33 s 309 | 310 | bounded_buffer_deque_based 29.45 s 311 | 312 | bounded_buffer_list_based 31.29 s 313 | 314 | //] //[bounded_buffer_comparison_output] 315 | 316 | */ 317 | -------------------------------------------------------------------------------- /example/circular_buffer_bound_example.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2003-2008 Jan Gaspar. 2 | // Copyright 2013 Paul A. Bristow. Added some Quickbook snippet markers. 3 | 4 | // Distributed under the Boost Software License, Version 1.0. 5 | // (See the accompanying file LICENSE_1_0.txt 6 | // or a copy at .) 7 | 8 | //[circular_buffer_bound_example_1 9 | /*` 10 | This example shows how the `circular_buffer` can be utilized 11 | as an underlying container of the bounded buffer. 12 | */ 13 | 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | 21 | #include // for auto_cpu_timer 22 | #include 23 | 24 | template 25 | class bounded_buffer 26 | { 27 | public: 28 | 29 | typedef boost::circular_buffer container_type; 30 | typedef typename container_type::size_type size_type; 31 | typedef typename container_type::value_type value_type; 32 | typedef typename boost::call_traits::param_type param_type; 33 | 34 | explicit bounded_buffer(size_type capacity) : m_unread(0), m_container(capacity) {} 35 | 36 | void push_front(typename boost::call_traits::param_type item) 37 | { // `param_type` represents the "best" way to pass a parameter of type `value_type` to a method. 38 | 39 | boost::mutex::scoped_lock lock(m_mutex); 40 | m_not_full.wait(lock, boost::bind(&bounded_buffer::is_not_full, this)); 41 | m_container.push_front(item); 42 | ++m_unread; 43 | lock.unlock(); 44 | m_not_empty.notify_one(); 45 | } 46 | 47 | void pop_back(value_type* pItem) { 48 | boost::mutex::scoped_lock lock(m_mutex); 49 | m_not_empty.wait(lock, boost::bind(&bounded_buffer::is_not_empty, this)); 50 | *pItem = m_container[--m_unread]; 51 | lock.unlock(); 52 | m_not_full.notify_one(); 53 | } 54 | 55 | private: 56 | bounded_buffer(const bounded_buffer&); // Disabled copy constructor. 57 | bounded_buffer& operator = (const bounded_buffer&); // Disabled assign operator. 58 | 59 | bool is_not_empty() const { return m_unread > 0; } 60 | bool is_not_full() const { return m_unread < m_container.capacity(); } 61 | 62 | size_type m_unread; 63 | container_type m_container; 64 | boost::mutex m_mutex; 65 | boost::condition m_not_empty; 66 | boost::condition m_not_full; 67 | }; // 68 | 69 | //] [/circular_buffer_bound_example_1] 70 | 71 | const unsigned long queue_size = 1000L; 72 | const unsigned long total_elements = queue_size * 1000L; 73 | 74 | //[circular_buffer_bound_example_2] 75 | /*`To demonstrate, create two classes to exercise the buffer. 76 | 77 | The producer class fills the buffer with elements. 78 | 79 | The consumer class consumes the buffer contents. 80 | 81 | */ 82 | 83 | template 84 | class Producer 85 | { 86 | 87 | typedef typename Buffer::value_type value_type; 88 | Buffer* m_container; 89 | 90 | public: 91 | Producer(Buffer* buffer) : m_container(buffer) 92 | {} 93 | 94 | void operator()() 95 | { 96 | for (unsigned long i = 0L; i < total_elements; ++i) 97 | { 98 | m_container->push_front(value_type()); 99 | } 100 | } 101 | }; 102 | 103 | template 104 | class Consumer 105 | { 106 | 107 | typedef typename Buffer::value_type value_type; 108 | Buffer* m_container; 109 | value_type m_item; 110 | 111 | public: 112 | Consumer(Buffer* buffer) : m_container(buffer) 113 | {} 114 | 115 | void operator()() 116 | { 117 | for (unsigned long i = 0L; i < total_elements; ++i) 118 | { 119 | m_container->pop_back(&m_item); 120 | } 121 | } 122 | }; 123 | 124 | /*`Create a first-int first-out test of the bound_buffer. 125 | Include a call to boost::progress_timer 126 | 127 | [@http://www.boost.org/doc/libs/1_53_0/libs/timer/doc/cpu_timers.html CPU timer] 128 | 129 | */ 130 | template 131 | void fifo_test(Buffer* buffer) 132 | { 133 | // Start of timing. 134 | boost::timer::auto_cpu_timer progress; 135 | 136 | // Initialize the buffer with some values before launching producer and consumer threads. 137 | for (unsigned long i = queue_size / 2L; i > 0; --i) 138 | { 139 | #if BOOST_WORKAROUND(BOOST_BORLANDC, BOOST_TESTED_AT(0x581)) 140 | buffer->push_front(Buffer::value_type()); 141 | #else 142 | buffer->push_front(BOOST_DEDUCED_TYPENAME Buffer::value_type()); 143 | #endif 144 | } 145 | 146 | // Construct the threads. 147 | Consumer consumer(buffer); 148 | Producer producer(buffer); 149 | 150 | // Start the threads. 151 | boost::thread consume(consumer); 152 | boost::thread produce(producer); 153 | 154 | // Wait for completion. 155 | consume.join(); 156 | produce.join(); 157 | 158 | // End of timing. 159 | // destructor of boost::timer::auto_cpu_timer will output the time to std::cout. 160 | 161 | } 162 | //] [/circular_buffer_bound_example_2] 163 | 164 | 165 | int main() 166 | { 167 | //[circular_buffer_bound_example_3] 168 | //`Construct a bounded_buffer to hold the chosen type, here int. 169 | bounded_buffer bb_int(queue_size); 170 | std::cout << "Testing bounded_buffer "; 171 | 172 | //`Start the fifo test. 173 | fifo_test(&bb_int); 174 | //` destructor of boost::timer::auto_cpu_timer will output the time to std::cout 175 | 176 | //] [/circular_buffer_bound_example_3] 177 | 178 | return 0; 179 | } // int main() 180 | 181 | /* 182 | 183 | //[circular_buffer_bound_output 184 | 185 | Description: Autorun "J:\Cpp\Misc\Debug\circular_buffer_bound_example.exe" 186 | 187 | Testing bounded_buffer 15.010692s wall, 9.188459s user + 7.207246s system = 16.395705s CPU (109.2%) 188 | 189 | //] [/circular_buffer_bound_output] 190 | */ 191 | 192 | 193 | -------------------------------------------------------------------------------- /example/circular_buffer_example.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2003-2008 Jan Gaspar. 2 | // Copyright 2013 Paul A. Bristow. Added some Quickbook snippet markers. 3 | 4 | // Distributed under the Boost Software License, Version 1.0. 5 | // (See the accompanying file LICENSE_1_0.txt 6 | // or a copy at .) 7 | 8 | //[circular_buffer_example_1 9 | /*`For all examples, we need this include: 10 | */ 11 | 12 | #include 13 | 14 | //] [/circular_buffer_example_1] 15 | 16 | int main() 17 | { 18 | 19 | //[circular_buffer_example_2 20 | // Create a circular buffer with a capacity for 3 integers. 21 | boost::circular_buffer cb(3); 22 | 23 | // Insert three elements into the buffer. 24 | cb.push_back(1); 25 | cb.push_back(2); 26 | cb.push_back(3); 27 | 28 | int a = cb[0]; // a == 1 29 | int b = cb[1]; // b == 2 30 | int c = cb[2]; // c == 3 31 | 32 | // The buffer is full now, so pushing subsequent 33 | // elements will overwrite the front-most elements. 34 | 35 | cb.push_back(4); // Overwrite 1 with 4. 36 | cb.push_back(5); // Overwrite 2 with 5. 37 | 38 | // The buffer now contains 3, 4 and 5. 39 | a = cb[0]; // a == 3 40 | b = cb[1]; // b == 4 41 | c = cb[2]; // c == 5 42 | 43 | // Elements can be popped from either the front or the back. 44 | cb.pop_back(); // 5 is removed. 45 | cb.pop_front(); // 3 is removed. 46 | 47 | // Leaving only one element with value = 4. 48 | int d = cb[0]; // d == 4 49 | 50 | //] [/circular_buffer_example_2] 51 | return 0; 52 | } 53 | 54 | /* 55 | 56 | //[circular_buffer_example_output 57 | 58 | There is no output from this example. 59 | 60 | //] [/circular_buffer_example_output] 61 | 62 | */ 63 | 64 | -------------------------------------------------------------------------------- /example/circular_buffer_examples.bat: -------------------------------------------------------------------------------- 1 | echo off 2 | rem quickbook doxygen auto-index docs template circular_buffer_html_index.bat 3 | rem echo circular_buffer_html_index_%date%_%time:~0,2%_%time:~3,2%.log 4 | rem The DOS time format is assumed 12:34 and the : separator is not used. 5 | set t=%time% /T 6 | set tim=%t:~0,2%%t:~3,2% 7 | rem pick just hours and minutes. 8 | rem time may include leading space, like " 915", so remove space. 9 | set tim=%tim: =% 10 | rem boost-no-inspect 11 | rem cd \boost-trunk/circular_buffer\libs\circular_buffer\example 12 | bjam -a > circular_buffer_examples_%date%_%tim%.log 13 | if not ERRORLEVEL 0 (echo Errorlevel is %ERRORLEVEL%) else (echo OK) 14 | pause 15 | -------------------------------------------------------------------------------- /example/circular_buffer_iter_example.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2003-2008 Jan Gaspar. 2 | // Copyright 2013 Paul A. Bristow. Added some Quickbook snippet markers. 3 | 4 | // Distributed under the Boost Software License, Version 1.0. 5 | // (See the accompanying file LICENSE_1_0.txt 6 | // or a copy at .) 7 | 8 | #undef BOOST_CB_ENABLE_DEBUG 9 | 10 | //[circular_buffer_iter_example_1 11 | /*` 12 | */ 13 | 14 | #define BOOST_CB_ENABLE_DEBUG 0 // The Debug Support has to be disabled, otherwise the code produces a runtime error. 15 | 16 | #include 17 | #include 18 | #include 19 | 20 | int main(int /*argc*/, char* /*argv*/[]) 21 | { 22 | 23 | boost::circular_buffer cb(3); 24 | 25 | cb.push_back(1); 26 | cb.push_back(2); 27 | cb.push_back(3); 28 | 29 | boost::circular_buffer::iterator it = cb.begin(); 30 | 31 | assert(*it == 1); 32 | 33 | cb.push_back(4); 34 | 35 | assert(*it == 4); // The iterator still points to the initialized memory. 36 | 37 | return 0; 38 | } 39 | 40 | //] [/circular_buffer_iter_example_1] 41 | -------------------------------------------------------------------------------- /example/circular_buffer_sum_example.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2003-2008 Jan Gaspar. 2 | // Copyright 2013 Paul A. Bristow. Added some Quickbook snippet markers. 3 | 4 | // Distributed under the Boost Software License, Version 1.0. 5 | // (See the accompanying file LICENSE_1_0.txt 6 | // or a copy at .) 7 | 8 | //[circular_buffer_sum_example_1 9 | 10 | /*`This example shows several functions, including summing all valid values. 11 | */ 12 | 13 | #include 14 | #include 15 | #include 16 | 17 | int main(int /*argc*/, char* /*argv*/[]) 18 | { 19 | // Create a circular buffer of capacity 3. 20 | boost::circular_buffer cb(3); 21 | assert(cb.capacity() == 3); 22 | // Check is empty. 23 | assert(cb.size() == 0); 24 | assert(cb.empty()); 25 | 26 | // Insert some elements into the circular buffer. 27 | cb.push_back(1); 28 | cb.push_back(2); 29 | 30 | // Assertions to check push_backs have expected effect. 31 | assert(cb[0] == 1); 32 | assert(cb[1] == 2); 33 | assert(!cb.full()); 34 | assert(cb.size() == 2); 35 | assert(cb.capacity() == 3); 36 | 37 | // Insert some other elements. 38 | cb.push_back(3); 39 | cb.push_back(4); 40 | 41 | // Evaluate the sum of all elements. 42 | int sum = std::accumulate(cb.begin(), cb.end(), 0); 43 | 44 | // Assertions to check state. 45 | assert(sum == 9); 46 | assert(cb[0] == 2); 47 | assert(cb[1] == 3); 48 | assert(cb[2] == 4); 49 | assert(*cb.begin() == 2); 50 | assert(cb.front() == 2); 51 | assert(cb.back() == 4); 52 | assert(cb.full()); 53 | assert(cb.size() == 3); 54 | assert(cb.capacity() == 3); 55 | 56 | return 0; 57 | } 58 | 59 | //] [/circular_buffer_sum_example_1] 60 | 61 | 62 | /* 63 | There is no output from this example. 64 | */ 65 | -------------------------------------------------------------------------------- /example/jamfile.v2: -------------------------------------------------------------------------------- 1 | # Copyright Paul A. Bristow 2013 2 | 3 | # Distributed under the Boost Software License, Version 1.0. 4 | # (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 5 | 6 | # jamfile.v2 to run all circular_buffer examples. 7 | 8 | # bring in the rules for testing. 9 | import testing ; 10 | 11 | project 12 | : requirements 13 | /boost/system//boost_system 14 | /boost/thread//boost_thread 15 | #BOOST_ALL_NO_LIB=1 16 | multi 17 | 18 | gcc:-Wno-missing-braces 19 | darwin:-Wno-missing-braces 20 | acc:+W2068,2461,2236,4070 21 | intel:-Qwd264,239 22 | msvc:all 23 | msvc:on 24 | msvc:_CRT_SECURE_NO_DEPRECATE 25 | msvc:_SCL_SECURE_NO_DEPRECATE 26 | msvc:_SCL_SECURE_NO_WARNINGS 27 | msvc:_CRT_SECURE_NO_WARNINGS 28 | msvc:/wd4996 29 | msvc:/wd4512 30 | msvc:/wd4610 31 | msvc:/wd4510 32 | msvc:/wd4127 33 | msvc:/wd4701 34 | msvc:/wd4127 35 | msvc:/wd4305 36 | ; 37 | 38 | run bounded_buffer_comparison.cpp ../../timer/build//boost_timer ; 39 | run circular_buffer_iter_example.cpp ; 40 | run circular_buffer_sum_example.cpp ; 41 | run circular_buffer_bound_example.cpp ../../thread/build//boost_thread ../../timer/build//boost_timer ; 42 | 43 | -------------------------------------------------------------------------------- /include/boost/circular_buffer.hpp: -------------------------------------------------------------------------------- 1 | // Circular buffer library header file. 2 | 3 | // Copyright (c) 2003-2008 Jan Gaspar 4 | 5 | // Use, modification, and distribution is subject to the Boost Software 6 | // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at 7 | // http://www.boost.org/LICENSE_1_0.txt) 8 | 9 | // See www.boost.org/libs/circular_buffer for documentation. 10 | 11 | /*! @file 12 | Includes 13 | */ 14 | 15 | #if !defined(BOOST_CIRCULAR_BUFFER_HPP) 16 | #define BOOST_CIRCULAR_BUFFER_HPP 17 | 18 | #if defined(_MSC_VER) 19 | #pragma once 20 | #endif 21 | 22 | #include 23 | #include 24 | #include 25 | 26 | /*! Debug support control. */ 27 | #if !defined(BOOST_CB_ENABLE_DEBUG) 28 | #define BOOST_CB_ENABLE_DEBUG 0 29 | #endif 30 | 31 | /*! INTERNAL ONLY */ 32 | #if BOOST_CB_ENABLE_DEBUG 33 | #include 34 | #define BOOST_CB_ASSERT(Expr) BOOST_ASSERT(Expr) 35 | #else 36 | #define BOOST_CB_ASSERT(Expr) ((void)0) 37 | #endif 38 | 39 | /*! INTERNAL ONLY */ 40 | #if BOOST_WORKAROUND(BOOST_BORLANDC, <= 0x0550) || BOOST_WORKAROUND(__MWERKS__, <= 0x2407) 41 | #define BOOST_CB_IS_CONVERTIBLE(Iterator, Type) ((void)0) 42 | #else 43 | #include 44 | #include 45 | #define BOOST_CB_IS_CONVERTIBLE(Iterator, Type) \ 46 | BOOST_STATIC_ASSERT((is_convertible::value_type, Type>::value)) 47 | #endif 48 | 49 | /*! INTERNAL ONLY */ 50 | #if defined(BOOST_NO_TEMPLATED_ITERATOR_CONSTRUCTORS) 51 | #define BOOST_CB_ASSERT_TEMPLATED_ITERATOR_CONSTRUCTORS BOOST_STATIC_ASSERT(false); 52 | #else 53 | #define BOOST_CB_ASSERT_TEMPLATED_ITERATOR_CONSTRUCTORS ((void)0); 54 | #endif 55 | 56 | #include 57 | #include 58 | #include 59 | #include 60 | 61 | #undef BOOST_CB_ASSERT_TEMPLATED_ITERATOR_CONSTRUCTORS 62 | #undef BOOST_CB_IS_CONVERTIBLE 63 | #undef BOOST_CB_ASSERT 64 | 65 | #endif // #if !defined(BOOST_CIRCULAR_BUFFER_HPP) 66 | -------------------------------------------------------------------------------- /include/boost/circular_buffer/debug.hpp: -------------------------------------------------------------------------------- 1 | // Debug support for the circular buffer library. 2 | 3 | // Copyright (c) 2003-2008 Jan Gaspar 4 | 5 | // Use, modification, and distribution is subject to the Boost Software 6 | // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at 7 | // http://www.boost.org/LICENSE_1_0.txt) 8 | 9 | #if !defined(BOOST_CIRCULAR_BUFFER_DEBUG_HPP) 10 | #define BOOST_CIRCULAR_BUFFER_DEBUG_HPP 11 | 12 | #if defined(_MSC_VER) 13 | #pragma once 14 | #endif 15 | 16 | #if BOOST_CB_ENABLE_DEBUG 17 | #include 18 | 19 | #if defined(BOOST_NO_STDC_NAMESPACE) 20 | namespace std { 21 | using ::memset; 22 | } 23 | #endif 24 | 25 | #endif // BOOST_CB_ENABLE_DEBUG 26 | namespace boost { 27 | 28 | namespace cb_details { 29 | 30 | #if BOOST_CB_ENABLE_DEBUG 31 | 32 | // The value the uninitialized memory is filled with. 33 | const int UNINITIALIZED = 0xcc; 34 | 35 | template 36 | inline void do_fill_uninitialized_memory(T* data, std::size_t size_in_bytes) BOOST_NOEXCEPT { 37 | std::memset(static_cast(data), UNINITIALIZED, size_in_bytes); 38 | } 39 | 40 | template 41 | inline void do_fill_uninitialized_memory(T& /*data*/, std::size_t /*size_in_bytes*/) BOOST_NOEXCEPT { 42 | // Do nothing 43 | } 44 | 45 | 46 | class debug_iterator_registry; 47 | 48 | /*! 49 | \class debug_iterator_base 50 | \brief Registers/unregisters iterators into the registry of valid iterators. 51 | 52 | This class is intended to be a base class of an iterator. 53 | */ 54 | class debug_iterator_base { 55 | 56 | private: 57 | // Members 58 | 59 | //! Iterator registry. 60 | mutable const debug_iterator_registry* m_registry; 61 | 62 | //! Next iterator in the iterator chain. 63 | mutable const debug_iterator_base* m_next; 64 | 65 | public: 66 | // Construction/destruction 67 | 68 | //! Default constructor. 69 | debug_iterator_base(); 70 | 71 | //! Constructor taking the iterator registry as a parameter. 72 | debug_iterator_base(const debug_iterator_registry* registry); 73 | 74 | //! Copy constructor. 75 | debug_iterator_base(const debug_iterator_base& rhs); 76 | 77 | //! Destructor. 78 | ~debug_iterator_base(); 79 | 80 | // Methods 81 | 82 | //! Assign operator. 83 | debug_iterator_base& operator = (const debug_iterator_base& rhs); 84 | 85 | //! Is the iterator valid? 86 | bool is_valid(const debug_iterator_registry* registry) const; 87 | 88 | //! Invalidate the iterator. 89 | /*! 90 | \note The method is const in order to invalidate const iterators, too. 91 | */ 92 | void invalidate() const; 93 | 94 | //! Return the next iterator in the iterator chain. 95 | const debug_iterator_base* next() const; 96 | 97 | //! Set the next iterator in the iterator chain. 98 | /*! 99 | \note The method is const in order to set a next iterator to a const iterator, too. 100 | */ 101 | void set_next(const debug_iterator_base* it) const; 102 | 103 | private: 104 | // Helpers 105 | 106 | //! Register self as a valid iterator. 107 | void register_self(); 108 | 109 | //! Unregister self from valid iterators. 110 | void unregister_self(); 111 | }; 112 | 113 | /*! 114 | \class debug_iterator_registry 115 | \brief Registry of valid iterators. 116 | 117 | This class is intended to be a base class of a container. 118 | */ 119 | class debug_iterator_registry { 120 | 121 | //! Pointer to the chain of valid iterators. 122 | mutable const debug_iterator_base* m_iterators; 123 | 124 | public: 125 | // Methods 126 | 127 | //! Default constructor. 128 | debug_iterator_registry() : m_iterators(0) {} 129 | 130 | //! Register an iterator into the list of valid iterators. 131 | /*! 132 | \note The method is const in order to register iterators into const containers, too. 133 | */ 134 | void register_iterator(const debug_iterator_base* it) const { 135 | it->set_next(m_iterators); 136 | m_iterators = it; 137 | } 138 | 139 | //! Unregister an iterator from the list of valid iterators. 140 | /*! 141 | \note The method is const in order to unregister iterators from const containers, too. 142 | */ 143 | void unregister_iterator(const debug_iterator_base* it) const { 144 | const debug_iterator_base* previous = 0; 145 | for (const debug_iterator_base* p = m_iterators; p != it; previous = p, p = p->next()) {} 146 | remove(it, previous); 147 | } 148 | 149 | //! Invalidate every iterator pointing to the same element as the iterator passed as a parameter. 150 | template 151 | void invalidate_iterators(const Iterator& it) { 152 | const debug_iterator_base* previous = 0; 153 | for (const debug_iterator_base* p = m_iterators; p != 0; p = p->next()) { 154 | if (((Iterator*)p)->m_it == it.m_it) { 155 | p->invalidate(); 156 | remove(p, previous); 157 | continue; 158 | } 159 | previous = p; 160 | } 161 | } 162 | 163 | //! Invalidate all iterators except an iterator poining to the same element as the iterator passed as a parameter. 164 | template 165 | void invalidate_iterators_except(const Iterator& it) { 166 | const debug_iterator_base* previous = 0; 167 | for (const debug_iterator_base* p = m_iterators; p != 0; p = p->next()) { 168 | if (((Iterator*)p)->m_it != it.m_it) { 169 | p->invalidate(); 170 | remove(p, previous); 171 | continue; 172 | } 173 | previous = p; 174 | } 175 | } 176 | 177 | //! Invalidate all iterators. 178 | void invalidate_all_iterators() { 179 | for (const debug_iterator_base* p = m_iterators; p != 0; p = p->next()) 180 | p->invalidate(); 181 | m_iterators = 0; 182 | } 183 | 184 | private: 185 | // Helpers 186 | 187 | //! Remove the current iterator from the iterator chain. 188 | void remove(const debug_iterator_base* current, 189 | const debug_iterator_base* previous) const { 190 | if (previous == 0) 191 | m_iterators = m_iterators->next(); 192 | else 193 | previous->set_next(current->next()); 194 | } 195 | }; 196 | 197 | // Implementation of the debug_iterator_base methods. 198 | 199 | inline debug_iterator_base::debug_iterator_base() : m_registry(0), m_next(0) {} 200 | 201 | inline debug_iterator_base::debug_iterator_base(const debug_iterator_registry* registry) 202 | : m_registry(registry), m_next(0) { 203 | register_self(); 204 | } 205 | 206 | inline debug_iterator_base::debug_iterator_base(const debug_iterator_base& rhs) 207 | : m_registry(rhs.m_registry), m_next(0) { 208 | register_self(); 209 | } 210 | 211 | inline debug_iterator_base::~debug_iterator_base() { unregister_self(); } 212 | 213 | inline debug_iterator_base& debug_iterator_base::operator = (const debug_iterator_base& rhs) { 214 | if (m_registry == rhs.m_registry) 215 | return *this; 216 | unregister_self(); 217 | m_registry = rhs.m_registry; 218 | register_self(); 219 | return *this; 220 | } 221 | 222 | inline bool debug_iterator_base::is_valid(const debug_iterator_registry* registry) const { 223 | return m_registry == registry; 224 | } 225 | 226 | inline void debug_iterator_base::invalidate() const { m_registry = 0; } 227 | 228 | inline const debug_iterator_base* debug_iterator_base::next() const { return m_next; } 229 | 230 | inline void debug_iterator_base::set_next(const debug_iterator_base* it) const { m_next = it; } 231 | 232 | inline void debug_iterator_base::register_self() { 233 | if (m_registry != 0) 234 | m_registry->register_iterator(this); 235 | } 236 | 237 | inline void debug_iterator_base::unregister_self() { 238 | if (m_registry != 0) 239 | m_registry->unregister_iterator(this); 240 | } 241 | 242 | #endif // #if BOOST_CB_ENABLE_DEBUG 243 | 244 | } // namespace cb_details 245 | 246 | } // namespace boost 247 | 248 | #endif // #if !defined(BOOST_CIRCULAR_BUFFER_DEBUG_HPP) 249 | -------------------------------------------------------------------------------- /include/boost/circular_buffer/details.hpp: -------------------------------------------------------------------------------- 1 | // Helper classes and functions for the circular buffer. 2 | 3 | // Copyright (c) 2003-2008 Jan Gaspar 4 | 5 | // Copyright 2014,2018 Glen Joseph Fernandes 6 | // (glenjofe@gmail.com) 7 | 8 | // Use, modification, and distribution is subject to the Boost Software 9 | // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at 10 | // http://www.boost.org/LICENSE_1_0.txt) 11 | 12 | #if !defined(BOOST_CIRCULAR_BUFFER_DETAILS_HPP) 13 | #define BOOST_CIRCULAR_BUFFER_DETAILS_HPP 14 | 15 | #if defined(_MSC_VER) 16 | #pragma once 17 | #endif 18 | 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | 27 | // Silence MS /W4 warnings like C4913: 28 | // "user defined binary operator ',' exists but no overload could convert all operands, default built-in binary operator ',' used" 29 | // This might happen when previously including some boost headers that overload the coma operator. 30 | #if defined(_MSC_VER) 31 | # pragma warning(push) 32 | # pragma warning(disable:4913) 33 | #endif 34 | 35 | namespace boost { 36 | 37 | namespace cb_details { 38 | 39 | template struct nonconst_traits; 40 | 41 | template 42 | void uninitialized_fill_n_with_alloc( 43 | ForwardIterator first, Diff n, const T& item, Alloc& alloc); 44 | 45 | template 46 | ForwardIterator uninitialized_copy(InputIterator first, InputIterator last, ForwardIterator dest, Alloc& a); 47 | 48 | template 49 | ForwardIterator uninitialized_move_if_noexcept(InputIterator first, InputIterator last, ForwardIterator dest, Alloc& a); 50 | 51 | /*! 52 | \struct const_traits 53 | \brief Defines the data types for a const iterator. 54 | */ 55 | template 56 | struct const_traits { 57 | // Basic types 58 | typedef typename Alloc::value_type value_type; 59 | typedef typename boost::allocator_const_pointer::type pointer; 60 | typedef const value_type& reference; 61 | typedef typename boost::allocator_size_type::type size_type; 62 | typedef typename boost::allocator_difference_type::type difference_type; 63 | 64 | // Non-const traits 65 | typedef nonconst_traits nonconst_self; 66 | }; 67 | 68 | /*! 69 | \struct nonconst_traits 70 | \brief Defines the data types for a non-const iterator. 71 | */ 72 | template 73 | struct nonconst_traits { 74 | // Basic types 75 | typedef typename Alloc::value_type value_type; 76 | typedef typename boost::allocator_pointer::type pointer; 77 | typedef value_type& reference; 78 | typedef typename boost::allocator_size_type::type size_type; 79 | typedef typename boost::allocator_difference_type::type difference_type; 80 | 81 | // Non-const traits 82 | typedef nonconst_traits nonconst_self; 83 | }; 84 | 85 | /*! 86 | \struct iterator_wrapper 87 | \brief Helper iterator dereference wrapper. 88 | */ 89 | template 90 | struct iterator_wrapper { 91 | mutable Iterator m_it; 92 | explicit iterator_wrapper(Iterator it) : m_it(it) {} 93 | Iterator operator () () const { return m_it++; } 94 | private: 95 | iterator_wrapper& operator = (const iterator_wrapper&); // do not generate 96 | }; 97 | 98 | /*! 99 | \struct item_wrapper 100 | \brief Helper item dereference wrapper. 101 | */ 102 | template 103 | struct item_wrapper { 104 | Value m_item; 105 | explicit item_wrapper(Value item) : m_item(item) {} 106 | Pointer operator () () const { return &m_item; } 107 | private: 108 | item_wrapper& operator = (const item_wrapper&); // do not generate 109 | }; 110 | 111 | /*! 112 | \struct assign_n 113 | \brief Helper functor for assigning n items. 114 | */ 115 | template 116 | struct assign_n { 117 | typedef typename boost::allocator_size_type::type size_type; 118 | size_type m_n; 119 | Value m_item; 120 | Alloc& m_alloc; 121 | assign_n(size_type n, Value item, Alloc& alloc) : m_n(n), m_item(item), m_alloc(alloc) {} 122 | template 123 | void operator () (Pointer p) const { 124 | uninitialized_fill_n_with_alloc(p, m_n, m_item, m_alloc); 125 | } 126 | private: 127 | assign_n& operator = (const assign_n&); // do not generate 128 | }; 129 | 130 | /*! 131 | \struct assign_range 132 | \brief Helper functor for assigning range of items. 133 | */ 134 | template 135 | struct assign_range { 136 | Iterator m_first; 137 | Iterator m_last; 138 | Alloc& m_alloc; 139 | 140 | assign_range(const Iterator& first, const Iterator& last, Alloc& alloc) 141 | : m_first(first), m_last(last), m_alloc(alloc) {} 142 | 143 | template 144 | void operator () (Pointer p) const { 145 | boost::cb_details::uninitialized_copy(m_first, m_last, p, m_alloc); 146 | } 147 | }; 148 | 149 | template 150 | inline assign_range make_assign_range(const Iterator& first, const Iterator& last, Alloc& a) { 151 | return assign_range(first, last, a); 152 | } 153 | 154 | /*! 155 | \class capacity_control 156 | \brief Capacity controller of the space optimized circular buffer. 157 | */ 158 | template 159 | class capacity_control { 160 | 161 | //! The capacity of the space-optimized circular buffer. 162 | Size m_capacity; 163 | 164 | //! The lowest guaranteed or minimum capacity of the adapted space-optimized circular buffer. 165 | Size m_min_capacity; 166 | 167 | public: 168 | 169 | //! Constructor. 170 | capacity_control(Size buffer_capacity, Size min_buffer_capacity = 0) 171 | : m_capacity(buffer_capacity), m_min_capacity(min_buffer_capacity) 172 | { // Check for capacity lower than min_capacity. 173 | BOOST_CB_ASSERT(buffer_capacity >= min_buffer_capacity); 174 | } 175 | 176 | // Default copy constructor. 177 | 178 | // Default assign operator. 179 | 180 | //! Get the capacity of the space optimized circular buffer. 181 | Size capacity() const { return m_capacity; } 182 | 183 | //! Get the minimal capacity of the space optimized circular buffer. 184 | Size min_capacity() const { return m_min_capacity; } 185 | 186 | //! Size operator - returns the capacity of the space optimized circular buffer. 187 | operator Size() const { return m_capacity; } 188 | }; 189 | 190 | /*! 191 | \struct iterator 192 | \brief Random access iterator for the circular buffer. 193 | \param Buff The type of the underlying circular buffer. 194 | \param Traits Basic iterator types. 195 | \note This iterator is not circular. It was designed 196 | for iterating from begin() to end() of the circular buffer. 197 | */ 198 | template 199 | struct iterator 200 | #if BOOST_CB_ENABLE_DEBUG 201 | : public debug_iterator_base 202 | #endif // #if BOOST_CB_ENABLE_DEBUG 203 | { 204 | // Helper types 205 | 206 | //! Non-const iterator. 207 | typedef iterator nonconst_self; 208 | 209 | // Basic types 210 | typedef std::random_access_iterator_tag iterator_category; 211 | 212 | //! The type of the elements stored in the circular buffer. 213 | typedef typename Traits::value_type value_type; 214 | 215 | //! Pointer to the element. 216 | typedef typename Traits::pointer pointer; 217 | 218 | //! Reference to the element. 219 | typedef typename Traits::reference reference; 220 | 221 | //! Size type. 222 | typedef typename Traits::size_type size_type; 223 | 224 | //! Difference type. 225 | typedef typename Traits::difference_type difference_type; 226 | 227 | // Member variables 228 | 229 | //! The circular buffer where the iterator points to. 230 | const Buff* m_buff; 231 | 232 | //! An internal iterator. 233 | pointer m_it; 234 | 235 | // Construction & assignment 236 | 237 | // Default copy constructor. 238 | 239 | //! Default constructor. 240 | iterator() : m_buff(0), m_it(0) {} 241 | 242 | #if BOOST_CB_ENABLE_DEBUG 243 | 244 | //! Copy constructor (used for converting from a non-const to a const iterator). 245 | iterator(const nonconst_self& it) : debug_iterator_base(it), m_buff(it.m_buff), m_it(it.m_it) {} 246 | 247 | //! Internal constructor. 248 | /*! 249 | \note This constructor is not intended to be used directly by the user. 250 | */ 251 | iterator(const Buff* cb, const pointer p) : debug_iterator_base(cb), m_buff(cb), m_it(p) {} 252 | 253 | #else 254 | 255 | iterator(const nonconst_self& it) : m_buff(it.m_buff), m_it(it.m_it) {} 256 | 257 | iterator(const Buff* cb, const pointer p) : m_buff(cb), m_it(p) {} 258 | 259 | #endif // #if BOOST_CB_ENABLE_DEBUG 260 | 261 | //! Assign operator. 262 | #if !defined(BOOST_NO_CXX11_DEFAULTED_FUNCTIONS) 263 | iterator& operator=(const iterator&) = default; 264 | #else 265 | iterator& operator=(const iterator& it) { 266 | if (this == &it) 267 | return *this; 268 | #if BOOST_CB_ENABLE_DEBUG 269 | debug_iterator_base::operator =(it); 270 | #endif // #if BOOST_CB_ENABLE_DEBUG 271 | m_buff = it.m_buff; 272 | m_it = it.m_it; 273 | return *this; 274 | } 275 | #endif 276 | 277 | // Random access iterator methods 278 | 279 | //! Dereferencing operator. 280 | reference operator * () const { 281 | BOOST_CB_ASSERT(is_valid(m_buff)); // check for uninitialized or invalidated iterator 282 | BOOST_CB_ASSERT(m_it != 0); // check for iterator pointing to end() 283 | return *m_it; 284 | } 285 | 286 | //! Dereferencing operator. 287 | pointer operator -> () const { return &(operator*()); } 288 | 289 | //! Difference operator. 290 | template 291 | difference_type operator - (const iterator& it) const { 292 | BOOST_CB_ASSERT(is_valid(m_buff)); // check for uninitialized or invalidated iterator 293 | BOOST_CB_ASSERT(it.is_valid(m_buff)); // check for uninitialized or invalidated iterator 294 | return linearize_pointer(*this) - linearize_pointer(it); 295 | } 296 | 297 | //! Increment operator (prefix). 298 | iterator& operator ++ () { 299 | BOOST_CB_ASSERT(is_valid(m_buff)); // check for uninitialized or invalidated iterator 300 | BOOST_CB_ASSERT(m_it != 0); // check for iterator pointing to end() 301 | m_buff->increment(m_it); 302 | if (m_it == m_buff->m_last) 303 | m_it = 0; 304 | return *this; 305 | } 306 | 307 | //! Increment operator (postfix). 308 | iterator operator ++ (int) { 309 | iterator tmp = *this; 310 | ++*this; 311 | return tmp; 312 | } 313 | 314 | //! Decrement operator (prefix). 315 | iterator& operator -- () { 316 | BOOST_CB_ASSERT(is_valid(m_buff)); // check for uninitialized or invalidated iterator 317 | BOOST_CB_ASSERT(m_it != m_buff->m_first); // check for iterator pointing to begin() 318 | if (m_it == 0) 319 | m_it = m_buff->m_last; 320 | m_buff->decrement(m_it); 321 | return *this; 322 | } 323 | 324 | //! Decrement operator (postfix). 325 | iterator operator -- (int) { 326 | iterator tmp = *this; 327 | --*this; 328 | return tmp; 329 | } 330 | 331 | //! Iterator addition. 332 | iterator& operator += (difference_type n) { 333 | BOOST_CB_ASSERT(is_valid(m_buff)); // check for uninitialized or invalidated iterator 334 | if (n > 0) { 335 | BOOST_CB_ASSERT(m_buff->end() - *this >= n); // check for too large n 336 | m_it = m_buff->add(m_it, n); 337 | if (m_it == m_buff->m_last) 338 | m_it = 0; 339 | } else if (n < 0) { 340 | *this -= -n; 341 | } 342 | return *this; 343 | } 344 | 345 | //! Iterator addition. 346 | iterator operator + (difference_type n) const { return iterator(*this) += n; } 347 | 348 | //! Iterator subtraction. 349 | iterator& operator -= (difference_type n) { 350 | BOOST_CB_ASSERT(is_valid(m_buff)); // check for uninitialized or invalidated iterator 351 | if (n > 0) { 352 | BOOST_CB_ASSERT(*this - m_buff->begin() >= n); // check for too large n 353 | m_it = m_buff->sub(m_it == 0 ? m_buff->m_last : m_it, n); 354 | } else if (n < 0) { 355 | *this += -n; 356 | } 357 | return *this; 358 | } 359 | 360 | //! Iterator subtraction. 361 | iterator operator - (difference_type n) const { return iterator(*this) -= n; } 362 | 363 | //! Element access operator. 364 | reference operator [] (difference_type n) const { return *(*this + n); } 365 | 366 | // Equality & comparison 367 | 368 | //! Equality. 369 | template 370 | bool operator == (const iterator& it) const { 371 | BOOST_CB_ASSERT(is_valid(m_buff)); // check for uninitialized or invalidated iterator 372 | BOOST_CB_ASSERT(it.is_valid(m_buff)); // check for uninitialized or invalidated iterator 373 | return m_it == it.m_it; 374 | } 375 | 376 | //! Inequality. 377 | template 378 | bool operator != (const iterator& it) const { 379 | BOOST_CB_ASSERT(is_valid(m_buff)); // check for uninitialized or invalidated iterator 380 | BOOST_CB_ASSERT(it.is_valid(m_buff)); // check for uninitialized or invalidated iterator 381 | return m_it != it.m_it; 382 | } 383 | 384 | //! Less. 385 | template 386 | bool operator < (const iterator& it) const { 387 | BOOST_CB_ASSERT(is_valid(m_buff)); // check for uninitialized or invalidated iterator 388 | BOOST_CB_ASSERT(it.is_valid(m_buff)); // check for uninitialized or invalidated iterator 389 | return linearize_pointer(*this) < linearize_pointer(it); 390 | } 391 | 392 | //! Greater. 393 | template 394 | bool operator > (const iterator& it) const { return it < *this; } 395 | 396 | //! Less or equal. 397 | template 398 | bool operator <= (const iterator& it) const { return !(it < *this); } 399 | 400 | //! Greater or equal. 401 | template 402 | bool operator >= (const iterator& it) const { return !(*this < it); } 403 | 404 | // Helpers 405 | 406 | //! Get a pointer which would point to the same element as the iterator in case the circular buffer is linearized. 407 | template 408 | typename Traits0::pointer linearize_pointer(const iterator& it) const { 409 | return it.m_it == 0 ? m_buff->m_buff + m_buff->size() : 410 | (it.m_it < m_buff->m_first ? it.m_it + (m_buff->m_end - m_buff->m_first) 411 | : m_buff->m_buff + (it.m_it - m_buff->m_first)); 412 | } 413 | }; 414 | 415 | //! Iterator addition. 416 | template 417 | inline iterator 418 | operator + (typename Traits::difference_type n, const iterator& it) { 419 | return it + n; 420 | } 421 | 422 | /*! 423 | \fn ForwardIterator uninitialized_copy(InputIterator first, InputIterator last, ForwardIterator dest) 424 | \brief Equivalent of std::uninitialized_copy but with explicit specification of value type. 425 | */ 426 | template 427 | inline ForwardIterator uninitialized_copy(InputIterator first, InputIterator last, ForwardIterator dest, Alloc& a) { 428 | ForwardIterator next = dest; 429 | BOOST_TRY { 430 | for (; first != last; ++first, ++dest) 431 | boost::allocator_construct(a, boost::to_address(dest), *first); 432 | } BOOST_CATCH(...) { 433 | for (; next != dest; ++next) 434 | boost::allocator_destroy(a, boost::to_address(next)); 435 | BOOST_RETHROW 436 | } 437 | BOOST_CATCH_END 438 | return dest; 439 | } 440 | 441 | template 442 | ForwardIterator uninitialized_move_if_noexcept_impl(InputIterator first, InputIterator last, ForwardIterator dest, Alloc& a, 443 | true_type) { 444 | for (; first != last; ++first, ++dest) 445 | boost::allocator_construct(a, boost::to_address(dest), boost::move(*first)); 446 | return dest; 447 | } 448 | 449 | template 450 | ForwardIterator uninitialized_move_if_noexcept_impl(InputIterator first, InputIterator last, ForwardIterator dest, Alloc& a, 451 | false_type) { 452 | return uninitialized_copy(first, last, dest, a); 453 | } 454 | 455 | /*! 456 | \fn ForwardIterator uninitialized_move_if_noexcept(InputIterator first, InputIterator last, ForwardIterator dest) 457 | \brief Equivalent of std::uninitialized_copy but with explicit specification of value type and moves elements if they have noexcept move constructors. 458 | */ 459 | template 460 | ForwardIterator uninitialized_move_if_noexcept(InputIterator first, InputIterator last, ForwardIterator dest, Alloc& a) { 461 | typedef typename boost::is_nothrow_move_constructible::type tag_t; 462 | return uninitialized_move_if_noexcept_impl(first, last, dest, a, tag_t()); 463 | } 464 | 465 | /*! 466 | \fn void uninitialized_fill_n_with_alloc(ForwardIterator first, Diff n, const T& item, Alloc& alloc) 467 | \brief Equivalent of std::uninitialized_fill_n with allocator. 468 | */ 469 | template 470 | inline void uninitialized_fill_n_with_alloc(ForwardIterator first, Diff n, const T& item, Alloc& alloc) { 471 | ForwardIterator next = first; 472 | BOOST_TRY { 473 | for (; n > 0; ++first, --n) 474 | boost::allocator_construct(alloc, boost::to_address(first), item); 475 | } BOOST_CATCH(...) { 476 | for (; next != first; ++next) 477 | boost::allocator_destroy(alloc, boost::to_address(next)); 478 | BOOST_RETHROW 479 | } 480 | BOOST_CATCH_END 481 | } 482 | 483 | } // namespace cb_details 484 | 485 | } // namespace boost 486 | 487 | #if defined(_MSC_VER) 488 | # pragma warning(pop) 489 | #endif 490 | 491 | #endif // #if !defined(BOOST_CIRCULAR_BUFFER_DETAILS_HPP) 492 | -------------------------------------------------------------------------------- /include/boost/circular_buffer_fwd.hpp: -------------------------------------------------------------------------------- 1 | // Forward declaration of the circular buffer and its adaptor. 2 | 3 | // Copyright (c) 2003-2008 Jan Gaspar 4 | 5 | // Use, modification, and distribution is subject to the Boost Software 6 | // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at 7 | // http://www.boost.org/LICENSE_1_0.txt) 8 | 9 | // See www.boost.org/libs/circular_buffer for documentation. 10 | 11 | #if !defined(BOOST_CIRCULAR_BUFFER_FWD_HPP) 12 | #define BOOST_CIRCULAR_BUFFER_FWD_HPP 13 | 14 | #if defined(_MSC_VER) 15 | #pragma once 16 | #endif 17 | 18 | #include 19 | #if !defined(BOOST_NO_STD_ALLOCATOR) 20 | #include 21 | #else 22 | #include 23 | #endif 24 | 25 | namespace boost { 26 | 27 | #if !defined(BOOST_NO_STD_ALLOCATOR) 28 | #define BOOST_CB_DEFAULT_ALLOCATOR(T) std::allocator 29 | #else 30 | #define BOOST_CB_DEFAULT_ALLOCATOR(T) BOOST_DEDUCED_TYPENAME std::vector::allocator_type 31 | #endif 32 | 33 | template 34 | class circular_buffer; 35 | 36 | template 37 | class circular_buffer_space_optimized; 38 | 39 | #undef BOOST_CB_DEFAULT_ALLOCATOR 40 | 41 | } // namespace boost 42 | 43 | #endif // #if !defined(BOOST_CIRCULAR_BUFFER_FWD_HPP) 44 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/boostorg/circular_buffer/0320ba365cf959d3f9c982eb05b78452571b35e5/index.html -------------------------------------------------------------------------------- /meta/libraries.json: -------------------------------------------------------------------------------- 1 | { 2 | "key": "circular_buffer", 3 | "name": "Circular Buffer", 4 | "authors": [ 5 | "Jan Gaspar" 6 | ], 7 | "description": "A STL compliant container also known as ring or cyclic buffer.", 8 | "category": [ 9 | "Containers" 10 | ], 11 | "maintainers": [ 12 | "Jan Gaspar " 13 | ], 14 | "cxxstd": "03" 15 | } 16 | -------------------------------------------------------------------------------- /test/Jamfile.v2: -------------------------------------------------------------------------------- 1 | # Boost circular_buffer test Jamfile. 2 | # 3 | # Copyright (c) 2003-2008 Jan Gaspar 4 | # 5 | # Distributed under the Boost Software License, Version 1.0. (See 6 | # accompanying file LICENSE_1_0.txt or copy at 7 | # http://www.boost.org/LICENSE_1_0.txt) 8 | 9 | # Added warning suppression Paul A. Bristow 25 Nov 2008 10 | 11 | # Bring in rules for testing. 12 | import testing ; 13 | 14 | project 15 | : requirements 16 | /boost/circular_buffer//boost_circular_buffer 17 | msvc:all 18 | msvc:on 19 | msvc:_SCL_SECURE_NO_WARNINGS 20 | msvc:/wd4996 # 'function': was declared deprecated 21 | msvc:/wd4244 # conversion from 'int' to 'unsigned short', possible loss of data 22 | # in date-time 23 | ; 24 | 25 | test-suite "circular_buffer" 26 | : [ run base_test.cpp : : : single : ] 27 | [ run space_optimized_test.cpp : : : single : ] 28 | [ run base_test.cpp : : : single "BOOST_CB_ENABLE_DEBUG=1" : base_test_dbg ] 29 | [ run space_optimized_test.cpp : : : single "BOOST_CB_ENABLE_DEBUG=1" : space_optimized_test_dbg ] 30 | [ run soft_iterator_invalidation.cpp : : : single : ] 31 | [ run constant_erase_test.cpp : : : single : ] 32 | [ compile bounded_buffer_comparison.cpp : multi /boost/thread//boost_thread /boost/timer//boost_timer : ] 33 | ; 34 | -------------------------------------------------------------------------------- /test/base_test.cpp: -------------------------------------------------------------------------------- 1 | // Test of the base circular buffer container. 2 | 3 | // Copyright (c) 2003-2008 Jan Gaspar 4 | // Copyright (c) 2013 Antony Polukhin 5 | 6 | // Use, modification, and distribution is subject to the Boost Software 7 | // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at 8 | // http://www.boost.org/LICENSE_1_0.txt) 9 | 10 | #include "test.hpp" 11 | 12 | #define CB_CONTAINER circular_buffer 13 | 14 | #include "common.ipp" 15 | 16 | void iterator_constructor_and_assign_test() { 17 | 18 | circular_buffer cb(4, 3); 19 | circular_buffer::iterator it = cb.begin(); 20 | circular_buffer::const_iterator cit2 = cb.cbegin(); 21 | circular_buffer::iterator itCopy; 22 | itCopy = it; 23 | it = it; 24 | circular_buffer::const_iterator cit; 25 | cit = it; 26 | circular_buffer::const_iterator end1 = cb.end(); 27 | circular_buffer::const_iterator end2 = end1; 28 | circular_buffer::const_iterator end3 = cb.cend(); 29 | 30 | BOOST_TEST(itCopy == it); 31 | BOOST_TEST(cit == it); 32 | BOOST_TEST(end1 == end2); 33 | BOOST_TEST(it != end1); 34 | BOOST_TEST(cit != end2); 35 | BOOST_TEST(cit2 == it); 36 | BOOST_TEST(end3 == end1); 37 | } 38 | 39 | void iterator_reference_test() { 40 | 41 | circular_buffer cb(3, Dummy()); 42 | circular_buffer::iterator it = cb.begin(); 43 | circular_buffer::const_iterator cit = cb.begin() + 1; 44 | 45 | BOOST_TEST((*it).m_n == Dummy::eVar); 46 | BOOST_TEST((*it).fnc() == Dummy::eFnc); 47 | BOOST_TEST((*cit).const_fnc() == Dummy::eConst); 48 | BOOST_TEST((*it).virtual_fnc() == Dummy::eVirtual); 49 | BOOST_TEST(it->m_n == Dummy::eVar); 50 | BOOST_TEST(it->fnc() == Dummy::eFnc); 51 | BOOST_TEST(cit->const_fnc() == Dummy::eConst); 52 | BOOST_TEST(it->virtual_fnc() == Dummy::eVirtual); 53 | } 54 | 55 | void iterator_difference_test() { 56 | 57 | circular_buffer cb(5, 1); 58 | cb.push_back(2); 59 | circular_buffer::iterator it1 = cb.begin() + 2; 60 | circular_buffer::iterator it2 = cb.begin() + 3; 61 | circular_buffer::const_iterator begin = cb.begin(); 62 | circular_buffer::iterator end = cb.end(); 63 | 64 | BOOST_TEST(begin - begin == 0); 65 | BOOST_TEST(end - cb.begin() == 5); 66 | BOOST_TEST(end - end == 0); 67 | BOOST_TEST(begin - cb.end() == -5); 68 | BOOST_TEST(it1 - cb.begin() == 2); 69 | BOOST_TEST(it1 - begin == 2); 70 | BOOST_TEST(end - it1 == 3); 71 | BOOST_TEST(it2 - it1 == 1); 72 | BOOST_TEST(it1 - it2 == -1); 73 | BOOST_TEST(it2 - it2 == 0); 74 | } 75 | 76 | void iterator_increment_test() { 77 | 78 | circular_buffer cb(10, 1); 79 | cb.push_back(2); 80 | circular_buffer::iterator it1 = cb.begin(); 81 | circular_buffer::iterator it2 = cb.begin() + 5; 82 | circular_buffer::iterator it3 = cb.begin() + 9; 83 | it1++; 84 | it2++; 85 | ++it3; 86 | 87 | BOOST_TEST(it1 == cb.begin() + 1); 88 | BOOST_TEST(it2 == cb.begin() + 6); 89 | BOOST_TEST(it3 == cb.end()); 90 | } 91 | 92 | void iterator_decrement_test() { 93 | 94 | circular_buffer cb(10, 1); 95 | cb.push_back(2); 96 | circular_buffer::iterator it1= cb.end(); 97 | circular_buffer::iterator it2= cb.end() - 5; 98 | circular_buffer::iterator it3= cb.end() - 9; 99 | --it1; 100 | it2--; 101 | --it3; 102 | 103 | BOOST_TEST(it1 == cb.end() - 1); 104 | BOOST_TEST(it2 == cb.end() - 6); 105 | BOOST_TEST(it3 == cb.begin()); 106 | } 107 | 108 | void iterator_addition_test() { 109 | 110 | circular_buffer cb(10, 1); 111 | cb.push_back(2); 112 | cb.push_back(2); 113 | circular_buffer::iterator it1 = cb.begin() + 2; 114 | circular_buffer::iterator it2 = cb.end(); 115 | circular_buffer::iterator it3 = cb.begin() + 5; 116 | circular_buffer::iterator it4 = cb.begin() + 9; 117 | it1 += 3; 118 | it2 += 0; 119 | it3 += 5; 120 | it4 += -2; 121 | 122 | BOOST_TEST(it1 == 5 + cb.begin()); 123 | BOOST_TEST(it2 == cb.end()); 124 | BOOST_TEST(it3 == cb.end()); 125 | BOOST_TEST(it4 + 3 == cb.end()); 126 | BOOST_TEST((-3) + it4 == cb.begin() + 4); 127 | BOOST_TEST(cb.begin() + 0 == cb.begin()); 128 | } 129 | 130 | void iterator_subtraction_test() { 131 | 132 | circular_buffer cb(10, 1); 133 | cb.push_back(2); 134 | cb.push_back(2); 135 | cb.push_back(2); 136 | circular_buffer::iterator it1 = cb.begin(); 137 | circular_buffer::iterator it2 = cb.end(); 138 | circular_buffer::iterator it3 = cb.end() - 5; 139 | circular_buffer::iterator it4 = cb.begin() + 7; 140 | it1 -= -2; 141 | it2 -= 0; 142 | it3 -= 5; 143 | 144 | BOOST_TEST(it1 == cb.begin() + 2); 145 | BOOST_TEST(it2 == cb.end()); 146 | BOOST_TEST(it3 == cb.begin()); 147 | BOOST_TEST(it4 - 7 == cb.begin()); 148 | BOOST_TEST(it4 - (-3) == cb.end()); 149 | BOOST_TEST(cb.begin() - 0 == cb.begin()); 150 | } 151 | 152 | void iterator_element_access_test() { 153 | 154 | circular_buffer cb(10); 155 | cb.push_back(1); 156 | cb.push_back(2); 157 | cb.push_back(3); 158 | cb.push_back(4); 159 | cb.push_back(5); 160 | cb.push_back(6); 161 | circular_buffer::iterator it = cb.begin() + 1; 162 | 163 | BOOST_TEST(it[0] == 2); 164 | BOOST_TEST(it[-1] == 1); 165 | BOOST_TEST(it[2] == 4); 166 | } 167 | 168 | void iterator_comparison_test() { 169 | 170 | circular_buffer cb(5, 1); 171 | cb.push_back(2); 172 | circular_buffer::iterator it = cb.begin() + 2; 173 | circular_buffer::const_iterator begin = cb.begin(); 174 | circular_buffer::iterator end = cb.end(); 175 | 176 | BOOST_TEST(begin == begin); 177 | BOOST_TEST(end > cb.begin()); 178 | BOOST_TEST(begin < end); 179 | BOOST_TEST(end > begin); 180 | BOOST_TEST(end == end); 181 | BOOST_TEST(begin < cb.end()); 182 | BOOST_TEST(!(begin + 1 > cb.end())); 183 | BOOST_TEST(it > cb.begin()); 184 | BOOST_TEST(end > it); 185 | BOOST_TEST(begin >= begin); 186 | BOOST_TEST(end >= cb.begin()); 187 | BOOST_TEST(end <= end); 188 | BOOST_TEST(begin <= cb.end()); 189 | BOOST_TEST(it >= cb.begin()); 190 | BOOST_TEST(end >= it); 191 | BOOST_TEST(!(begin + 4 < begin + 4)); 192 | BOOST_TEST(begin + 4 < begin + 5); 193 | BOOST_TEST(!(begin + 5 < begin + 4)); 194 | BOOST_TEST(it < end - 1); 195 | BOOST_TEST(!(end - 1 < it)); 196 | } 197 | 198 | void iterator_invalidation_test() { 199 | 200 | #if BOOST_CB_ENABLE_DEBUG 201 | 202 | circular_buffer::iterator it1; 203 | circular_buffer::const_iterator it2; 204 | circular_buffer::iterator it3; 205 | circular_buffer::const_iterator it4; 206 | circular_buffer::const_iterator it5; 207 | circular_buffer::const_iterator it6; 208 | 209 | BOOST_TEST(it1.is_valid(0)); 210 | BOOST_TEST(it2.is_valid(0)); 211 | BOOST_TEST(it3.is_valid(0)); 212 | BOOST_TEST(it4.is_valid(0)); 213 | BOOST_TEST(it5.is_valid(0)); 214 | BOOST_TEST(it6.is_valid(0)); 215 | 216 | { 217 | circular_buffer cb(5, 0); 218 | const circular_buffer ccb(5, 0); 219 | 220 | it1 = cb.begin(); 221 | it2 = ccb.begin(); 222 | it3 = cb.end(); 223 | it4 = it1; 224 | it5 = it2; 225 | it6 = it1; 226 | 227 | BOOST_TEST(it1.is_valid(&cb)); 228 | BOOST_TEST(it2.is_valid(&ccb)); 229 | BOOST_TEST(it3.is_valid(&cb)); 230 | BOOST_TEST(it4.is_valid(&cb)); 231 | BOOST_TEST(it5.is_valid(&ccb)); 232 | BOOST_TEST(it6.is_valid(&cb)); 233 | } 234 | 235 | BOOST_TEST(it1.is_valid(0)); 236 | BOOST_TEST(it2.is_valid(0)); 237 | BOOST_TEST(it3.is_valid(0)); 238 | BOOST_TEST(it4.is_valid(0)); 239 | BOOST_TEST(it5.is_valid(0)); 240 | BOOST_TEST(it6.is_valid(0)); 241 | 242 | circular_buffer cb(10, 0); 243 | it1 = cb.end(); 244 | cb.clear(); 245 | BOOST_TEST(it1.is_valid(&cb)); 246 | cb.push_back(1); 247 | cb.push_back(2); 248 | cb.push_back(3); 249 | int i = 0; 250 | for (it2 = cb.begin(); it2 != it1; it2++, i++); 251 | BOOST_TEST(i == 3); 252 | 253 | circular_buffer cb1(10, 0); 254 | circular_buffer cb2(20, 0); 255 | it1 = cb1.end(); 256 | it2 = cb2.begin(); 257 | BOOST_TEST(it1.is_valid(&cb1)); 258 | BOOST_TEST(it2.is_valid(&cb2)); 259 | 260 | cb1.swap(cb2); 261 | BOOST_TEST(!it1.is_valid(&cb1)); 262 | BOOST_TEST(!it2.is_valid(&cb2)); 263 | 264 | it1 = cb1.begin() + 3; 265 | it2 = cb1.begin(); 266 | cb1.push_back(1); 267 | BOOST_TEST(it1.is_valid(&cb1)); 268 | BOOST_TEST(!it2.is_valid(&cb1)); 269 | BOOST_TEST(*it2.m_it == 1); 270 | 271 | circular_buffer cb3(5); 272 | cb3.push_back(1); 273 | cb3.push_back(2); 274 | cb3.push_back(3); 275 | cb3.push_back(4); 276 | cb3.push_back(5); 277 | it1 = cb3.begin() + 2; 278 | it2 = cb3.begin(); 279 | cb3.insert(cb3.begin() + 3, 6); 280 | BOOST_TEST(it1.is_valid(&cb3)); 281 | BOOST_TEST(!it2.is_valid(&cb3)); 282 | BOOST_TEST(*it2.m_it == 5); 283 | 284 | it1 = cb3.begin() + 3; 285 | it2 = cb3.end() - 1; 286 | cb3.push_front(7); 287 | BOOST_TEST(it1.is_valid(&cb3)); 288 | BOOST_TEST(!it2.is_valid(&cb3)); 289 | BOOST_TEST(*it2.m_it == 7); 290 | 291 | circular_buffer cb4(5); 292 | cb4.push_back(1); 293 | cb4.push_back(2); 294 | cb4.push_back(3); 295 | cb4.push_back(4); 296 | cb4.push_back(5); 297 | it1 = cb4.begin() + 3; 298 | it2 = cb4.begin(); 299 | cb4.rinsert(cb4.begin() + 2, 6); 300 | BOOST_TEST(it1.is_valid(&cb4)); 301 | BOOST_TEST(!it2.is_valid(&cb4)); 302 | BOOST_TEST(*it2.m_it == 2); 303 | 304 | it1 = cb1.begin() + 5; 305 | it2 = cb1.end() - 1; 306 | cb1.pop_back(); 307 | BOOST_TEST(it1.is_valid(&cb1)); 308 | BOOST_TEST(!it2.is_valid(&cb1)); 309 | 310 | it1 = cb1.begin() + 5; 311 | it2 = cb1.begin(); 312 | cb1.pop_front(); 313 | BOOST_TEST(it1.is_valid(&cb1)); 314 | BOOST_TEST(!it2.is_valid(&cb1)); 315 | 316 | circular_buffer cb5(20, 0); 317 | it1 = cb5.begin() + 5; 318 | it2 = it3 = cb5.begin() + 15; 319 | cb5.erase(cb5.begin() + 10); 320 | BOOST_TEST(it1.is_valid(&cb5)); 321 | BOOST_TEST(!it2.is_valid(&cb5)); 322 | BOOST_TEST(!it3.is_valid(&cb5)); 323 | 324 | it1 = cb5.begin() + 1; 325 | it2 = it3 = cb5.begin() + 8; 326 | cb5.erase(cb5.begin() + 3, cb5.begin() + 7); 327 | BOOST_TEST(it1.is_valid(&cb5)); 328 | BOOST_TEST(!it2.is_valid(&cb5)); 329 | BOOST_TEST(!it3.is_valid(&cb5)); 330 | 331 | circular_buffer cb6(20, 0); 332 | it4 = it1 = cb6.begin() + 5; 333 | it2 = cb6.begin() + 15; 334 | cb6.rerase(cb6.begin() + 10); 335 | BOOST_TEST(!it1.is_valid(&cb6)); 336 | BOOST_TEST(!it4.is_valid(&cb6)); 337 | BOOST_TEST(it2.is_valid(&cb6)); 338 | 339 | it4 = it1 = cb6.begin() + 1; 340 | it2 = cb6.begin() + 8; 341 | cb6.rerase(cb6.begin() + 3, cb6.begin() + 7); 342 | BOOST_TEST(!it1.is_valid(&cb6)); 343 | BOOST_TEST(!it4.is_valid(&cb6)); 344 | BOOST_TEST(it2.is_valid(&cb6)); 345 | 346 | circular_buffer cb7(10, 1); 347 | cb7.push_back(2); 348 | cb7.push_back(3); 349 | cb7.push_back(4); 350 | it1 = cb7.end(); 351 | it2 = cb7.begin(); 352 | it3 = cb7.begin() + 6; 353 | cb7.linearize(); 354 | BOOST_TEST(it1.is_valid(&cb7)); 355 | BOOST_TEST(!it2.is_valid(&cb7)); 356 | BOOST_TEST(!it3.is_valid(&cb7)); 357 | it1 = cb7.end(); 358 | it2 = cb7.begin(); 359 | it3 = cb7.begin() + 6; 360 | cb7.linearize(); 361 | BOOST_TEST(it1.is_valid(&cb7)); 362 | BOOST_TEST(it2.is_valid(&cb7)); 363 | BOOST_TEST(it3.is_valid(&cb7)); 364 | 365 | cb7.push_back(5); 366 | cb7.push_back(6); 367 | it1 = cb7.end(); 368 | it2 = cb7.begin(); 369 | it3 = cb7.begin() + 6; 370 | cb7.set_capacity(10); 371 | BOOST_TEST(it1.is_valid(&cb7)); 372 | BOOST_TEST(it2.is_valid(&cb7)); 373 | BOOST_TEST(it3.is_valid(&cb7)); 374 | cb7.set_capacity(20); 375 | BOOST_TEST(it1.is_valid(&cb7)); 376 | BOOST_TEST(!it2.is_valid(&cb7)); 377 | BOOST_TEST(!it3.is_valid(&cb7)); 378 | cb7.push_back(7); 379 | it1 = cb7.end(); 380 | it2 = cb7.begin(); 381 | it3 = cb7.begin() + 6; 382 | cb7.set_capacity(10); 383 | BOOST_TEST(it1.is_valid(&cb7)); 384 | BOOST_TEST(!it2.is_valid(&cb7)); 385 | BOOST_TEST(!it3.is_valid(&cb7)); 386 | 387 | cb7.push_back(8); 388 | cb7.push_back(9); 389 | it1 = cb7.end(); 390 | it2 = cb7.begin(); 391 | it3 = cb7.begin() + 6; 392 | cb7.rset_capacity(10); 393 | BOOST_TEST(it1.is_valid(&cb7)); 394 | BOOST_TEST(it2.is_valid(&cb7)); 395 | BOOST_TEST(it3.is_valid(&cb7)); 396 | cb7.rset_capacity(20); 397 | BOOST_TEST(it1.is_valid(&cb7)); 398 | BOOST_TEST(!it2.is_valid(&cb7)); 399 | BOOST_TEST(!it3.is_valid(&cb7)); 400 | cb7.push_back(10); 401 | it1 = cb7.end(); 402 | it2 = cb7.begin(); 403 | it3 = cb7.begin() + 6; 404 | cb7.rset_capacity(10); 405 | BOOST_TEST(it1.is_valid(&cb7)); 406 | BOOST_TEST(!it2.is_valid(&cb7)); 407 | BOOST_TEST(!it3.is_valid(&cb7)); 408 | 409 | circular_buffer cb8(10, 1); 410 | cb8.push_back(2); 411 | cb8.push_back(3); 412 | it1 = cb8.end(); 413 | it2 = cb8.begin(); 414 | it3 = cb8.begin() + 6; 415 | cb8.resize(10); 416 | BOOST_TEST(it1.is_valid(&cb8)); 417 | BOOST_TEST(it2.is_valid(&cb8)); 418 | BOOST_TEST(it3.is_valid(&cb8)); 419 | cb8.resize(20); 420 | BOOST_TEST(it1.is_valid(&cb8)); 421 | BOOST_TEST(!it2.is_valid(&cb8)); 422 | BOOST_TEST(!it3.is_valid(&cb8)); 423 | cb8.push_back(4); 424 | it1 = cb8.end(); 425 | it2 = cb8.begin(); 426 | it3 = cb8.begin() + 6; 427 | it4 = cb8.begin() + 12; 428 | cb8.resize(10); 429 | BOOST_TEST(it1.is_valid(&cb8)); 430 | BOOST_TEST(it2.is_valid(&cb8)); 431 | BOOST_TEST(it3.is_valid(&cb8)); 432 | BOOST_TEST(!it4.is_valid(&cb8)); 433 | 434 | cb8.set_capacity(10); 435 | cb8.push_back(5); 436 | cb8.push_back(6); 437 | it1 = cb8.end(); 438 | it2 = cb8.begin(); 439 | it3 = cb8.begin() + 6; 440 | cb8.rresize(10); 441 | BOOST_TEST(it1.is_valid(&cb8)); 442 | BOOST_TEST(it2.is_valid(&cb8)); 443 | BOOST_TEST(it3.is_valid(&cb8)); 444 | cb8.rresize(20); 445 | BOOST_TEST(it1.is_valid(&cb8)); 446 | BOOST_TEST(!it2.is_valid(&cb8)); 447 | BOOST_TEST(!it3.is_valid(&cb8)); 448 | cb8.push_back(7); 449 | it1 = cb8.end(); 450 | it2 = cb8.begin(); 451 | it3 = cb8.begin() + 6; 452 | it4 = cb8.begin() + 12; 453 | cb8.rresize(10); 454 | BOOST_TEST(it1.is_valid(&cb8)); 455 | BOOST_TEST(!it2.is_valid(&cb8)); 456 | BOOST_TEST(!it3.is_valid(&cb8)); 457 | BOOST_TEST(it4.is_valid(&cb8)); 458 | 459 | circular_buffer cb9(15, 1); 460 | it1 = cb9.end(); 461 | it2 = cb9.begin(); 462 | it3 = cb9.begin() + 6; 463 | it4 = cb9.begin() + 12; 464 | cb9 = cb8; 465 | BOOST_TEST(it1.is_valid(&cb9)); 466 | BOOST_TEST(!it2.is_valid(&cb9)); 467 | BOOST_TEST(!it3.is_valid(&cb9)); 468 | BOOST_TEST(!it4.is_valid(&cb9)); 469 | 470 | circular_buffer cb10(10, 1); 471 | it1 = cb10.end(); 472 | it2 = cb10.begin(); 473 | it3 = cb10.begin() + 3; 474 | it4 = cb10.begin() + 7; 475 | cb10.assign(5, 2); 476 | BOOST_TEST(it1.is_valid(&cb10)); 477 | BOOST_TEST(!it2.is_valid(&cb10)); 478 | BOOST_TEST(!it3.is_valid(&cb10)); 479 | BOOST_TEST(!it4.is_valid(&cb10)); 480 | 481 | circular_buffer cb11(10, 1); 482 | it1 = cb11.end(); 483 | it2 = cb11.begin(); 484 | it3 = cb11.begin() + 3; 485 | it4 = cb11.begin() + 7; 486 | cb11.assign(15, 5, 2); 487 | BOOST_TEST(it1.is_valid(&cb11)); 488 | BOOST_TEST(!it2.is_valid(&cb11)); 489 | BOOST_TEST(!it3.is_valid(&cb11)); 490 | BOOST_TEST(!it4.is_valid(&cb11)); 491 | 492 | circular_buffer cb12(10, 1); 493 | it1 = cb12.end(); 494 | it2 = cb12.begin(); 495 | it3 = cb12.begin() + 3; 496 | it4 = cb12.begin() + 7; 497 | cb12.assign(cb11.begin(), cb11.end()); 498 | BOOST_TEST(it1.is_valid(&cb12)); 499 | BOOST_TEST(!it2.is_valid(&cb12)); 500 | BOOST_TEST(!it3.is_valid(&cb12)); 501 | BOOST_TEST(!it4.is_valid(&cb12)); 502 | 503 | circular_buffer cb13(10, 1); 504 | it1 = cb13.end(); 505 | it2 = cb13.begin(); 506 | it3 = cb13.begin() + 3; 507 | it4 = cb13.begin() + 7; 508 | cb13.assign(15, cb11.begin(), cb11.end()); 509 | BOOST_TEST(it1.is_valid(&cb13)); 510 | BOOST_TEST(!it2.is_valid(&cb13)); 511 | BOOST_TEST(!it3.is_valid(&cb13)); 512 | BOOST_TEST(!it4.is_valid(&cb13)); 513 | 514 | circular_buffer cb14(10); 515 | cb14.push_back(1); 516 | cb14.push_back(2); 517 | cb14.push_back(3); 518 | cb14.push_back(4); 519 | cb14.push_back(5); 520 | cb14.push_back(6); 521 | cb14.push_back(7); 522 | it1 = cb14.end(); 523 | it2 = cb14.begin() + 2; 524 | it3 = cb14.begin() + 1; 525 | it4 = cb14.begin() + 5; 526 | cb14.rotate(it2); 527 | BOOST_TEST(it1.is_valid(&cb14)); 528 | BOOST_TEST(it2.is_valid(&cb14)); 529 | BOOST_TEST(!it3.is_valid(&cb14)); 530 | BOOST_TEST(it4.is_valid(&cb14)); 531 | 532 | circular_buffer cb15(7); 533 | cb15.push_back(1); 534 | cb15.push_back(2); 535 | cb15.push_back(3); 536 | cb15.push_back(4); 537 | cb15.push_back(5); 538 | cb15.push_back(6); 539 | cb15.push_back(7); 540 | cb15.push_back(8); 541 | cb15.push_back(9); 542 | it1 = cb15.end(); 543 | it2 = cb15.begin() + 2; 544 | it3 = cb15.begin() + 1; 545 | it4 = cb15.begin() + 5; 546 | cb15.rotate(it3); 547 | BOOST_TEST(it1.is_valid(&cb15)); 548 | BOOST_TEST(it2.is_valid(&cb15)); 549 | BOOST_TEST(it3.is_valid(&cb15)); 550 | BOOST_TEST(it4.is_valid(&cb15)); 551 | 552 | circular_buffer cb16(10); 553 | cb16.push_back(1); 554 | cb16.push_back(2); 555 | cb16.push_back(3); 556 | cb16.push_back(4); 557 | cb16.push_back(5); 558 | cb16.push_back(6); 559 | cb16.push_back(7); 560 | it1 = cb16.end(); 561 | it2 = cb16.begin() + 6; 562 | it3 = cb16.begin(); 563 | it4 = cb16.begin() + 5; 564 | cb16.rotate(it4); 565 | BOOST_TEST(it1.is_valid(&cb16)); 566 | BOOST_TEST(!it2.is_valid(&cb16)); 567 | BOOST_TEST(it3.is_valid(&cb16)); 568 | BOOST_TEST(!it4.is_valid(&cb16)); 569 | 570 | #endif // #if BOOST_CB_ENABLE_DEBUG 571 | } 572 | 573 | // basic exception safety test (it is useful to use any memory-leak detection tool) 574 | void exception_safety_test() { 575 | 576 | #if !defined(BOOST_NO_EXCEPTIONS) 577 | 578 | circular_buffer cb1(3, 5); 579 | MyInteger::set_exception_trigger(3); 580 | BOOST_TEST_THROWS(cb1.set_capacity(5), std::exception); 581 | BOOST_TEST(cb1.capacity() == 3); 582 | MyInteger::set_exception_trigger(3); 583 | BOOST_TEST_THROWS(cb1.rset_capacity(5), std::exception); 584 | BOOST_TEST(cb1.capacity() == 3); 585 | generic_test(cb1); 586 | 587 | MyInteger::set_exception_trigger(3); 588 | BOOST_TEST_THROWS(circular_buffer cb2(5, 10), std::exception); 589 | 590 | circular_buffer cb3(5, 10); 591 | MyInteger::set_exception_trigger(3); 592 | BOOST_TEST_THROWS(circular_buffer cb4(cb3), std::exception); 593 | 594 | vector v(5, MyInteger(10)); 595 | MyInteger::set_exception_trigger(3); 596 | BOOST_TEST_THROWS(circular_buffer cb5(8, v.begin(), v.end()), std::exception); 597 | 598 | circular_buffer cb6(5, 10); 599 | circular_buffer cb7(8, 3); 600 | MyInteger::set_exception_trigger(3); 601 | BOOST_TEST_THROWS(cb7 = cb6, std::exception); 602 | BOOST_TEST(cb7.size() == 8); 603 | BOOST_TEST(cb7.capacity() == 8); 604 | BOOST_TEST(cb7[0] == 3); 605 | BOOST_TEST(cb7[7] == 3); 606 | generic_test(cb7); 607 | 608 | circular_buffer cb8(5, 10); 609 | MyInteger::set_exception_trigger(2); 610 | BOOST_TEST_THROWS(cb8.push_front(1), std::exception); 611 | 612 | circular_buffer cb9(5); 613 | cb9.push_back(1); 614 | cb9.push_back(2); 615 | cb9.push_back(3); 616 | MyInteger::set_exception_trigger(3); 617 | BOOST_TEST_THROWS(cb9.insert(cb9.begin() + 1, 4), std::exception); 618 | 619 | circular_buffer cb10(5); 620 | cb10.push_back(1); 621 | cb10.push_back(2); 622 | cb10.push_back(3); 623 | MyInteger::set_exception_trigger(3); 624 | BOOST_TEST_THROWS(cb10.rinsert(cb10.begin() + 1, 4), std::exception); 625 | 626 | circular_buffer cb11(5); 627 | cb11.push_back(1); 628 | cb11.push_back(2); 629 | MyInteger::set_exception_trigger(2); 630 | BOOST_TEST_THROWS(cb11.rinsert(cb11.begin(), 1), std::exception); 631 | 632 | circular_buffer cb12(5, 1); 633 | MyInteger::set_exception_trigger(3); 634 | BOOST_TEST_THROWS(cb12.assign(4, 2), std::exception); 635 | 636 | circular_buffer cb13(5, 1); 637 | MyInteger::set_exception_trigger(3); 638 | BOOST_TEST_THROWS(cb13.assign(6, 2), std::exception); 639 | 640 | circular_buffer cb14(5); 641 | cb14.push_back(1); 642 | cb14.push_back(2); 643 | MyInteger::set_exception_trigger(3); 644 | BOOST_TEST_THROWS(cb14.insert(cb14.begin(), 10, 3), std::exception); 645 | 646 | circular_buffer cb15(5); 647 | cb15.push_back(1); 648 | cb15.push_back(2); 649 | MyInteger::set_exception_trigger(3); 650 | BOOST_TEST_THROWS(cb15.insert(cb15.end(), 10, 3), std::exception); 651 | 652 | circular_buffer cb16(5); 653 | cb16.push_back(1); 654 | cb16.push_back(2); 655 | MyInteger::set_exception_trigger(3); 656 | BOOST_TEST_THROWS(cb16.rinsert(cb16.begin(), 10, 3), std::exception); 657 | 658 | circular_buffer cb17(5); 659 | cb17.push_back(1); 660 | cb17.push_back(2); 661 | MyInteger::set_exception_trigger(3); 662 | BOOST_TEST_THROWS(cb17.rinsert(cb17.end(), 10, 3), std::exception); 663 | 664 | circular_buffer cb18(5, 0); 665 | cb18.push_back(1); 666 | cb18.push_back(2); 667 | cb18.pop_front(); 668 | MyInteger::set_exception_trigger(4); 669 | BOOST_TEST_THROWS(cb18.linearize(), std::exception); 670 | 671 | circular_buffer cb19(5, 0); 672 | cb19.push_back(1); 673 | cb19.push_back(2); 674 | MyInteger::set_exception_trigger(5); 675 | BOOST_TEST_THROWS(cb19.linearize(), std::exception); 676 | 677 | circular_buffer cb20(5, 0); 678 | cb20.push_back(1); 679 | cb20.push_back(2); 680 | MyInteger::set_exception_trigger(6); 681 | BOOST_TEST_THROWS(cb20.linearize(), std::exception); 682 | 683 | circular_buffer cb21(5); 684 | cb21.push_back(1); 685 | cb21.push_back(2); 686 | cb21.push_back(3); 687 | MyInteger::set_exception_trigger(2); 688 | BOOST_TEST_THROWS(cb21.insert(cb21.begin() + 1, 4), std::exception); 689 | 690 | circular_buffer cb22(5); 691 | cb22.push_back(1); 692 | cb22.push_back(2); 693 | cb22.push_back(3); 694 | MyInteger::set_exception_trigger(2); 695 | BOOST_TEST_THROWS(cb22.insert(cb22.end(), 4), std::exception); 696 | 697 | circular_buffer cb23(5, 0); 698 | MyInteger::set_exception_trigger(2); 699 | BOOST_TEST_THROWS(cb23.insert(cb23.begin() + 1, 4), std::exception); 700 | 701 | circular_buffer cb24(5); 702 | cb24.push_back(1); 703 | cb24.push_back(2); 704 | cb24.push_back(3); 705 | MyInteger::set_exception_trigger(2); 706 | BOOST_TEST_THROWS(cb24.rinsert(cb24.begin() + 1, 4), std::exception); 707 | 708 | circular_buffer cb25(5, 0); 709 | MyInteger::set_exception_trigger(2); 710 | BOOST_TEST_THROWS(cb25.rinsert(cb25.begin() + 3, 4), std::exception); 711 | 712 | circular_buffer cb26(5); 713 | cb26.push_back(1); 714 | cb26.push_back(2); 715 | MyInteger::set_exception_trigger(5); 716 | BOOST_TEST_THROWS(cb26.insert(cb26.begin(), 10, 3), std::exception); 717 | 718 | circular_buffer cb27(5); 719 | cb27.push_back(1); 720 | cb27.push_back(2); 721 | MyInteger::set_exception_trigger(5); 722 | BOOST_TEST_THROWS(cb27.insert(cb27.end(), 10, 3), std::exception); 723 | 724 | circular_buffer cb28(5); 725 | cb28.push_back(1); 726 | cb28.push_back(2); 727 | MyInteger::set_exception_trigger(5); 728 | BOOST_TEST_THROWS(cb28.rinsert(cb28.begin(), 10, 3), std::exception); 729 | 730 | circular_buffer cb29(5); 731 | cb29.push_back(1); 732 | cb29.push_back(2); 733 | MyInteger::set_exception_trigger(5); 734 | BOOST_TEST_THROWS(cb29.rinsert(cb29.end(), 10, 3), std::exception); 735 | 736 | circular_buffer cb30(10); 737 | cb30.push_back(1); 738 | cb30.push_back(2); 739 | cb30.push_back(3); 740 | MyInteger::set_exception_trigger(2); 741 | BOOST_TEST_THROWS(cb30.rinsert(cb30.begin(), 10, 3), std::exception); 742 | 743 | #endif // #if !defined(BOOST_NO_EXCEPTIONS) 744 | } 745 | 746 | 747 | void move_container_values_except() { 748 | move_container_values_impl(); 749 | } 750 | 751 | template 752 | void move_container_values_resetting_impl() { 753 | typedef T noncopyable_movable_test_t; 754 | CB_CONTAINER cb1(1); 755 | noncopyable_movable_test_t var; 756 | cb1.push_back(); 757 | 758 | cb1.push_back(boost::move(var)); 759 | BOOST_TEST(!cb1.back().is_moved()); 760 | BOOST_TEST(var.is_moved()); 761 | BOOST_TEST(cb1.size() == 1); 762 | var = boost::move(cb1.back()); 763 | BOOST_TEST(cb1.back().is_moved()); 764 | 765 | cb1.push_front(boost::move(var)); 766 | BOOST_TEST(!cb1.front().is_moved()); 767 | BOOST_TEST(var.is_moved()); 768 | BOOST_TEST(cb1.size() == 1); 769 | var = boost::move(cb1.back()); 770 | BOOST_TEST(cb1.back().is_moved()); 771 | 772 | cb1.push_back(); 773 | BOOST_TEST(!cb1.back().is_moved()); 774 | BOOST_TEST(cb1.size() == 1); 775 | var = boost::move(cb1.back()); 776 | BOOST_TEST(cb1.back().is_moved()); 777 | 778 | cb1.push_front(); 779 | BOOST_TEST(!cb1.front().is_moved()); 780 | BOOST_TEST(cb1.size() == 1); 781 | var = boost::move(cb1.back()); 782 | BOOST_TEST(cb1.back().is_moved()); 783 | 784 | 785 | cb1.insert(cb1.begin()); 786 | // If the circular_buffer is full and the pos points to begin(), 787 | // then the item will not be inserted. 788 | BOOST_TEST(cb1.front().is_moved()); 789 | BOOST_TEST(cb1.size() == 1); 790 | var = boost::move(cb1.back()); 791 | BOOST_TEST(cb1.back().is_moved()); 792 | 793 | cb1.insert(cb1.begin(), boost::move(var)); 794 | // If the circular_buffer is full and the pos points to begin(), 795 | // then the item will not be inserted. 796 | BOOST_TEST(cb1.front().is_moved()); 797 | BOOST_TEST(cb1.size() == 1); 798 | var = boost::move(cb1.back()); 799 | BOOST_TEST(cb1.back().is_moved()); 800 | 801 | cb1.rinsert(cb1.begin()); 802 | BOOST_TEST(!cb1.back().is_moved()); 803 | BOOST_TEST(cb1.size() == 1); 804 | var = boost::move(cb1.back()); 805 | BOOST_TEST(cb1.back().is_moved()); 806 | 807 | var.reinit(); 808 | cb1.rinsert(cb1.begin(), boost::move(var)); 809 | BOOST_TEST(!cb1.back().is_moved()); 810 | BOOST_TEST(cb1.size() == 1); 811 | var = boost::move(cb1.back()); 812 | BOOST_TEST(cb1.back().is_moved()); 813 | 814 | cb1.rinsert(cb1.end()); 815 | BOOST_TEST(cb1.back().is_moved()); 816 | BOOST_TEST(cb1.size() == 1); 817 | var = boost::move(cb1.back()); 818 | BOOST_TEST(cb1.back().is_moved()); 819 | 820 | var.reinit(); 821 | cb1.rinsert(cb1.end(), boost::move(var)); 822 | BOOST_TEST(cb1.back().is_moved()); 823 | BOOST_TEST(cb1.size() == 1); 824 | var = boost::move(cb1.back()); 825 | BOOST_TEST(cb1.back().is_moved()); 826 | cb1.push_back(); 827 | BOOST_TEST(!cb1[0].is_moved()); 828 | 829 | const int val = cb1[0].value(); 830 | cb1.linearize(); 831 | BOOST_TEST(!cb1[0].is_moved()); 832 | BOOST_TEST(cb1[0].value() == val); 833 | 834 | cb1.rotate(cb1.begin()); 835 | BOOST_TEST(!cb1[0].is_moved()); 836 | BOOST_TEST(cb1[0].value() == val); 837 | } 838 | 839 | void move_container_values_resetting_except() { 840 | move_container_values_resetting_impl(); 841 | } 842 | 843 | void move_container_values_resetting_noexcept() { 844 | move_container_values_resetting_impl(); 845 | } 846 | 847 | // test main 848 | int main() 849 | { 850 | run_common_tests(); 851 | iterator_constructor_and_assign_test(); 852 | iterator_reference_test(); 853 | iterator_difference_test(); 854 | iterator_increment_test(); 855 | iterator_decrement_test(); 856 | iterator_addition_test(); 857 | iterator_subtraction_test(); 858 | iterator_element_access_test(); 859 | iterator_comparison_test(); 860 | iterator_invalidation_test(); 861 | exception_safety_test(); 862 | move_container_values_except(); 863 | move_container_values_resetting_except(); 864 | move_container_values_resetting_noexcept(); 865 | return boost::report_errors(); 866 | } 867 | -------------------------------------------------------------------------------- /test/bounded_buffer_comparison.cpp: -------------------------------------------------------------------------------- 1 | // Comparison of bounded buffers based on different containers. 2 | 3 | // Copyright (c) 2003-2008 Jan Gaspar 4 | // Copyright 2013 Paul A. Bristow. Added some Quickbook snippet markers. 5 | 6 | // Use, modification, and distribution is subject to the Boost Software 7 | // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at 8 | // http://www.boost.org/LICENSE_1_0.txt) 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | const unsigned long QUEUE_SIZE = 1000L; 23 | const unsigned long TOTAL_ELEMENTS = QUEUE_SIZE * 1000L; 24 | 25 | template 26 | class bounded_buffer { 27 | public: 28 | 29 | typedef boost::circular_buffer container_type; 30 | typedef typename container_type::size_type size_type; 31 | typedef typename container_type::value_type value_type; 32 | typedef typename boost::call_traits::param_type param_type; 33 | 34 | explicit bounded_buffer(size_type capacity) : m_unread(0), m_container(capacity) {} 35 | 36 | void push_front(param_type item) { 37 | boost::unique_lock lock(m_mutex); 38 | m_not_full.wait(lock, boost::bind(&bounded_buffer::is_not_full, this)); 39 | m_container.push_front(item); 40 | ++m_unread; 41 | lock.unlock(); 42 | m_not_empty.notify_one(); 43 | } 44 | 45 | void pop_back(value_type* pItem) { 46 | boost::unique_lock lock(m_mutex); 47 | m_not_empty.wait(lock, boost::bind(&bounded_buffer::is_not_empty, this)); 48 | *pItem = m_container[--m_unread]; 49 | lock.unlock(); 50 | m_not_full.notify_one(); 51 | } 52 | 53 | private: 54 | bounded_buffer(const bounded_buffer&); // Disabled copy constructor 55 | bounded_buffer& operator = (const bounded_buffer&); // Disabled assign operator 56 | 57 | bool is_not_empty() const { return m_unread > 0; } 58 | bool is_not_full() const { return m_unread < m_container.capacity(); } 59 | 60 | size_type m_unread; 61 | container_type m_container; 62 | boost::mutex m_mutex; 63 | boost::condition_variable m_not_empty; 64 | boost::condition_variable m_not_full; 65 | }; 66 | 67 | template 68 | class bounded_buffer_space_optimized { 69 | public: 70 | 71 | typedef boost::circular_buffer_space_optimized container_type; 72 | typedef typename container_type::size_type size_type; 73 | typedef typename container_type::value_type value_type; 74 | typedef typename boost::call_traits::param_type param_type; 75 | 76 | explicit bounded_buffer_space_optimized(size_type capacity) : m_container(capacity) {} 77 | 78 | void push_front(param_type item) { 79 | boost::unique_lock lock(m_mutex); 80 | m_not_full.wait(lock, boost::bind(&bounded_buffer_space_optimized::is_not_full, this)); 81 | m_container.push_front(item); 82 | lock.unlock(); 83 | m_not_empty.notify_one(); 84 | } 85 | 86 | void pop_back(value_type* pItem) { 87 | boost::unique_lock lock(m_mutex); 88 | m_not_empty.wait(lock, boost::bind(&bounded_buffer_space_optimized::is_not_empty, this)); 89 | *pItem = m_container.back(); 90 | m_container.pop_back(); 91 | lock.unlock(); 92 | m_not_full.notify_one(); 93 | } 94 | 95 | private: 96 | 97 | bounded_buffer_space_optimized(const bounded_buffer_space_optimized&); // Disabled copy constructor 98 | bounded_buffer_space_optimized& operator = (const bounded_buffer_space_optimized&); // Disabled assign operator 99 | 100 | bool is_not_empty() const { return m_container.size() > 0; } 101 | bool is_not_full() const { return m_container.size() < m_container.capacity(); } 102 | 103 | container_type m_container; 104 | boost::mutex m_mutex; 105 | boost::condition_variable m_not_empty; 106 | boost::condition_variable m_not_full; 107 | }; 108 | 109 | template 110 | class bounded_buffer_deque_based { 111 | public: 112 | 113 | typedef std::deque container_type; 114 | typedef typename container_type::size_type size_type; 115 | typedef typename container_type::value_type value_type; 116 | typedef typename boost::call_traits::param_type param_type; 117 | 118 | explicit bounded_buffer_deque_based(size_type capacity) : m_capacity(capacity) {} 119 | 120 | void push_front(param_type item) { 121 | boost::unique_lock lock(m_mutex); 122 | m_not_full.wait(lock, boost::bind(&bounded_buffer_deque_based::is_not_full, this)); 123 | m_container.push_front(item); 124 | lock.unlock(); 125 | m_not_empty.notify_one(); 126 | } 127 | 128 | void pop_back(value_type* pItem) { 129 | boost::unique_lock lock(m_mutex); 130 | m_not_empty.wait(lock, boost::bind(&bounded_buffer_deque_based::is_not_empty, this)); 131 | *pItem = m_container.back(); 132 | m_container.pop_back(); 133 | lock.unlock(); 134 | m_not_full.notify_one(); 135 | } 136 | 137 | private: 138 | 139 | bounded_buffer_deque_based(const bounded_buffer_deque_based&); // Disabled copy constructor 140 | bounded_buffer_deque_based& operator = (const bounded_buffer_deque_based&); // Disabled assign operator 141 | 142 | bool is_not_empty() const { return m_container.size() > 0; } 143 | bool is_not_full() const { return m_container.size() < m_capacity; } 144 | 145 | const size_type m_capacity; 146 | container_type m_container; 147 | boost::mutex m_mutex; 148 | boost::condition_variable m_not_empty; 149 | boost::condition_variable m_not_full; 150 | }; 151 | 152 | template 153 | class bounded_buffer_list_based { 154 | public: 155 | 156 | typedef std::list container_type; 157 | typedef typename container_type::size_type size_type; 158 | typedef typename container_type::value_type value_type; 159 | typedef typename boost::call_traits::param_type param_type; 160 | 161 | explicit bounded_buffer_list_based(size_type capacity) : m_capacity(capacity) {} 162 | 163 | void push_front(param_type item) { 164 | boost::unique_lock lock(m_mutex); 165 | m_not_full.wait(lock, boost::bind(&bounded_buffer_list_based::is_not_full, this)); 166 | m_container.push_front(item); 167 | lock.unlock(); 168 | m_not_empty.notify_one(); 169 | } 170 | 171 | void pop_back(value_type* pItem) { 172 | boost::unique_lock lock(m_mutex); 173 | m_not_empty.wait(lock, boost::bind(&bounded_buffer_list_based::is_not_empty, this)); 174 | *pItem = m_container.back(); 175 | m_container.pop_back(); 176 | lock.unlock(); 177 | m_not_full.notify_one(); 178 | } 179 | 180 | private: 181 | 182 | bounded_buffer_list_based(const bounded_buffer_list_based&); // Disabled copy constructor 183 | bounded_buffer_list_based& operator = (const bounded_buffer_list_based&); // Disabled assign operator 184 | 185 | bool is_not_empty() const { return m_container.size() > 0; } 186 | bool is_not_full() const { return m_container.size() < m_capacity; } 187 | 188 | const size_type m_capacity; 189 | container_type m_container; 190 | boost::mutex m_mutex; 191 | boost::condition_variable m_not_empty; 192 | boost::condition_variable m_not_full; 193 | }; 194 | 195 | template 196 | class Consumer { 197 | 198 | typedef typename Buffer::value_type value_type; 199 | Buffer* m_container; 200 | value_type m_item; 201 | 202 | public: 203 | Consumer(Buffer* buffer) : m_container(buffer) {} 204 | 205 | void operator()() { 206 | for (unsigned long i = 0L; i < TOTAL_ELEMENTS; ++i) { 207 | m_container->pop_back(&m_item); 208 | } 209 | } 210 | }; 211 | 212 | template 213 | class Producer { 214 | 215 | typedef typename Buffer::value_type value_type; 216 | Buffer* m_container; 217 | 218 | public: 219 | Producer(Buffer* buffer) : m_container(buffer) {} 220 | 221 | void operator()() { 222 | for (unsigned long i = 0L; i < TOTAL_ELEMENTS; ++i) { 223 | m_container->push_front(value_type()); 224 | } 225 | } 226 | }; 227 | 228 | template 229 | void fifo_test(Buffer* buffer) { 230 | 231 | // Start of measurement 232 | boost::timer::auto_cpu_timer progress; 233 | 234 | // Initialize the buffer with some values before launching producer and consumer threads. 235 | for (unsigned long i = QUEUE_SIZE / 2L; i > 0; --i) { 236 | #if BOOST_WORKAROUND(BOOST_BORLANDC, BOOST_TESTED_AT(0x581)) 237 | buffer->push_front(Buffer::value_type()); 238 | #else 239 | buffer->push_front(BOOST_DEDUCED_TYPENAME Buffer::value_type()); 240 | #endif 241 | } 242 | 243 | Consumer consumer(buffer); 244 | Producer producer(buffer); 245 | 246 | // Start the threads. 247 | boost::thread consume(consumer); 248 | boost::thread produce(producer); 249 | 250 | // Wait for completion. 251 | consume.join(); 252 | produce.join(); 253 | 254 | // End of measurement 255 | } 256 | 257 | int main(int /*argc*/, char* /*argv*/[]) { 258 | 259 | bounded_buffer bb_int(QUEUE_SIZE); 260 | std::cout << "bounded_buffer "; 261 | fifo_test(&bb_int); 262 | 263 | bounded_buffer_space_optimized bb_space_optimized_int(QUEUE_SIZE); 264 | std::cout << "bounded_buffer_space_optimized "; 265 | fifo_test(&bb_space_optimized_int); 266 | 267 | bounded_buffer_deque_based bb_deque_based_int(QUEUE_SIZE); 268 | std::cout << "bounded_buffer_deque_based "; 269 | fifo_test(&bb_deque_based_int); 270 | 271 | bounded_buffer_list_based bb_list_based_int(QUEUE_SIZE); 272 | std::cout << "bounded_buffer_list_based "; 273 | fifo_test(&bb_list_based_int); 274 | 275 | bounded_buffer bb_string(QUEUE_SIZE); 276 | std::cout << "bounded_buffer "; 277 | fifo_test(&bb_string); 278 | 279 | bounded_buffer_space_optimized bb_space_optimized_string(QUEUE_SIZE); 280 | std::cout << "bounded_buffer_space_optimized "; 281 | fifo_test(&bb_space_optimized_string); 282 | 283 | bounded_buffer_deque_based bb_deque_based_string(QUEUE_SIZE); 284 | std::cout << "bounded_buffer_deque_based "; 285 | fifo_test(&bb_deque_based_string); 286 | 287 | bounded_buffer_list_based bb_list_based_string(QUEUE_SIZE); 288 | std::cout << "bounded_buffer_list_based "; 289 | fifo_test(&bb_list_based_string); 290 | 291 | return 0; 292 | } 293 | /* 294 | 295 | //[bounded_buffer_comparison_output 296 | 297 | Description: Autorun "J:\Cpp\Misc\Debug\bounded_buffer_comparison.exe" 298 | bounded_buffer 5.15 s 299 | 300 | bounded_buffer_space_optimized 5.71 s 301 | 302 | bounded_buffer_deque_based 15.57 s 303 | 304 | bounded_buffer_list_based 17.33 s 305 | 306 | bounded_buffer 24.49 s 307 | 308 | bounded_buffer_space_optimized 28.33 s 309 | 310 | bounded_buffer_deque_based 29.45 s 311 | 312 | bounded_buffer_list_based 31.29 s 313 | 314 | //] //[bounded_buffer_comparison_output] 315 | 316 | */ 317 | -------------------------------------------------------------------------------- /test/constant_erase_test.cpp: -------------------------------------------------------------------------------- 1 | // Special tests for erase_begin, erase_end and clear methods. 2 | 3 | // Copyright (c) 2009 Jan Gaspar 4 | 5 | // Use, modification, and distribution is subject to the Boost Software 6 | // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at 7 | // http://www.boost.org/LICENSE_1_0.txt) 8 | 9 | #include "test.hpp" 10 | 11 | int MyInteger::ms_exception_trigger = 0; 12 | int InstanceCounter::ms_count = 0; 13 | 14 | void erase_begin_test() { 15 | 16 | circular_buffer cb1(5); 17 | cb1.push_back(1); 18 | cb1.push_back(2); 19 | cb1.push_back(3); 20 | cb1.push_back(4); 21 | cb1.push_back(5); 22 | cb1.push_back(6); 23 | 24 | circular_buffer::pointer p = &cb1[0]; 25 | 26 | cb1.erase_begin(2); 27 | 28 | BOOST_TEST(cb1.size() == 3); 29 | BOOST_TEST(cb1[0] == 4); 30 | BOOST_TEST(cb1[1] == 5); 31 | BOOST_TEST(cb1[2] == 6); 32 | 33 | cb1.erase_begin(3); 34 | BOOST_TEST(cb1.empty()); 35 | BOOST_TEST(*p == 2); 36 | BOOST_TEST(*(p + 1) == 3); 37 | BOOST_TEST(*(p + 2) == 4); 38 | 39 | cb1.push_back(10); 40 | cb1.push_back(11); 41 | cb1.push_back(12); 42 | 43 | BOOST_TEST(cb1.size() == 3); 44 | BOOST_TEST(cb1[0] == 10); 45 | BOOST_TEST(cb1[1] == 11); 46 | BOOST_TEST(cb1[2] == 12); 47 | 48 | circular_buffer cb2(5, InstanceCounter()); 49 | 50 | BOOST_TEST(cb2.size() == 5); 51 | BOOST_TEST(InstanceCounter::count() == 5); 52 | 53 | cb2.erase_begin(2); 54 | 55 | BOOST_TEST(cb2.size() == 3); 56 | BOOST_TEST(InstanceCounter::count() == 3); 57 | 58 | circular_buffer cb3(5); 59 | cb3.push_back(1); 60 | cb3.push_back(2); 61 | cb3.push_back(3); 62 | cb3.push_back(4); 63 | cb3.push_back(5); 64 | cb3.push_back(6); 65 | cb3.erase_begin(2); 66 | 67 | BOOST_TEST(cb3.size() == 3); 68 | BOOST_TEST(cb3[0] == 4); 69 | BOOST_TEST(cb3[1] == 5); 70 | BOOST_TEST(cb3[2] == 6); 71 | } 72 | 73 | void erase_end_test() { 74 | 75 | circular_buffer cb1(5); 76 | cb1.push_back(1); 77 | cb1.push_back(2); 78 | cb1.push_back(3); 79 | cb1.push_back(4); 80 | cb1.push_back(5); 81 | cb1.push_back(6); 82 | 83 | circular_buffer::pointer p = &cb1[3]; 84 | 85 | cb1.erase_end(2); 86 | 87 | BOOST_TEST(cb1.size() == 3); 88 | BOOST_TEST(cb1[0] == 2); 89 | BOOST_TEST(cb1[1] == 3); 90 | BOOST_TEST(cb1[2] ==4); 91 | 92 | cb1.erase_end(3); 93 | BOOST_TEST(cb1.empty()); 94 | BOOST_TEST(*p == 5); 95 | BOOST_TEST(*(p - 1) == 4); 96 | BOOST_TEST(*(p - 2) == 3); 97 | 98 | cb1.push_back(10); 99 | cb1.push_back(11); 100 | cb1.push_back(12); 101 | 102 | BOOST_TEST(cb1.size() == 3); 103 | BOOST_TEST(cb1[0] == 10); 104 | BOOST_TEST(cb1[1] == 11); 105 | BOOST_TEST(cb1[2] == 12); 106 | 107 | circular_buffer cb2(5, InstanceCounter()); 108 | 109 | BOOST_TEST(cb2.size() == 5); 110 | BOOST_TEST(InstanceCounter::count() == 5); 111 | 112 | cb2.erase_end(2); 113 | 114 | BOOST_TEST(cb2.size() == 3); 115 | BOOST_TEST(InstanceCounter::count() == 3); 116 | 117 | circular_buffer cb3(5); 118 | cb3.push_back(1); 119 | cb3.push_back(2); 120 | cb3.push_back(3); 121 | cb3.push_back(4); 122 | cb3.push_back(5); 123 | cb3.push_back(6); 124 | cb3.erase_end(2); 125 | 126 | BOOST_TEST(cb3.size() == 3); 127 | BOOST_TEST(cb3[0] == 2); 128 | BOOST_TEST(cb3[1] == 3); 129 | BOOST_TEST(cb3[2] == 4); 130 | } 131 | 132 | void clear_test() { 133 | 134 | circular_buffer cb1(5); 135 | cb1.push_back(1); 136 | cb1.push_back(2); 137 | cb1.push_back(3); 138 | cb1.push_back(4); 139 | cb1.push_back(5); 140 | cb1.push_back(6); 141 | 142 | circular_buffer::pointer p = &cb1[0]; 143 | 144 | cb1.clear(); 145 | 146 | BOOST_TEST(cb1.empty()); 147 | BOOST_TEST(*p == 2); 148 | BOOST_TEST(*(p + 1) == 3); 149 | BOOST_TEST(*(p + 2) == 4); 150 | BOOST_TEST(*(p + 3) == 5); 151 | BOOST_TEST(*(p - 1) == 6); 152 | 153 | circular_buffer cb2(5, InstanceCounter()); 154 | 155 | BOOST_TEST(cb2.size() == 5); 156 | BOOST_TEST(InstanceCounter::count() == 5); 157 | 158 | cb2.clear(); 159 | 160 | BOOST_TEST(cb2.empty()); 161 | BOOST_TEST(InstanceCounter::count() == 0); 162 | 163 | circular_buffer cb3(5); 164 | cb3.push_back(1); 165 | cb3.push_back(2); 166 | cb3.push_back(3); 167 | cb3.push_back(4); 168 | cb3.push_back(5); 169 | cb3.push_back(6); 170 | cb3.clear(); 171 | 172 | BOOST_TEST(cb3.empty()); 173 | } 174 | 175 | // test main 176 | int main() 177 | { 178 | erase_begin_test(); 179 | erase_end_test(); 180 | clear_test(); 181 | return boost::report_errors(); 182 | } 183 | -------------------------------------------------------------------------------- /test/soft_iterator_invalidation.cpp: -------------------------------------------------------------------------------- 1 | // Demonstration of rules when an iterator is considered to be valid if the soft 2 | // iterator invalidation definition is applied. 3 | // Note: The soft iterator invalidation definition CAN NOT be applied 4 | // to the space optimized circular buffer. 5 | 6 | // Copyright (c) 2003-2008 Jan Gaspar 7 | 8 | // Use, modification, and distribution is subject to the Boost Software 9 | // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at 10 | // http://www.boost.org/LICENSE_1_0.txt) 11 | 12 | #include "test.hpp" 13 | 14 | // test of the example (introduced in the documentation) 15 | void validity_example_test() { 16 | 17 | circular_buffer cb(3); 18 | 19 | cb.push_back(1); 20 | cb.push_back(2); 21 | cb.push_back(3); 22 | 23 | circular_buffer::iterator it = cb.begin(); 24 | 25 | BOOST_TEST(*it == 1); 26 | 27 | cb.push_back(4); 28 | 29 | BOOST_TEST(*it == 4); 30 | } 31 | 32 | void validity_insert_test() { 33 | 34 | int array[] = { 1, 2, 3 }; 35 | 36 | // memory placement: { 1, 2, 3 } 37 | // circular buffer: { 1, 2, 3 } 38 | circular_buffer cb(4, array, array + 3); 39 | 40 | // it1 -> 1, it2 -> 2, it3 -> 3 41 | circular_buffer::iterator it1 = cb.begin(); 42 | circular_buffer::iterator it2 = cb.begin() + 1; 43 | circular_buffer::iterator it3 = cb.begin() + 2; 44 | 45 | cb.insert(cb.begin() + 1, 4); 46 | 47 | // memory placement: { 1, 4, 2, 3 } 48 | // circular buffer: { 1, 4, 2, 3 } 49 | BOOST_TEST(*it1 == 1); 50 | BOOST_TEST(*it2 == 4); 51 | BOOST_TEST(*it3 == 2); 52 | BOOST_TEST(cb[0] == 1); 53 | BOOST_TEST(cb[1] == 4); 54 | BOOST_TEST(cb[2] == 2); 55 | BOOST_TEST(cb[3] == 3); 56 | 57 | // it4 -> 3 58 | circular_buffer::iterator it4 = it1 + 3; 59 | 60 | cb.insert(cb.begin() + 1, 5); 61 | 62 | // memory placement: { 3, 5, 4, 2 } 63 | // circular buffer: { 5, 4, 2, 3 } 64 | BOOST_TEST(*it1 == 3); 65 | BOOST_TEST(*it2 == 5); 66 | BOOST_TEST(*it3 == 4); 67 | BOOST_TEST(*it4 == 2); 68 | BOOST_TEST(cb[0] == 5); 69 | BOOST_TEST(cb[1] == 4); 70 | BOOST_TEST(cb[2] == 2); 71 | BOOST_TEST(cb[3] == 3); 72 | } 73 | 74 | void validity_insert_n_test() { 75 | 76 | // memory placement: { 1, 2, 3 } 77 | // circular buffer: { 1, 2, 3 } 78 | circular_buffer cb(5); 79 | cb.push_back(1); 80 | cb.push_back(2); 81 | cb.push_back(3); 82 | 83 | // it1 -> 1, it2 -> 2, it3 -> 3 84 | circular_buffer::iterator it1 = cb.begin(); 85 | circular_buffer::iterator it2 = cb.begin() + 1; 86 | circular_buffer::iterator it3 = cb.begin() + 2; 87 | 88 | cb.insert(cb.begin() + 1, 2, 4); 89 | 90 | // memory placement: { 1, 4, 4, 2, 3 } 91 | // circular buffer: { 1, 4, 4, 2, 3 } 92 | BOOST_TEST(*it1 == 1); 93 | BOOST_TEST(*it2 == 4); 94 | BOOST_TEST(*it3 == 4); 95 | BOOST_TEST(cb[0] == 1); 96 | BOOST_TEST(cb[1] == 4); 97 | BOOST_TEST(cb[2] == 4); 98 | BOOST_TEST(cb[3] == 2); 99 | BOOST_TEST(cb[4] == 3); 100 | 101 | // it4 -> 2, it5 -> 3 102 | circular_buffer::iterator it4 = it1 + 3; 103 | circular_buffer::iterator it5 = it1 + 4; 104 | 105 | cb.insert(cb.begin() + 1, 2, 5); 106 | 107 | // memory placement: { 3, 5, 4, 4, 2 } - 5 inserted only once 108 | // circular buffer: { 5, 4, 4, 2, 3 } 109 | BOOST_TEST(*it1 == 3); 110 | BOOST_TEST(*it2 == 5); 111 | BOOST_TEST(*it3 == 4); 112 | BOOST_TEST(*it4 == 4); 113 | BOOST_TEST(*it5 == 2); 114 | BOOST_TEST(cb[0] == 5); 115 | BOOST_TEST(cb[1] == 4); 116 | BOOST_TEST(cb[2] == 4); 117 | BOOST_TEST(cb[3] == 2); 118 | BOOST_TEST(cb[4] == 3); 119 | } 120 | 121 | void validity_insert_range_test() { 122 | 123 | vector v1; 124 | v1.push_back(4); 125 | v1.push_back(5); 126 | 127 | vector v2; 128 | v2.push_back(6); 129 | v2.push_back(7); 130 | 131 | 132 | // memory placement: { 1, 2, 3 } 133 | // circular buffer: { 1, 2, 3 } 134 | circular_buffer cb1(5); 135 | cb1.push_back(1); 136 | cb1.push_back(2); 137 | cb1.push_back(3); 138 | 139 | // it11 -> 1, it12 -> 2, it13 -> 3 140 | circular_buffer::iterator it11 = cb1.begin(); 141 | circular_buffer::iterator it12 = cb1.begin() + 1; 142 | circular_buffer::iterator it13 = cb1.begin() + 2; 143 | 144 | cb1.insert(cb1.begin() + 1, v1.begin(), v1.end()); 145 | 146 | // memory placement: { 1, 4, 5, 2, 3 } 147 | // circular buffer: { 1, 4, 5, 2, 3 } 148 | BOOST_TEST(*it11 == 1); 149 | BOOST_TEST(*it12 == 4); 150 | BOOST_TEST(*it13 == 5); 151 | BOOST_TEST(cb1[0] == 1); 152 | BOOST_TEST(cb1[1] == 4); 153 | BOOST_TEST(cb1[2] == 5); 154 | BOOST_TEST(cb1[3] == 2); 155 | BOOST_TEST(cb1[4] == 3); 156 | 157 | // it14 -> 2, it15 -> 3 158 | circular_buffer::iterator it14 = it11 + 3; 159 | circular_buffer::iterator it15 = it11 + 4; 160 | 161 | cb1.insert(cb1.begin() + 1, v2.begin(), v2.end()); 162 | 163 | // memory placement: { 3, 7, 4, 5, 2 } - 7 inserted only 164 | // circular buffer: { 7, 4, 5, 2, 3 } 165 | BOOST_TEST(*it11 == 3); 166 | BOOST_TEST(*it12 == 7); 167 | BOOST_TEST(*it13 == 4); 168 | BOOST_TEST(*it14 == 5); 169 | BOOST_TEST(*it15 == 2); 170 | BOOST_TEST(cb1[0] == 7); 171 | BOOST_TEST(cb1[1] == 4); 172 | BOOST_TEST(cb1[2] == 5); 173 | BOOST_TEST(cb1[3] == 2); 174 | BOOST_TEST(cb1[4] == 3); 175 | 176 | // memory placement: { 1, 2, 3 } 177 | // circular buffer: { 1, 2, 3 } 178 | circular_buffer cb2(5); 179 | cb2.push_back(1); 180 | cb2.push_back(2); 181 | cb2.push_back(3); 182 | 183 | // it21 -> 1, it22 -> 2, it23 -> 3 184 | circular_buffer::iterator it21 = cb2.begin(); 185 | circular_buffer::iterator it22 = cb2.begin() + 1; 186 | circular_buffer::iterator it23 = cb2.begin() + 2; 187 | 188 | cb2.insert(cb2.begin() + 1, MyInputIterator(v1.begin()), MyInputIterator(v1.end())); 189 | 190 | // memory placement: { 1, 4, 5, 2, 3 } 191 | // circular buffer: { 1, 4, 5, 2, 3 } 192 | BOOST_TEST(*it21 == 1); 193 | BOOST_TEST(*it22 == 4); 194 | BOOST_TEST(*it23 == 5); 195 | BOOST_TEST(cb2[0] == 1); 196 | BOOST_TEST(cb2[1] == 4); 197 | BOOST_TEST(cb2[2] == 5); 198 | BOOST_TEST(cb2[3] == 2); 199 | BOOST_TEST(cb2[4] == 3); 200 | 201 | // it24 -> 2, it25 -> 3 202 | circular_buffer::iterator it24 = it21 + 3; 203 | circular_buffer::iterator it25 = it21 + 4; 204 | 205 | cb2.insert(cb2.begin() + 1, MyInputIterator(v2.begin()), MyInputIterator(v2.end())); 206 | 207 | // memory placement: { 2, 3, 7, 4, 5 } - using input iterator inserts all items even if they are later replaced 208 | // circular buffer: { 7, 4, 5, 2, 3 } 209 | BOOST_TEST(*it21 == 2); 210 | BOOST_TEST(*it22 == 3); 211 | BOOST_TEST(*it23 == 7); 212 | BOOST_TEST(*it24 == 4); 213 | BOOST_TEST(*it25 == 5); 214 | BOOST_TEST(cb2[0] == 7); 215 | BOOST_TEST(cb2[1] == 4); 216 | BOOST_TEST(cb2[2] == 5); 217 | BOOST_TEST(cb2[3] == 2); 218 | BOOST_TEST(cb2[4] == 3); 219 | } 220 | 221 | void validity_rinsert_test() { 222 | 223 | int array[] = { 1, 2, 3 }; 224 | 225 | // memory placement: { 1, 2, 3 } 226 | // circular buffer: { 1, 2, 3 } 227 | circular_buffer cb(4, array, array + 3); 228 | 229 | // it1 -> 1, it2 -> 2, it3 -> 3 230 | circular_buffer::iterator it1 = cb.begin(); 231 | circular_buffer::iterator it2 = cb.begin() + 1; 232 | circular_buffer::iterator it3 = cb.begin() + 2; 233 | 234 | cb.rinsert(cb.begin() + 2, 4); 235 | 236 | // memory placement: { 2, 4, 3, 1 } 237 | // circular buffer: { 1, 2, 4, 3 } 238 | BOOST_TEST(*it1 == 2); 239 | BOOST_TEST(*it2 == 4); 240 | BOOST_TEST(*it3 == 3); 241 | BOOST_TEST(cb[0] == 1); 242 | BOOST_TEST(cb[1] == 2); 243 | BOOST_TEST(cb[2] == 4); 244 | BOOST_TEST(cb[3] == 3); 245 | 246 | // it4 -> 1 247 | circular_buffer::iterator it4 = it1 - 1; 248 | 249 | cb.rinsert(cb.begin() + 2, 5); 250 | 251 | // memory placement: { 5, 4, 1, 2 } 252 | // circular buffer: { 1, 2, 5, 4 } 253 | BOOST_TEST(*it1 == 5); 254 | BOOST_TEST(*it2 == 4); 255 | BOOST_TEST(*it3 == 1); 256 | BOOST_TEST(*it4 == 2); 257 | BOOST_TEST(cb[0] == 1); 258 | BOOST_TEST(cb[1] == 2); 259 | BOOST_TEST(cb[2] == 5); 260 | BOOST_TEST(cb[3] == 4); 261 | } 262 | 263 | void validity_rinsert_n_test() { 264 | 265 | // memory placement: { 1, 2, 3 } 266 | // circular buffer: { 1, 2, 3 } 267 | circular_buffer cb(5); 268 | cb.push_back(1); 269 | cb.push_back(2); 270 | cb.push_back(3); 271 | 272 | // it1 -> 1, it2 -> 2, it3 -> 3 273 | circular_buffer::iterator it1 = cb.begin(); 274 | circular_buffer::iterator it2 = cb.begin() + 1; 275 | circular_buffer::iterator it3 = cb.begin() + 2; 276 | 277 | cb.rinsert(cb.begin() + 2, 2, 4); 278 | 279 | // memory placement: { 4, 4, 3, 1, 2 } 280 | // circular buffer: { 1, 2, 4, 4, 3 } 281 | BOOST_TEST(*it1 == 4); 282 | BOOST_TEST(*it2 == 4); 283 | BOOST_TEST(*it3 == 3); 284 | BOOST_TEST(cb[0] == 1); 285 | BOOST_TEST(cb[1] == 2); 286 | BOOST_TEST(cb[2] == 4); 287 | BOOST_TEST(cb[3] == 4); 288 | BOOST_TEST(cb[4] == 3); 289 | 290 | // it4 -> 1, it5 -> 2 291 | circular_buffer::iterator it4 = it1 - 2; 292 | circular_buffer::iterator it5 = it1 - 1; 293 | 294 | cb.rinsert(cb.begin() + 4, 2, 5); 295 | 296 | // memory placement: { 4, 5, 1, 2, 4 } - 5 inserted only once 297 | // circular buffer: { 1, 2, 4, 4, 5 } 298 | BOOST_TEST(*it1 == 4); 299 | BOOST_TEST(*it2 == 5); 300 | BOOST_TEST(*it3 == 1); 301 | BOOST_TEST(*it4 == 2); 302 | BOOST_TEST(*it5 == 4); 303 | BOOST_TEST(cb[0] == 1); 304 | BOOST_TEST(cb[1] == 2); 305 | BOOST_TEST(cb[2] == 4); 306 | BOOST_TEST(cb[3] == 4); 307 | BOOST_TEST(cb[4] == 5); 308 | } 309 | 310 | void validity_rinsert_range_test() { 311 | 312 | vector v1; 313 | v1.push_back(4); 314 | v1.push_back(5); 315 | 316 | vector v2; 317 | v2.push_back(6); 318 | v2.push_back(7); 319 | 320 | 321 | // memory placement: { 1, 2, 3 } 322 | // circular buffer: { 1, 2, 3 } 323 | circular_buffer cb1(5); 324 | cb1.push_back(1); 325 | cb1.push_back(2); 326 | cb1.push_back(3); 327 | 328 | // it1 -> 1, it2 -> 2, it3 -> 3 329 | circular_buffer::iterator it11 = cb1.begin(); 330 | circular_buffer::iterator it12 = cb1.begin() + 1; 331 | circular_buffer::iterator it13 = cb1.begin() + 2; 332 | 333 | cb1.rinsert(cb1.begin() + 2, v1.begin(), v1.end()); 334 | 335 | // memory placement: { 4, 5, 3, 1, 2 } 336 | // circular buffer: { 1, 2, 4, 5, 3 } 337 | BOOST_TEST(*it11 == 4); 338 | BOOST_TEST(*it12 == 5); 339 | BOOST_TEST(*it13 == 3); 340 | BOOST_TEST(cb1[0] == 1); 341 | BOOST_TEST(cb1[1] == 2); 342 | BOOST_TEST(cb1[2] == 4); 343 | BOOST_TEST(cb1[3] == 5); 344 | BOOST_TEST(cb1[4] == 3); 345 | 346 | // it14 -> 1, it15 -> 2 347 | circular_buffer::iterator it14 = it11 - 2; 348 | circular_buffer::iterator it15 = it11 - 1; 349 | 350 | cb1.rinsert(cb1.begin() + 4, v2.begin(), v2.end()); 351 | 352 | // memory placement: { 5, 6, 1, 2, 4 } - 6 inserted only 353 | // circular buffer: { 1, 2, 4, 5, 6 } 354 | BOOST_TEST(*it11 == 5); 355 | BOOST_TEST(*it12 == 6); 356 | BOOST_TEST(*it13 == 1); 357 | BOOST_TEST(*it14 == 2); 358 | BOOST_TEST(*it15 == 4); 359 | BOOST_TEST(cb1[0] == 1); 360 | BOOST_TEST(cb1[1] == 2); 361 | BOOST_TEST(cb1[2] == 4); 362 | BOOST_TEST(cb1[3] == 5); 363 | BOOST_TEST(cb1[4] == 6); 364 | 365 | // memory placement: { 1, 2, 3 } 366 | // circular buffer: { 1, 2, 3 } 367 | circular_buffer cb2(5); 368 | cb2.push_back(1); 369 | cb2.push_back(2); 370 | cb2.push_back(3); 371 | 372 | // it1 -> 1, it2 -> 2, it3 -> 3 373 | circular_buffer::iterator it21 = cb2.begin(); 374 | circular_buffer::iterator it22 = cb2.begin() + 1; 375 | circular_buffer::iterator it23 = cb2.begin() + 2; 376 | 377 | cb2.rinsert(cb2.begin() + 2, MyInputIterator(v1.begin()), MyInputIterator(v1.end())); 378 | 379 | // memory placement: { 4, 5, 3, 1, 2 } 380 | // circular buffer: { 1, 2, 4, 5, 3 } 381 | BOOST_TEST(*it21 == 4); 382 | BOOST_TEST(*it22 == 5); 383 | BOOST_TEST(*it23 == 3); 384 | BOOST_TEST(cb2[0] == 1); 385 | BOOST_TEST(cb2[1] == 2); 386 | BOOST_TEST(cb2[2] == 4); 387 | BOOST_TEST(cb2[3] == 5); 388 | BOOST_TEST(cb2[4] == 3); 389 | 390 | // it24 -> 1, it25 -> 2 391 | circular_buffer::iterator it24 = it21 - 2; 392 | circular_buffer::iterator it25 = it21 - 1; 393 | 394 | cb2.rinsert(cb2.begin() + 4, MyInputIterator(v2.begin()), MyInputIterator(v2.end())); 395 | 396 | // memory placement: { 5, 6, 1, 2, 4 } 397 | // circular buffer: { 1, 2, 4, 5, 6 } 398 | BOOST_TEST(*it21 == 5); 399 | BOOST_TEST(*it22 == 6); 400 | BOOST_TEST(*it23 == 1); 401 | BOOST_TEST(*it24 == 2); 402 | BOOST_TEST(*it25 == 4); 403 | BOOST_TEST(cb2[0] == 1); 404 | BOOST_TEST(cb2[1] == 2); 405 | BOOST_TEST(cb2[2] == 4); 406 | BOOST_TEST(cb2[3] == 5); 407 | BOOST_TEST(cb2[4] == 6); 408 | } 409 | 410 | void validity_erase_test() { 411 | 412 | // memory placement: { 4, 5, 1, 2, 3 } 413 | // circular buffer: { 1, 2, 3, 4, 5 } 414 | circular_buffer cb(5); 415 | cb.push_back(-1); 416 | cb.push_back(0); 417 | cb.push_back(1); 418 | cb.push_back(2); 419 | cb.push_back(3); 420 | cb.push_back(4); 421 | cb.push_back(5); 422 | 423 | // it1 -> 1, it2 -> 2, it3 -> 3, it4 -> 4 424 | circular_buffer::iterator it1 = cb.begin(); 425 | circular_buffer::iterator it2 = cb.begin() + 1; 426 | circular_buffer::iterator it3 = cb.begin() + 2; 427 | circular_buffer::iterator it4 = cb.begin() + 3; 428 | 429 | cb.erase(cb.begin() + 1); 430 | 431 | // memory placement: { 5, X, 1, 3, 4 } 432 | // circular buffer: { 1, 3, 4, 5 } 433 | BOOST_TEST(*it1 == 1); 434 | BOOST_TEST(*it2 == 3); 435 | BOOST_TEST(*it3 == 4); 436 | BOOST_TEST(*it4 == 5); 437 | BOOST_TEST(cb[0] == 1); 438 | BOOST_TEST(cb[1] == 3); 439 | BOOST_TEST(cb[2] == 4); 440 | BOOST_TEST(cb[3] == 5); 441 | } 442 | 443 | void validity_erase_range_test() { 444 | 445 | // memory placement: { 4, 5, 6, 1, 2, 3 } 446 | // circular buffer: { 1, 2, 3, 4, 5, 6 } 447 | circular_buffer cb(6); 448 | cb.push_back(-2); 449 | cb.push_back(-1); 450 | cb.push_back(0); 451 | cb.push_back(1); 452 | cb.push_back(2); 453 | cb.push_back(3); 454 | cb.push_back(4); 455 | cb.push_back(5); 456 | cb.push_back(6); 457 | 458 | // it1 -> 1, it2 -> 2, it3 -> 3, it4 -> 4 459 | circular_buffer::iterator it1 = cb.begin(); 460 | circular_buffer::iterator it2 = cb.begin() + 1; 461 | circular_buffer::iterator it3 = cb.begin() + 2; 462 | circular_buffer::iterator it4 = cb.begin() + 3; 463 | 464 | cb.erase(cb.begin() + 2, cb.begin() + 4); 465 | 466 | // memory placement: { 6, X, X, 1, 2, 5 } 467 | // circular buffer: { 1, 2, 5, 6 } 468 | BOOST_TEST(*it1 == 1); 469 | BOOST_TEST(*it2 == 2); 470 | BOOST_TEST(*it3 == 5); 471 | BOOST_TEST(*it4 == 6); 472 | BOOST_TEST(cb[0] == 1); 473 | BOOST_TEST(cb[1] == 2); 474 | BOOST_TEST(cb[2] == 5); 475 | BOOST_TEST(cb[3] == 6); 476 | } 477 | 478 | void validity_rerase_test() { 479 | 480 | // memory placement: { 4, 5, 1, 2, 3 } 481 | // circular buffer: { 1, 2, 3, 4, 5 } 482 | circular_buffer cb(5); 483 | cb.push_back(-1); 484 | cb.push_back(0); 485 | cb.push_back(1); 486 | cb.push_back(2); 487 | cb.push_back(3); 488 | cb.push_back(4); 489 | cb.push_back(5); 490 | 491 | // it1 -> 2, it2 -> 3, it3 -> 4, it4 -> 5 492 | circular_buffer::iterator it1 = cb.begin() + 1; 493 | circular_buffer::iterator it2 = cb.begin() + 2; 494 | circular_buffer::iterator it3 = cb.begin() + 3; 495 | circular_buffer::iterator it4 = cb.begin() + 4; 496 | 497 | cb.rerase(cb.begin() + 1); 498 | 499 | // memory placement: { 4, 5, X, 1, 3 } 500 | // circular buffer: { 1, 3, 4, 5 } 501 | BOOST_TEST(*it1 == 1); 502 | BOOST_TEST(*it2 == 3); 503 | BOOST_TEST(*it3 == 4); 504 | BOOST_TEST(*it4 == 5); 505 | BOOST_TEST(cb[0] == 1); 506 | BOOST_TEST(cb[1] == 3); 507 | BOOST_TEST(cb[2] == 4); 508 | BOOST_TEST(cb[3] == 5); 509 | } 510 | 511 | void validity_rerase_range_test() { 512 | 513 | // memory placement: { 4, 5, 6, 1, 2, 3 } 514 | // circular buffer: { 1, 2, 3, 4, 5, 6 } 515 | circular_buffer cb(6); 516 | cb.push_back(-2); 517 | cb.push_back(-1); 518 | cb.push_back(0); 519 | cb.push_back(1); 520 | cb.push_back(2); 521 | cb.push_back(3); 522 | cb.push_back(4); 523 | cb.push_back(5); 524 | cb.push_back(6); 525 | 526 | // it1 -> 3, it2 -> 4, it3 -> 5, it4 -> 6 527 | circular_buffer::iterator it1 = cb.begin() + 2; 528 | circular_buffer::iterator it2 = cb.begin() + 3; 529 | circular_buffer::iterator it3 = cb.begin() + 4; 530 | circular_buffer::iterator it4 = cb.begin() + 5; 531 | 532 | cb.rerase(cb.begin() + 2, cb.begin() + 4); 533 | 534 | // memory placement: { 2, 5, 6, X, X, 1 } 535 | // circular buffer: { 1, 2, 5, 6 } 536 | BOOST_TEST(*it1 == 1); 537 | BOOST_TEST(*it2 == 2); 538 | BOOST_TEST(*it3 == 5); 539 | BOOST_TEST(*it4 == 6); 540 | BOOST_TEST(cb[0] == 1); 541 | BOOST_TEST(cb[1] == 2); 542 | BOOST_TEST(cb[2] == 5); 543 | BOOST_TEST(cb[3] == 6); 544 | } 545 | 546 | void validity_linearize_test() { 547 | 548 | // memory placement: { 3, 1, 2 } 549 | // circular buffer: { 1, 2, 3 } 550 | circular_buffer cb(3); 551 | cb.push_back(0); 552 | cb.push_back(1); 553 | cb.push_back(2); 554 | cb.push_back(3); 555 | 556 | // it1 -> 1, it2 -> 2, it3 -> 3 557 | circular_buffer::iterator it1 = cb.begin(); 558 | circular_buffer::iterator it2 = cb.begin() + 1; 559 | circular_buffer::iterator it3 = cb.begin() + 2; 560 | 561 | cb.linearize(); 562 | 563 | // memory placement: { 1, 2, 3 } 564 | // circular buffer: { 1, 2, 3 } 565 | BOOST_TEST(*it1 == 2); 566 | BOOST_TEST(*it2 == 3); 567 | BOOST_TEST(*it3 == 1); 568 | BOOST_TEST(cb[0] == 1); 569 | BOOST_TEST(cb[1] == 2); 570 | BOOST_TEST(cb[2] == 3); 571 | } 572 | 573 | void validity_swap_test() { 574 | 575 | // memory placement: { 3, 1, 2 } 576 | // circular buffer: { 1, 2, 3 } 577 | circular_buffer cb1(3); 578 | cb1.push_back(0); 579 | cb1.push_back(1); 580 | cb1.push_back(2); 581 | cb1.push_back(3); 582 | 583 | // it11 -> 1, it12 -> 2, it13 -> 3 584 | circular_buffer::iterator it11 = cb1.begin(); 585 | circular_buffer::iterator it12 = cb1.begin() + 1; 586 | circular_buffer::iterator it13 = cb1.begin() + 2; 587 | 588 | // memory placement: { 4, 5, 6 } 589 | // circular buffer: { 4, 5, 6 } 590 | circular_buffer cb2(5); 591 | cb2.push_back(4); 592 | cb2.push_back(5); 593 | cb2.push_back(6); 594 | 595 | // it21 -> 4, it22 -> 5, it23 -> 6 596 | circular_buffer::iterator it21 = cb2.begin(); 597 | circular_buffer::iterator it22 = cb2.begin() + 1; 598 | circular_buffer::iterator it23 = cb2.begin() + 2; 599 | 600 | cb1.swap(cb2); 601 | 602 | // Although iterators refer to the original elements, 603 | // their internal state is inconsistent and no other operation 604 | // (except dereferencing) can be invoked on them any more. 605 | BOOST_TEST(*it11 == 1); 606 | BOOST_TEST(*it12 == 2); 607 | BOOST_TEST(*it13 == 3); 608 | BOOST_TEST(*it21 == 4); 609 | BOOST_TEST(*it22 == 5); 610 | BOOST_TEST(*it23 == 6); 611 | BOOST_TEST(cb1[0] == 4); 612 | BOOST_TEST(cb1[1] == 5); 613 | BOOST_TEST(cb1[2] == 6); 614 | BOOST_TEST(cb2[0] == 1); 615 | BOOST_TEST(cb2[1] == 2); 616 | BOOST_TEST(cb2[2] == 3); 617 | } 618 | 619 | void validity_push_back_test() { 620 | 621 | // memory placement: { 3, 1, 2 } 622 | // circular buffer: { 1, 2, 3 } 623 | circular_buffer cb(3); 624 | cb.push_back(0); 625 | cb.push_back(1); 626 | cb.push_back(2); 627 | cb.push_back(3); 628 | 629 | // it1 -> 1, it2 -> 2, it3 -> 3 630 | circular_buffer::iterator it1 = cb.begin(); 631 | circular_buffer::iterator it2 = cb.begin() + 1; 632 | circular_buffer::iterator it3 = cb.begin() + 2; 633 | 634 | cb.push_back(4); 635 | 636 | // memory placement: { 3, 4, 2 } 637 | // circular buffer: { 2, 3, 4 } 638 | BOOST_TEST(*it1 == 4); 639 | BOOST_TEST(*it2 == 2); 640 | BOOST_TEST(*it3 == 3); 641 | BOOST_TEST(cb[0] == 2); 642 | BOOST_TEST(cb[1] == 3); 643 | BOOST_TEST(cb[2] == 4); 644 | } 645 | 646 | void validity_push_front_test() { 647 | 648 | // memory placement: { 3, 1, 2 } 649 | // circular buffer: { 1, 2, 3 } 650 | circular_buffer cb(3); 651 | cb.push_back(0); 652 | cb.push_back(1); 653 | cb.push_back(2); 654 | cb.push_back(3); 655 | 656 | // it1 -> 1, it2 -> 2, it3 -> 3 657 | circular_buffer::iterator it1 = cb.begin(); 658 | circular_buffer::iterator it2 = cb.begin() + 1; 659 | circular_buffer::iterator it3 = cb.begin() + 2; 660 | 661 | cb.push_front(4); 662 | 663 | // memory placement: { 4, 1, 2 } 664 | // circular buffer: { 4, 1, 2 } 665 | BOOST_TEST(*it1 == 1); 666 | BOOST_TEST(*it2 == 2); 667 | BOOST_TEST(*it3 == 4); 668 | BOOST_TEST(cb[0] == 4); 669 | BOOST_TEST(cb[1] == 1); 670 | BOOST_TEST(cb[2] == 2); 671 | } 672 | 673 | void validity_pop_back_test() { 674 | 675 | // memory placement: { 3, 1, 2 } 676 | // circular buffer: { 1, 2, 3 } 677 | circular_buffer cb(3); 678 | cb.push_back(0); 679 | cb.push_back(1); 680 | cb.push_back(2); 681 | cb.push_back(3); 682 | 683 | // it1 -> 1, it2 -> 2 684 | circular_buffer::iterator it1 = cb.begin(); 685 | circular_buffer::iterator it2 = cb.begin() + 1; 686 | 687 | cb.pop_back(); 688 | 689 | // memory placement: { X, 1, 2 } 690 | // circular buffer: { 1, 2 } 691 | BOOST_TEST(*it1 == 1); 692 | BOOST_TEST(*it2 == 2); 693 | BOOST_TEST(cb[0] == 1); 694 | BOOST_TEST(cb[1] == 2); 695 | } 696 | 697 | void validity_pop_front_test() { 698 | 699 | // memory placement: { 3, 1, 2 } 700 | // circular buffer: { 1, 2, 3 } 701 | circular_buffer cb(3); 702 | cb.push_back(0); 703 | cb.push_back(1); 704 | cb.push_back(2); 705 | cb.push_back(3); 706 | 707 | // it1 -> 2, it2 -> 3 708 | circular_buffer::iterator it1 = cb.begin() + 1; 709 | circular_buffer::iterator it2 = cb.begin() + 2; 710 | 711 | cb.pop_front(); 712 | 713 | // memory placement: { 3, X, 2 } 714 | // circular buffer: { 2, 3 } 715 | BOOST_TEST(*it1 == 2); 716 | BOOST_TEST(*it2 == 3); 717 | BOOST_TEST(cb[0] == 2); 718 | BOOST_TEST(cb[1] == 3); 719 | } 720 | 721 | // test main 722 | int main() 723 | { 724 | validity_example_test(); 725 | validity_insert_test(); 726 | validity_insert_n_test(); 727 | validity_insert_range_test(); 728 | validity_rinsert_test(); 729 | validity_rinsert_n_test(); 730 | validity_rinsert_range_test(); 731 | validity_erase_test(); 732 | validity_erase_range_test(); 733 | validity_rerase_test(); 734 | validity_rerase_range_test(); 735 | validity_linearize_test(); 736 | validity_swap_test(); 737 | validity_push_back_test(); 738 | validity_push_front_test(); 739 | validity_pop_back_test(); 740 | validity_pop_front_test(); 741 | return boost::report_errors(); 742 | } 743 | -------------------------------------------------------------------------------- /test/space_optimized_test.cpp: -------------------------------------------------------------------------------- 1 | // Test of the space optimized adaptor of the circular buffer. 2 | 3 | // Copyright (c) 2003-2008 Jan Gaspar 4 | 5 | // Use, modification, and distribution is subject to the Boost Software 6 | // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at 7 | // http://www.boost.org/LICENSE_1_0.txt) 8 | 9 | #include "test.hpp" 10 | 11 | #define CB_CONTAINER circular_buffer_space_optimized 12 | 13 | #include "common.ipp" 14 | 15 | typedef circular_buffer_space_optimized cb_space_optimized; 16 | typedef cb_space_optimized::capacity_type capacity_ctrl; 17 | 18 | // min_capacity test (it is useful to use a debug tool) 19 | void min_capacity_test() { 20 | 21 | vector v; 22 | v.push_back(1); 23 | v.push_back(2); 24 | v.push_back(3); 25 | v.push_back(4); 26 | v.push_back(5); 27 | 28 | cb_space_optimized cb1(capacity_ctrl(10, 10)); 29 | cb_space_optimized cb2(capacity_ctrl(10, 5), 1); 30 | cb_space_optimized cb3(capacity_ctrl(20, 10), v.begin(), v.end()); 31 | 32 | BOOST_TEST(cb1.size() == 0); 33 | BOOST_TEST(cb1.capacity().capacity() == 10); 34 | BOOST_TEST(cb1.capacity().min_capacity() == 10); 35 | BOOST_TEST(cb2[0] == 1); 36 | BOOST_TEST(cb2.size() == 10); 37 | BOOST_TEST(cb2.capacity() == 10); 38 | BOOST_TEST(cb2.capacity().min_capacity() == 5); 39 | BOOST_TEST(cb3[0] == 1); 40 | BOOST_TEST(cb3.size() == 5); 41 | BOOST_TEST(cb3.capacity() == 20); 42 | BOOST_TEST(cb3.capacity().min_capacity() == 10); 43 | BOOST_TEST(cb1.capacity().min_capacity() <= cb1.internal_capacity()); 44 | BOOST_TEST(cb2.capacity().min_capacity() <= cb2.internal_capacity()); 45 | BOOST_TEST(cb3.capacity().min_capacity() <= cb3.internal_capacity()); 46 | 47 | cb2.erase(cb2.begin() + 2, cb2.end()); 48 | 49 | BOOST_TEST(cb2.size() == 2); 50 | BOOST_TEST(cb2.capacity().min_capacity() <= cb2.internal_capacity()); 51 | 52 | cb2.clear(); 53 | cb3.clear(); 54 | 55 | BOOST_TEST(cb2.empty()); 56 | BOOST_TEST(cb3.empty()); 57 | BOOST_TEST(cb2.capacity().min_capacity() <= cb2.internal_capacity()); 58 | BOOST_TEST(cb3.capacity().min_capacity() <= cb3.internal_capacity()); 59 | } 60 | 61 | void capacity_control_test() { 62 | 63 | circular_buffer_space_optimized::capacity_type c1 = 10; 64 | circular_buffer_space_optimized::capacity_type c2 = 65 | circular_buffer_space_optimized::capacity_type(20, 5); 66 | circular_buffer_space_optimized::capacity_type c3 = c2; 67 | 68 | BOOST_TEST(c1.capacity() == 10); 69 | BOOST_TEST(c1.min_capacity() == 0); 70 | BOOST_TEST(c2.capacity() == 20); 71 | BOOST_TEST(c2.min_capacity() == 5); 72 | BOOST_TEST(c3.capacity() == 20); 73 | BOOST_TEST(c3.min_capacity() == 5); 74 | 75 | c1 = c2; 76 | 77 | BOOST_TEST(c1.capacity() == 20); 78 | BOOST_TEST(c1.min_capacity() == 5); 79 | } 80 | 81 | void specific_constructors_test() { 82 | 83 | cb_space_optimized cb1; 84 | BOOST_TEST(cb1.capacity() == 0); 85 | BOOST_TEST(cb1.capacity().min_capacity() == 0); 86 | BOOST_TEST(cb1.internal_capacity() == 0); 87 | BOOST_TEST(cb1.size() == 0); 88 | 89 | cb1.push_back(1); 90 | cb1.push_back(2); 91 | cb1.push_back(3); 92 | 93 | BOOST_TEST(cb1.size() == 0); 94 | BOOST_TEST(cb1.capacity() == 0); 95 | 96 | vector v; 97 | v.push_back(1); 98 | v.push_back(2); 99 | v.push_back(3); 100 | 101 | cb_space_optimized cb2(v.begin(), v.end()); 102 | 103 | BOOST_TEST(cb2.capacity() == 3); 104 | BOOST_TEST(cb2.capacity().min_capacity() == 0); 105 | BOOST_TEST(cb2.size() == 3); 106 | } 107 | 108 | void shrink_to_fit_test() { 109 | 110 | cb_space_optimized cb(1000); 111 | cb.push_back(1); 112 | cb.push_back(2); 113 | cb.push_back(3); 114 | 115 | BOOST_TEST(cb.size() == 3); 116 | BOOST_TEST(cb.capacity() == 1000); 117 | 118 | size_t internal_capacity = cb.internal_capacity(); 119 | cb_space_optimized(cb).swap(cb); 120 | 121 | BOOST_TEST(cb.size() == 3); 122 | BOOST_TEST(cb.capacity() == 1000); 123 | BOOST_TEST(internal_capacity >= cb.internal_capacity()); 124 | } 125 | 126 | void iterator_invalidation_test() { 127 | 128 | #if BOOST_CB_ENABLE_DEBUG 129 | 130 | cb_space_optimized cb1(10, 1); 131 | cb1.push_back(2); 132 | cb1.push_back(3); 133 | cb1.push_back(4); 134 | cb_space_optimized::iterator it1 = cb1.end(); 135 | cb_space_optimized::const_iterator it2 = cb1.begin(); 136 | cb_space_optimized::iterator it3 = cb1.begin() + 6; 137 | 138 | cb1.set_capacity(10); 139 | BOOST_TEST(it1.is_valid(&cb1)); 140 | BOOST_TEST(!it2.is_valid(&cb1)); 141 | BOOST_TEST(!it3.is_valid(&cb1)); 142 | 143 | it1 = cb1.end(); 144 | it2 = cb1.begin(); 145 | it3 = cb1.begin() + 6; 146 | cb1.rset_capacity(10); 147 | BOOST_TEST(it1.is_valid(&cb1)); 148 | BOOST_TEST(!it2.is_valid(&cb1)); 149 | BOOST_TEST(!it3.is_valid(&cb1)); 150 | 151 | it1 = cb1.end(); 152 | it2 = cb1.begin(); 153 | it3 = cb1.begin() + 6; 154 | cb1.resize(10); 155 | BOOST_TEST(it1.is_valid(&cb1)); 156 | BOOST_TEST(!it2.is_valid(&cb1)); 157 | BOOST_TEST(!it3.is_valid(&cb1)); 158 | 159 | it1 = cb1.end(); 160 | it2 = cb1.begin(); 161 | it3 = cb1.begin() + 6; 162 | cb1.rresize(10); 163 | BOOST_TEST(it1.is_valid(&cb1)); 164 | BOOST_TEST(!it2.is_valid(&cb1)); 165 | BOOST_TEST(!it3.is_valid(&cb1)); 166 | 167 | { 168 | cb_space_optimized cb2(10, 1); 169 | cb2.push_back(2); 170 | cb2.push_back(3); 171 | cb2.push_back(4); 172 | it1 = cb2.end(); 173 | it2 = cb2.begin(); 174 | it3 = cb2.begin() + 6; 175 | } 176 | BOOST_TEST(!it1.is_valid(&cb1)); 177 | BOOST_TEST(!it2.is_valid(&cb1)); 178 | BOOST_TEST(!it3.is_valid(&cb1)); 179 | 180 | #endif // #if BOOST_CB_ENABLE_DEBUG 181 | } 182 | 183 | // test main 184 | int main() 185 | { 186 | run_common_tests(); 187 | min_capacity_test(); 188 | capacity_control_test(); 189 | specific_constructors_test(); 190 | shrink_to_fit_test(); 191 | iterator_invalidation_test(); 192 | return boost::report_errors(); 193 | } 194 | -------------------------------------------------------------------------------- /test/test.hpp: -------------------------------------------------------------------------------- 1 | // Header file for the test of the circular buffer library. 2 | 3 | // Copyright (c) 2003-2008 Jan Gaspar 4 | 5 | // Use, modification, and distribution is subject to the Boost Software 6 | // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at 7 | // http://www.boost.org/LICENSE_1_0.txt) 8 | 9 | #if !defined(BOOST_CIRCULAR_BUFFER_TEST_HPP) 10 | #define BOOST_CIRCULAR_BUFFER_TEST_HPP 11 | 12 | #if defined(_MSC_VER) && _MSC_VER >= 1200 13 | #pragma once 14 | #endif 15 | 16 | #define BOOST_CB_TEST 17 | 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #if !defined(BOOST_NO_EXCEPTIONS) 24 | #include 25 | #endif 26 | 27 | // Integer (substitute for int) - more appropriate for testing 28 | class MyInteger { 29 | private: 30 | int* m_pValue; 31 | static int ms_exception_trigger; 32 | void check_exception() { 33 | if (ms_exception_trigger > 0) { 34 | if (--ms_exception_trigger == 0) { 35 | delete m_pValue; 36 | m_pValue = 0; 37 | #if !defined(BOOST_NO_EXCEPTIONS) 38 | throw std::exception(); 39 | #endif 40 | } 41 | } 42 | } 43 | public: 44 | MyInteger() : m_pValue(new int(0)) { check_exception(); } 45 | MyInteger(int i) : m_pValue(new int(i)) { check_exception(); } 46 | MyInteger(const MyInteger& src) : m_pValue(new int(src)) { check_exception(); } 47 | ~MyInteger() { delete m_pValue; } 48 | MyInteger& operator = (const MyInteger& src) { 49 | if (this == &src) 50 | return *this; 51 | check_exception(); 52 | delete m_pValue; 53 | m_pValue = new int(src); 54 | return *this; 55 | } 56 | operator int () const { return *m_pValue; } 57 | static void set_exception_trigger(int n) { ms_exception_trigger = n; } 58 | }; 59 | 60 | // default constructible class 61 | class MyDefaultConstructible 62 | { 63 | public: 64 | MyDefaultConstructible() : m_n(1) {} 65 | MyDefaultConstructible(int n) : m_n(n) {} 66 | int m_n; 67 | }; 68 | 69 | // class counting instances of self 70 | class InstanceCounter { 71 | public: 72 | InstanceCounter() { increment(); } 73 | InstanceCounter(const InstanceCounter& y) { y.increment(); } 74 | InstanceCounter& operator=(const InstanceCounter& y) { 75 | decrement(); 76 | y.increment(); 77 | return *this; 78 | } 79 | ~InstanceCounter() { decrement(); } 80 | static int count() { return ms_count; } 81 | private: 82 | void increment() const { ++ms_count; } 83 | void decrement() const { --ms_count; } 84 | static int ms_count; 85 | }; 86 | 87 | // dummy class suitable for iterator referencing test 88 | class Dummy { 89 | public: 90 | enum DummyEnum { 91 | eVar, 92 | eFnc, 93 | eConst, 94 | eVirtual 95 | }; 96 | Dummy() : m_n(eVar) {} 97 | virtual ~Dummy() {} 98 | DummyEnum fnc() { return eFnc; } 99 | DummyEnum const_fnc() const { return eConst; } 100 | virtual DummyEnum virtual_fnc() { return eVirtual; } 101 | DummyEnum m_n; 102 | }; 103 | 104 | // simulator of an input iterator 105 | struct MyInputIterator { 106 | typedef std::vector::iterator vector_iterator; 107 | typedef std::input_iterator_tag iterator_category; 108 | typedef int value_type; 109 | typedef int* pointer; 110 | typedef int& reference; 111 | typedef size_t size_type; 112 | typedef ptrdiff_t difference_type; 113 | explicit MyInputIterator(const vector_iterator& it) : m_it(it) {} 114 | 115 | // Default assignment operator 116 | 117 | reference operator * () const { return *m_it; } 118 | pointer operator -> () const { return &(operator*()); } 119 | MyInputIterator& operator ++ () { 120 | ++m_it; 121 | return *this; 122 | } 123 | MyInputIterator operator ++ (int) { 124 | MyInputIterator tmp = *this; 125 | ++*this; 126 | return tmp; 127 | } 128 | bool operator == (const MyInputIterator& it) const { return m_it == it.m_it; } 129 | bool operator != (const MyInputIterator& it) const { return m_it != it.m_it; } 130 | private: 131 | vector_iterator m_it; 132 | }; 133 | 134 | #if defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) && !defined(BOOST_MSVC_STD_ITERATOR) 135 | 136 | inline std::input_iterator_tag iterator_category(const MyInputIterator&) { 137 | return std::input_iterator_tag(); 138 | } 139 | inline int* value_type(const MyInputIterator&) { return 0; } 140 | inline ptrdiff_t* distance_type(const MyInputIterator&) { return 0; } 141 | 142 | #endif // #if defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) && !defined(BOOST_MSVC_STD_ITERATOR) 143 | 144 | using namespace boost; 145 | using namespace std; 146 | 147 | #endif // #if !defined(BOOST_CIRCULAR_BUFFER_TEST_HPP) 148 | --------------------------------------------------------------------------------