source: NonGTP/Boost/boost/spirit/fusion/sequence/detail/io.hpp @ 857

Revision 857, 4.3 KB checked in by igarcia, 19 years ago (diff)
Line 
1/*=============================================================================
2    Copyright (c) 1999-2003 Jaakko Järvi
3    Copyright (c) 1999-2003 Jeremiah Willcock
4    Copyright (c) 2001-2003 Joel de Guzman
5
6    Use, modification and distribution is subject to the Boost Software
7    License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
8    http://www.boost.org/LICENSE_1_0.txt)
9==============================================================================*/
10#if !defined(FUSION_SEQUENCE_DETAIL_IO_HPP)
11#define FUSION_SEQUENCE_DETAIL_IO_HPP
12
13#include <iostream>
14#include <boost/spirit/fusion/sequence/detail/manip.hpp>
15
16#include <boost/mpl/bool.hpp>
17#include <boost/spirit/fusion/sequence/begin.hpp>
18#include <boost/spirit/fusion/sequence/end.hpp>
19#include <boost/spirit/fusion/iterator/deref.hpp>
20#include <boost/spirit/fusion/iterator/next.hpp>
21#include <boost/spirit/fusion/iterator/equal_to.hpp>
22
23namespace boost { namespace fusion { namespace detail
24{
25    template <typename Tag>
26    struct delimiter_io
27    {
28        // print a delimiter
29        template <typename OS>
30        static void
31        print(OS& os, char const* delim, mpl::false_ = mpl::false_())
32        {
33            detail::string_ios_manip<Tag, OS> manip(os);
34            manip.print(delim);
35        }
36
37        template <typename OS>
38        static void
39        print(OS& os, char const* delim, mpl::true_)
40        {
41        }
42
43        // read a delimiter
44        template <typename IS>
45        static void
46        read(IS& is, char const* delim, mpl::false_ = mpl::false_())
47        {
48            detail::string_ios_manip<Tag, IS> manip(is);
49            manip.read(delim);
50        }
51
52        template <typename IS>
53        static void
54        read(IS& is, char const* delim, mpl::true_)
55        {
56        }
57    };
58
59    struct print_sequence_loop
60    {
61        template <typename OS, typename First, typename Last>
62        static void
63        call(OS& os, First const&, Last const&, mpl::true_)
64        {
65        }
66
67        template <typename OS, typename First, typename Last>
68        static void
69        call(OS& os, First const& first, Last const& last, mpl::false_)
70        {
71            meta::equal_to<
72                BOOST_DEDUCED_TYPENAME meta::next<First>::type
73              , Last
74            >
75            is_last;
76
77            os << *first;
78            delimiter_io<tuple_delimiter_tag>::print(os, " ", is_last);
79            call(os, fusion::next(first), last, is_last);
80        }
81
82        template <typename OS, typename First, typename Last>
83        static void
84        call(OS& os, First const& first, Last const& last)
85        {
86            meta::equal_to<First, Last> eq;
87            call(os, first, last, eq);
88        }
89    };
90
91    struct read_sequence_loop
92    {
93        template <typename IS, typename First, typename Last>
94        static void
95        call(IS& is, First const&, Last const&, mpl::true_)
96        {
97        }
98
99        template <typename IS, typename First, typename Last>
100        static void
101        call(IS& is, First const& first, Last const& last, mpl::false_)
102        {
103            meta::equal_to<
104                BOOST_DEDUCED_TYPENAME meta::next<First>::type
105              , Last
106            >
107            is_last;
108
109            is >> *first;
110            delimiter_io<tuple_delimiter_tag>::read(is, " ", is_last);
111            call(is, fusion::next(first), last, is_last);
112        }
113
114        template <typename IS, typename First, typename Last>
115        static void
116        call(IS& is, First const& first, Last const& last)
117        {
118            meta::equal_to<First, Last> eq;
119            call(is, first, last, eq);
120        }
121    };
122
123    template <typename OS, typename Sequence>
124    inline void
125    print_sequence(OS& os, Sequence const& seq)
126    {
127        delimiter_io<tuple_open_tag>::print(os, "(");
128        print_sequence_loop::call(os, fusion::begin(seq), fusion::end(seq));
129        delimiter_io<tuple_close_tag>::print(os, ")");
130    }
131
132    template <typename IS, typename Sequence>
133    inline void
134    read_sequence(IS& is, Sequence& seq)
135    {
136        delimiter_io<tuple_open_tag>::read(is, "(");
137        read_sequence_loop::call(is, fusion::begin(seq), fusion::end(seq));
138        delimiter_io<tuple_close_tag>::read(is, ")");
139    }
140}}}
141
142#endif
Note: See TracBrowser for help on using the repository browser.