-
function callbacks?
i'm not exactly sure what i'm looking for but here goes.
i've started making an editor for an engine that i started making, it supports meshes and textures so far, but i have no editor for it. i'm using simple windows controls that i researched on msdn. in the past i've made very unreusable code, and this time around i wasn't going to make the same mistake this time. i thought in addition to the 4 views in the editor, i'd make a 'internal plugin system' where you could make a new class called CTabbedPlugin or something like that. when you made one, it would have a few functions you had to write. a function called when it recieved the WM_CREATE message, and then a function to respond to when that tab gets switched to (haven't learned that yet), a function that fills the HWND of the tab area, a wndProc that responds to messages in that HWND, and finally a destroy message. so in the class i'd need to somehow call functions you pointed to the class
so for a texture browser for an example you'd go
Code:
CTabbedPlugin * TextureBrowser;
TextureBrowser = NULL;
TextureBrowser = new CTabbedPlugin(HWND parentWindowToPutTabIn, char * Name);
i've already figured that part out, i've got a class that does that. now what i need is a way to 'bind' functions to calls in the class. so if the WM_CREATE message came along, it'd go TextureBrowser->CreateControls();
then i'd need a function like this
Code:
void CTabbedPlugin::CreateControls(void)
{
callSpecifiedFunctionForCreate();
}
i think what i need are like the things in WNDCLASS, the WNDPROC lpfnWndProc. it lets you specify a function to use as the message processor. that way when you made a new CTabbedPlugin, you'd go through and set each function to use. you'd have to make and set a CreateControls(), a Proc(), and a Destroy() and probably a few others i haven't thought of yet. so if i made the TextureBrowser plugin, i'd need to do something like this:
Code:
CTabbedPlugin * TextureBrowser;
TextureBrowser = NULL;
TextureBrowser = new CTabbedPlugin(HWND parentWindowToPutTabIn, char * Name);
if(TextureBrowser == NULL)
{
//error
}
TextureBrowser->SetCreateControls(??? aCreateControls function);
TextureBrowser->SetProcess(??? a function for processing messages from the controls you created);
TextureBrowser->SetDestroyControls(??? a function that is called on quit, lets you unload any viewports/controls/etc. that you might ahve created in this window);
i realize that is really incoherent and stuipd sounding, and i probably have something wrong, but basically i just need a way to set a function that gets called from a class without having to make a unique class for each 'plugin' or browser.
another idea i had to solve this problem is make the same class but with each of the 3 functions virtual, so to make a plugin you'd inherit the CTabbedPlugin and overwrite the 3 virtual functions.
-
You could use template arguments to your class and function objects. WARNING: The code below is just a thrown together example from 12:30 am, it probably doesnt compile, and doesnt do anything useful. Just to give you an idea.
Something like this:
Code:
template <class CreateObject,
class DestroyObject>
class CTabbedObject {
CreateObject m_create;
DestroyObject m_destroy;
private:
void Set(CreateObject& c) {/* you code it */ }
void Set(DestroyObject& d) { /* you code it */ }
void CreateControls() { m_create(); }
void DestroyControls(){ m_destroy(); }
};
class MyCreate {
int data;
public:
MyCreate(int i) { data = i; }
void operator()(void){ /* whatever you want to do here */ }
};
class MyDestroy {
public:
void operator()(void){ /* whatever */ }
};
int main()
{
CTabbedObject<MyCreate,MyDestroy> tab;
// pass in any parameters needed by create via a
//constructor, store in private data section, use in
// operator()
tab.Set(MyCreate(35));
tab.Set(MyDestroy());
}
So hopefully you can see that all you have to rewrite the second time around is the MyCreate class and the MyDestroy class, which are essentially just wrappers for function calls.
-
i'm going to go read up on templates before i try to understand that, thanks :) west coast? its 12:50 for me too, heh.
-
thanks, with a little fiddling that is working now :)