doublecpp -- double dispatch in C++

doublecpp -- double dispatch in C++

Post by Lorenzo Bettin » Tue, 29 Jul 2003 02:12:00



Hi

I've just released doublecpp:

Doublecpp is a preprocessor for C++ that handles a new linguistic
construct for defining branches of a multi-method.  The "right" branch
of such a method will be selected dynamically at run-time according to
the actual type of the object on which the method is invoked and to the
actual type of the first argument: double dispatch.

http://www.lorenzobettini.it/software/doublecpp/index.html

Doublecpp is free software; you are free to use, share and modify it
under the terms of the GNU General Public License (see COPYING).

Doublecpp is delivered with sources that have to be compiled. It can be
downloaded from the following site:

http://www.lorenzobettini.it/software/doublecpp/doublecpp-0.2.tar.gz

Doublecpp has been developed using GNU gcc 3.2.2

You can build doublecpp also under Windows, using cygwin
(http://www.cygwin.com).  If you have problems with that, you can
request the windows binaries directly to me.

IMPORTANT: you have to use a 3.x version of gcc: earlier versions have
problems handling code generated by doublecpp.

You can find the documentation in the file

http://www.lorenzobettini.it/software/doublecpp/doublecpp.ps

I look forward to receiving comments!

regards
        Lorenzo

--
+-----------------------------------------------------+
|  Lorenzo Bettini          ICQ# lbetto, 16080134     |
|  PhD in Computer Science                            |
|  Dip. Sistemi e Informatica, Univ. di Firenze       |
|  Tel +39 055 4796741, Fax +39 055 4796730           |
|  Florence - Italy        (GNU/Linux User # 158233)  |
|  Home Page        : http://www.lorenzobettini.it    |
|  http://music.dsi.unifi.it         XKlaim language  |
|  http://www.lorenzobettini.it/purple    Cover Band  |
|  http://www.gnu.org/software/src-highlite           |
|  http://www.gnu.org/software/gengetopt              |
|  http://www.lorenzobettini.it/software/gengen       |
+-----------------------------------------------------+

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

doublecpp -- double dispatch in C++

Post by Siemel Nara » Tue, 29 Jul 2003 09:16:41



> Doublecpp is a preprocessor for C++ that handles a new linguistic
> construct for defining branches of a multi-method.  The "right" branch
> of such a method will be selected dynamically at run-time according to
> the actual type of the object on which the method is invoked and to the
> actual type of the first argument: double dispatch.

> http://www.lorenzobettini.it/software/doublecpp/index.html

Can you briefly explain what this tool does?  BTW, I don't know how to read
your documentation file doublecpp.ps.

My double dispatch idea works but might not be as efficient as yours.  One
writes a function

struct Function : public std::binary_function<Base1,Base2,bool>
{

Quote:};

to represent a function that returns bool, and takes const Base1& and const
Base2& as function arguments.

Also define your standalone functions

    bool f1(const Derived1&, const Derived2&);
    bool f2(const Derived1&, const MoreDerived2&);
    bool f3(const MoreDerived1&, const Derived2&);
    bool f4(const MoreDerived1&, const MoreDerived2&);

There is a class double_dispatch<Function> that is a registry.  Create an
instance of this class with name dd.  Add your f1 to f4 functions to the
registry.  Then use the registry to lookup and invoke the right function.  I
make use of templates and typeid to make things easy for the end-user so
they can say dd(var1,var2).  But everything happens at runtime, so it might
not be efficient.  And also it doesn't support non-const reference arguments
like (Base1&, const Base2&), or additional function arguments like (const
Base1&, const Base2&, Thing), or triple dispatch.

--
+++++++++++
Siemel Naran

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

doublecpp -- double dispatch in C++

Post by ap » Wed, 30 Jul 2003 19:26:05



 >
 > > Doublecpp is a preprocessor for C++
 > Can you briefly explain what this tool does?  BTW, I don't know how to read
 > your documentation file doublecpp.ps.

It's postscript. Use ghostview to view it
(http://www.cs.wisc.edu/~ghost). BTW, note to the author: I see that
it was written using TeX. Good. But the TeX tools include dvipdfm,
which converts from dvi to Adobe Acrobat format. Perhaps a PDF version
could be made available as well as a postscript version?

Regards,

Andrew Marlow.

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

doublecpp -- double dispatch in C++

Post by Lorenzo Bettin » Thu, 31 Jul 2003 07:41:31



>  > Can you briefly explain what this tool does?  BTW, I don't know how to read
>  > your documentation file doublecpp.ps.

> It's postscript. Use ghostview to view it
> (http://www.cs.wisc.edu/~ghost). BTW, note to the author: I see that
> it was written using TeX. Good. But the TeX tools include dvipdfm,
> which converts from dvi to Adobe Acrobat format. Perhaps a PDF version
> could be made available as well as a postscript version?

just released a pdf version it :-)

thanks
        Lorenzo

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

doublecpp -- double dispatch in C++

Post by Lorenzo Bettin » Thu, 31 Jul 2003 07:43:03



 >>
 >>
 >
 >>>>Doublecpp is a preprocessor for C++ that handles a new linguistic
 >>>>construct for defining branches of a multi-method.  The "right" branch
 >>>>of such a method will be selected dynamically at run-time according to
 >>>>the actual type of the object on which the method is invoked and to the
 >>>>actual type of the first argument: double dispatch.
 >>>>
 >>>>http://www.lorenzobettini.it/software/doublecpp/index.html
 >
 >>
 >>
 >> Can you briefly explain what this tool does?  BTW, I don't know how
to read
 >> your documentation file doublecpp.ps.

Hi

I've just released also pdf documentation, since others have
requested it (it is available at the same site).

 >>
 >> My double dispatch idea works but might not be as efficient as
yours.  One
 >> writes a function
 >>
 >> struct Function : public std::binary_function<Base1,Base2,bool>
 >> {
 >> };
 >>
 >> to represent a function that returns bool, and takes const Base1&
and const
 >> Base2& as function arguments.
 >>
 >> Also define your standalone functions
 >>
 >>     bool f1(const Derived1&, const Derived2&);
 >>     bool f2(const Derived1&, const MoreDerived2&);
 >>     bool f3(const MoreDerived1&, const Derived2&);
 >>     bool f4(const MoreDerived1&, const MoreDerived2&);
 >>
 >> There is a class double_dispatch<Function> that is a registry.
Create an
 >> instance of this class with name dd.  Add your f1 to f4 functions to the
 >> registry.  Then use the registry to lookup and invoke the right
function.  I

the basic idea of doublecpp is to relieve the programmer from explicitly
writing code for obtaining double dispatch.  Indeed there are some
programming techniques to "simulate" double dispatch: yours is a
solution (based on table lookup), another solution is the Visitor design
pattern that dispatches the method twice in order to exploit dynamic
binding twice thus obtaining the "same semantics" of double dispatch (in
this sense it is more efficient than table lookup).

The code generated by doublecpp uses an idea pretty close to the visitor
pattern; this is just the basic idea behind the code transformation
performed by doublecpp.

Doublecpp also tries to minimize the cyclic dependences that is the main
drawback of visitor pattern.  Let me stress once again that the
programmer does not have to explicitly write any additional code

please let me know if you're interested in further inner details.
I hope you'll try doublecpp and please do not hesitate to contact me for
any problem (as I said it is still in beta stage).

best regards
         Lorenzo

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]