Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions include/boost/serialization/collection_size_type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
#define BOOST_SERIALIZATION_COLLECTION_SIZE_TYPE_HPP

// (C) Copyright 2005 Matthias Troyer
// Copyright 2026 Gennaro Prota
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#include <cstddef> // size_t
#include <istream>
#include <boost/serialization/strong_typedef.hpp>
#include <boost/serialization/level.hpp>
#include <boost/serialization/split_free.hpp>
Expand Down Expand Up @@ -41,9 +43,23 @@ class collection_size_type {
operator base_type () const {
return t;
}
// used for text input
operator base_type & () {
return t;
collection_size_type & operator++(){
++t;
return *this;
}
collection_size_type operator++(int){
collection_size_type old(*this);
++t;
return old;
}
collection_size_type & operator--(){
--t;
return *this;
}
collection_size_type operator--(int){
collection_size_type old(*this);
--t;
return old;
}
bool operator==(const collection_size_type & rhs) const {
return t == rhs.t;
Expand All @@ -53,6 +69,14 @@ class collection_size_type {
}
};

template<class Ch, class Tr>
std::basic_istream<Ch, Tr> &
operator>>(std::basic_istream<Ch, Tr> & is, collection_size_type & t){
std::size_t x = 0;
is >> x;
t = collection_size_type(x);
return is;
}

} } // end namespace boost::serialization

Expand Down
1 change: 1 addition & 0 deletions test/Jamfile.v2
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ test-suite "serialization" :
[ test-bsl-run_files test_class_info_load ]
[ test-bsl-run_files test_class_template_version ]
[ test-bsl-run_files test_bitset ]
[ test-bsl-run_files test_collection_size_type ]
[ test-bsl-run_files test_complex ]
[ test-bsl-run_files test_contained_class : A ]
[ test-bsl-run_files test_cyclic_ptrs : A ]
Expand Down
74 changes: 74 additions & 0 deletions test/test_collection_size_type.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
// test_collection_size_type.cpp

// Copyright 2026 Gennaro Prota
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// https://www.boost.org/LICENSE_1_0.txt)

// Exercises the interface of collection_size_type. See also
// issue #192.

#include <cstddef>
#include <cstdio>
#include <fstream>

#include <boost/config.hpp>

#if defined(BOOST_NO_STDC_NAMESPACE)
namespace std{
using ::remove;
}
#endif

#include <boost/serialization/collection_size_type.hpp>
#include <boost/serialization/nvp.hpp>
#include "test_tools.hpp"

int test_main(int /* argc */, char * /* argv */[]){
using boost::serialization::collection_size_type;

collection_size_type c(5);
BOOST_CHECK(static_cast<std::size_t>(c) == 5);
BOOST_CHECK(c == collection_size_type(5));
BOOST_CHECK(collection_size_type(4) < c);

++c;
BOOST_CHECK(static_cast<std::size_t>(c) == 6);
--c;
BOOST_CHECK(static_cast<std::size_t>(c) == 5);

const collection_size_type post_inc = c++;
BOOST_CHECK(static_cast<std::size_t>(post_inc) == 5);
BOOST_CHECK(static_cast<std::size_t>(c) == 6);
const collection_size_type post_dec = c--;
BOOST_CHECK(static_cast<std::size_t>(post_dec) == 6);
BOOST_CHECK(static_cast<std::size_t>(c) == 5);

collection_size_type count(3);
std::size_t iterations = 0;
while(count-- > 0){
++iterations;
}
BOOST_CHECK(iterations == 3);

// round trip through an archive
const char * testfile = boost::archive::tmpnam(NULL);
BOOST_REQUIRE(NULL != testfile);
const collection_size_type saved(1234);
{
test_ostream os(testfile, TEST_STREAM_FLAGS);
test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
oa << boost::serialization::make_nvp("count", saved);
}
collection_size_type loaded(0);
{
test_istream is(testfile, TEST_STREAM_FLAGS);
test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
ia >> boost::serialization::make_nvp("count", loaded);
}
BOOST_CHECK(static_cast<std::size_t>(loaded) == 1234);

std::remove(testfile);
return EXIT_SUCCESS;
}
Loading