C ++ proxy class

Is there a way to easily implement a proxy class template in C ++? Do not use AspectC ++ or other heavy tools, just built-in macros or templates.

Explaining what I want:

class base_class { public: virtual void method_one() { ... } virtual void method_two() { ... } } class class_proxy : base_class { protected: void before_any_method_call() { do stuff }; void after_any_method_call(std::exception* ex) { do stuff } } 

Here is the script. The class that I want the proxy (base_class) makes remote calls, however, when the network is disconnected, it will throw a transport exception, obtained from std :: exception. Base_class has tons of method, and I would like to catch a transport exception, answer with an empty result and reconnect until the next method call.

+7
source share
3 answers

If you mean something that is automatically generated through something like reflection, no. One common way to implement simple proxies is to override operator-> proxies. This works if at that time you can do what you need to do in the proxy server. This method is found in GoF's “Design Patterns” under “Implementation”.

Edit (based on your additional information): If you want to do something before each call, the operator->() overload mechanism works well for this. To do something after each call is not so simple as to automate, but I could imagine that something is created from a special return object that calls it when it destroys.

+3
source

This may help http://www.stroustrup.com/wrapper.pdf , however I don't think it is possible to handle exceptions this way.

+1
source

I'm not sure derivation is the best design for a proxy template. Your post may be outdated, but I have a sample version of the proxy template to protect the container element and STL elements here. It uses template programming and the latest features of C ++ 17: instructions for subtraction!

0
source

All Articles