Timer method inside a class
Question: First of all an introduction to my development environment:

    
    
    OS: Windows.
    SDK: Microsoft Visual Studio 2008.
    

Earlier today I was facing the problem of trying to define a Timer inside a
class. My class is interfacing a Python embedded module and a C++ backend, My
problem is that I need to receive some time event on the python module. Also
it is important to notice that there will be only one instance of this class.

The main problem is that when I define a timer using:

    
    
    /* Null, 0, mseconds, CALLBACK_METHOD */
    SetTimer(NULL, 0, 100, (TIMERPROC) OnTimer);
    

The method activated on the timer event (OnTimer) needs to be a static method
on my Class (and then I cannot access any non-static methods or variables
inside that class).

Reading some code on codeproject I have found:
<http://www.codeproject.com/Articles/4817/How-to-use-SetTimer-with-callback-
to-a-non-static>

I have a similar implementation but without the lines:

    
    
    void * CSleeperThread::pObject;
    

and

    
    
    CSleeperThread *pSomeClass = (CSleeperThread*)pObject; // cast the void pointer
    pSomeClass->TimerProc(hwnd, uMsg, idEvent, dwTime); // call non-static function
    

Is this the only way to implement the functionality I'm looking for? Is there
an easier way I may have skipped on my information gathering process?


Answer: Create a static map of your Class object:

    
    
    static std::map<UINT_PTR, CMyClass*> m_CMyClassMap; //declaration
    

At the time of object creation insert the object in this map:

    
    
    CMyClass myClassObj;
    CMyClassMap.insert(std::pair<int, CMyClass*>(0, &myClassObj));
    

Now you can use it in static methods to access its non static members.

    
    
    int a = m_CMyClassMap[0]->m_someNonStaticMember;
    

