summaryrefslogtreecommitdiffstats
blob: ff523ab9723c9a7bc45c43cf7a3db6f153926869 (plain)
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
156
157
158
159
/*=============================================================================
    Copyright (c) 2001-2011 Joel de Guzman

    Distributed under 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)
==============================================================================*/
#if !defined(BOOST_SPIRIT_INFO_NOVEMBER_22_2008_1132AM)
#define BOOST_SPIRIT_INFO_NOVEMBER_22_2008_1132AM

#if defined(_MSC_VER)
#pragma once
#endif

#include <boost/variant/variant.hpp>
#include <boost/variant/recursive_variant.hpp>
#include <boost/variant/apply_visitor.hpp>
#include <boost/foreach.hpp>
#include <boost/spirit/home/support/utf8.hpp>
#include <list>
#include <iterator>
#include <utility>

namespace boost { namespace spirit
{
    // info provides information about a component. Each component
    // has a what member function that returns an info object.
    // strings in the info object are assumed to be encoded as UTF8
    // for uniformity.
    struct info
    {
        struct nil {};

        typedef
            boost::variant<
                nil
              , utf8_string
              , recursive_wrapper<info>
              , recursive_wrapper<std::pair<info, info> >
              , recursive_wrapper<std::list<info> >
            >
        value_type;

        explicit info(utf8_string const& tag)
          : tag(tag), value(nil()) {}

        template <typename T>
        info(utf8_string const& tag, T const& value)
          : tag(tag), value(value) {}

        info(utf8_string const& tag, char value)
          : tag(tag), value(utf8_string(1, value)) {}

        info(utf8_string const& tag, wchar_t value)
          : tag(tag), value(to_utf8(value)) {}

        info(utf8_string const& tag, ucs4_char value)
          : tag(tag), value(to_utf8(value)) {}

        template <typename Char>
        info(utf8_string const& tag, Char const* str)
          : tag(tag), value(to_utf8(str)) {}

        template <typename Char, typename Traits, typename Allocator>
        info(utf8_string const& tag
              , std::basic_string<Char, Traits, Allocator> const& str)
          : tag(tag), value(to_utf8(str)) {}

        utf8_string tag;
        value_type value;
    };

    template <typename Callback>
    struct basic_info_walker
    {
        typedef void result_type;
        typedef basic_info_walker<Callback> this_type;

        basic_info_walker(Callback& callback, utf8_string const& tag, int depth)
          : callback(callback), tag(tag), depth(depth) {}

        void operator()(info::nil) const
        {
            callback.element(tag, "", depth);
        }

        void operator()(utf8_string const& str) const
        {
            callback.element(tag, str, depth);
        }

        void operator()(info const& what) const
        {
            boost::apply_visitor(
                this_type(callback, what.tag, depth+1), what.value);
        }

        void operator()(std::pair<info, info> const& pair) const
        {
            callback.element(tag, "", depth);
            boost::apply_visitor(
                this_type(callback, pair.first.tag, depth+1), pair.first.value);
            boost::apply_visitor(
                this_type(callback, pair.second.tag, depth+1), pair.second.value);
        }

        void operator()(std::list<info> const& l) const
        {
            callback.element(tag, "", depth);
            BOOST_FOREACH(info const& what, l)
            {
                boost::apply_visitor(
                    this_type(callback, what.tag, depth+1), what.value);
            }
        }

        Callback& callback;
        utf8_string const& tag;
        int depth;

    private:
        // silence MSVC warning C4512: assignment operator could not be generated
        basic_info_walker& operator= (basic_info_walker const&);
    };

    // bare-bones print support
    template <typename Out>
    struct simple_printer
    {
        typedef utf8_string string;

        simple_printer(Out& out)
          : out(out) {}

        void element(string const& tag, string const& value, int /*depth*/) const
        {
            if (value == "")
                out << '<' << tag << '>';
            else
                out << '"' << value << '"';
        }

        Out& out;

    private:
        // silence MSVC warning C4512: assignment operator could not be generated
        simple_printer& operator= (simple_printer const&);
    };

    template <typename Out>
    Out& operator<<(Out& out, info const& what)
    {
        simple_printer<Out> pr(out);
        basic_info_walker<simple_printer<Out> > walker(pr, what.tag, 0);
        boost::apply_visitor(walker, what.value);
        return out;
    }
}}

#endif