I have a set of functions at work which are incredibly useful, however it only supports labels that come from a specific database because that database contains information on the type. I'd like to re-create it to make it more applicable to any member/static/global variables but don't know how to store the type.
I've already re-written it to an extent, but it only accepts int types. I'd like to template it to accept any type. The trick is storing that type so that the pointer can be dereferenced at a later time which I don't know how to do.
Interface:
1 2 3 4 5 6 7
typedefint T; // The goal is to remove this line!
namespace TimerDelay
{
void SetAfterDelay ( T* lpLabelAddress, float delay, T target = T(1)); // Queues the set
void ManageDelays ( float dt ); // sets the labels when appropriate
}
#include <vector>
namespace TimerDelay{
struct DelayObject
{
void* address; // I will probably need to add a container
void* target; // to hold the type, but how can this be done?
float timeLeft;
// typename type; // Would LOVE if this would work, but no dice
};
std::vector<DelayObject> g_list; // global object
void SetAfterDelay( T* lpLabelAddress, float delay, T target )
{
DelayObject temp;
temp.address = (void*)lpLabelAddress;
temp.target = (void*)(new T(target));
temp.timeLeft = delay;
// temp.type = T;
g_list.push_back(temp);
}
void ManageDelays(float dt)
{
for ( std::vector<DelayObject>::iterator it = g_list.begin(); it != g_list.end(); ++it )
{
it->timeLeft -= dt;
if ( it->timeLeft <= 0.0f )
{
*( (T*)it->address ) = *( (T*)it->target );
// *( (it->type*) it->address ) = *( (it->type*) it->target );
delete it->target;
g_list.erase(it--);
}
}
}
} // namespace TimerDelay
Edit:
Is it possible to store a std::iterator_traits<> struct as a member of my structure? The g_list isn't templated as it needs to accept all types at the same time. That means that DelayObject cannot be templated. I think that means that I cannot use a templated member class as the size may be inconsistant. I think it's close to what I'm looking for.