00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00076 #ifndef LUMIERA_VISITOR_H
00077 #define LUMIERA_VISITOR_H
00078
00079 #include "lib/visitorpolicies.hpp"
00080 #include "lib/visitordispatcher.hpp"
00081
00082 #include "lib/meta/typelist.hpp"
00083
00084
00085 namespace lumiera
00086 {
00087 namespace visitor
00088 {
00089
00090
00098 template
00099 < typename RET = void,
00100 template <class> class ERR = UseDefault
00101 >
00102 class Tool : public ERR<RET>
00103 {
00104 public:
00105 typedef RET ReturnType;
00106 typedef Tool ToolBase;
00107
00108 virtual ~Tool () { };
00109
00112 virtual Tag<ToolBase> getTag() = 0;
00113 };
00114
00115
00116
00117
00134 template
00135 < class TOOLImpl,
00136 class TYPES,
00137 class BASE=Tool<>
00138 >
00139 class Applicable;
00140
00141 template
00142 < class TOOLImpl,
00143 class BASE
00144 >
00145 class Applicable<TOOLImpl, typelist::NullType, BASE>
00146 : public BASE
00147 { }
00148 ;
00149
00150 template
00151 < class TOOLImpl,
00152 class TAR, class TYPES,
00153 class BASE
00154 >
00155 class Applicable<TOOLImpl, typelist::Node<TAR, TYPES>, BASE>
00156 : public Applicable<TOOLImpl, TYPES, BASE>
00157 {
00158
00159 typedef typename BASE::ToolBase ToolBase;
00160
00161 protected:
00162 virtual ~Applicable () {}
00163 Applicable ()
00164 {
00165 TOOLImpl* typeref = 0;
00166 Dispatcher<TAR,ToolBase>::instance().enroll (typeref);
00167 }
00168
00169 public:
00170 virtual Tag<ToolBase>
00171 getTag ()
00172 {
00173 TOOLImpl* typeref = 0;
00174 return Tag<ToolBase>::get (typeref);
00175 }
00176 };
00177
00178 using typelist::Types;
00179
00180
00181
00189 template
00190 < class TOOL = Tool<>
00191 >
00192 class Visitable
00193 {
00194 protected:
00195 virtual ~Visitable () { };
00196
00198 typedef typename TOOL::ToolBase ToolBase;
00199 typedef typename TOOL::ReturnType ReturnType;
00200
00206 template <class TAR>
00207 static inline ReturnType
00208 dispatchOp (TAR& target, TOOL& tool)
00209 {
00210 return Dispatcher<TAR,ToolBase>::instance().forwardCall (target,tool);
00211 }
00212
00213 public:
00216 virtual ReturnType apply (TOOL&) = 0;
00217 };
00218
00219
00224 #define DEFINE_PROCESSABLE_BY(TOOL) \
00225 virtual ReturnType apply (TOOL& tool) \
00226 { return dispatchOp (*this, tool); }
00227
00228
00229
00230 }
00231
00232 }
00233 #endif