paludis  Version 1.4.2
visitor.hh
1 /* vim: set sw=4 sts=4 et foldmethod=syntax : */
2 
3 /*
4  * Copyright (c) 2008, 2009, 2010, 2011 Ciaran McCreesh
5  *
6  * This file is part of the Paludis package manager. Paludis is free software;
7  * you can redistribute it and/or modify it under the terms of the GNU General
8  * Public License version 2, as published by the Free Software Foundation.
9  *
10  * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
11  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13  * details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
17  * Place, Suite 330, Boston, MA 02111-1307 USA
18  */
19 
20 #ifndef PALUDIS_GUARD_PALUDIS_UTIL_VISITOR_HH
21 #define PALUDIS_GUARD_PALUDIS_UTIL_VISITOR_HH 1
22 
24 #include <paludis/util/visitor-fwd.hh>
25 #include <paludis/util/no_type.hh>
26 
27 namespace paludis
28 {
29  template <>
31  {
32  public:
33  void forward_visit(const NoType<0u> &);
34  };
35 
36  template <typename TypeList_>
38  public virtual DeclareAbstractVisitMethods<typename TypeList_::Tail>
39  {
40  public:
42 
43  virtual void forward_visit(typename TypeList_::Item &) = 0;
44  };
45 
46  template <typename TypeList_>
47  class WrappedVisitorBase :
48  public virtual DeclareAbstractVisitMethods<TypeList_>
49  {
50  };
51 
52  template <typename RealClass_>
54  {
55  public:
56  void forward_visit(const NoType<1u> &);
57  };
58 
59  template <typename RealClass_, typename TypeList_>
60  class ImplementVisitMethods :
61  public virtual DeclareAbstractVisitMethods<TypeList_>,
62  public ImplementVisitMethods<RealClass_, typename TypeList_::Tail>
63  {
64  public:
66 
67  virtual void forward_visit(typename TypeList_::Item & n)
68  {
69  /* avoid gcc being too clever about noreturn */
70  if (this)
71  static_cast<RealClass_ *>(this)->perform_visit(n);
72  }
73  };
74 
75  template <typename TypeList_, typename UnwrappedVisitor_>
76  class WrappedVoidResultVisitor :
77  public WrappedVisitorBase<TypeList_>,
78  public ImplementVisitMethods<WrappedVoidResultVisitor<TypeList_, UnwrappedVisitor_>, TypeList_>
79  {
80  private:
81  UnwrappedVisitor_ & _unwrapped_visitor;
82 
83  public:
84  WrappedVoidResultVisitor(UnwrappedVisitor_ & v) :
85  _unwrapped_visitor(v)
86  {
87  }
88 
89  template <typename C_>
90  void perform_visit(C_ & t)
91  {
92  _unwrapped_visitor.visit(t);
93  }
94  };
95 
96  template <typename TypeList_, typename Result_, typename UnwrappedVisitor_>
97  class WrappedNonVoidResultVisitor :
98  public WrappedVisitorBase<TypeList_>,
99  public ImplementVisitMethods<WrappedNonVoidResultVisitor<TypeList_, Result_, UnwrappedVisitor_>,
100  TypeList_>
101  {
102  private:
103  UnwrappedVisitor_ & _unwrapped_visitor;
104 
105  public:
106  Result_ result;
107 
108  WrappedNonVoidResultVisitor(UnwrappedVisitor_ & v, const Result_ & r) :
109  _unwrapped_visitor(v),
110  result(r)
111  {
112  }
113 
114  template <typename C_>
115  void perform_visit(C_ & t)
116  {
117  result = _unwrapped_visitor.visit(t);
118  }
119  };
120 
121  template <typename BaseClass_, typename VisitableTypeList_>
122  class DeclareAbstractAcceptMethods
123  {
124  private:
125  virtual void _real_accept(WrappedVisitorBase<VisitableTypeList_> &) = 0;
126  virtual void _real_accept_const(WrappedVisitorBase<typename MakeTypeListConst<VisitableTypeList_>::Type> &) const = 0;
127 
128  public:
129  typedef VisitableTypeList_ VisitableTypeList;
130  typedef BaseClass_ VisitableBaseClass;
131 
132  template <typename UnwrappedVisitor_>
133  void accept(UnwrappedVisitor_ & v)
134  {
135  WrappedVoidResultVisitor<VisitableTypeList_, UnwrappedVisitor_> vv(v);
136  _real_accept(vv);
137  }
138 
139  template <typename UnwrappedVisitor_>
140  void accept(UnwrappedVisitor_ & v) const
141  {
142  WrappedVoidResultVisitor<typename MakeTypeListConst<VisitableTypeList_>::Type, UnwrappedVisitor_> vv(v);
143  _real_accept_const(vv);
144  }
145 
146  template <typename UnwrappedVisitor_>
147  void accept(const UnwrappedVisitor_ & v)
148  {
149  WrappedVoidResultVisitor<VisitableTypeList_, const UnwrappedVisitor_> vv(v);
150  _real_accept(vv);
151  }
152 
153  template <typename UnwrappedVisitor_>
154  void accept(const UnwrappedVisitor_ & v) const
155  {
156  WrappedVoidResultVisitor<typename MakeTypeListConst<VisitableTypeList_>::Type, const UnwrappedVisitor_> vv(v);
157  _real_accept_const(vv);
158  }
159 
160  template <typename Result_, typename UnwrappedVisitor_>
161  Result_ accept_returning(UnwrappedVisitor_ & v, const Result_ & r = Result_())
162  {
163  WrappedNonVoidResultVisitor<VisitableTypeList_, Result_, UnwrappedVisitor_> vv(v, r);
164  _real_accept(vv);
165  return vv.result;
166  }
167 
168  template <typename Result_, typename UnwrappedVisitor_>
169  Result_ accept_returning(const UnwrappedVisitor_ & v, const Result_ & r = Result_())
170  {
171  WrappedNonVoidResultVisitor<VisitableTypeList_, Result_, const UnwrappedVisitor_> vv(v, r);
172  _real_accept(vv);
173  return vv.result;
174  }
175 
176  template <typename Result_, typename UnwrappedVisitor_>
177  Result_ accept_returning(UnwrappedVisitor_ & v, const Result_ & r = Result_()) const
178  {
179  WrappedNonVoidResultVisitor<typename MakeTypeListConst<VisitableTypeList_>::Type, Result_, UnwrappedVisitor_> vv(v, r);
180  _real_accept_const(vv);
181  return vv.result;
182  }
183 
184  template <typename Result_, typename UnwrappedVisitor_>
185  Result_ accept_returning(const UnwrappedVisitor_ & v, const Result_ & r = Result_()) const
186  {
187  WrappedNonVoidResultVisitor<typename MakeTypeListConst<VisitableTypeList_>::Type, Result_, const UnwrappedVisitor_> vv(v, r);
188  _real_accept_const(vv);
189  return vv.result;
190  }
191  };
192 
193  template <typename BaseClass_, typename RealClass_>
194  class PALUDIS_VISIBLE ImplementAcceptMethods :
195  public virtual DeclareAbstractAcceptMethods<BaseClass_, typename BaseClass_::VisitableTypeList>
196  {
197  private:
198  void _real_accept(WrappedVisitorBase<typename BaseClass_::VisitableTypeList> & v)
199  {
200  v.forward_visit(*static_cast<RealClass_ *>(this));
201  };
202 
203  void _real_accept_const(WrappedVisitorBase<typename MakeTypeListConst<typename BaseClass_::VisitableTypeList>::Type> & v) const
204  {
205  v.forward_visit(*static_cast<const RealClass_ *>(this));
206  };
207  };
208 }
209 
210 #endif
Definition: visitor-fwd.hh:28
Definition: type_list.hh:27
Definition: no_type.hh:43
Definition: visitor-fwd.hh:37
#define PALUDIS_VISIBLE
Definition: attributes.hh:71