1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
// Boost.Assign library
//
// Copyright Thorsten Ottosen 2003-2004. Use, modification and
// distribution is subject to 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)
//
// For more information, see http://www.boost.org/libs/assign/
//
#include <boost/detail/workaround.hpp>
#if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
# pragma warn -8091 // supress warning in Boost.Test
# pragma warn -8057 // unused argument argc/argv in Boost.Test
#endif
#include <boost/assign/list_inserter.hpp>
#include <boost/test/test_tools.hpp>
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <vector>
#include <map>
namespace ba = boost::assign;
class email
{
public:
enum address_option
{
check_addr_book,
dont_check_addr_book
};
typedef std::pair<std::string,address_option> bcc_type;
typedef std::vector< bcc_type > bcc_map;
typedef std::map<std::string,address_option> address_map;
private:
mutable address_map cc_list;
mutable address_map to_list;
bcc_map bcc_list;
struct add_to_map
{
address_map& m;
add_to_map( address_map& m ) : m(m)
{}
void operator()( const std::string& name, address_option ao )
{
m[ name ] = ao;
}
void operator()( const std::string& name )
{
m[ name ] = check_addr_book;
}
};
struct add_to_vector
{
bcc_map& m;
add_to_vector( bcc_map& m ) : m(m)
{}
void operator()( const bcc_type& r )
{
m.push_back( r );
}
};
public:
ba::list_inserter< add_to_map >
add_cc( std::string name, address_option ao )
{
return ba::make_list_inserter( add_to_map( cc_list ) )( name, ao );
}
ba::list_inserter< add_to_map >
add_to( const std::string& name )
{
return ba::make_list_inserter( add_to_map( to_list ) )( name );
}
ba::list_inserter< add_to_vector, bcc_type >
add_bcc( const bcc_type& bcc )
{
return ba::make_list_inserter( add_to_vector( bcc_list ) )( bcc );
}
address_option
cc_at( const std::string& name ) const
{
return cc_list[ name ];
}
address_option
to_at( const std::string& name ) const
{
return to_list[ name ];
}
address_option
bcc_at( unsigned index ) const
{
return bcc_list.at( index ).second;
}
};
void check_list_inserter()
{
using namespace boost::assign;
email e;
e.add_cc( "franz", email::dont_check_addr_book )
( "hanz", email::check_addr_book )
( "betty", email::dont_check_addr_book );
BOOST_CHECK_EQUAL( e.cc_at( "franz" ), email::dont_check_addr_book );
BOOST_CHECK_EQUAL( e.cc_at( "hanz" ), email::check_addr_book );
BOOST_CHECK_EQUAL( e.cc_at( "betty" ), email::dont_check_addr_book );
e.add_to( "betsy" )( "peter" );
BOOST_CHECK_EQUAL( e.cc_at( "betsy" ), email::check_addr_book );
BOOST_CHECK_EQUAL( e.cc_at( "peter" ), email::check_addr_book );
e.add_bcc( email::bcc_type( "Mr. Foo", email::check_addr_book ) )
( "Mr. Bar", email::dont_check_addr_book );
BOOST_CHECK_EQUAL( e.bcc_at( 0 ), email::check_addr_book );
BOOST_CHECK_EQUAL( e.bcc_at( 1 ), email::dont_check_addr_book );
}
#include <boost/test/unit_test.hpp>
using boost::unit_test::test_suite;
test_suite* init_unit_test_suite( int argc, char* argv[] )
{
test_suite* test = BOOST_TEST_SUITE( "List Test Suite" );
test->add( BOOST_TEST_CASE( &check_list_inserter ) );
return test;
}
|