I just wanted to see if someone has a good solution to this.
Initially, I have this function named Synchronize:
Which is a template function due to the fact that it can do a callback.Code:template<typename Class, typename Function> void Synchronize(const UINT64 dwTimeFrame, const UINT64 dwFramesPerTimeFrame, Class pClass = NULL, Function pFunction = NULL)
However, I'm trying to make it possible to do both a callback to a single function and a function within a class, so a class function callback to speak.
But as you've probably have guessed, the compiler won't allow that, since the syntax is incompatible for both. If it's a function only, the class callback code will bark and vice versa.
So now the question is what the best solution is for this?
The function itself can be a little problematic to split into different parts and a wrapper for the callback:
I already tried assembly, but it won't let me call other template functions via assembly. The other option I see is to do callback directly through asm, but that may be a little far fetched and error prone.Code:template<typename Class, typename Function> void Synchronize(const UINT64 dwTimeFrame, const UINT64 dwFramesPerTimeFrame, Class pClass = NULL, Function pFunction = NULL)
{
// Let's set it to update 50 frames/sec.
//const DWORD dwTimeFrame = 1000; // The time frame we're targeting (in ms)
//const DWORD dwFramesPerTimeFrame = 50; // Number of times per time frame cycle to do rendering
const UINT64 dwTickRate = dwTimeFrame / dwFramesPerTimeFrame; // Calculate amount of time to sleep each time
INT32 nSleepNeeded; // Stores the amount of time we need to sleep
UINT64 dwTimeElapsed; // Time elapsed since beginning of time frame
UINT64 dwNextSleepBoundary; // The next "target" time to sleep until.
DWORD dwTick; // Used to hold a snapshot of timeGetTime so we can calculate elapsed time
//DWORD Count = 1; // Simple counter just for cosmetics - just to print something
DWORD dwTick2 = timeGetTime(); // Snapshot of the beginning of the code sample
for(;;)
{
// Initialize variables
nSleepNeeded = 0;
dwTimeElapsed = 0;
dwNextSleepBoundary = dwTickRate; // Set the next sleep boundary to the next tick.
dwTick = timeGetTime(); // Get a snapshot of the current time
while (dwNextSleepBoundary <= dwTimeFrame)
{
// Do callback here
dwTimeElapsed = timeGetTime() - dwTick; // Calculate elapsed time
INT64 nTemp = dwNextSleepBoundary - dwTimeElapsed; // Calculate amount of time we need to sleep (at least)
ASSERT(nTemp <= 0xFFFFFFFF); // Must not exceed the precision of a UINT32
if (nTemp > 0) // Safesty check; we don't want to do Sleep(0) or sleep a negative value since Sleep takes a DWORD
{
nSleepNeeded = (UINT32)nTemp;
Sleep(nSleepNeeded); // Sleep for the amount of time necessary before next drawing
}
dwNextSleepBoundary += dwTickRate; // Increase sleep "boundary" to next time frame tick
//cout << Count++ << endl; // Print counter
}
//DWORD dwTick3 = timeGetTime() - dwTick2; // Calculate amount of time the entire loop took
//cout << "Took " << dwTick3 << " ms!\n"; // Print out the time it took
//__asm int 3; // Do a debug break
}
}
Your thoughts?