C++ Blog

C++ Proxy Template

Posted in templates by Umesh Sirsiwal on January 14, 2009

Earlier we had discussed Java Proxy class and had looked for ways to develop similar facility with C++. It turns out there is a rather easy way to provide such a facility. If you have used smart pointers, you have already used one form C++ proxy template. Smart pointers essentially provide a proxy facility. Bjarne Stroustrup covered this subject in this paper.

The key to the wrapper definition is that the fact that all objects created on the stack are eventually destroyed when the object goes out of the scope. For example:

class Test{
    void method(){
         X a;
       // Implementation
       return;
     }

}

In the above case the X’s destructor is called when Test::method returns due to return call or exception.

Now let us use this knowledge to define the Wrap class. Let us first define the prefix, suffix and the wrapped class:

#include <iostream>
 using namespace std;
 void prefix() { cout << "prefix\n" }
 void suffix() { cout << "suffix\n"; }

// The person class looks like this:

class Person{
  std::string mName;
  Person(std::string pName): mName(name){}
  void printName(){
     std::cout << mName << std::endl;
  }

};

Now we can define the Wrap class as the following:

template <class T >
class Wrap {
     T * p ;
     public:
             Wrap (T * pp ) :p (pp) { }
             Call_proxy <T> operator ->() {
                   prefix ();
                   return Call_proxy<T>(p);
             }
};
template <class T >
class Call_proxy {
       T * p ;
       public :
              Call_proxy (T * pp ) :p (pp ){ }
              ˜Call_proxy () {
                     suffix ();
               }
               T * operator ->() {
                          return p ;
                }
 };

We can now wrap any object  of type T in the Wrap class. For example: we can take class Person and wrap it like Wrap<Person> person(new Person);

Any dereferencce of person using -> operator, results in call to operator-> of Wrap class. The method calls the prefix function , creates a new object of type Call_proxy containing the pointer to object. The method is called. On return from the method, Call_proxy is destroyed which implicitly results in call to suffix().

Let us test it. the following code snippet:

Wrap<Person> person(new Person("test"));
person->printName();

should print:

prefix
test
suffix

Rather straightforward and elegant to use. But, syntatic sugar to implement.

Note that suffix will be called even if the method returned due to exception.  This is better than Java, where the programmer has to take explicit action to make sure suffix is called in case of exception.

The Stroustrup’s paper also covers other issues like ownership, parametrization etc.  In most cases you may not need to worry about those. If you need to, take a look at the paper.

Happy proxying!

About these ads

One Response

Subscribe to comments with RSS.

  1. [...] Style Dynamic Proxy in C++ Posted in c++0x by Umesh Sirsiwal on January 9th, 2009 This post defines C++ template based implementation for C++ [...]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: