ResourceManagementSystem.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "ResourceProvider.hpp"
4 #include "ResourcePath.hpp"
6 #include <map>
7 
8 namespace iv
9 {
10 
12 {
13 public:
16  cm( inst, this, "ResourcesRoot" ),
17  inst( inst )
18  {
19  }
20 
22  {
23  return this->inst;
24  }
25 
27 
28 private:
29  Instance * inst;
30 };
31 
32 
38 {
39 public:
42  virtual std::string debug_name() const override { return "ResourceManagementSystem"; }
43  virtual void status( TextDebugView * view ) override;
44 
45 //------- resource requests --------
46  template< class TResource >
47  bool has_path( ResourcePath path );
48 
51  template< class TResource >
52  TResource const * get( ResourcePath path );
53 
54  template< class TResource >
55  void each_resource( std::function< void( ResourcePath const & path ) > const & );
56 
57 //------ provider registration ------
64  void register_provider( ResourceProvider * provider, size_t priority );
65  void unregister_provider( ResourceProvider * provider );
66 
67 private:
68  ResourceProvider * choose_provider( ResourcePath const & path, std::type_index type );
69 
70 private:
71  I< ResourcesRoot > root;
72 
73 // provider data
74  using PriorityCmp = std::greater< size_t >;
75  std::multimap< size_t /*priority*/, ResourceProvider *, PriorityCmp > providers;
76 
77 // resource data
78  struct Resource
79  {
80  Resource() : type( typeid( void ) ), client( nullptr ), inst( nullptr ){}
81 
82  std::type_index type;
83  void * client;
84  Instance * inst;
85  };
86 
87  std::unordered_multimap< ResourcePath, Resource > resources;
88 };
89 
90 }
91