[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [CPROG] ANSI C++ og events



On Mon, 2 Jul 2001, Peter Aagaard Kristensen wrote:

>> Jeg sidder og bikser med et C++ program som skal bruge 
events(callbacks).
>> Ideen er at jeg har en klasse som har en public variabel som brugeren 
af
>> klassen kan sætte til at pege på en funktion, som bliver kaldt af 
klassen
>> når noget bestemt sker.

>Jeg har gjort noget lidt simplere (tror jeg); klasser der skal kunne
>kaldes (altså modtage en event) nedarver alle fra en baseklasse, der har
>en public "modtag event" funktion, som er parametriseret med en event.
>De klasser der skal kalde en anden klasse, sættes så til at pege på den
>klasse de skal kalde, og kalder den veldefinerede metode i klassen.
>Mads

Mit design som er nemt at genbruge har den svaghed at callback events kun 
bør kaldes fra *End_user_class*-constructors/destructor pga virtuelle 
funktioner ser nogenlunde sådan ud (ideen er en videreudvikling af en 
windowklasse fra djgpp):

extern "C++" {
  class Base_class {
  public:
    static int pubcallback(HANDLE handle, EVENT event, ...) {
      return this->callback(event, ...);
    }
  protected:
    HANDLE Handle;
  private:
    virtual int callback(EVENT event, ...)= 0;
  };

  class Intermediate_base_class : public Base_class {
  protected:
    /*virtual*/ int callback(EVENT event, ...) {
      switch (event) {
      case MOUSE_EVENT:
        return this->mouse_event(...);
      case SOUME_EVENT: // pronounced SOME_EVENT
        return this->soume_event(...);
      default:
        return C_CALLBACK_PROCEDURE(Handle, event, ...); // call system 
default
      }
    }
    virtual int mouse_event(...) { /* do_my_default */ }
    virtual int soume_event(...) { /* do_my_default */ }
  };

  class User_class : public Intermediate_base_class {
  protected:
    /*virtual*/ int callback(EVENT event, ...) {
      switch (event) {
      case SUOME_EVENT: // pronounced ZOOM_EVENT
        return this->suome_event(...);
      default:
        return Intermediate_base_class::callback(event, ...);
      }
    }
    /*virtual*/ int mouse_event(...) { // override
      // do_my_specialization - may return if do_my_default is unwanted;
      Intermediate_base_class::mouse_event(...);
    }
    virtual int suome_event(...) {
      // ad_lib
    }
  };
}

Mvh
Nils Åke Ljunggren


 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 20:08 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *