Re: [sigc] Template mystery



>How it is that this little dilema is solved, it is the case that you can
>attach any old slot to any old signal right? How are the template parameters
>that signal has resolved? However it is done, it must be a pretty clever
>solution.

its pretty basic template programming, at least for the basic
solution.

a slot for a function that returns void and takes no args looks like:

    struct slot {
       slot (void (*f)()) : the_function (f) {}

       void (*the_function)();

       void operator() () { the_function(); }
    }

now add an argument and a return type:

    struct slot {
      slot (void (*f)(), int arg) : the_function (f), the_arg (arg) {}

      int (*the_function)(int arg);
      int the_arg;

      int operator() () { return the_function (the_arg); }
    }

now make it use an object plus an argument:

    struct slot {
      slot (SomeObject& obj, void (*SomeObject::f)(int arg), int arg)
          : object (obj), ptr_to_memfun (f), the_arg (arg) {}

      SomeObject& object;
      int (*SomeObject::ptr_to_memfun) (int arg);
      int the_arg;
      
      int operator() () { return (object.*)(ptr_to_memfun) (the_arg);}
    }

now generalize:

    template struct slot<class T, class A> {
      slot (T& obj, void (*T::f)(A arg), A arg)
          : object (obj), ptr_to_memfun (f), the_arg (arg) {}

      T& object;
      A (*T::ptr_to_memfun) (A arg);
      A the_arg;
      
      A operator() () { return (object.*)(ptr_to_memfun) (the_arg);}
    }

now include, say, 8 of these templates to handle 0-7 arguments, and
you're mostly done. well, done with sigc++ 0.7 :))

the key element is the presence of a virtual method, in this case
operator(), which invokes the function (or member function) that was
stored in the slot, along with any stored arguments.

--p



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]