Logo  0.95.0-final
Finite Element Embedded Library and Language in C++
Feel++ Feel++ on Github Feel++ community
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
visitor.hpp
Go to the documentation of this file.
1 /* -*- mode: c++; coding: utf-8; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; show-trailing-whitespace: t -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4
2 
3  This file is part of the Feel library
4 
5  Author(s): Christophe Prud'homme <christophe.prudhomme@feelpp.org>
6  Date: 2005-03-21
7 
8  Copyright (C) 2007,2009 Université de Grenoble 1
9  Copyright (C) 2005,2006,2009 EPFL
10 
11  This library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU Lesser General Public
13  License as published by the Free Software Foundation; either
14  version 3.0 of the License, or (at your option) any later version.
15 
16  This library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  Lesser General Public License for more details.
20 
21  You should have received a copy of the GNU Lesser General Public
22  License along with this library; if not, write to the Free Software
23  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 */
30 #ifndef __Visitor_H
31 #define __Visitor_H 1
32 
33 #include <boost/mpl/assert.hpp>
34 #include <boost/mpl/list.hpp>
35 #include <boost/mpl/front.hpp>
36 #include <boost/mpl/pop_front.hpp>
37 
38 #include <feel/feelcore/feel.hpp>
39 
40 namespace Feel
41 {
42 namespace mpl=boost::mpl;
53 {
54 public:
55  virtual ~VisitorBase() {}
56 };
57 
85 template<
86 class T,
87  typename R = void
88  >
89 class Visitor
90 {
91 public:
92 
96  typedef R return_type;
97 
99 
103 
105  virtual ~Visitor()
106  {
107  // do nothing here
108  }
109 
111  virtual return_type visit( T* ) = 0;
112 
114  return_type visit( T& __t )
115  {
116  return visit( &__t );
117  }
118 
120 };
121 
122 template<
123 class TList,
124  typename R = void
125  >
126 class VisitorList
127  :
128 public Visitor<typename mpl::front<TList>::type, R>,
129 public mpl::if_<mpl::greater<mpl::size<TList>,mpl::long_<1l> >,
130  mpl::identity<VisitorList<mpl::pop_front<TList>,R> >,
131  mpl::identity<mpl::identity<VisitorBase> > >::type::type
132 {
133 #if 0
134  typedef typename mpl::if_<mpl::equal_to<mpl::size<TList>,mpl::int_<2> >,
135  mpl::identity<A>,
136  mpl::identity<C> >::type::type the_type;
137  //BOOST_MPL_ASSERT( ( boost::is_same<typename mpl::size<TList>::type, mpl::long_<2l> > ) );
138  //BOOST_MPL_ASSERT( ( boost::is_same<the_type, A> ) );
139 #endif
140 };
141 
142 
143 #if 0
144 
169 template <
170 class Head,
171  class Tail,
172  typename R
173  >
174 class Visitor< mpl::list<Head, Tail>, R >
175  :
176 public Visitor<Head, R>,
177 public Visitor<Tail, R>
178 {
179 public:
180  typedef R return_type;
182  using Visitor<Tail, R>::visit;
183 };
184 
185 template <
186 class Head,
187  typename R
188  >
189 class Visitor< mpl::list<Head>, R>
190  :
191 public Visitor<Head, R>
192 {
193 public:
194  typedef R return_type;
196 };
197 #endif
198 
205 template <
206 class TList,
207  typename R = void
208  >
210 
211 
212 template <
213 class Head,
214  class Tail,
215  typename R
216  >
217 class VisitorBaseImpl< mpl::list<Head, Tail>, R >
218  :
219 public Visitor<Head, R>,
220 public VisitorBaseImpl<Tail, R>
221 {
222 public:
223  // using BaseVisitorImpl<Tail, R>::Visit;
224 
225  virtual R visit( Head* )
226  {
227  return R();
228  }
229 
230 };
231 
232 template <
233 class Head,
234  typename R
235  >
236 class VisitorBaseImpl< mpl::list<Head>, R >
237  :
238 public Visitor<Head, R>
239 {
240 public:
241  virtual R visit( Head* )
242  {
243  return R();
244  }
245 };
246 
247 
251 template <
252 typename R,
253  typename Visited
254  >
255 struct VisitableCatchAllDefault
256 {
257  static R onUnknownVisitor( Visited&, VisitorBase& )
258  {
259  return R();
260  }
261 
262  static R onUnknownVisitor( Visited*, VisitorBase* )
263  {
264  return R();
265  }
266 
267 };
268 
272 template
273 <
274 typename R = void,
275  template <class, class> class CatchAll = VisitableCatchAllDefault
276  >
277 class VisitableBase
278 {
279 public:
280 
281  typedef R return_type;
282 
283  virtual ~VisitableBase() {}
284 
286  virtual return_type accept( VisitorBase& ) = 0;
287 
289  virtual return_type accept( VisitorBase* ) = 0;
290 
291 protected:
292 
298  template <class T>
299  static
300  return_type
301  acceptImpl( T* visited, VisitorBase* guest )
302  {
303  // Apply the Acyclic Visitor
304  if ( Visitor<T>* p = dynamic_cast< Visitor<T>* >( guest ) )
305  {
306  return p->visit( visited );
307  }
308 
309  return CatchAll<R, T>::onUnknownVisitor( visited, guest );
310  }
311 };
312 
319 #define FEELPP_DEFINE_VISITABLE() \
320  virtual return_type accept( VisitorBase& guest ) \
321  { \
322  return this->acceptImpl( this, &guest ); \
323  } \
324  virtual return_type accept( VisitorBase* guest ) \
325  { \
326  return this->acceptImpl( this, guest ); \
327  }
328 
334 template <
335 typename R,
336  class TList
337  >
339  :
340 public Visitor<TList, R>
341 {
342 public:
343  typedef R return_type;
344  // using Visitor<TList, R>::Visit;
345 
346  template <class Visited>
347  return_type genericVisit( Visited* host )
348  {
349  Visitor<Visited, return_type>& subObj = *this;
350  return subObj.visit( host );
351  }
352 
353  template <class Visited>
354  return_type genericVisit( Visited& host )
355  {
356  Visitor<Visited, return_type>& subObj = *this;
357  return subObj.visit( host );
358  }
359 
360 };
361 
367 #define FEELPP__DEFINE_CYCLIC_VISITABLE(SomeVisitor) \
368  virtual SomeVisitor::return_type Accept(SomeVisitor& guest) \
369  { \
370  return guest.genericVisit(*this); \
371  } \
372  virtual SomeVisitor::return_type Accept(SomeVisitor* guest) \
373  { \
374  return guest->genericVisit(*this); \
375  }
376 
377 }
378 #endif /* __Visitor_H */
The base class of any Acyclic Visitor.
Definition: visitor.hpp:52
Implements non-strict visitation (you can implement only part of the Visit functions) ...
Definition: visitor.hpp:209
return_type visit(T &__t)
visit a data structure
Definition: visitor.hpp:114
Definition: visitor.hpp:89
Definition: visitor.hpp:338
virtual ~Visitor()
virtual base destructor
Definition: visitor.hpp:105
virtual return_type visit(T *)=0
visit a data structure

Generated on Sun Dec 22 2013 13:11:15 for Feel++ by doxygen 1.8.5