StreamResourceProvider.cpp
Go to the documentation of this file.
2 #include "../../Defs.hpp"
3 
4 namespace iv
5 {
6 
8 {
9  if( !StreamResourceProvider::Markers )
10  return;
11 
12  delete StreamResourceProvider::Markers; // This should delete all members via unique_ptr.
13  StreamResourceProvider::Markers = nullptr;
14 }
15 
17  cm( inst, this, "StreamResource", ClientMarker::Status() ),
18  inst( inst ),
19  provider( provider ),
20  path( path )
21 {
22 }
23 
25 {
26  static iv::TableId DebugTable = TableId::create( "StreamResource" );
27 
28  auto row = view->Table( DebugTable ).Row( this );
29  row.Column( "path", this->path );
30 }
31 
33 {
34  return this->inst;
35 }
36 
37 void StreamResource::with_resource_stream( std::function< void( std::istream & ) > const & f ) const
38 {
39  this->provider->with_stream( this->path, f );
40 }
41 
43 {
44  return this->path;
45 }
46 
47 std::unordered_multimap< std::type_index, std::unique_ptr< StreamResourceProvider::Marker > > * StreamResourceProvider::Markers;
48 
50  ResourceProvider( inst, priority ),
51  cm( inst, this, "StreamResourceProvider" )
52 {
53  this->cm.inherits( this->ResourceProvider::cm );
54 }
55 
56 bool StreamResourceProvider::has_path( ResourcePath const & path, std::type_index type )
57 {
58  if( !Markers )
59  return false;
60 
61  auto const & [ begin, end ] = Markers->equal_range( type );
62  for( auto it = begin; it != end; ++it )
63  {
64  auto & marker = it->second;
65  if( marker->HasResource( this, path ) )
66  return true;
67  }
68 
69  return false;
70 }
71 
72 void StreamResourceProvider::each_resource( std::type_index type, std::function< void( ResourcePath const & ) > const & f )
73 {
74  if( !Markers )
75  return;
76 
77  auto const & [ begin, end ] = Markers->equal_range( type );
78  for( auto it = begin; it != end; ++it )
79  {
80  auto & marker = it->second;
81  marker->EachResource( this, f );
82  }
83 }
84 
85 void StreamResourceProvider::each_type( std::function< void( std::type_index type ) > const & f )
86 {
87  if( !Markers )
88  return;
89 
90  for( auto it = Markers->begin(); it != Markers->end(); ++it )
91  f( it->second->GetType() );
92 }
93 
94 std::pair< void *, Instance * > StreamResourceProvider::create_resource( ResourcePath const & path, std::type_index type, ResourcesRoot * parent )
95 {
96  if( !Markers )
97  return std::pair< void *, Instance * >( nullptr, nullptr );
98 
99  auto const & [ begin, end ] = Markers->equal_range( type );
100  for( auto it = begin; it != end; ++it )
101  {
102  auto & marker = it->second;
103  if( marker->HasResource( this, path ) )
104  return marker->MakeResource( this, parent, path );
105  }
106 
107  return std::pair< void *, Instance * >( nullptr, nullptr );
108 }
109 
110 }