Thread: How to pass non-static method to signal() function?

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    41

    How to pass non-static method to signal() function?

    Hi all,
    I'm trying to call a non-static method when SIGALRM is received. Since it's non-static I can't just call the method, I need to qualify it. However then it's not a void* it's a void (class*).

    This is my class, the method in question, and the only variable concerned:

    Code:
    class RemoraServer : public CommonApplication
    {
     ....................
       public:
    .......................
          void CatchAlarm(int signo)
          {
             if (signo == SIGALRM)
             {
                timeToSendEmail = TRUE;
             } //end if
          } //end CatchAlarm(...)
    ..............
       private:
          
          Boolean   timeToSendEmail;  //should we send our QA email yet?
          
    }; // end class RemoraServer
    
    
    
    RemoraServer::RemoraServer()
       : CommonApplication("/nerr/remora/.RemoraServer.debug", "SERVER"),
         dataListenSocket(-1),
         dataServerSocket(-1),
         defaultDataPort(11428),
         myProtocol("tcp"),
         numberOfHosts(0),
         repositoryURI("/pages/ProcessRemoteData.php"),
         repositoryURL("www.nerrenvirons.org"),
         repositoryPort(80),
         myConfigFile("/nerr/remora/host.config"),
         CDMODataString(""),
         CDMOCmdsCollectedSoFar(0),
         CDMOMaxCmdsPerDataString(50), //start out small for experimentation
         amITheCDMO(FALSE),
         CDMOSocket(-1),
         numberOfFailedAttempts(0),
         numberOfChildren(0),
         timeToSendEmail(FALSE)
    {
       //setup our signal handlers
       
    ......................
       
       if (signal(SIGALRM, &RemoraServer::CatchAlarm) == SIG_ERR)
       {
          //this is us telling ourselves it's time to send an email
          ;
       } //end if ALARM
    
    } //RemoraServer::RemoraServer()
    and this is the error I'm getting:

    cannot convert 'void (RemoraServer::*)(int) to 'void (*)(int)' for argument '2' to 'void (* signal(int, void (*)(int)))(int)


    I don't want to make the method static, because each object can send emails at different times.

    Any ideas?

  2. #2
    Registered User
    Join Date
    Jun 2003
    Posts
    41
    Okay, rather than make it static what I tried to do is make a global pointer to my object, then a global function that could be used as the wrapper to the callback. However that's not working either and I don't know why.

    This is what I've got now:

    Code:
    main.cc:
    
    RemoraServer *ServerList;
    void WrapperSignalHandler()
    {
       ServerList->CatchAlarm();
    }
    
    int main(int argc, char *argv[], char *envp[])
    {
       MakeMyselfaDaemon();
    
       RemoraServer    *server = NULL;
    
       ServerList = server;
       
       //we could put a loop here, so if Run ever finishes we just create a new one.......
       server = new RemoraServer();
    
       server->Init();
    
       server->Run();
    
       delete server;
    
       return (0);
    
    } //end main(...)
    
    
    
    RemoraServer.h:
    ...........
    extern void WrapperSignalHandler();
    ...........
    
    
    RemoraServer.cc:
    
    RemoraServer::RemoraServer()
       : CommonApplication("/nerr/remora/.RemoraServer.debug", "SERVER"),
         ....................
    {
       //setup our signal handlers
       ..............................
       //CatchAlarm is not and must not be static!
       if (signal(SIGALRM, WrapperSignalHandler) == SIG_ERR)
       {
          //this is us telling ourselves it's time to send an email
          ;
       } //end if ALARM
    
    } //RemoraServer::RemoraServer()

    and the complaint I get is when I call signal() in the constructor:

    invalid conversion from 'void (*)()' to void(*)(int)'

    ??

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    WrapperSignalHandler needs to have an int parameter.

    gg

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    41
    Oops....thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with Free.
    By chakra in forum C Programming
    Replies: 9
    Last Post: 12-15-2008, 11:20 AM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. [GLUT] Pointers to class methods
    By cboard_member in forum C++ Programming
    Replies: 13
    Last Post: 02-16-2006, 04:03 PM
  5. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 07:48 PM