From 91bf40c116d4dbc7bb21d97c7c97415a256be841 Mon Sep 17 00:00:00 2001 From: jpbl <jpbl> Date: Thu, 14 Jul 2005 14:44:01 +0000 Subject: [PATCH] added missing files --- utilspp/singleton/lifetime_with_longevity.inl | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 utilspp/singleton/lifetime_with_longevity.inl diff --git a/utilspp/singleton/lifetime_with_longevity.inl b/utilspp/singleton/lifetime_with_longevity.inl new file mode 100644 index 0000000000..c09c97acee --- /dev/null +++ b/utilspp/singleton/lifetime_with_longevity.inl @@ -0,0 +1,68 @@ +#include <algorithm> +#include <stdexcept> + +template< typename T, typename T_destroyer > +void +utilspp::set_longevity( T *obj, unsigned int longevity, T_destroyer d ) +{ + using namespace utilspp::private_members; + + tracker_array new_array = static_cast< tracker_array >( + std::realloc( + m_tracker_array, + m_nb_elements + 1 + ) + ); + if( new_array == NULL ) + { + throw std::bad_alloc(); + } + + lifetime_tracker *p = new concrete_lifetime_tracker< T, T_destroyer >( + obj, + longevity, + d + ); + + m_tracker_array = new_array; + + tracker_array pos = std::upper_bound( + m_tracker_array, + m_tracker_array + m_nb_elements, + p, + &lifetime_tracker::compare + ); + std::copy_backward( + pos, + m_tracker_array + m_nb_elements, + m_tracker_array + m_nb_elements + 1 + ); + + *pos = p; + m_nb_elements++; + std::atexit( &at_exit_func ); +}; + +template< typename T > +void +utilspp::lifetime_with_longevity< T >::schedule_destruction( T *obj, void (*func)() ) +{ + utilspp::private_members::adapter<T> adapter = { func }; + utilspp::set_longevity( obj, get_longevity( obj ), adapter ); +} + +template< typename T > +void +utilspp::lifetime_with_longevity< T >::on_dead_reference() +{ + throw std::logic_error("Dead reference detected"); +} + +template< typename T > +unsigned int +utilspp::get_longevity( T * ) +{ + return 1000; +} + + -- GitLab