Member function pointer, delegates

M

mosfet

Hi,

In my project I am supposed to test if some HTTP headers belongs to a
know list of system headers so I wrote the following code.
I get a WebRequest class(ported from .net world) and a HttpWebRequest
deriving from it.
In my WebRequest I have some getters/setters defined like this
:

typedef std::basic_string<TCHAR> tstring;

#define DECL_GET_PROP( PropName, Type ) \
Type& get_##PropName() { return m_##PropName; }

#define DECL_SET_PROP( PropName, Type ) \
void set_##PropName(Type PropVal) { m_##PropName = PropVal; }

#define DECL_GETSET_PROP( PropName, Type ) \
DECL_GET_PROP(PropName, Type) \
DECL_SET_PROP(PropName, Type) \

class WebRequest
{
public:
....
// Properties
virtual DECL_GETSET_PROP(Accept, tstring);
virtual DECL_GETSET_PROP(Connection, tstring);
virtual DECL_GETSET_PROP(ConnectionGroupName, tstring);
virtual DECL_GETSET_PROP(ContentLength, long);
virtual DECL_GETSET_PROP(ContentType, tstring);
virtual DECL_GETSET_PROP(Headers, WebHeaderCollection);
virtual DECL_GETSET_PROP(Method, tstring);
virtual DECL_GETSET_PROP(PreAuthenticate, bool);
virtual DECL_GETSET_PROP(UserAgent, tstring);
virtual DECL_GETSET_PROP(Proxy, tstring);
....
};
I know macros are evil but in my case I find it useful, so the first
line for instance will be expanded as :
virtual tstring& get_Accept() { return m_Accept; }
virtual void set_Accept(tstring PropVal) {m_Accept = PropVal; }


class HttpWebRequest : public WebRequest
{
....
};



In my user class I would like

Ret_t CHttpAdapter::FilterHeader(BOOL a_fSpecific,
LPTSTR a_pHeader, LPTSTR a_pSpecificHeader, LPTSTR a_pValue )
{
Ret_t nRet = TP_ERR_OK;

typedef tstring& (HttpWebRequest::* get_Prop)(void);
typedef void (HttpWebRequest::* set_Prop)(tstring);

struct
{
const TCHAR* szHeader;
get_Prop pfnGet;
set_Prop pfnSet;
}
HdrFilter_t[] =
{
HdrFilter_t[] =
{
{_T("Accept"), &HttpWebRequest::get_Accept,
&HttpWebRequest::set_Accept },
{_T("Connection"), &HttpWebRequest::get_Connection,
&HttpWebRequest::set_Connection },
{_T("Content-Length"), &HttpWebRequest::get_ContentLength,
&HttpWebRequest::set_ContentLength },
{_T("Content-Type"), &HttpWebRequest::get_ContentType,
&HttpWebRequest::set_ContentType },

};

return nRet;
}

My goal is to test the parameters a_pHeader and if it correponds to one
of my struct array to call the member function pointer.
For instance if a_pHeader = "Accept" I would like to call
HttpWebRequest::setAccept( a_pHeader );

The problem is my get/set methods don't have the same signature, for
instance Accept takes a string while ContentLength takes a long.
My question is how to solve this ?
Should I use delegated instaead of member function pointers ?
 
I

Ian Collins

mosfet said:
Hi,

In my project I am supposed to test if some HTTP headers belongs to a
know list of system headers so I wrote the following code.
I get a WebRequest class(ported from .net world) and a HttpWebRequest
deriving from it.
In my WebRequest I have some getters/setters defined like this
:

typedef std::basic_string<TCHAR> tstring;
What's a TCHAR and isn't there an alternative standard type?
#define DECL_GET_PROP( PropName, Type ) \
Type& get_##PropName() { return m_##PropName; }
class WebRequest
{
public:
....
// Properties
virtual DECL_GETSET_PROP(Accept, tstring);

....
};
I know macros are evil but in my case I find it useful, so the first
line for instance will be expanded as :
virtual tstring& get_Accept() { return m_Accept; }
virtual void set_Accept(tstring PropVal) {m_Accept = PropVal; }
It's not the macros that would bother me, its the presence of all the
setter and getter methods, which tent to be indicative of a poor design.
The problem is my get/set methods don't have the same signature, for
instance Accept takes a string while ContentLength takes a long.
My question is how to solve this ?
Should I use delegated instaead of member function pointers ?
There you are, there is a problem with the design! You probably want to
look at the factory pattern, with a process object for each header item,
build a list of header objects for each message and process that.
 
M

mosfet

Ian Collins a écrit :
What's a TCHAR and isn't there an alternative standard type?




It's not the macros that would bother me, its the presence of all the
setter and getter methods, which tent to be indicative of a poor design.

There you are, there is a problem with the design! You probably want to
look at the factory pattern, with a process object for each header item,
build a list of header objects for each message and process that.

It's not my design, tell Microsoft they have bad design in their .NET
languages.
 
M

mosfet

mosfet a écrit :
It's not my design, tell Microsoft they have bad design in their .NET
languages.




I wanted to do something different but finally I think I am going to use
an old good
if (strcmp("Accept", a_pHeader) == 0)
m_Request->set_Accept(a_pHeader);
else if
....
 
G

Guest

Ian Collins a écrit :

It's not my design, tell Microsoft they have bad design in their .NET
languages.

If you are using the .Net Framework you should use C++/CLR, you will
probably never get it to work correctly with standard C++. For more help
with C++/CLR ask in one of the microsoft.public.* newsgroups or on
http://forums.microsoft.com/.
 
Joined
Oct 4, 2007
Messages
4
Reaction score
0
Pass UNION instead of long,string etc.

Wrap function in another function with uniform signature.
Or pass a union as parameters,so that all have same signature.
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top