Dynamic Proxy in C++

G

Guillermo Schwarz

C++ers,

I have programmed in C++ since 1991 and I probably created my best
abstraction layers in C++ in those days. Since 2001 I've been
programming in Java (at first against my will, then I realized the
market was moving into Java) and I'm trying to reimplement in Java
what I did back in those days.

The problem I have is that I have a job offer for programming in C++,
again!!!, and I don't know if I should take it. My problem is mainly
that I use to program using Dynamic proxies in Java, and the mere idea
of going back in time on that technology makes me feel very uneasy and
worrying that I will spend many years writing code that could be
written in months.

In short, is there a mechanism in C++ that is similar to the Dynamic
Proxy mechanism?

If you don't know what a dynamic proxy is, it is simply an interceptor
class that performs some code before and after any method of the
intercepted class, given by the interceptor class. The dynamic proxy
is like a normal proxy, except that a normal proxy must be rewritten
for each intercepted class, while the dynamic proxy is written pnly
once and reused for all intercepted classes.

http://java.sun.com/j2se/1.3/docs/guide/reflection/proxy.html

It can be used for logging, for returning resources (like
connections), for validating inputs, for converting normal classes
into EJBs (so remoting them and making them implicitly transactional),
etc.

Cheers,
Guillermo.
 
M

Marcus Kwok

Guillermo Schwarz said:
In short, is there a mechanism in C++ that is similar to the Dynamic
Proxy mechanism?

If you don't know what a dynamic proxy is, it is simply an interceptor
class that performs some code before and after any method of the
intercepted class, given by the interceptor class. The dynamic proxy
is like a normal proxy, except that a normal proxy must be rewritten
for each intercepted class, while the dynamic proxy is written pnly
once and reused for all intercepted classes.

http://java.sun.com/j2se/1.3/docs/guide/reflection/proxy.html

It can be used for logging, for returning resources (like
connections), for validating inputs, for converting normal classes
into EJBs (so remoting them and making them implicitly transactional),
etc.

Maybe this paper will be of interest to you:

Wrapping C++ Member Function Calls
by Bjarne Stroustrup
http://www.research.att.com/~bs/wrapper.pdf
 
G

Gianni Mariani

Guillermo said:
... the mere idea
of going back in time on that technology makes me feel very uneasy and
worrying that I will spend many years writing code that could be
written in months.

This should answer your question.

C++ has many features, some that map the same way Java does some that
don't. If you're looking for Java, go find a Java job.
 
S

Stuart Redmann

Guillermo said:
C++ers,

I have programmed in C++ since 1991 and I probably created my best
abstraction layers in C++ in those days. Since 2001 I've been
programming in Java (at first against my will, then I realized the
market was moving into Java) and I'm trying to reimplement in Java
what I did back in those days.

The problem I have is that I have a job offer for programming in C++,
again!!!, and I don't know if I should take it. My problem is mainly
that I use to program using Dynamic proxies in Java, and the mere idea
of going back in time on that technology makes me feel very uneasy and
worrying that I will spend many years writing code that could be
written in months.

In short, is there a mechanism in C++ that is similar to the Dynamic
Proxy mechanism?

If you don't know what a dynamic proxy is, it is simply an interceptor
class that performs some code before and after any method of the
intercepted class, given by the interceptor class. The dynamic proxy
is like a normal proxy, except that a normal proxy must be rewritten
for each intercepted class, while the dynamic proxy is written pnly
once and reused for all intercepted classes.

http://java.sun.com/j2se/1.3/docs/guide/reflection/proxy.html

It can be used for logging, for returning resources (like
connections), for validating inputs, for converting normal classes
into EJBs (so remoting them and making them implicitly transactional),
etc.

As C++ offers no reflection, you have to find a solution to this for yourself.

We have written a small tool that generates proxy classes (actually proxy
templates) for COM interfaces. These proxy classes map the COM types to C++
types (like VARIANT_BOOL to bool), so that the user can call COM methods just
like C++ interfaces (thus avoiding the mess with COM return arguments). The
generated proxies look quite similiar to the ones that are generated by Visual C
when you #import a type library, except that logging facilities are added. As
this tool only has to deal with COM interfaces (and only very well-formed COM
interfaces that are Automation compatible), we didn't have to spend to much time
for the mapping of all possible C++ types. This tool is made part of the
compilation process, so we don't have to care much about it.

Another solution would be to provide some kind of reflection feature yourself.
As this seems to be quite challenging, I'd suggest to write your own tool.

Regards,
Stuart
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top