What Is Proxy Class in C++

Write a proxy class in C++

There are 2 common ways of expressing this:

  1. provide an implicit conversion:

 

class DeviceWrapper
{
operator PhysicalDevice&() { return mDevice; }
private:
PhysicalDevice mDevice;
};

 


  1. re-implement the function as a method (for reasons mentioned in another answer, I would favour this approach):

 

class DeviceWrapper
{
auto someFunc() { return ::somefunc(mDevice); }

private:
PhysicalDevice mDevice;
};

Is a proxy class the same as a class wrapper?

There are a couple of ways of handling the problem at hand.

One is to map to a common domain model. This works fine if you have the same basic behavior, but might not serve you well in the particulars (different keys for different types of clients).

Another is to move the common bits down into a base class and then inherit for the different specifics in state (different properties) or the different behaviors (primary key only id, etc). Considering this is both differing behavior and state, this is a direction you can use.

Now, patterns. A proxy pattern is one where the new object can provide some of the behavior and state of another object, but is not the object. Think of it like what a person voting for you as a proxy means, and then relate it to software. I would not think of it like a wrapper, as a wrapper is normally used to present a different face from the underlying object. This can be due to the need to hide something in the underlying object or to add further behavior on top of the object. The proxy pattern will not help with the varying keys; Possible with the wrapper, although I am not convinced it is the simplest way to do this.

Proxy class and correct syntax c++

It looks like you're referring to the wrong scope for your operator[]. Perhaps you meant:

CScreen::Proxy CScreen::operator[] (int index) const 
{
return Proxy ( m_playground, index, m_y );
}

proxy class in rvalue - how to implement assignment operator?

Add a conversion function to your proxy class:

class proxy
{
public:
operator double() const { return data[index]; }

// ...
};

C++11 how to proxy class function having only its name and parent class?

How about this:

proxy_macro.hpp

#include <type_traits>
#include <utility>

#define PROXY(proxified, member_function, proxy_name) \
class proxy_name \
{ \
private: \
proxified & ref_; \
\
public: \
proxy_name(proxified &ref) \
: ref_(ref) \
{ \
} \
\
/* general version */ \
template<typename ... Args> \
auto member_function(Args&& ... args) \
-> typename std::enable_if<!std::is_void<decltype(ref_.member_function(std::forward<Args>(args)...))>::value, \
decltype(ref_.member_function(std::forward<Args>(args)...))>::type \
{ \
return (ref_.member_function(std::forward<Args>(args)...)); \
} \
\
/* void return type version */ \
template<typename ... Args> \
auto member_function(Args&& ... args) \
-> typename std::enable_if<std::is_void<decltype(ref_.member_function(std::forward<Args>(args)...))>::value, \
void>::type \
{ \
ref_.member_function(std::forward<Args>(args)...); \
} \
\
};

This compiles and work fine for me on g++ 4.7:

#include "proxy_macro.hpp"

#include <iostream>
#include <string>

class Email
{
public:
void set_email(const ::std::string& value)
{
std::cout << value << std::endl;
}

void set_email(const char* value)
{
std::cout << value << std::endl;
}

int set_email()
{
return (42);
}

};

PROXY(Email, set_email, MyEmail)

int main(void)
{
Email mail;
MyEmail my_mail(mail);

std::string str = "test string";
const char * ptr = "test char pointer";

my_mail.set_email(str);
my_mail.set_email(ptr);

std::cout << "test return: " << my_mail.set_email() << std::endl;

return (0);
}

Edit (smaller version thanks to comments)

proxy_macro.hpp

#include <type_traits>
#include <utility>

#define PROXY(proxified, member_function, proxy_name) \
class proxy_name \
{ \
private: \
proxified & ref_; \
\
public: \
proxy_name(proxified &ref) \
: ref_(ref) \
{ \
} \
\
template<typename ... Args> \
auto member_function(Args&& ... args) \
-> decltype(ref_.member_function(std::forward<Args>(args)...)) \
{ \
return (ref_.member_function(std::forward<Args>(args)...)); \
} \
};

Dynamic proxy classes in C++. Is it possible?

No. This is designed specifically to be non intrusive, All the wrappers do is facilitate the calling of prefix and suffix and then return the object that is being referenced so that it can call the specified function. If the -> operator is overloaded then, object->function() gets expanded to object.operator->()->function().

Here is a link to the paper Stroustrup wrote, it's very informative http://www.stroustrup.com/wrapper.pdf



Related Topics



Leave a reply



Submit