• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Boost.Assign library
2 //
3 //  Copyright Thorsten Ottosen 2003-2004. Use, modification and
4 //  distribution is subject to the Boost Software License, Version
5 //  1.0. (See accompanying file LICENSE_1_0.txt or copy at
6 //  http://www.boost.org/LICENSE_1_0.txt)
7 //
8 // For more information, see http://www.boost.org/libs/assign/
9 //
10 
11 
12 #include <boost/detail/workaround.hpp>
13 
14 #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
15 #  pragma warn -8091 // suppress warning in Boost.Test
16 #  pragma warn -8057 // unused argument argc/argv in Boost.Test
17 #endif
18 
19 #include <boost/assign/list_inserter.hpp>
20 #include <boost/test/test_tools.hpp>
21 #include <boost/function.hpp>
22 #include <boost/bind.hpp>
23 #include <vector>
24 #include <stdexcept>
25 
26 namespace ba = boost::assign;
27 
28 
29 
30 template< class C >
31 class range_inserter
32 {
33     typedef typename C::iterator iterator;
34     iterator begin, end;
35 public:
range_inserter(C & c)36     range_inserter( C& c )
37     : begin( c.begin() ), end( c.end() )
38     { }
39 
40     template< class T >
operator ()(T r)41     void operator()( T r )
42     {
43         if( begin == end )
44             throw std::range_error( "range error: <range_inserter>" );
45         *begin = r;
46         ++begin;
47     }
48 };
49 
50 
51 
52 template< class C >
make_range_inserter(C & c)53 inline range_inserter<C> make_range_inserter( C& c )
54 {
55     return range_inserter<C>( c );
56 }
57 
58 
59 
60 template< class T >
61 class my_vector
62 {
63     typedef std::vector<T>                vector_t;
64     typedef typename vector_t::size_type  size_type;
65     vector_t data_;
66 
67 public:
my_vector()68     my_vector() : data_( 10, 0 )
69     { }
70 
71     ba::list_inserter< range_inserter< vector_t >, T >
operator =(T r)72     operator=( T r )
73     {
74         return ba::make_list_inserter( make_range_inserter( data_ ) )( r );
75     }
76 
size() const77     size_type size() const
78     {
79         return data_.size();
80     }
81 
operator [](size_type index)82     const T& operator[]( size_type index )
83     {
84         return data_.at( index );
85     }
86 };
87 
88 
89 
check_list_inserter()90 void check_list_inserter()
91 {
92     using namespace std;
93     using namespace boost::assign;
94 
95     my_vector<int> vec;
96     vec = 1,2,3,4,5,6,7,8,9,10;
97     BOOST_CHECK_EQUAL( vec.size(), 10u );
98     BOOST_CHECK_EQUAL( vec[0], 1 );
99     BOOST_CHECK_EQUAL( vec[9], 10 );
100 }
101 
102 
103 
104 #include <boost/test/unit_test.hpp>
105 using boost::unit_test::test_suite;
106 
init_unit_test_suite(int argc,char * argv[])107 test_suite* init_unit_test_suite( int argc, char* argv[] )
108 {
109     test_suite* test = BOOST_TEST_SUITE( "List Test Suite" );
110 
111     test->add( BOOST_TEST_CASE( &check_list_inserter ) );
112 
113     return test;
114 }
115 
116