| 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(BOOST_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 <map> |
| 25 | |
| 26 | namespace ba = boost::assign; |
| 27 | |
| 28 | class email |
| 29 | { |
| 30 | public: |
| 31 | enum address_option |
| 32 | { |
| 33 | check_addr_book, |
| 34 | dont_check_addr_book |
| 35 | }; |
| 36 | |
| 37 | typedef std::pair<std::string,address_option> bcc_type; |
| 38 | typedef std::vector< bcc_type > bcc_map; |
| 39 | typedef std::map<std::string,address_option> address_map; |
| 40 | |
| 41 | |
| 42 | private: |
| 43 | |
| 44 | mutable address_map cc_list; |
| 45 | mutable address_map to_list; |
| 46 | bcc_map bcc_list; |
| 47 | |
| 48 | struct add_to_map |
| 49 | { |
| 50 | address_map& m; |
| 51 | |
| 52 | add_to_map( address_map& m ) : m(m) |
| 53 | {} |
| 54 | |
| 55 | void operator()( const std::string& name, address_option ao ) |
| 56 | { |
| 57 | m[ name ] = ao; |
| 58 | } |
| 59 | |
| 60 | void operator()( const std::string& name ) |
| 61 | { |
| 62 | m[ name ] = check_addr_book; |
| 63 | } |
| 64 | }; |
| 65 | |
| 66 | struct add_to_vector |
| 67 | { |
| 68 | bcc_map& m; |
| 69 | |
| 70 | add_to_vector( bcc_map& m ) : m(m) |
| 71 | {} |
| 72 | |
| 73 | void operator()( const bcc_type& r ) |
| 74 | { |
| 75 | m.push_back( x: r ); |
| 76 | } |
| 77 | }; |
| 78 | |
| 79 | public: |
| 80 | |
| 81 | ba::list_inserter< add_to_map > |
| 82 | add_cc( std::string name, address_option ao ) |
| 83 | { |
| 84 | return ba::make_list_inserter( fun: add_to_map( cc_list ) )( name, ao ); |
| 85 | } |
| 86 | |
| 87 | ba::list_inserter< add_to_map > |
| 88 | add_to( const std::string& name ) |
| 89 | { |
| 90 | return ba::make_list_inserter( fun: add_to_map( to_list ) )( name ); |
| 91 | } |
| 92 | |
| 93 | ba::list_inserter< add_to_vector, bcc_type > |
| 94 | add_bcc( const bcc_type& bcc ) |
| 95 | { |
| 96 | return ba::make_list_inserter( fun: add_to_vector( bcc_list ) )( bcc ); |
| 97 | } |
| 98 | |
| 99 | address_option |
| 100 | cc_at( const std::string& name ) const |
| 101 | { |
| 102 | return cc_list[ name ]; |
| 103 | } |
| 104 | |
| 105 | address_option |
| 106 | to_at( const std::string& name ) const |
| 107 | { |
| 108 | return to_list[ name ]; |
| 109 | } |
| 110 | |
| 111 | address_option |
| 112 | bcc_at( unsigned index ) const |
| 113 | { |
| 114 | return bcc_list.at( n: index ).second; |
| 115 | } |
| 116 | }; |
| 117 | |
| 118 | |
| 119 | |
| 120 | void check_list_inserter() |
| 121 | { |
| 122 | using namespace boost::assign; |
| 123 | |
| 124 | email e; |
| 125 | e.add_cc( name: "franz" , ao: email::dont_check_addr_book ) |
| 126 | ( "hanz" , email::check_addr_book ) |
| 127 | ( "betty" , email::dont_check_addr_book ); |
| 128 | BOOST_CHECK_EQUAL( e.cc_at( "franz" ), email::dont_check_addr_book ); |
| 129 | BOOST_CHECK_EQUAL( e.cc_at( "hanz" ), email::check_addr_book ); |
| 130 | BOOST_CHECK_EQUAL( e.cc_at( "betty" ), email::dont_check_addr_book ); |
| 131 | |
| 132 | e.add_to( name: "betsy" )( "peter" ); |
| 133 | BOOST_CHECK_EQUAL( e.cc_at( "betsy" ), email::check_addr_book ); |
| 134 | BOOST_CHECK_EQUAL( e.cc_at( "peter" ), email::check_addr_book ); |
| 135 | |
| 136 | e.add_bcc( bcc: email::bcc_type( "Mr. Foo" , email::check_addr_book ) ) |
| 137 | ( "Mr. Bar" , email::dont_check_addr_book ); |
| 138 | BOOST_CHECK_EQUAL( e.bcc_at( 0 ), email::check_addr_book ); |
| 139 | BOOST_CHECK_EQUAL( e.bcc_at( 1 ), email::dont_check_addr_book ); |
| 140 | |
| 141 | } |
| 142 | |
| 143 | |
| 144 | #include <boost/test/unit_test.hpp> |
| 145 | using boost::unit_test::test_suite; |
| 146 | |
| 147 | test_suite* init_unit_test_suite( int argc, char* argv[] ) |
| 148 | { |
| 149 | test_suite* test = BOOST_TEST_SUITE( "List Test Suite" ); |
| 150 | |
| 151 | test->add( BOOST_TEST_CASE( &check_list_inserter ) ); |
| 152 | |
| 153 | return test; |
| 154 | } |
| 155 | |
| 156 | |