Thread: function callbacks?

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    21

    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.

  2. #2
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    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.
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    21
    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.

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    21
    thanks, with a little fiddling that is working now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  5. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM