library of assembled shared sources

http://lass.cocamware.com

visitor.h

Go to the documentation of this file.
00001 /** @file
00002  *  @author Bram de Greve (bramz@users.sourceforge.net)
00003  *  @author Tom De Muer (tomdemuer@users.sourceforge.net)
00004  *
00005  *  *** BEGIN LICENSE INFORMATION ***
00006  *  
00007  *  The contents of this file are subject to the Common Public Attribution License 
00008  *  Version 1.0 (the "License"); you may not use this file except in compliance with 
00009  *  the License. You may obtain a copy of the License at 
00010  *  http://lass.sourceforge.net/cpal-license. The License is based on the 
00011  *  Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover 
00012  *  use of software over a computer network and provide for limited attribution for 
00013  *  the Original Developer. In addition, Exhibit A has been modified to be consistent 
00014  *  with Exhibit B.
00015  *  
00016  *  Software distributed under the License is distributed on an "AS IS" basis, WITHOUT 
00017  *  WARRANTY OF ANY KIND, either express or implied. See the License for the specific 
00018  *  language governing rights and limitations under the License.
00019  *  
00020  *  The Original Code is LASS - Library of Assembled Shared Sources.
00021  *  
00022  *  The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
00023  *  The Original Developer is the Initial Developer.
00024  *  
00025  *  All portions of the code written by the Initial Developer are:
00026  *  Copyright (C) 2004-2007 the Initial Developer.
00027  *  All Rights Reserved.
00028  *  
00029  *  Contributor(s):
00030  *
00031  *  Alternatively, the contents of this file may be used under the terms of the 
00032  *  GNU General Public License Version 2 or later (the GPL), in which case the 
00033  *  provisions of GPL are applicable instead of those above.  If you wish to allow use
00034  *  of your version of this file only under the terms of the GPL and not to allow 
00035  *  others to use your version of this file under the CPAL, indicate your decision by 
00036  *  deleting the provisions above and replace them with the notice and other 
00037  *  provisions required by the GPL License. If you do not delete the provisions above,
00038  *  a recipient may use your version of this file under either the CPAL or the GPL.
00039  *  
00040  *  *** END LICENSE INFORMATION ***
00041  */
00042 
00043 #ifndef LASS_GUARDIAN_OF_INCLUSION_UTIL_VISITOR_H
00044 #define LASS_GUARDIAN_OF_INCLUSION_UTIL_VISITOR_H
00045 
00046 namespace lass
00047 {
00048 namespace util
00049 {
00050 
00051 class BadVisit: public Exception
00052 {
00053 public:
00054     BadVisit(const std::string& msg, const std::string& loc): Exception(msg, loc) {}
00055 private:
00056     LASS_UTIL_EXCEPTION_PRIVATE_IMPL(BadVisit)
00057 };
00058 
00059 
00060 
00061 class LASS_DLL VisitorBase
00062 {
00063 public:
00064     virtual ~VisitorBase();
00065 protected:
00066     VisitorBase();
00067 };
00068 
00069 
00070 
00071 template <typename T>
00072 class Visitor
00073 {
00074 public:
00075     void preVisit(T& visitable)
00076     {
00077         doPreVisit(visitable);
00078     }
00079     void postVisit(T& visitable)
00080     {
00081         doPostVisit(visitable);
00082     }
00083 private:
00084     virtual void doPreVisit(T& visitable) = 0;
00085     virtual void doPostVisit(T& visitable) {}
00086 };
00087 
00088 
00089 
00090 struct ThrowOnUnknownVisit
00091 {
00092     template <typename T> static void onUnknownPreVisit(VisitorBase& visitor, T& visitable)
00093     {
00094         LASS_THROW_EX(BadVisit, "unkown previsit on type '" << typeid(T).name() << "'.");
00095     }
00096     template <typename T> static void onUnknownPostVisit(VisitorBase& visitor, T& visitable)
00097     {
00098         LASS_THROW_EX(BadVisit, "unkown postvisit on type '" << typeid(T).name() << "'.");
00099     }
00100 };
00101 
00102 
00103 struct IgnoreUnknownVisit
00104 {
00105     template <typename T> static void onUnknownPreVisit(VisitorBase& visitor, T& visitable) {}
00106     template <typename T> static void onUnknownPostVisit(VisitorBase& visitor, T& visitable) {}
00107 };
00108 
00109 
00110 
00111 template <typename CatchAll = ThrowOnUnknownVisit>
00112 class VisitableBase
00113 {
00114 public:
00115     virtual ~VisitableBase() {}
00116 
00117     void accept(VisitorBase& visitor) { doAccept(visitor); }
00118 
00119     template <typename T>
00120     static void preAccept(VisitorBase& visitor, T& visitable)
00121     {
00122         if (Visitor<T>* p = dynamic_cast<Visitor<T>*>(&visitor))
00123         {
00124             p->preVisit(visitable);
00125         }
00126         else
00127         {
00128             CatchAll::onUnknownPreVisit(visitor, visitable);
00129         }
00130     }
00131 
00132     template <typename T>
00133     static void postAccept(VisitorBase& visitor, T& visitable)
00134     {
00135         if (Visitor<T>* p = dynamic_cast<Visitor<T>*>(&visitor))
00136         {
00137             p->postVisit(visitable);
00138         }
00139         else
00140         {
00141             CatchAll::onUnknownPostVisit(visitor, visitable);
00142         }
00143     }
00144 
00145 private:
00146 
00147     virtual void doAccept(VisitorBase& visitor) = 0;
00148 };
00149 
00150 #define LASS_UTIL_VISITOR_DO_ACCEPT\
00151     void doAccept(::lass::util::VisitorBase& visitor)\
00152     {\
00153         preAccept(visitor, *this);\
00154         postAccept(visitor, *this);\
00155     }
00156 
00157 }
00158 }
00159 
00160 #endif
00161 
00162 // EOF

Generated on Mon Nov 10 14:22:02 2008 for Library of Assembled Shared Sources by doxygen 1.5.7.1
SourceForge.net Logo