2002-08-11 05:49:45 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// The Loki Library
|
|
|
|
// Copyright (c) 2001 by Andrei Alexandrescu
|
|
|
|
// This code accompanies the book:
|
2010-04-19 03:09:59 +00:00
|
|
|
// Alexandrescu, Andrei. "Modern C++ Design: Generic Programming and Design
|
2002-08-11 05:49:45 +00:00
|
|
|
// Patterns Applied". Copyright (c) 2001. Addison-Wesley.
|
2010-04-19 03:09:59 +00:00
|
|
|
// Permission to use, copy, modify, distribute and sell this software for any
|
|
|
|
// purpose is hereby granted without fee, provided that the above copyright
|
|
|
|
// notice appear in all copies and that both that copyright notice and this
|
2002-08-11 05:49:45 +00:00
|
|
|
// permission notice appear in supporting documentation.
|
2010-04-19 03:09:59 +00:00
|
|
|
// The author or Addison-Wesley Longman make no representations about the
|
|
|
|
// suitability of this software for any purpose. It is provided "as is"
|
2002-08-11 05:49:45 +00:00
|
|
|
// without express or implied warranty.
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2006-10-17 19:50:37 +00:00
|
|
|
#ifndef LOKI_VISITOR_INC_
|
|
|
|
#define LOKI_VISITOR_INC_
|
2002-08-11 05:49:45 +00:00
|
|
|
|
2006-10-17 19:50:37 +00:00
|
|
|
// $Id$
|
2002-08-11 05:49:45 +00:00
|
|
|
|
2006-01-02 11:52:03 +00:00
|
|
|
|
2006-10-17 19:50:37 +00:00
|
|
|
/// \defgroup VisitorGroup Visitor
|
2002-08-11 05:49:45 +00:00
|
|
|
|
2010-04-19 03:09:59 +00:00
|
|
|
#include <loki/Typelist.h>
|
|
|
|
#include <loki/HierarchyGenerators.h>
|
2002-08-11 05:49:45 +00:00
|
|
|
|
|
|
|
namespace Loki
|
|
|
|
{
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2006-01-02 11:52:03 +00:00
|
|
|
/// \class BaseVisitor
|
2010-04-19 03:09:59 +00:00
|
|
|
///
|
2006-01-02 11:52:03 +00:00
|
|
|
/// \ingroup VisitorGroup
|
|
|
|
/// The base class of any Acyclic Visitor
|
2002-08-11 05:49:45 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
class BaseVisitor
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual ~BaseVisitor() {}
|
|
|
|
};
|
2010-04-19 03:09:59 +00:00
|
|
|
|
2002-08-11 05:49:45 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2006-01-02 11:52:03 +00:00
|
|
|
/// \class Visitor
|
|
|
|
///
|
|
|
|
/// \ingroup VisitorGroup
|
|
|
|
/// The building block of Acyclic Visitor
|
|
|
|
///
|
|
|
|
/// \par Usage
|
|
|
|
///
|
|
|
|
/// Defining the visitable class:
|
2010-04-19 03:09:59 +00:00
|
|
|
///
|
2006-01-02 11:52:03 +00:00
|
|
|
/// \code
|
|
|
|
/// class RasterBitmap : public BaseVisitable<>
|
|
|
|
/// {
|
|
|
|
/// public:
|
|
|
|
/// LOKI_DEFINE_VISITABLE()
|
|
|
|
/// };
|
|
|
|
/// \endcode
|
|
|
|
///
|
|
|
|
/// Way 1 to define a visitor:
|
|
|
|
/// \code
|
2010-04-19 03:09:59 +00:00
|
|
|
/// class SomeVisitor :
|
2006-01-02 11:52:03 +00:00
|
|
|
/// public BaseVisitor // required
|
|
|
|
/// public Visitor<RasterBitmap>,
|
|
|
|
/// public Visitor<Paragraph>
|
|
|
|
/// {
|
|
|
|
/// public:
|
|
|
|
/// void Visit(RasterBitmap&); // visit a RasterBitmap
|
|
|
|
/// void Visit(Paragraph &); // visit a Paragraph
|
|
|
|
/// };
|
|
|
|
/// \endcode
|
|
|
|
///
|
|
|
|
/// Way 2 to define the visitor:
|
|
|
|
/// \code
|
2010-04-19 03:09:59 +00:00
|
|
|
/// class SomeVisitor :
|
2006-01-02 11:52:03 +00:00
|
|
|
/// public BaseVisitor // required
|
|
|
|
/// public Visitor<LOKI_TYPELIST_2(RasterBitmap, Paragraph)>
|
|
|
|
/// {
|
|
|
|
/// public:
|
|
|
|
/// void Visit(RasterBitmap&); // visit a RasterBitmap
|
|
|
|
/// void Visit(Paragraph &); // visit a Paragraph
|
|
|
|
/// };
|
|
|
|
/// \endcode
|
|
|
|
///
|
|
|
|
/// Way 3 to define the visitor:
|
|
|
|
/// \code
|
2010-04-19 03:09:59 +00:00
|
|
|
/// class SomeVisitor :
|
2006-01-02 11:52:03 +00:00
|
|
|
/// public BaseVisitor // required
|
|
|
|
/// public Visitor<Seq<RasterBitmap, Paragraph>::Type>
|
|
|
|
/// {
|
|
|
|
/// public:
|
|
|
|
/// void Visit(RasterBitmap&); // visit a RasterBitmap
|
|
|
|
/// void Visit(Paragraph &); // visit a Paragraph
|
|
|
|
/// };
|
|
|
|
/// \endcode
|
|
|
|
///
|
|
|
|
/// \par Using const visit functions:
|
|
|
|
///
|
|
|
|
/// Defining the visitable class (true for const):
|
2010-04-19 03:09:59 +00:00
|
|
|
///
|
2006-01-02 11:52:03 +00:00
|
|
|
/// \code
|
|
|
|
/// class RasterBitmap : public BaseVisitable<void, DefaultCatchAll, true>
|
|
|
|
/// {
|
|
|
|
/// public:
|
|
|
|
/// LOKI_DEFINE_CONST_VISITABLE()
|
|
|
|
/// };
|
|
|
|
/// \endcode
|
|
|
|
///
|
|
|
|
/// Defining the visitor which only calls const member functions:
|
|
|
|
/// \code
|
2010-04-19 03:09:59 +00:00
|
|
|
/// class SomeVisitor :
|
2006-01-02 11:52:03 +00:00
|
|
|
/// public BaseVisitor // required
|
|
|
|
/// public Visitor<RasterBitmap, void, true>,
|
|
|
|
/// {
|
|
|
|
/// public:
|
|
|
|
/// void Visit(const RasterBitmap&); // visit a RasterBitmap by a const member function
|
|
|
|
/// };
|
|
|
|
/// \endcode
|
|
|
|
///
|
|
|
|
/// \par Example:
|
|
|
|
///
|
2010-04-19 03:09:59 +00:00
|
|
|
/// test/Visitor/main.cpp
|
2002-08-11 05:49:45 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2006-01-02 11:52:03 +00:00
|
|
|
template <class T, typename R = void, bool ConstVisit = false>
|
2006-01-02 10:36:45 +00:00
|
|
|
class Visitor;
|
|
|
|
|
|
|
|
template <class T, typename R>
|
|
|
|
class Visitor<T, R, false>
|
2002-08-11 05:49:45 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef R ReturnType;
|
2006-01-02 11:52:03 +00:00
|
|
|
typedef T ParamType;
|
2006-01-05 09:55:09 +00:00
|
|
|
virtual ~Visitor() {}
|
2006-01-02 10:36:45 +00:00
|
|
|
virtual ReturnType Visit(ParamType&) = 0;
|
2002-08-11 05:49:45 +00:00
|
|
|
};
|
2006-01-02 10:36:45 +00:00
|
|
|
|
|
|
|
template <class T, typename R>
|
|
|
|
class Visitor<T, R, true>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef R ReturnType;
|
2006-01-02 11:52:03 +00:00
|
|
|
typedef const T ParamType;
|
2006-01-05 09:55:09 +00:00
|
|
|
virtual ~Visitor() {}
|
2006-01-02 10:36:45 +00:00
|
|
|
virtual ReturnType Visit(ParamType&) = 0;
|
|
|
|
};
|
|
|
|
|
2002-08-11 05:49:45 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// class template Visitor (specialization)
|
|
|
|
// This specialization is not present in the book. It makes it easier to define
|
|
|
|
// Visitors for multiple types in a shot by using a typelist. Example:
|
|
|
|
//
|
2010-04-19 03:09:59 +00:00
|
|
|
// class SomeVisitor :
|
2002-08-11 05:49:45 +00:00
|
|
|
// public BaseVisitor // required
|
2006-01-02 10:36:45 +00:00
|
|
|
// public Visitor<LOKI_TYPELIST_2(RasterBitmap, Paragraph)>
|
2002-08-11 05:49:45 +00:00
|
|
|
// {
|
|
|
|
// public:
|
|
|
|
// void Visit(RasterBitmap&); // visit a RasterBitmap
|
|
|
|
// void Visit(Paragraph &); // visit a Paragraph
|
|
|
|
// };
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
template <class Head, class Tail, typename R>
|
2006-01-02 10:36:45 +00:00
|
|
|
class Visitor<Typelist<Head, Tail>, R, false>
|
|
|
|
: public Visitor<Head, R, false>, public Visitor<Tail, R, false>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef R ReturnType;
|
|
|
|
// using Visitor<Head, R>::Visit;
|
|
|
|
// using Visitor<Tail, R>::Visit;
|
|
|
|
};
|
2010-04-19 03:09:59 +00:00
|
|
|
|
2006-01-02 10:36:45 +00:00
|
|
|
template <class Head, typename R>
|
|
|
|
class Visitor<Typelist<Head, NullType>, R, false> : public Visitor<Head, R, false>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef R ReturnType;
|
|
|
|
using Visitor<Head, R, false>::Visit;
|
|
|
|
};
|
|
|
|
|
2006-01-02 11:52:03 +00:00
|
|
|
template <class Head, class Tail, typename R>
|
2006-01-02 10:36:45 +00:00
|
|
|
class Visitor<Typelist<Head, Tail>, R, true>
|
|
|
|
: public Visitor<Head, R, true>, public Visitor<Tail, R, true>
|
2002-08-11 05:49:45 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef R ReturnType;
|
|
|
|
// using Visitor<Head, R>::Visit;
|
|
|
|
// using Visitor<Tail, R>::Visit;
|
|
|
|
};
|
2010-04-19 03:09:59 +00:00
|
|
|
|
2002-08-11 05:49:45 +00:00
|
|
|
template <class Head, typename R>
|
2006-01-02 10:36:45 +00:00
|
|
|
class Visitor<Typelist<Head, NullType>, R, true> : public Visitor<Head, R, true>
|
2002-08-11 05:49:45 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef R ReturnType;
|
2006-01-02 10:36:45 +00:00
|
|
|
using Visitor<Head, R, true>::Visit;
|
2002-08-11 05:49:45 +00:00
|
|
|
};
|
|
|
|
|
2006-01-02 10:36:45 +00:00
|
|
|
|
2002-08-11 05:49:45 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// class template BaseVisitorImpl
|
|
|
|
// Implements non-strict visitation (you can implement only part of the Visit
|
|
|
|
// functions)
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
template <class TList, typename R = void> class BaseVisitorImpl;
|
|
|
|
|
|
|
|
template <class Head, class Tail, typename R>
|
|
|
|
class BaseVisitorImpl<Typelist<Head, Tail>, R>
|
|
|
|
: public Visitor<Head, R>
|
|
|
|
, public BaseVisitorImpl<Tail, R>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// using BaseVisitorImpl<Tail, R>::Visit;
|
|
|
|
|
|
|
|
virtual R Visit(Head&)
|
|
|
|
{ return R(); }
|
|
|
|
};
|
2010-04-19 03:09:59 +00:00
|
|
|
|
2002-08-11 05:49:45 +00:00
|
|
|
template <class Head, typename R>
|
|
|
|
class BaseVisitorImpl<Typelist<Head, NullType>, R>
|
|
|
|
: public Visitor<Head, R>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual R Visit(Head&)
|
|
|
|
{ return R(); }
|
|
|
|
};
|
2010-04-19 03:09:59 +00:00
|
|
|
|
2002-08-11 05:49:45 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// class template BaseVisitable
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
template <typename R, typename Visited>
|
|
|
|
struct DefaultCatchAll
|
|
|
|
{
|
|
|
|
static R OnUnknownVisitor(Visited&, BaseVisitor&)
|
|
|
|
{ return R(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// class template BaseVisitable
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2010-04-19 03:09:59 +00:00
|
|
|
template
|
2002-08-11 05:49:45 +00:00
|
|
|
<
|
2010-04-19 03:09:59 +00:00
|
|
|
typename R = void,
|
2006-01-02 10:36:45 +00:00
|
|
|
template <typename, class> class CatchAll = DefaultCatchAll,
|
2006-01-02 11:52:03 +00:00
|
|
|
bool ConstVisitable = false
|
2002-08-11 05:49:45 +00:00
|
|
|
>
|
2006-01-02 10:36:45 +00:00
|
|
|
class BaseVisitable;
|
|
|
|
|
|
|
|
template<typename R,template <typename, class> class CatchAll>
|
|
|
|
class BaseVisitable<R, CatchAll, false>
|
2002-08-11 05:49:45 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef R ReturnType;
|
|
|
|
virtual ~BaseVisitable() {}
|
|
|
|
virtual ReturnType Accept(BaseVisitor&) = 0;
|
2010-04-19 03:09:59 +00:00
|
|
|
|
2002-08-11 05:49:45 +00:00
|
|
|
protected: // give access only to the hierarchy
|
|
|
|
template <class T>
|
|
|
|
static ReturnType AcceptImpl(T& visited, BaseVisitor& guest)
|
|
|
|
{
|
|
|
|
// Apply the Acyclic Visitor
|
2003-02-27 20:09:08 +00:00
|
|
|
if (Visitor<T,R>* p = dynamic_cast<Visitor<T,R>*>(&guest))
|
2002-08-11 05:49:45 +00:00
|
|
|
{
|
|
|
|
return p->Visit(visited);
|
|
|
|
}
|
|
|
|
return CatchAll<R, T>::OnUnknownVisitor(visited, guest);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2006-01-02 10:36:45 +00:00
|
|
|
template<typename R,template <typename, class> class CatchAll>
|
|
|
|
class BaseVisitable<R, CatchAll, true>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef R ReturnType;
|
|
|
|
virtual ~BaseVisitable() {}
|
|
|
|
virtual ReturnType Accept(BaseVisitor&) const = 0;
|
2010-04-19 03:09:59 +00:00
|
|
|
|
2006-01-02 10:36:45 +00:00
|
|
|
protected: // give access only to the hierarchy
|
|
|
|
template <class T>
|
|
|
|
static ReturnType AcceptImpl(const T& visited, BaseVisitor& guest)
|
|
|
|
{
|
|
|
|
// Apply the Acyclic Visitor
|
|
|
|
if (Visitor<T,R,true>* p = dynamic_cast<Visitor<T,R,true>*>(&guest))
|
|
|
|
{
|
|
|
|
return p->Visit(visited);
|
|
|
|
}
|
|
|
|
return CatchAll<R, T>::OnUnknownVisitor(const_cast<T&>(visited), guest);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2006-01-02 11:52:03 +00:00
|
|
|
|
2002-08-11 05:49:45 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2006-01-02 11:52:03 +00:00
|
|
|
/// \def LOKI_DEFINE_VISITABLE()
|
|
|
|
/// \ingroup VisitorGroup
|
2010-04-19 03:09:59 +00:00
|
|
|
/// Put it in every class that you want to make visitable
|
2006-01-02 11:52:03 +00:00
|
|
|
/// (in addition to deriving it from BaseVisitable<R>)
|
2002-08-11 05:49:45 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2005-09-26 07:33:05 +00:00
|
|
|
#define LOKI_DEFINE_VISITABLE() \
|
2005-10-30 14:03:23 +00:00
|
|
|
virtual ReturnType Accept(::Loki::BaseVisitor& guest) \
|
2002-08-11 05:49:45 +00:00
|
|
|
{ return AcceptImpl(*this, guest); }
|
|
|
|
|
2006-01-02 11:52:03 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
/// \def LOKI_DEFINE_CONST_VISITABLE()
|
|
|
|
/// \ingroup VisitorGroup
|
2010-04-19 03:09:59 +00:00
|
|
|
/// Put it in every class that you want to make visitable by const member
|
2006-01-02 11:52:03 +00:00
|
|
|
/// functions (in addition to deriving it from BaseVisitable<R>)
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2006-01-02 10:36:45 +00:00
|
|
|
#define LOKI_DEFINE_CONST_VISITABLE() \
|
|
|
|
virtual ReturnType Accept(::Loki::BaseVisitor& guest) const \
|
|
|
|
{ return AcceptImpl(*this, guest); }
|
|
|
|
|
2002-08-11 05:49:45 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2006-01-02 11:52:03 +00:00
|
|
|
/// \class CyclicVisitor
|
|
|
|
///
|
|
|
|
/// \ingroup VisitorGroup
|
2010-04-19 03:09:59 +00:00
|
|
|
/// Put it in every class that you want to make visitable (in addition to
|
2006-01-02 11:52:03 +00:00
|
|
|
/// deriving it from BaseVisitable<R>
|
2002-08-11 05:49:45 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
template <typename R, class TList>
|
|
|
|
class CyclicVisitor : public Visitor<TList, R>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef R ReturnType;
|
|
|
|
// using Visitor<TList, R>::Visit;
|
2010-04-19 03:09:59 +00:00
|
|
|
|
2002-08-11 05:49:45 +00:00
|
|
|
template <class Visited>
|
|
|
|
ReturnType GenericVisit(Visited& host)
|
|
|
|
{
|
|
|
|
Visitor<Visited, ReturnType>& subObj = *this;
|
|
|
|
return subObj.Visit(host);
|
|
|
|
}
|
|
|
|
};
|
2010-04-19 03:09:59 +00:00
|
|
|
|
2002-08-11 05:49:45 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2006-01-02 11:52:03 +00:00
|
|
|
/// \def LOKI_DEFINE_CYCLIC_VISITABLE(SomeVisitor)
|
|
|
|
/// \ingroup VisitorGroup
|
|
|
|
/// Put it in every class that you want to make visitable by a cyclic visitor
|
2002-08-11 05:49:45 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2005-09-26 07:33:05 +00:00
|
|
|
#define LOKI_DEFINE_CYCLIC_VISITABLE(SomeVisitor) \
|
2002-08-11 05:49:45 +00:00
|
|
|
virtual SomeVisitor::ReturnType Accept(SomeVisitor& guest) \
|
|
|
|
{ return guest.GenericVisit(*this); }
|
|
|
|
|
|
|
|
} // namespace Loki
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-10-17 19:31:51 +00:00
|
|
|
#endif // end file guardian
|
|
|
|
|