Are there temporary pointers?

In some cases, I wanted to have some kind of smart pointer based on time, for example, to cache some bulky object, but release it automatically after a certain time if it is not used. When the pointer touches (dereferenced), the countdown is restarted, and you can also stop the countdown if you need to “lock” the object alive for the duration of the calculation. Sort of:

timed_ptr<Type,30> p = new Type(); \\object is deleted after 30 seconds, and pointer set to a checkable 'null' state ... p.stop_count_down(); // do something with the object, guaranteed it won't expire while we still need it. p.start_count_down(); 

Is there any of this type in boost or in another library?

+6
source share
1 answer

I think you are losing the benefit of a smart pointer by synchronizing it. Just have some object responsible for managing these resources and requesting a resource from it when necessary. Otherwise, you will need to put checks if the resource is still loaded before using the pointer. This is the exact problem of generic pointers, where some other code might invalidate the resource / memory, and you should always check the pointer resource before using it.

0
source

All Articles