Re: isolate a static method
Quote:
Originally Posted by
munteanu24d
Hello everybody!
Here is my problme description.
In my class I have a method which is static, but which, in the same time has to access some data members of my class. Conseqeuntly, all the data members it uses become static, plus other methods of the class...so...everything is transforming, little by little, to static... which is something I obviously do not want to...
I am wondering if there is some design pattern which could help me isolate the static method.
I have to mention that the reason why the method is static is because I need a reference/pointer to it, which has to be known at the run time.
Thank you in advance,
D.M.
You explanation is confusing me, so I'll try to explain the best I can.
You can have pointers to member functions, so I don't see the problem.
If you are affraid of pointers to member and insist on static, why not just make an instance of your class an argument to your function?
Code:
class my_class
{
public:
static void static_multiply_by_two(my_class& object)
{
int val = object.get();
val *= 2;
object.set(val);
}
void set(int);
int get() const;
private:
int _i;
}
An even better alternative is to make the implementation in a member function, and have the static be nothing more than a wrapper. The advantage is that this also works if you have a class hierarchy, and want a virtual member function pointer.
Code:
class my_class
{
public:
static void call_member_function_as_static(my_class& object)
{
object.member_function();
}
virtual void member_function();
}
I hope this helps.
Could you post your class, so we might have a better idea of what yo are trying to achieve?
Re: isolate a static method
This is the function signature which I cannot change, because the following StartServer method
is waiting for a reference to a function with exactly that signature. So I cannot add any additional paramater to AnswerTo Connection.
Code:
static int AnswerToConnection (void *cls, struct Connection *connection,
const char *url, const char *method, const char *version,
const char *upload_data, size_t *upload_data_size, void **ptr);
This is where I need the reference to my function;
Code:
mServer = mDaemonServer->StartServer(USE_SELECT_INTERNALLY, PORT, NULL, NULL,
&AnswerToConnection, (void *)ASK_PAGE,OPTION_END);
I also tryed to remove the static keyword from AnswerToConnection and get a reference to the method in this why, but i still does not work.
Code:
static AccessHandler pRqUpdateFn = &HTTPServer::AnswerToConnection;
mServer = mDaemonServer->StartServer(USE_SELECT_INTERNALLY, PORT, NULL, NULL,
pRqUpdateFn, (void *)ASK_PAGE,OPTION_END);
Re: isolate a static method
Quote:
Originally Posted by
munteanu24d
This is the function signature which I cannot change, because the following StartServer method
is waiting for a reference to a function with exactly that signature. So I cannot add any additional paramater to AnswerTo Connection.
Normally, a framework that uses function pointers like this allows for an extra parameter (often as void*) that can be used to pass the class instance in a generic way. When passing the function pointer, you cast the this pointer to a void* and in the callback function, you do the reverse cast.
Code:
class MyClass
{
static void Callback(void* extra_param) {
MyClass* pThis = reinterpret_cast<MyClass*>(extra_param);
pThis->MemberFunction();
}
void MemberFunction(); // does the real work
};
int main()
{
MyClass c;
// call function that takes function pointer
// pass pointer to class instance as extra parameter
Register(&MyClass::Callback, reinterpret_cast<void*>(&c));
}
Check the documentation of the framework you are using to see which parameter you can use to pass custom data.
Re: isolate a static method
Thank you very much for your answer. Yes, my function really has a void* extra argument, but I have to admit that I did not how to use it...
Re: isolate a static method
Quote:
Originally Posted by
munteanu24d
Thank you very much for your answer. Yes, my function really has a void* extra argument, but I have to admit that I did not how to use it...
You pass 'this' instead of one of the NULL arguments of StartServer (look at the prototype to find out which argument it is) and the StartServer will pass that pointer to the server which will return it (most probably) as first argument when calling AnswerToConnection.
If this is right so far you could have code like
Code:
MyClass * p = (MyClass*)cls;
at the beginning of the AnswerToConnection function and thus you have a pointer to the class where you called the StartServer.