On Tue, Sep 16, 2003 at 12:32:36PM +0200, Lars Gullik Bjønnes wrote:
> Andre Poenitz <[EMAIL PROTECTED]> writes:
> 
> | On Tue, Sep 16, 2003 at 12:20:58PM +0200, Alfredo Braunstein wrote:
> >> > I think you could try to convert the math dispatch to your scheme
> >> > without affecting the rest of LyX. This would give some reference
> >> > implementation of the scheme without duplicated work...
> >> 
> >> I was thinking... how about implementing factory.C in a similar way also?
> >> 
> >> Every inset could register a constructor standalone function that returns a
> >> base inset given some data.
> >> 
> >> Does it make sense?
> >
> | Yes. [And works fine, I do this in my day time job all over the place].
> >
> | The only tricky part is to get the 'seemingly unused' static dummy variables
> | whose constructors are used for the registration on startup  from a
> | library in a binary ;-}
> 
> IMHO that is the wrong aproach then. No static variables should be
> used for this.
> 
> Rather pretend that your are dynamically loading a lib, and use a
> function in that lib to give you the object you need for further work
> (or send registration info to that function and let it handle it.)
> 
> (Or I do not really understand what you are talking about.)

I am talking about

        handler.h:

                void register_func(string cmd, void (*f)(Args));
                void dispatch(string cmd, Args args);


        handler.C:

                map<string, void (*f)(Args)> theDispatcher;

                void register_func(string cmd, void (*f)(Args))
                {
                        theDispatcher[cmd] = f;
                }

                void dispatch(string cmd, Args args)
                {
                        theDispatcher[cmd](args);
                }



        foo.C:

                #include "handler.h"

                namespace {

                        void handler1(Args args) {...}
                        void handler2(Args args) {...}
                        void handler3(Args args) {...}

                        struct init {
                                init() {
                                        register_func("foo cmd1", handler1);
                                        register_func("foo cmd2", handler2);
                                        register_func("foo cmd3", handler3);
                                }
                        };
                        
                        // The constructor of this dummy var registers the handlers in
                        // the dispatcher on program startup, i.e. before main() is
                        // entered and without any visible dependence of 'main' from
                        // foo.C.
                        // further 'dynamic' registering i.e. by parsing some setup 
file
                        // or some user command is possible by using dynamic libraries.
                        // but that's not strictly required.

                        static const init dummy;
        
                }

Andre'

-- 
Those who desire to give up Freedom in order to gain Security, will not have,
nor do they deserve, either one.     (T. Jefferson or B. Franklin or both...)

Reply via email to