aboutsummaryrefslogtreecommitdiff
path: root/paludis/spec_tree.hh
blob: 37b09967fab9662f55d1b905356ef4953d51b42a (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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2008, 2009, 2010, 2011 Ciaran McCreesh
 *
 * This file is part of the Paludis package manager. Paludis is free software;
 * you can redistribute it and/or modify it under the terms of the GNU General
 * Public License version 2, as published by the Free Software Foundation.
 *
 * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA  02111-1307  USA
 */

#ifndef PALUDIS_GUARD_PALUDIS_SPEC_TREE_HH
#define PALUDIS_GUARD_PALUDIS_SPEC_TREE_HH 1

#include <paludis/spec_tree-fwd.hh>
#include <paludis/util/wrapped_forward_iterator.hh>
#include <paludis/util/simple_visitor.hh>
#include <paludis/util/sequence.hh>
#include <type_traits>

namespace paludis
{
    namespace spec_tree_internals
    {
        template <typename Tree_>
        class PALUDIS_VISIBLE BasicNode :
            public virtual DeclareAbstractAcceptMethods<BasicNode<Tree_>, typename Tree_::VisitableTypeList>
        {
        };

        template <typename Tree_, typename Item_>
        class PALUDIS_VISIBLE LeafNode :
            public BasicNode<Tree_>,
            public ImplementAcceptMethods<BasicNode<Tree_>, LeafNode<Tree_, Item_> >
        {
            private:
                const std::shared_ptr<const Item_> _spec;

            public:
                explicit LeafNode(const std::shared_ptr<const Item_> & i);

                template <typename OtherTree_>
                operator LeafNode<OtherTree_, Item_> () const;

                const std::shared_ptr<const Item_> spec() const;
        };

        template <typename Tree_, typename Item_>
        struct InnerNode;

        template <typename Tree_>
        class PALUDIS_VISIBLE BasicInnerNode :
            public BasicNode<Tree_>
        {
            private:
                typedef Sequence<std::shared_ptr<const BasicNode<Tree_> > > ChildList;
                std::shared_ptr<ChildList> _child_list;

            public:
                BasicInnerNode();

                typedef BasicInnerNodeConstIteratorTag<Tree_> ConstIteratorTag;
                typedef WrappedForwardIterator<ConstIteratorTag,
                        const std::shared_ptr<const BasicNode<Tree_> > > ConstIterator;

                ConstIterator begin() const;

                ConstIterator end() const;

                void append_node(const std::shared_ptr<const BasicNode<Tree_> > & t);

                template <typename T_>
                const std::shared_ptr<typename Tree_::template NodeType<T_>::Type>
                append(const std::shared_ptr<const T_> & t);

                template <typename T_>
                const std::shared_ptr<typename Tree_::template NodeType<T_>::Type>
                append(const std::shared_ptr<T_> & t);
        };

        template <typename Tree_, typename Item_>
        class PALUDIS_VISIBLE InnerNode :
            public BasicInnerNode<Tree_>,
            public ImplementAcceptMethods<BasicNode<Tree_>, InnerNode<Tree_, Item_> >
        {
            private:
                const std::shared_ptr<const Item_> _spec;

            public:
                explicit InnerNode(const std::shared_ptr<const Item_> & i);

                template <typename OtherTree_>
                operator InnerNode<OtherTree_, Item_> () const;

                const std::shared_ptr<const Item_> spec() const;
        };

        template <typename Tree_>
        struct MakeVisitableTypeListEntry<Tree_, TypeListTail>
        {
            typedef TypeListTail Type;
        };

        template <typename Tree_, typename Item_, typename Tail_>
        struct MakeVisitableTypeListEntry<Tree_, TypeListEntry<SpecTreeInnerNodeType<Item_>, Tail_> >
        {
            typedef TypeListEntry<InnerNode<Tree_, Item_>,
                    typename MakeVisitableTypeListEntry<Tree_, Tail_>::Type> Type;
        };

        template <typename Tree_, typename Item_, typename Tail_>
        struct MakeVisitableTypeListEntry<Tree_, TypeListEntry<SpecTreeLeafNodeType<Item_>, Tail_> >
        {
            typedef TypeListEntry<LeafNode<Tree_, Item_>,
                    typename MakeVisitableTypeListEntry<Tree_, Tail_>::Type> Type;
        };

        template <typename Tree_, typename NodeList_>
        struct MakeVisitableTypeList
        {
            typedef typename MakeVisitableTypeListEntry<Tree_, NodeList_>::Type Type;
        };
    }

    template <typename NodeList_, typename RootNode_>
    class PALUDIS_VISIBLE SpecTree
    {
        public:
            typedef typename spec_tree_internals::MakeVisitableTypeList<SpecTree, NodeList_>::Type VisitableTypeList;

            typedef spec_tree_internals::BasicNode<SpecTree> BasicNode;
            typedef spec_tree_internals::BasicInnerNode<SpecTree> BasicInnerNode;

            template <typename Node_>
            struct LeafNodeType
            {
                typedef spec_tree_internals::LeafNode<SpecTree, Node_> Type;
            };

            template <typename Node_>
            struct InnerNodeType
            {
                typedef spec_tree_internals::InnerNode<SpecTree, Node_> Type;
            };

            template <typename Node_>
            struct NodeType
            {
                typedef typename std::conditional<
                    TypeListContains<VisitableTypeList, typename LeafNodeType<Node_>::Type>::value,
                    typename SpecTree::template LeafNodeType<Node_>::Type,
                    typename std::conditional<
                        TypeListContains<VisitableTypeList, typename InnerNodeType<Node_>::Type>::value,
                        typename InnerNodeType<Node_>::Type,
                        spec_tree_internals::TreeCannotContainNodeType<SpecTree, Node_>
                            >::type
                    >::type Type;
            };

            explicit SpecTree(const std::shared_ptr<RootNode_> & spec);

            explicit SpecTree(const std::shared_ptr<const RootNode_> & spec);

            const std::shared_ptr<typename InnerNodeType<RootNode_>::Type> top();

            const std::shared_ptr<const typename InnerNodeType<RootNode_>::Type> top() const;

        private:
            const std::shared_ptr<typename InnerNodeType<RootNode_>::Type> _top;
    };

    extern template class WrappedForwardIterator<spec_tree_internals::BasicInnerNode<GenericSpecTree>::ConstIteratorTag,
           const std::shared_ptr<const spec_tree_internals::BasicNode<GenericSpecTree> > >;
    extern template class WrappedForwardIterator<spec_tree_internals::BasicInnerNode<DependencySpecTree>::ConstIteratorTag,
           const std::shared_ptr<const spec_tree_internals::BasicNode<DependencySpecTree> > >;
    extern template class WrappedForwardIterator<spec_tree_internals::BasicInnerNode<SetSpecTree>::ConstIteratorTag,
             const std::shared_ptr<const spec_tree_internals::BasicNode<SetSpecTree> > >;
    extern template class WrappedForwardIterator<spec_tree_internals::BasicInnerNode<PlainTextSpecTree>::ConstIteratorTag,
             const std::shared_ptr<const spec_tree_internals::BasicNode<PlainTextSpecTree> > >;
    extern template class WrappedForwardIterator<spec_tree_internals::BasicInnerNode<RequiredUseSpecTree>::ConstIteratorTag,
             const std::shared_ptr<const spec_tree_internals::BasicNode<RequiredUseSpecTree> > >;
    extern template class WrappedForwardIterator<spec_tree_internals::BasicInnerNode<ProvideSpecTree>::ConstIteratorTag,
             const std::shared_ptr<const spec_tree_internals::BasicNode<ProvideSpecTree> > >;
    extern template class WrappedForwardIterator<spec_tree_internals::BasicInnerNode<SimpleURISpecTree>::ConstIteratorTag,
             const std::shared_ptr<const spec_tree_internals::BasicNode<SimpleURISpecTree> > >;
    extern template class WrappedForwardIterator<spec_tree_internals::BasicInnerNode<FetchableURISpecTree>::ConstIteratorTag,
             const std::shared_ptr<const spec_tree_internals::BasicNode<FetchableURISpecTree> > >;
    extern template class WrappedForwardIterator<spec_tree_internals::BasicInnerNode<LicenseSpecTree>::ConstIteratorTag,
             const std::shared_ptr<const spec_tree_internals::BasicNode<LicenseSpecTree> > >;

}

#endif