Heap.inl
Go to the documentation of this file.
1 namespace iv
2 {
3 
4 template< class T >
5 void Heap::DeleterImpl( void * ptr )
6 {
7  delete reinterpret_cast< T * >( ptr );
8 }
9 
10 template< class T, class ... CArgs >
11 T * Heap::create( CArgs const & ... cargs )
12 {
13  T * t = new T( cargs ... );
14  this->items.push_back( Item( Heap::identity_cast( t ), &DeleterImpl< T > ) );
15  return t;
16 }
17 
18 template< class T, std::enable_if_t< std::is_polymorphic_v< T >, int > >
19 void * Heap::identity_cast( T * ptr )
20 {
21  return dynamic_cast< void * >( ptr );
22 }
23 
24 template< class T, std::enable_if_t< !std::is_polymorphic_v< T >, int > >
25 void * Heap::identity_cast( T * ptr )
26 {
27  return static_cast< void * >( ptr );
28 }
29 
30 template< class T, class ... CArgs >
31 T * Heap::createInstance( std::string const & name, CArgs const & ... cargs )
32 {
33  return this->create< I< T > >( name, this->inst, cargs ... );
34 }
35 
36 template< class T, class ... CArgs >
37 T * Heap::createClient( CArgs const & ... cargs )
38 {
39  auto client = this->create< T >( this->inst, cargs ... );
40  this->cm->owns( client->cm );
41  return client;
42 }
43 
44 template< class T >
45 void Heap::destroy( T * ptr )
46 {
47  void * identity = Heap::identity_cast( ptr );
48 
49  auto it = std::find_if( this->items.begin(), this->items.end(), [ identity ]( Item const & item ){ return item.identity == identity; } );
50 
51  if( it == this->items.end() )
52  {
53  auto error = "Given object can not be destroyed by this Heap because it was not created by it.";
54  if( this->cm )
55  this->cm->warning( SRC_INFO, error );
56  else
57  runtime_warning( SRC_INFO, error );
58 
59  return;
60  }
61 
62  Item item = *it;
63  this->items.erase( it );
64  item.deleter( item.identity );
65 }
66 
67 }