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 47 48 49 50 51 52 53 54
| template< typename T, typename TDestroyer >
void
utilspp::setLongevity( T *obj, unsigned int longevity, TDestroyer d )
{
using namespace utilspp::PrivateMembers;
TrackerArray newArray = static_cast< TrackerArray >(
std::realloc(mTrackerArray, mNbElements + 1));
if( newArray == NULL )
{
throw std::bad_alloc();
}
LifetimeTracker *p =
new ConcreteLifetimeTracker< T, TDestroyer >(obj, longevity, d);
mTrackerArray = newArray;
TrackerArray pos = std::upper_bound(
mTrackerArray,
mTrackerArray + mNbElements,
p,
&LifetimeTracker::compare);
std::copy_backward(
pos,
mTrackerArray + mNbElements,
mTrackerArray + mNbElements + 1);
*pos = p;
mNbElements++;
std::atexit( &atExitFunc );
};
template< typename T >
void
utilspp::LifetimeWithLongevity< T >::scheduleDestruction( T *obj, void (*func)() )
{
utilspp::PrivateMembers::adapter<T> adapter = { func };
utilspp::setLongevity( obj, getLongevity( obj ), adapter );
}
template< typename T >
void
utilspp::LifetimeWithLongevity< T >::onDeadReference()
{
throw std::logic_error("Dead reference detected");
}
template< typename T >
unsigned int
utilspp::getLongevity( T * )
{
return 1000;
} |
Partager