Archived

This topic is now archived and is closed to further replies.

Toolmaker

Static member functions, how to?

Recommended Posts

Toolmaker    967
I am having a little problem I can''t seem to find the answer too, so hopefully someone else can. Lua requires that the functions I write in C and register with the script are static, in this form: static int LuaFunctionInC(lua_State *L); Since my project(server app) consists out of a set of classes I run into problems when I create these functions. I need a Send function in the script to send data to the client, since everything is scripted. However, I can''y access the sockets from within a static function. How do I solve this? Toolmaker
-Earth is 98% full. Please delete anybody you can.

Share this post


Link to post
Share on other sites
kuphryn    210
One solution is to design the send and receive function such that they are static or global. Past in all required data to process the socket.

Kuphryn

Share this post


Link to post
Share on other sites
Toolmaker    967
Won''t work, that would be mean I need to make variables global aswell. Bad design imho. I rather stick with the CServer class, which handles the connections and has functions to send.

For me, the only solution I see now is making the CServer a singleton, but there must be a better solution...

Toolmaker

Share this post


Link to post
Share on other sites
Crawl    136
You could use a static pointer to the current class ("this" pointer"),.....

  
class Bla
{
private:
int a, b, c;

static Bla* mThis;

static int LuaFunctionInC(lua_State *L);

public:

Bla();
};

Bla::Bla()
{
mThis = this;
}

int Bla::LuaFunctionInC(lua_State *L)
{
mThis->a = mThis->b;
}

I asked a similair question not to long ago...

Ik hoop dat het een beetje duidelijk is...

-Crawl

Share this post


Link to post
Share on other sites
sbennett    124
I''m not sure how applicable this is to Lua, but what I do with Windows callbacks is: declare your window proc as a static member function, and hold a (static) std::map<HWND, CWindow*>. Use this map to find the CWindow object associated with the HWND passed into the callback.

It looks like you may be able to adapt this method to find the object to call methods on.

Share this post


Link to post
Share on other sites
petewood    819
quote:
sbennett: I''m not sure how applicable this is to Lua, but what I do with Windows callbacks is: declare your window proc as a static member function, and hold a (static) std::map. Use this map to find the CWindow object associated with the HWND passed into the callback.

It looks like you may be able to adapt this method to find the object to call methods on.


you can associate whatever data you want with a window and retrieve it for a given HWND. You don''t need to keep a separate map (although that obviously also works). You can get the ''this'' pointer using the HWND passed to the static function.

Share this post


Link to post
Share on other sites
Toolmaker    967
Currently the server runs in console. I will convert it to Win32, but I use console for onscreen debugging purposes now. So I think I might go with the singleton idea and the idea from Crawl.

Toolmaker



-Earth is 98% full. Please delete anybody you can.

Share this post


Link to post
Share on other sites