ResourceManagementSystem.inl
Go to the documentation of this file.
1 namespace iv
2 {
3 
4 template< class TResource >
6 {
7  ResourcePath res_path( path );
8  return this->choose_provider( res_path, typeid( TResource ) );
9 }
10 
11 template< class TResource >
12 TResource const * ResourceManagementSystem::get( ResourcePath res_path )
13 {
14  //----------- try to find existing instance --------------------
15  {
16  auto equal_range = this->resources.equal_range( res_path );
17  for( auto it = equal_range.first; it != equal_range.second; ++it )
18  {
19  Resource & res = it->second;
20  if( res.type == typeid( TResource ) )
21  return reinterpret_cast< TResource * >( res.client );
22  }
23  }
24 
25  //---------------- create the instance -------------------------
26  {
27  // select provider
28  ResourceProvider * provider = this->choose_provider( res_path, typeid( TResource ) );
29  if( !provider )
30  {
31  this->warning( SRC_INFO, "Path '", res_path, "' can not be resolved. No registered provider provides this path for type '", typeid( TResource ).name(), "'." );
32  return nullptr;
33  }
34 
35  // create client
36  std::pair< void *, Instance * > created = provider->create_resource( res_path, typeid( TResource ), &this->root );
37  if( !created.first )
38  {
39  this->warning( SRC_INFO, "Resource client construction failed." );
40  return nullptr;
41  }
42 
43  // add to resource set
44  Resource res;
45  res.type = typeid( TResource );
46  res.client = created.first;
47  res.inst = created.second;
48  this->resources.insert( std::make_pair( res_path, res ) );
49 
50  // return
51  TResource * client = reinterpret_cast< TResource * >( created.first );
52  return client;
53  }
54 }
55 
56 template< class TResource >
57 void ResourceManagementSystem::each_resource( std::function< void( ResourcePath const & path ) > const & f )
58 {
59  for( auto & p_provider : this->providers )
60  p_provider.second->each_resource( typeid( TResource ), f );
61 }
62 
63 }