StreamResourceSubprovider.cpp
Go to the documentation of this file.
2 
4 
5 namespace iv
6 {
7 
8 //=========== StreamResourceSubprovider ==============================
10  cm( inst, this, "StreamResourceSubprovider" ),
11  lex( inst )
12 {
13  this->cm.owns( this->lex.cm );
14 }
15 
17 {
18  return this->lex.instance();
19 }
20 
21 void StreamResourceSubprovider::Parse_EachResource( std::istream & metadata_file )
22 {
23  this->lex.Init( metadata_file );
24 
25  Lex_LogTrace _trace( this->lex.lex() );
26 
28 
29  // filenames
30  while( !this->lex.Failed() )
31  {
32  if( !this->lex.IsNext( JsonLex::TableKey ) )
33  break;
34 
35  std::string filename = this->lex.AcceptTableKey();
36 
37  // parameters
38  this->lex.Accept( JsonLex::TableBegin );
39 
40  // class
41  std::string cl_key = this->lex.AcceptTableKey();
42  std::string cl_val = this->lex.AcceptString();
43 
44  if( cl_key != "class" )
45  this->lex.LogicFail( "Parameter 'class' must be the first specified parameter." );
46 
47  this->Resource( cl_val, ResourcePath( filename ) );
48 
50  }
51 
52  this->lex.Accept( JsonLex::TableEnd );
53 }
54 
55 void StreamResourceSubprovider::Parse_EachParameter( std::function< void( std::string const & param, std::string const & val ) > const & f )
56 {
57  while( !this->lex.Failed() )
58  {
59  if( !this->lex.IsNext( JsonLex::TableKey ) )
60  break;
61 
62  std::string param = this->lex.AcceptTableKey();
63  std::string value = this->lex.AcceptString();
64 
65  if( f && !this->lex.Failed() )
66  f( param, value );
67  }
68 }
69 
70 //========================== Plain_StreamResourceSubprovider ===================================
73  cm( inst, this, "Plain_StreamResourceSubprovider" ),
74  metadata_class( metadata_class )
75 {
77 
78  provider->with_metadata_stream(
79  [&]( std::istream & metadata_file )
80  {
81  this->Parse_EachResource( metadata_file );
82  }
83  );
84  this->metadata_class = nullptr;
85 }
86 
87 void Plain_StreamResourceSubprovider::Resource( std::string const & resource_class, ResourcePath path )
88 {
89  if( resource_class == this->metadata_class )
90  this->resources.insert( path );
91 
92  this->Parse_EachParameter();
93 }
94 
95 void Plain_StreamResourceSubprovider::each_resource( std::function< void( ResourcePath const & ) > const & f ) const
96 {
97  for( auto resource : this->resources )
98  f( resource );
99 }
100 
102 {
103  return this->resources.count( path );
104 }
105 
106 }