Re: [gtkmm] General C++ Design Question: relation of GUI toapplication



>>You've made it one or more steps more complex than necessary
>
>  Your example made that clear.    My grasp of the general idea:  Suppose we n
>eed
>a static function from class W which refers to functions of classes X1,X2,..
>through pointers that W keeps to those classes.  Then to create the static
>function within W that uses member functions of the Xi, we write such a functi
>on
>that also takes those pointers as arguments.  Within a scope where all the
>pointers have been assigned, we indeed have a static function.

you're thinking much too hard :)

the general prototype of a generic C callback is:
    
    rettype callback (void *argument);
    
its passed into a registration function like gtk_timeout_add using a
pointer-to-function, not a pointer-to-member-function. This is where
the problem arises when you actually want to call a member function -
you can't do it directly since ptr-to-fct is not interchangeable with
ptr-to-memfct.

in a simple world, you would like to do:

   register_callback (some_object_instance, &SomeObject::some_method);

but C (GTK+'s language won't let you do that). So instead, you need to
do:

   register_callback (SomeObject::some_static_method, &some_object_instance);
                       ^^^^^^^^                       ^^^^^^^^^^
		       ptr-to-fct		      generic void* ptr

then have SomeObject::some_static_method() call the "real"
SomeObject::some_method on some_object_instance, first casting it to 
SomeObject *.

But more importantly, you're missing several basic things here. First:

    Gtk::TimeoutSig.connect (slot (some_object_instance,
			            &SomeObject::some_method), 1000);

Secondly, I think you're getting too caught up in a deep analysis of
MVC without actually writing the code.

Yes, of course there is, somewhere, a reference (in the non-technical
sense) to the instance of the class that represents the model. There
has to be. the issue of the C requirement for static member functions is
solved either by using gtkmm wrappers, as above, or by using static
member functions that invoke non-static member functions.

It really doesn't have anything to do with what pointers the classes
hold, how the Model is accessed etc.

>a major area of my ignorance.  On one hand I read that gtkmm makes "type safe"
>callbacks possible.  I ask myself:  How could this be done?

see the TimeoutSig example above.

>to do.  But I really don't understand exactly what kind of animal slot(..) is 
>and
>how it can really be "type safe"?    The same for gtk_timeout_add(..).

gtk_timeout_add is not typesafe. Gtk::TimeoutSig is.

a slot is a template type. the best way i have to explain how it works
is like this, a highly simplified example of what it does:

   template<class T>
   class Slot {
      public:
         Slot (T& t, void (T::*ptmf)()) 
	   : obj (t), ptr_to_member_function (ptmf) {}

         void operator()() {
	      // invoke the member function on the object
	      (obj.*)(ptr_to_member_function)();
         }	      
      
      private:
         T& obj;
	 void (T::*ptr_to_member_function)();
   };

With this object, you just do this:

     Foo       aFoo;
     Slot<Foo> aSlot (aFoo, &Foo::some_method);

     aSlot();  // calls aFoo.some_method() via operator()

Its just a way of using templates to remove the cruft of writing
callback structs/classes for every single possible type and argument
combination and return type.

--p
      



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