www.digitalmars.com         C & C++   DMDScript  

c++ - Fast Template Visitor Library

reply Anand Krishnamoorthi <anand.shankar.k gmail.com> writes:
Hi,

I've been working on a `Visitor' generation library.
The main advantages being aimed for are:

1) N number of arguments and return type ( templatized )
2) Prevent coupling between classes
3) Easy to add new classes
4) Not having to declare a Visitor interface with visit methods
5) Efficient dispatch: equivalent to a virtual function call
( normally a visitor requires 2 virt calls; acyclic visitor
requires a dynamic cast )
6) Add new visit methods at runtime

One drawback includes having to register each visit method.
But this is also allows runtime flexibility.

A visitor can be created as a singleton if needed.

The code has been developed using VC++ 8.0.
The implementation techniques include assigning tags to classes
on the fly, building virtual table internally, creating thunk
functions, a cast policy etc.

Cvisitor.h contains the library.
test1.cpp contains an example for a Shape heirarchy.

Please feel free to discuss the code.
Comments and suggestions are welcome.

Thanks,
Anand.
begin 644 CVisitor.h
M(VEF;F1E9B!?0U9)4TE43U)?2%\-"B-D969I;F4 7T-625-)5$]27TA?#0H-
M"B-I;F-L=61E(#QS=&1D968N:#X-"B-I;F-L=61E(#P =F5C=&]R(#X-"B-I
M;F-L=61E(#P =71I;&ET>2`^#0H-"G1E;7!L871E/"!T>7!E;F%M92!$97)V
M(#X-"G-T<G5C="!$969A=6QT0V%S=%!O;&EC>2![#0H-"G1E;7!L871E(#P 
M='EP96YA;64 0F%S92`^#0IS=&%T:6, 1&5R=BH 0V%S="  0F%S92H 8B`I
M('L <F5T=7)N('-T871I8U]C87-T/"!$97)V*B`^*"!B("D[('T-" T*?3L-
M" T*=&5M<&QA=&4\('1Y<&5N86UE($)A<V4L('1E;7!L871E/"!C;&%S<R`^
M(&-L87-S($-A<W10;VQI8WD /2!$969A=6QT0V%S=%!O;&EC>2` / T*8VQA
M<W, 1&ES<&%T8V  >PT*#0IS=&%T:6, <VEZ95]T('-?=&%G7SL-" T*=&5M
M<&QA=&4\('1Y<&5N86UE($1E<G8 / T*9G)I96YD('-T<G5C="!486<[#0H-
M"G1E;7!L871E/"!T>7!E;F%M92!$97)V(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T*"7)E='5R;B!V=&%B;&5?6R!I
M;F1E>"!=.PT*?0T*#0IP<FEV871E. T*"71Y<&5D968 <W1D.CIV96-T;W(\
M($9U;F, /B!486)L93L-" E486)L92!V=&%B;&5?.PT*"49U;F, 9&5F875L
M=%\[#0I].PT*#0H-"G!U8FQI8SH-" T*=&5M<&QA=&4\(`T*"71Y<&5N86UE
M(%)E="`]('9O:60L(`T*"71Y<&5N86UE($$Q(#T =F]I9"P-" ET>7!E;F%M
M92!!,B`]('9O:60L#0H)='EP96YA;64 03, /2!V;VED+`T*"71Y<&5N86UE
M($$T(#T =F]I9"P-" ET>7!E;F%M92!!-2`]('9O:60L#0H)='EP96YA;64 
M038 /2!V;VED#0H^#0IC;&%S<R!6:7-I=&]R('L-"G!R:79A=&4Z#0H)='EP
M961E9B!2970 *%9I<VET;W(Z.BI&=6YC*2  0F%S92HL($$Q+"!!,BP 03,L
M($$T+"!!-2P 038 *3L-" E65&%B;&4\($9U;F, /B!V=&%B;&5?.PT*#0IP
M<F]T96-T960Z#0IT96UP;&%T93P ='EP96YA;64 5FES:71O<D1E<G8L('1Y
M<&5N86UE($1E<G8L(%)E="`H5FES:71O<D1E<G8Z.BIF=6YC*2  1&5R=BHL
M($$Q+"!!,BP 03,L($$T+"!!-2P 038I(#X-"E)E="!T:'5N:R  0F%S92H 
M8BP 03$ 83$L($$R(&$R+"!!,R!A,RP 030 830L($$U(&$U+"!!-B!A-B`I
M#0I[#0H)<F5T=7)N("  <W1A=&EC7V-A<W0\(%9I<VET;W)$97)V*B`^*"!T
M:&ES*2T^*F9U;F, *2`H($-A<W10;VQI8WD\($1E<G8 /CHZ0V%S="  8B`I
M+"!A,2P 83(L(&$S+"!A-"P 834L(&$V("D[#0I]#0H-"G!U8FQI8SH-" T*
M5FES:71O<B  1G5N8R!D968 *2`Z('9T86)L95\H(&1E9B`I('L ?0T*#0I6
M:7-I=&]R)B!O=F5R<FED92 I('L <F5T=7)N("IT:&ES.R!]#0H-"G1E;7!L
M871E/"!T>7!E;F%M92!6:7-I=&]R1&5R=BP ='EP96YA;64 1&5R=BP 4F5T
M("A6:7-I=&]R1&5R=CHZ*F9U;F,I*"!$97)V*BP 03$L($$R+"!!,RP 030L
M($$U+"!!-BD / T*5FES:71O<B8 861D*"D-"GL-" EV=&%B;&5?+F%D9#P 
M1&5R=B`^*"`F5FES:71O<CHZ=&AU;FL\(%9I<VET;W)$97)V+"!$97)V+"!F
M=6YC/B`I.PT*"7)E='5R;B`J=&AI<SL-"GT-" T*#0I2970 ;W!E<F%T;W(H
M*2  0F%S92H 8BP 03$ 83$L($$R(&$R+"!!,R!A,RP 030 830L($$U(&$U
M+"!!-B!A-B`I#0I[#0H)<F5T=7)N("  =&AI<RT^*G9T86)L95\N9V5T*"!B
M("D *2`H(&(L(&$Q+"!A,BP 83,L(&$T+"!A-2P 838 *3L-"GT-" T*?3L-
M" T*#0IT96UP;&%T93P #0H)='EP96YA;64 4F5T("P #0H)='EP96YA;64 
M03$L#0H)='EP96YA;64 03(L#0H)='EP96YA;64 03,L#0H)='EP96YA;64 
M030L#0H)='EP96YA;64 034-"CX-"F-L87-S(%9I<VET;W(\(%)E="P 03$L
M($$R+"!!,RP 030L($$U+"!V;VED(#X >PT*<')I=F%T93H-" ET>7!E9&5F
M(%)E="`H5FES:71O<CHZ*D9U;F,I*"!"87-E*BP 03$L($$R+"!!,RP 030L
M($$U("D[#0H)5E1A8FQE/"!&=6YC(#X =G1A8FQE7SL-" T*<')O=&5C=&5D
M. T*=&5M<&QA=&4\('1Y<&5N86UE(%9I<VET;W)$97)V+"!T>7!E;F%M92!$
M97)V+"!2970 *%9I<VET;W)$97)V.CHJ9G5N8RDH($1E<G8J+"!!,2P 03(L
M($$S+"!!-"P 034 *2`^#0I2970 =&AU;FLH($)A<V4J(&(L($$Q(&$Q+"!!
M,B!A,BP 03, 83,L($$T(&$T+"!!-2!A-2`I#0I[#0H)<F5T=7)N("  <W1A
M=&EC7V-A<W0\(%9I<VET;W)$97)V*B`^*"!T:&ES*2T^*F9U;F, *2`H($-A
M<W10;VQI8WD\($1E<G8 /CHZ0V%S="  8B`I+"!A,2P 83(L(&$S+"!A-"P 
M834 *3L-"GT-" T*<'5B;&EC. T*#0I6:7-I=&]R*"!&=6YC(&1E9B`I(#H 
M=G1A8FQE7R  9&5F("D >R!]#0H-"E9I<VET;W(F(&]V97)R:61E*"D >R!R
M971U<FX *G1H:7,[('T-" T*=&5M<&QA=&4\('1Y<&5N86UE(%9I<VET;W)$
M97)V+"!T>7!E;F%M92!$97)V+"!2970 *%9I<VET;W)$97)V.CHJ9G5N8RDH
M($1E<G8J+"!!,2P 03(L($$S+"!!-"P 034 *2`^#0I6:7-I=&]R)B!A9&0H
M*0T*>PT*"79T86)L95\N861D/"!$97)V(#XH("96:7-I=&]R.CIT:'5N:SP 
M5FES:71O<D1E<G8L($1E<G8L(&9U;F,^("D[#0H)<F5T=7)N("IT:&ES.PT*
M?0T*#0H-"E)E="!O<&5R871O<B I*"!"87-E*B!B+"!!,2!A,2P 03( 83(L
M($$S(&$S+"!!-"!A-"P 034 834 *0T*>PT*"7)E='5R;B`H('1H:7,M/BIV
M=&%B;&5?+F=E="  8B`I("D *"!B+"!A,2P 83(L(&$S+"!A-"P 834 *3L-
M"GT-" T*?3L-" T*=&5M<&QA=&4\(`T*"71Y<&5N86UE(%)E="`L(`T*"71Y
M<&5N86UE($$Q+`T*"71Y<&5N86UE($$R+`T*"71Y<&5N86UE($$S+`T*"71Y
M<&5N86UE($$T#0H^#0IC;&%S<R!6:7-I=&]R/"!2970L($$Q+"!!,BP 03,L
M($$T+"!V;VED+"!V;VED(#X >PT*<')I=F%T93H-" ET>7!E9&5F(%)E="`H
M5FES:71O<CHZ*D9U;F,I*"!"87-E*BP 03$L($$R+"!!,RP 030 *3L-" E6
M5&%B;&4\($9U;F, /B!V=&%B;&5?.PT*#0IP<F]T96-T960Z#0IT96UP;&%T
M93P ='EP96YA;64 5FES:71O<D1E<G8L('1Y<&5N86UE($1E<G8L(%)E="`H
M5FES:71O<D1E<G8Z.BIF=6YC*2  1&5R=BHL($$Q+"!!,BP 03,L($$T("D 
M/ T*4F5T('1H=6YK*"!"87-E*B!B+"!!,2!A,2P 03( 83(L($$S(&$S+"!!
M-"!A-"`I#0I[#0H)<F5T=7)N("  <W1A=&EC7V-A<W0\(%9I<VET;W)$97)V
M*B`^*"!T:&ES*2T^*F9U;F, *2`H($-A<W10;VQI8WD\($1E<G8 /CHZ0V%S
M="  8B`I+"!A,2P 83(L(&$S+"!A-"`I.PT*?0T*#0IP=6)L:6,Z#0H-"E9I
M<VET;W(H($9U;F, 9&5F("D .B!V=&%B;&5?*"!D968 *2![('T-" T*5FES
M:71O<B8 ;W9E<G)I9&4H*2![(')E='5R;B`J=&AI<SL ?0T*#0IT96UP;&%T
M93P ='EP96YA;64 5FES:71O<D1E<G8L('1Y<&5N86UE($1E<G8L(%)E="`H
M5FES:71O<D1E<G8Z.BIF=6YC*2  1&5R=BHL($$Q+"!!,BP 03,L($$T("D 
M/ T*5FES:71O<B8 861D*"D-"GL-" EV=&%B;&5?+F%D9#P 1&5R=B`^*"`F
M5FES:71O<CHZ=&AU;FL\(%9I<VET;W)$97)V+"!$97)V+"!F=6YC/B`I.PT*
M"7)E='5R;B`J=&AI<SL-"GT-" T*#0I2970 ;W!E<F%T;W(H*2  0F%S92H 
M8BP 03$ 83$L($$R(&$R+"!!,R!A,RP 030 830 *0T*>PT*"7)E='5R;B`H
M('1H:7,M/BIV=&%B;&5?+F=E="  8B`I("D *"!B+"!A,2P 83(L(&$S+"!A
M-"`I.PT*?0T*#0I].PT*#0IT96UP;&%T93P #0H)='EP96YA;64 4F5T("P 
M#0H)='EP96YA;64 03$L#0H)='EP96YA;64 03(L#0H)='EP96YA;64 03,-
M"CX-"F-L87-S(%9I<VET;W(\(%)E="P 03$L($$R+"!!,RP =F]I9"P =F]I
M9"P =F]I9"`^('L-"G!R:79A=&4Z#0H)='EP961E9B!2970 *%9I<VET;W(Z
M.BI&=6YC*2  0F%S92HL($$Q+"!!,BP 03, *3L-" E65&%B;&4\($9U;F, 
M/B!V=&%B;&5?.PT*#0IP<F]T96-T960Z#0IT96UP;&%T93P ='EP96YA;64 
M5FES:71O<D1E<G8L('1Y<&5N86UE($1E<G8L(%)E="`H5FES:71O<D1E<G8Z
M.BIF=6YC*2  1&5R=BHL($$Q+"!!,BP 03, *2`^#0I2970 =&AU;FLH($)A
M<V4J(&(L($$Q(&$Q+"!!,B!A,BP 03, 83, *0T*>PT*"7)E='5R;B`H('-T
M871I8U]C87-T/"!6:7-I=&]R1&5R=BH /B  =&AI<RDM/BIF=6YC("D *"!#
M87-T4&]L:6-Y/"!$97)V(#XZ.D-A<W0H(&( *2P 83$L(&$R+"!A,R`I.PT*
M?0T*#0IP=6)L:6,Z#0H-"E9I<VET;W(H($9U;F, 9&5F("D .B!V=&%B;&5?
M*"!D968 *2![('T-" T*5FES:71O<B8 ;W9E<G)I9&4H*2![(')E='5R;B`J
M=&AI<SL ?0T*#0IT96UP;&%T93P ='EP96YA;64 5FES:71O<D1E<G8L('1Y
M<&5N86UE($1E<G8L(%)E="`H5FES:71O<D1E<G8Z.BIF=6YC*2  1&5R=BHL
M($$Q+"!!,BP 03, *2`^#0I6:7-I=&]R)B!A9&0H*0T*>PT*"79T86)L95\N
M861D/"!$97)V(#XH("96:7-I=&]R.CIT:'5N:SP 5FES:71O<D1E<G8L($1E
M<G8L(&9U;F,^("D[#0H)<F5T=7)N("IT:&ES.PT*?0T*#0H-"E)E="!O<&5R
M871O<B I*"!"87-E*B!B+"!!,2!A,2P 03( 83(L($$S(&$S("D-"GL-" ER
M971U<FX *"!T:&ES+3XJ=G1A8FQE7RYG970H(&( *2`I("  8BP 83$L(&$R
M+"!A,R`I.PT*?0T*#0I].PT*#0IT96UP;&%T93P #0H)='EP96YA;64 4F5T
M("P #0H)='EP96YA;64 03$L#0H)='EP96YA;64 03(-"CX-"F-L87-S(%9I
M<VET;W(\(%)E="P 03$L($$R+"!V;VED+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7-I=&]R/"!2970L($$Q
M+"!V;VED+"!V;VED+"!V;VED+"!V;VED+"!V;VED(#X >PT*<')I=F%T93H-
M" ET>7!E9&5F(%)E="`H5FES:71O<CHZ*D9U;F,I*"!"87-E*BP 03$ *3L-
M" E65&%B;&4\($9U;F, /B!V=&%B;&5?.PT*#0IP<F]T96-T960Z#0IT96UP
M;&%T93P ='EP96YA;64 5FES:71O<D1E<G8L('1Y<&5N86UE($1E<G8L(%)E
M="`H5FES:71O<D1E<G8Z.BIF=6YC*2  1&5R=BHL($$Q("D / T*4F5T('1H
M=6YK*"!"87-E*B!B+"!!,2!A,2`I#0I[#0H)<F5T=7)N("  <W1A=&EC7V-A
M<W0\(%9I<VET;W)$97)V*B`^*"!T:&ES*2T^*F9U;F, *2`H($-A<W10;VQI
M8WD\($1E<G8 /CHZ0V%S="  8B`I+"!A,2`I.PT*?0T*#0IP=6)L:6,Z#0H-
M"E9I<VET;W(H($9U;F, 9&5F("D .B!V=&%B;&5?*"!D968 *2![('T-" T*
M5FES:71O<B8 ;W9E<G)I9&4H*2![(')E='5R;B`J=&AI<SL ?0T*#0IT96UP
M;&%T93P ='EP96YA;64 5FES:71O<D1E<G8L('1Y<&5N86UE($1E<G8L(%)E
M="`H5FES:71O<D1E<G8Z.BIF=6YC*2  1&5R=BHL($$Q("D / T*5FES:71O
M<B8 861D*"D-"GL-" EV=&%B;&5?+F%D9#P 1&5R=B`^*"`F5FES:71O<CHZ
M=&AU;FL\(%9I<VET;W)$97)V+"!$97)V+"!F=6YC/B`I.PT*"7)E='5R;B`J
M=&AI<SL-"GT-" T*#0I2970 ;W!E<F%T;W(H*2  0F%S92H 8BP 03$ 83$ 
M*0T*>PT*"7)E='5R;B`H('1H:7,M/BIV=&%B;&5?+F=E="  8B`I("D *"!B
M+"!A,2`I.PT*?0T*#0I].PT*#0IT96UP;&%T93P #0H)='EP96YA;64 4F5T
M#0H^#0IC;&%S<R!6:7-I=&]R/"!2970L('9O:60L('9O:60L('9O:60L('9O
M:60L('9O:60L('9O:60 /B![#0IP<F]T96-T960Z#0H)='EP961E9B!2970 
M*%9I<VET;W(Z.BI&=6YC*2  0F%S92H *3L-" E65&%B;&4\($9U;F, /B!V
M=&%B;&5?.PT*#0IP<F]T96-T960Z#0IT96UP;&%T93P ='EP96YA;64 5FES
M:71O<D1E<G8L('1Y<&5N86UE($1E<G8L(%)E="`H5FES:71O<D1E<G8Z.BIF
M=6YC*2  1&5R=BH *2`^#0I2970 =&AU;FLH($)A<V4J(&( *0T*>PT*"7)E
M='5R;B`H('-T871I8U]C87-T/"!6:7-I=&]R1&5R=BH /B  =&AI<RDM/BIF
M=6YC("D *"!#87-T4&]L:6-Y/"!$97)V(#XZ.D-A<W0H(&( *2`I.PT*?0T*
M#0IP=6)L:6,Z#0H-"E9I<VET;W(H($9U;F, 9&5F("D .B!V=&%B;&5?*"!D
M968 *2![('T-" T*5FES:71O<B8 ;W9E<G)I9&4H*2![(')E='5R;B`J=&AI
M<SL ?0T*#0IT96UP;&%T93P ='EP96YA;64 5FES:71O<D1E<G8L('1Y<&5N
M86UE($1E<G8L(%)E="`H5FES:71O<D1E<G8Z.BIF=6YC*2  1&5R=BH *2`^
M#0I6:7-I=&]R)B!A9&0H*0T*>PT*"79T86)L95\N861D/"!$97)V(#XH("96
M:7-I=&]R.CIT:'5N:SP 5FES:71O<D1E<G8L($1E<G8L(&9U;F,^("D[#0H)
M<F5T=7)N("IT:&ES.PT*?0T*#0H-"E)E="!O<&5R871O<B I*"!"87-E*B!B
M("D-"GL-" ER971U<FX *"!T:&ES+3XJ=G1A8FQE7RYG970H(&( *2`I("  
M8B`I.PT*?0T*#0I].PT*#0I].PT*#0IT96UP;&%T93P ='EP96YA;64 0F%S
M92P =&5M<&QA=&4\(&-L87-S(#X 8VQA<W, 0V%S=%!O;&EC>2`^#0IS:7IE
M7W0 1&ES<&%T8V \($)A<V4L($-A<W10;VQI8WD /CHZ<U]T86=?(#T ,#L-
M" T*=&5M<&QA=&4\('1Y<&5N86UE($)A<V4L('1E;7!L871E/"!C;&%S<R`^
M(&-L87-S($-A<W10;VQI8WD / T*=&5M<&QA=&4\('1Y<&5N86UE($1E<G8 
M/ T*='EP96YA;64 1&ES<&%T8V \($)A<V4L($-A<W10;VQI8WD /CHZ=&5M
M<&QA=&4 3F5W5&%G/"!$97)V(#X-"G1Y<&5N86UE($1I<W!A=&-H/"!"87-E
M+"!#87-T4&]L:6-Y(#XZ.G1E;7!L871E($YE=U1A9SP 1&5R=B`^.CIS7VYE
5=U]T86=?.PT*#0H-"B-E;F1I9 T*
`
end
begin 644 test1.cpp
M(VEN8VQU9&4 (D-6:7-I=&]R+F B#0H-"B-I;F-L=61E(#QI;W-T<F5A;3X-
M"B-I;F-L=61E(#QV96-T;W(^#0HC:6YC;'5D92`\<W1R:6YG/ T*#0H-"B\O
M(&$ :&5L<&5R(&-L87-S('1O(&%S<VEG;B!R:6=H="!T86< 9F]R(&$ 8VQA
M<W,-"B\O(&5A8V  8VQA<W, :6X =&AE(&AE:7)A<F-H>2!N965D<R!T;R!H
M879E('1H:7, 87, #0HO+R!T:&4 ;&%S="!B87-E(&-L87-S(&EN(&ET<R!B
M87-E(&-L87-S(&QI<W0-"G1E;7!L871E/"!T>7!E;F%M92!"87-E+"!T>7!E
M;F%M92!$97)V("`^#0IS=')U8W0 5&%G9V5D('L #0I486=G960H*2`-"GL 
M#0H)0F%S92H 8B`]('-T871I8U]C87-T/"!$97)V*B`^*"!T:&ES("D[#0H)
M8BT^7U]S971?=&%G*"!$:7-P871C:#P 0F%S92`^.CI486<\($1E<G8 /B I
M("D[(`T*?0T*?3L-" T*8VQA<W, 4VAA<&4 .B!486=G960\(%-H87!E+"!3
M:&%P92`^("![#0IP=6)L:6,Z#0H-"E-H87!E*"D >WT-" T*+R\ =&AE(')O
M;W0 8VQA<W, :6X =&AE(&AE:7)A<F-H>0T*+R\ ;F5E9', =&\ <')O=FED
M92!T:&ES(&9U;F-T:6]N#0IV;VED(%]?<V5T7W1A9R  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E*"`S+C$T,35F("D *3L-" T*"5!R:6YT
M97( <')I;G0[#0H)<')O8V5S<R  )G!R:6YT+"`F<VAA<&5S("D[#0H-" ES
M=&0Z.F-O=70\/")<;EQN7&XM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM
M+2U<;EQN7&XB.PT*#0H)4')I;G1E<C( <')I;G0R.PT*"7!R;V-E<W,H("9P
M<FEN=#(L("9S:&%P97, *3L-" T*"7-T9#HZ8V]U=#P\(EQN7&Y<;BTM+2TM
M+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+5QN7&Y<;B([#0H-" EP<FEN=#( 
M/2!0<FEN=&5R,B I.PT*"65X86UP;&5?861D7VYE=U]C;&%S<U]A;F1?=7!D
M871E7W9I<VET;W(H("9S:&%P97,L("9P<FEN=#( *3L-" EP<F]C97-S*"`F
;<')I;G0R+"`F<VAA<&5S("D[#0H-"GT-" T*
`
end
Apr 24 2007
parent Anand Krishnamoorthi <anand.shankar.k gmail.com> writes:
I'll test it in Digital Mars C++, G++ soon and post the results.

Anand.
Apr 24 2007