Ticket #8270: doc_test.patch

File doc_test.patch, 5.3 KB (added by Jamboree <tongari95@…>, 10 years ago)
  • doc/container.qbk

     
    19181918
    19191919[*Semantics]: Convert a fusion sequence, `seq`, to a __map__.
    19201920
    1921 [*Precondition]: The elements of the sequence are assumed to be
     1921[*Precondition]: For non-associative sequence, the elements are assumed to be
    19221922__fusion_pair__s. There may be no duplicate __fusion_pair__ key types.
    19231923
    19241924[heading Header]
     
    19281928
    19291929[heading Example]
    19301930
     1931    // from sequence of __fusion_pair__
    19311932    as_map(__make_vector__(
    19321933        __fusion_make_pair__<int>('X')
    19331934      , __fusion_make_pair__<double>("Men")))
     1935   
     1936    // from associative sequence
     1937    namespace ns
     1938    {
     1939        struct x_member;
     1940        struct y_member;
     1941    }
     1942    BOOST_FUSION_DEFINE_ASSOC_STRUCT(
     1943        (ns),
     1944        point,
     1945        (int, x, ns::x_member)
     1946        (int, y, ns::y_member)
     1947    )
     1948    ...
     1949    as_map(ns::point(123, 456))
    19341950
    19351951[endsect]
    19361952
     
    21192135
    21202136[*Semantics]: Convert a fusion sequence, `Sequence`, to a __map__.
    21212137
    2122 [*Precondition]: The elements of the sequence are assumed to be
     2138[*Precondition]: For non-associative sequence, the elements are assumed to be
    21232139__fusion_pair__s. There may be no duplicate __fusion_pair__ key types.
    21242140
    21252141[heading Header]
     
    21292145
    21302146[heading Example]
    21312147
     2148    // from sequence of __fusion_pair__
    21322149    result_of::as_map<__vector__<
    21332150        __fusion_pair__<int, char>
    21342151      , __fusion_pair__<double, std::string> > >::type
     2152   
     2153    // from associative sequence
     2154    namespace ns
     2155    {
     2156        struct x_member;
     2157        struct y_member;
     2158    }
     2159    BOOST_FUSION_DEFINE_ASSOC_STRUCT(
     2160        (ns),
     2161        point,
     2162        (int, x, ns::x_member)
     2163        (int, y, ns::y_member)
     2164    )
     2165    ...
     2166    result_of::as_map<ns::point>::type // __map__<__fusion_pair__<ns::x_member, int>, __fusion_pair__<ns::y_member, int> >
    21352167
    21362168[endsect]
    21372169
  • test/Jamfile

     
    5757
    5858    [ run sequence/as_list.cpp :  :  :  : ]
    5959    [ run sequence/as_map.cpp :  :  :  : ]
     60    [ run sequence/as_map_assoc.cpp :  :  :  : ]
    6061    [ run sequence/as_set.cpp :  :  :  : ]
    6162    [ run sequence/as_vector.cpp :  :  :  : ]
    6263    [ run sequence/boost_tuple.cpp :  :  :  : ]
  • test/sequence/as_map_assoc.cpp

     
     1/*=============================================================================
     2    Copyright (c) 2001-2011 Joel de Guzman
     3
     4    Distributed under the Boost Software License, Version 1.0. (See accompanying
     5    file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
     6==============================================================================*/
     7#include <boost/detail/lightweight_test.hpp>
     8#include <boost/fusion/adapted/struct/adapt_assoc_struct.hpp>
     9#include <boost/fusion/container/vector/vector.hpp>
     10#include <boost/fusion/adapted/mpl.hpp>
     11#include <boost/fusion/container/generation/make_vector.hpp>
     12#include <boost/fusion/container/map/convert.hpp>
     13#include <boost/fusion/container/generation/make_set.hpp>
     14#include <boost/fusion/container/vector/convert.hpp>
     15#include <boost/fusion/sequence/comparison/equal_to.hpp>
     16#include <boost/fusion/sequence/intrinsic/at_key.hpp>
     17#include <boost/fusion/sequence/io/out.hpp>
     18#include <boost/fusion/support/pair.hpp>
     19
     20namespace ns
     21{
     22    struct x_member;
     23    struct y_member;
     24    struct z_member;
     25
     26    struct point
     27    {
     28        int x;
     29        int y;
     30    };
     31}
     32
     33BOOST_FUSION_ADAPT_ASSOC_STRUCT(
     34    ns::point,
     35    (int, x, ns::x_member)
     36    (int, y, ns::y_member)
     37)
     38
     39int
     40main()
     41{
     42    using namespace boost::fusion;
     43    using namespace boost;
     44
     45    std::cout << tuple_open('[');
     46    std::cout << tuple_close(']');
     47    std::cout << tuple_delimiter(", ");
     48
     49    {
     50        ns::point p = {123, 456};
     51        std::cout << as_map(p) << std::endl;
     52    }
     53
     54    {
     55        ns::point p = {123, 456};
     56        boost::fusion::result_of::as_map<ns::point>::type map(p);
     57        std::cout << at_key<ns::x_member>(map) << std::endl;
     58        std::cout << at_key<ns::y_member>(map) << std::endl;
     59        BOOST_TEST(at_key<ns::x_member>(map) == 123);
     60        BOOST_TEST(at_key<ns::y_member>(map) == 456);
     61    }
     62
     63    {
     64        boost::fusion::result_of::as_map<set<int, char> >::type map(make_set(1, '2'));
     65        BOOST_TEST(at_key<int>(map) == 1);
     66        BOOST_TEST(at_key<char>(map) == '2');
     67    }
     68   
     69    {
     70        // test conversion
     71        typedef map<
     72            pair<ns::x_member, int>
     73          , pair<ns::y_member, int> >
     74        map_type;
     75
     76        ns::point p = {123, 456};
     77        map_type m(p);
     78        BOOST_TEST(as_vector(m) == make_vector(make_pair<ns::x_member>(123), make_pair<ns::y_member>(456)));
     79        m = (make_vector(make_pair<ns::x_member>(123), make_pair<ns::y_member>(456))); // test assign
     80        BOOST_TEST(as_vector(m) == make_vector(make_pair<ns::x_member>(123), make_pair<ns::y_member>(456)));
     81    }
     82
     83    return boost::report_errors();
     84}
     85