Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/assign/test/my_vector_example.cpp @ 29

Last change on this file since 29 was 29, checked in by landauf, 16 years ago

updated boost from 1_33_1 to 1_34_1

File size: 2.3 KB
Line 
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 // supress 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
26namespace ba = boost::assign;
27
28
29
30template< class C >
31class range_inserter
32{
33    typedef typename C::iterator iterator;
34    iterator begin, end;
35public:
36    range_inserter( C& c )
37    : begin( c.begin() ), end( c.end() )
38    { }
39   
40    template< class T >
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
52template< class C >
53inline range_inserter<C> make_range_inserter( C& c )
54{
55    return range_inserter<C>( c );
56}
57
58
59
60template< class T >
61class my_vector
62{
63    typedef std::vector<T>                vector_t;
64    typedef typename vector_t::size_type  size_type;
65    vector_t data_;
66
67public:
68    my_vector() : data_( 10, 0 )
69    { }
70   
71    ba::list_inserter< range_inserter< vector_t >, T >
72    operator=( T r )
73    {
74        return ba::make_list_inserter( make_range_inserter( data_ ) )( r );
75    }
76   
77    size_type size() const
78    {
79        return data_.size();
80    }
81   
82    const T& operator[]( size_type index )
83    {
84        return data_.at( index );
85    }
86};
87
88
89
90void 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>
105using boost::unit_test::test_suite;
106
107test_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
Note: See TracBrowser for help on using the repository browser.