email_example.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. #include <boost/detail/workaround.hpp>
  11. #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
  12. # pragma warn -8091 // suppress warning in Boost.Test
  13. # pragma warn -8057 // unused argument argc/argv in Boost.Test
  14. #endif
  15. #include <boost/assign/list_inserter.hpp>
  16. #include <boost/test/test_tools.hpp>
  17. #include <boost/function.hpp>
  18. #include <boost/bind.hpp>
  19. #include <vector>
  20. #include <map>
  21. namespace ba = boost::assign;
  22. class email
  23. {
  24. public:
  25. enum address_option
  26. {
  27. check_addr_book,
  28. dont_check_addr_book
  29. };
  30. typedef std::pair<std::string,address_option> bcc_type;
  31. typedef std::vector< bcc_type > bcc_map;
  32. typedef std::map<std::string,address_option> address_map;
  33. private:
  34. mutable address_map cc_list;
  35. mutable address_map to_list;
  36. bcc_map bcc_list;
  37. struct add_to_map
  38. {
  39. address_map& m;
  40. add_to_map( address_map& m ) : m(m)
  41. {}
  42. void operator()( const std::string& name, address_option ao )
  43. {
  44. m[ name ] = ao;
  45. }
  46. void operator()( const std::string& name )
  47. {
  48. m[ name ] = check_addr_book;
  49. }
  50. };
  51. struct add_to_vector
  52. {
  53. bcc_map& m;
  54. add_to_vector( bcc_map& m ) : m(m)
  55. {}
  56. void operator()( const bcc_type& r )
  57. {
  58. m.push_back( r );
  59. }
  60. };
  61. public:
  62. ba::list_inserter< add_to_map >
  63. add_cc( std::string name, address_option ao )
  64. {
  65. return ba::make_list_inserter( add_to_map( cc_list ) )( name, ao );
  66. }
  67. ba::list_inserter< add_to_map >
  68. add_to( const std::string& name )
  69. {
  70. return ba::make_list_inserter( add_to_map( to_list ) )( name );
  71. }
  72. ba::list_inserter< add_to_vector, bcc_type >
  73. add_bcc( const bcc_type& bcc )
  74. {
  75. return ba::make_list_inserter( add_to_vector( bcc_list ) )( bcc );
  76. }
  77. address_option
  78. cc_at( const std::string& name ) const
  79. {
  80. return cc_list[ name ];
  81. }
  82. address_option
  83. to_at( const std::string& name ) const
  84. {
  85. return to_list[ name ];
  86. }
  87. address_option
  88. bcc_at( unsigned index ) const
  89. {
  90. return bcc_list.at( index ).second;
  91. }
  92. };
  93. void check_list_inserter()
  94. {
  95. using namespace boost::assign;
  96. email e;
  97. e.add_cc( "franz", email::dont_check_addr_book )
  98. ( "hanz", email::check_addr_book )
  99. ( "betty", email::dont_check_addr_book );
  100. BOOST_CHECK_EQUAL( e.cc_at( "franz" ), email::dont_check_addr_book );
  101. BOOST_CHECK_EQUAL( e.cc_at( "hanz" ), email::check_addr_book );
  102. BOOST_CHECK_EQUAL( e.cc_at( "betty" ), email::dont_check_addr_book );
  103. e.add_to( "betsy" )( "peter" );
  104. BOOST_CHECK_EQUAL( e.cc_at( "betsy" ), email::check_addr_book );
  105. BOOST_CHECK_EQUAL( e.cc_at( "peter" ), email::check_addr_book );
  106. e.add_bcc( email::bcc_type( "Mr. Foo", email::check_addr_book ) )
  107. ( "Mr. Bar", email::dont_check_addr_book );
  108. BOOST_CHECK_EQUAL( e.bcc_at( 0 ), email::check_addr_book );
  109. BOOST_CHECK_EQUAL( e.bcc_at( 1 ), email::dont_check_addr_book );
  110. }
  111. #include <boost/test/unit_test.hpp>
  112. using boost::unit_test::test_suite;
  113. test_suite* init_unit_test_suite( int argc, char* argv[] )
  114. {
  115. test_suite* test = BOOST_TEST_SUITE( "List Test Suite" );
  116. test->add( BOOST_TEST_CASE( &check_list_inserter ) );
  117. return test;
  118. }