Skip to content

Commit d7ffcab

Browse files
committed
Moved shared_container_iterator.hpp in iterator directory, modernized code.
shared_container_iterator now uses std::shared_ptr to store the reference to the container. boost::shared_ptr is still supported and is wrapped into std::shared_ptr on construction, so there is overhead due to allocation of std::shared_ptr state. Going forward, std::shared_ptr is expected to be the primary use case. As a bonus, this eliminates the dependency on Boost.SmartPtr. Moved shared_container_iterator.hpp into the iterator directory and left a forwarding header for backward compatibility.
1 parent 14efe7c commit d7ffcab

File tree

7 files changed

+151
-78
lines changed

7 files changed

+151
-78
lines changed

CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ target_link_libraries(boost_iterator
2222
Boost::fusion
2323
Boost::mpl
2424
Boost::optional
25-
Boost::smart_ptr
2625
Boost::type_traits
2726
Boost::utility
2827
)

build.jam

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ constant boost_dependencies :
1414
/boost/fusion//boost_fusion
1515
/boost/mpl//boost_mpl
1616
/boost/optional//boost_optional
17-
/boost/smart_ptr//boost_smart_ptr
1817
/boost/type_traits//boost_type_traits
1918
/boost/utility//boost_utility ;
2019

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
// (C) Copyright Ronald Garcia 2002. Permission to copy, use, modify, sell and
2+
// distribute this software is granted provided this copyright notice appears
3+
// in all copies. This software is provided "as is" without express or implied
4+
// warranty, and with no claim as to its suitability for any purpose.
5+
6+
// See http://www.boost.org/libs/utility/shared_container_iterator.html for documentation.
7+
8+
#ifndef BOOST_ITERATOR_SHARED_CONTAINER_ITERATOR_HPP_INCLUDED_
9+
#define BOOST_ITERATOR_SHARED_CONTAINER_ITERATOR_HPP_INCLUDED_
10+
11+
#include <memory>
12+
#include <utility>
13+
#include <boost/iterator/iterator_adaptor.hpp>
14+
15+
namespace boost {
16+
17+
// For backward compatibility with boost::shared_ptr
18+
template< class T >
19+
class shared_ptr;
20+
21+
namespace iterators {
22+
namespace detail {
23+
24+
// Fake deleter that holds an instance of boost::shared_ptr and through it keeps the pointed object from deletion
25+
template< typename T >
26+
class shared_container_iterator_bsptr_holder
27+
{
28+
private:
29+
boost::shared_ptr< T > m_ptr;
30+
31+
public:
32+
explicit shared_container_iterator_bsptr_holder(boost::shared_ptr< T > const& ptr) :
33+
m_ptr(ptr)
34+
{}
35+
36+
void operator()(T*) const noexcept {}
37+
};
38+
39+
} // namespace detail
40+
41+
template< typename Container >
42+
class shared_container_iterator :
43+
public iterator_adaptor<
44+
shared_container_iterator< Container >,
45+
typename Container::iterator
46+
>
47+
{
48+
private:
49+
using super_t = iterator_adaptor<
50+
shared_container_iterator< Container >,
51+
typename Container::iterator
52+
>;
53+
54+
using iterator_t = typename Container::iterator;
55+
using container_ref_t = std::shared_ptr< Container >;
56+
57+
public:
58+
shared_container_iterator() = default;
59+
60+
shared_container_iterator(iterator_t const& x, container_ref_t const& c) :
61+
super_t(x),
62+
m_container_ref(c)
63+
{}
64+
65+
// Constructor for backward compatibility with boost::shared_ptr
66+
shared_container_iterator(iterator_t const& x, boost::shared_ptr< Container > const& c) :
67+
super_t(x),
68+
m_container_ref(c.get(), detail::shared_container_iterator_bsptr_holder< Container >(c))
69+
{}
70+
71+
private:
72+
container_ref_t m_container_ref;
73+
};
74+
75+
template< typename Container >
76+
inline shared_container_iterator< Container >
77+
make_shared_container_iterator(typename Container::iterator iter, std::shared_ptr< Container > const& container)
78+
{
79+
return shared_container_iterator< Container >(iter, container);
80+
}
81+
82+
template< typename Container >
83+
inline std::pair< shared_container_iterator< Container >, shared_container_iterator< Container > >
84+
make_shared_container_range(std::shared_ptr< Container > const& container)
85+
{
86+
return std::make_pair
87+
(
88+
iterators::make_shared_container_iterator(container->begin(), container),
89+
iterators::make_shared_container_iterator(container->end(), container)
90+
);
91+
}
92+
93+
// Factory functions for backward compatibility with boost::shared_ptr
94+
template< typename Container >
95+
inline shared_container_iterator< Container >
96+
make_shared_container_iterator(typename Container::iterator iter, boost::shared_ptr< Container > const& container)
97+
{
98+
return shared_container_iterator< Container >(iter, container);
99+
}
100+
101+
template< typename Container >
102+
inline std::pair< shared_container_iterator< Container >, shared_container_iterator< Container > >
103+
make_shared_container_range(boost::shared_ptr< Container > const& container)
104+
{
105+
std::shared_ptr< Container > c(container.get(), detail::shared_container_iterator_bsptr_holder< Container >(container));
106+
return iterators::make_shared_container_range(std::move(c));
107+
}
108+
109+
} // namespace iterators
110+
111+
using iterators::shared_container_iterator;
112+
using iterators::make_shared_container_iterator;
113+
using iterators::make_shared_container_range;
114+
115+
} // namespace boost
116+
117+
#endif // BOOST_ITERATOR_SHARED_CONTAINER_ITERATOR_HPP_INCLUDED_
Lines changed: 8 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,14 @@
1-
// (C) Copyright Ronald Garcia 2002. Permission to copy, use, modify, sell and
2-
// distribute this software is granted provided this copyright notice appears
3-
// in all copies. This software is provided "as is" without express or implied
4-
// warranty, and with no claim as to its suitability for any purpose.
5-
6-
// See http://www.boost.org/libs/utility/shared_container_iterator.html for documentation.
1+
// (C) Copyright Andrey Semashev 2025.
2+
// Distributed under the Boost Software License, Version 1.0. (See
3+
// accompanying file LICENSE_1_0.txt or copy at
4+
// http://www.boost.org/LICENSE_1_0.txt)
75

86
#ifndef BOOST_SHARED_CONTAINER_ITERATOR_HPP
97
#define BOOST_SHARED_CONTAINER_ITERATOR_HPP
108

11-
#include "boost/iterator_adaptors.hpp"
12-
#include "boost/shared_ptr.hpp"
13-
#include <utility>
14-
15-
namespace boost {
16-
namespace iterators {
17-
18-
template <typename Container>
19-
class shared_container_iterator : public iterator_adaptor<
20-
shared_container_iterator<Container>,
21-
typename Container::iterator> {
22-
23-
typedef iterator_adaptor<
24-
shared_container_iterator<Container>,
25-
typename Container::iterator> super_t;
26-
27-
typedef typename Container::iterator iterator_t;
28-
typedef boost::shared_ptr<Container> container_ref_t;
29-
30-
container_ref_t container_ref;
31-
public:
32-
shared_container_iterator() { }
33-
34-
shared_container_iterator(iterator_t const& x,container_ref_t const& c) :
35-
super_t(x), container_ref(c) { }
36-
37-
38-
};
39-
40-
template <typename Container>
41-
inline shared_container_iterator<Container>
42-
make_shared_container_iterator(typename Container::iterator iter,
43-
boost::shared_ptr<Container> const& container) {
44-
typedef shared_container_iterator<Container> iterator;
45-
return iterator(iter,container);
46-
}
47-
48-
49-
50-
template <typename Container>
51-
inline std::pair<
52-
shared_container_iterator<Container>,
53-
shared_container_iterator<Container> >
54-
make_shared_container_range(boost::shared_ptr<Container> const& container) {
55-
return
56-
std::make_pair(
57-
make_shared_container_iterator(container->begin(),container),
58-
make_shared_container_iterator(container->end(),container));
59-
}
60-
61-
} // namespace iterators
62-
63-
using iterators::shared_container_iterator;
64-
using iterators::make_shared_container_iterator;
65-
using iterators::make_shared_container_range;
9+
// This is a deprecated header left for backward compatibility.
10+
// Please use <boost/iterator/shared_container_iterator.hpp> instead.
6611

67-
} // namespace boost
12+
#include <boost/iterator/shared_container_iterator.hpp>
6813

69-
#endif
14+
#endif // BOOST_SHARED_CONTAINER_ITERATOR_HPP

test/Jamfile.v2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,5 +74,5 @@ test-suite iterator
7474
[ compile adl_test.cpp : <library>/boost/array//boost_array ]
7575
[ compile range_distance_compat_test.cpp : <library>/boost/range//boost_range ]
7676

77-
[ run shared_iterator_test.cpp ]
77+
[ run shared_iterator_test.cpp : : : <library>/boost/smart_ptr//smart_ptr ]
7878
;

test/shared_iterator_test.cpp

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,35 @@
1414
//
1515

1616

17-
#include "boost/shared_container_iterator.hpp"
18-
#include "boost/shared_ptr.hpp"
17+
#include <boost/iterator/shared_container_iterator.hpp>
18+
#include <boost/shared_ptr.hpp>
1919
#include <boost/core/lightweight_test.hpp>
2020
#include <vector>
21+
#include <memory>
2122

22-
struct resource {
23+
struct resource
24+
{
2325
static int count;
2426
resource() { ++count; }
2527
resource(resource const&) { ++count; }
2628
~resource() { --count; }
2729
};
2830
int resource::count = 0;
2931

30-
typedef std::vector<resource> resources_t;
32+
typedef std::vector< resource > resources_t;
3133

3234
typedef boost::shared_container_iterator< resources_t > iterator;
3335

34-
35-
void set_range(iterator& i, iterator& end) {
36-
37-
boost::shared_ptr< resources_t > objs(new resources_t());
36+
template< typename SharedPtr >
37+
void set_range(iterator& i, iterator& end)
38+
{
39+
SharedPtr objs(new resources_t());
3840

3941
for (int j = 0; j != 6; ++j)
4042
objs->push_back(resource());
4143

42-
i = iterator(objs->begin(),objs);
43-
end = iterator(objs->end(),objs);
44+
i = iterator(objs->begin(), objs);
45+
end = iterator(objs->end(), objs);
4446
BOOST_TEST_EQ(resource::count, 6);
4547
}
4648

@@ -53,7 +55,18 @@ int main() {
5355
iterator i;
5456
{
5557
iterator end;
56-
set_range(i,end);
58+
set_range< boost::shared_ptr< resources_t > >(i, end);
59+
BOOST_TEST_EQ(resource::count, 6);
60+
}
61+
BOOST_TEST_EQ(resource::count, 6);
62+
}
63+
BOOST_TEST_EQ(resource::count, 0);
64+
65+
{
66+
iterator i;
67+
{
68+
iterator end;
69+
set_range< std::shared_ptr< resources_t > >(i, end);
5770
BOOST_TEST_EQ(resource::count, 6);
5871
}
5972
BOOST_TEST_EQ(resource::count, 6);

test/test_cmake/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#include <boost/indirect_reference.hpp>
88
#include <boost/next_prior.hpp>
99
#include <boost/pointee.hpp>
10-
#include <boost/shared_container_iterator.hpp>
1110
#include <boost/iterator/advance.hpp>
1211
#include <boost/iterator/counting_iterator.hpp>
1312
#include <boost/iterator/distance.hpp>
@@ -31,6 +30,7 @@
3130
#include <boost/iterator/new_iterator_tests.hpp>
3231
#include <boost/iterator/permutation_iterator.hpp>
3332
#include <boost/iterator/reverse_iterator.hpp>
33+
#include <boost/iterator/shared_container_iterator.hpp>
3434
#include <boost/iterator/transform_iterator.hpp>
3535
#include <boost/iterator/zip_iterator.hpp>
3636

0 commit comments

Comments
 (0)