Storing data as void* and dereferencing them later

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
typedef int 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
}


Source:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#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.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
struct DelayObject
{
    float timeLeft;

    virtual void timeExpired() = 0;
};

template <typename T>
struct TDelayObject : public DelayObject
{
    T* address;
    T* target;

    void TDelayObject( T* lpLabelAddress, float delay, T target_ )
    {
        address = lpLabelAddress;
        target = new T(target_);
        timeLeft = delay;
    }

    virtual void timeExpired()
    {
        address = target;
        delete target;
    }
};

std::vector<DelayObject*> g_list; // global object

void SetAfterDelay( T* lpLabelAddress, float delay, T target )
{
    g_list.push_back(new TDelayObject<T>(pLabelAddress, delay, target));
}

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 )
        {
            (*it)->timeExpired();
            g_list.erase(it--);
        }
    }
}

Of course you probably need virtual destructors for DelayObject/TDelayObject, and copy constructor/assignment operator for the latter.
Of course! I knew we couldn't template the class because that would make the size of it variable, but making it a pointer is perfect!
Topic archived. No new replies allowed.