DelayedLoadSystem.cpp
Go to the documentation of this file.
1 #include "DelayedLoadSystem.hpp"
2 #include "DelayedLoader.hpp"
3 #include "../Defs.hpp"
4 
5 namespace iv
6 {
7 
9  items_total( 0 ),
10  items_done( 0 ),
11  complexity_total( 0 ),
12  complexity_done( 0 )
13 {
14 }
15 
17  System( sc ),
18  _to_load(),
19  _loaders(),
20  _blockers(),
21  _loading( false ),
22  status()
23 {
24 }
25 
27 {
28  if( this->_blockers.size() )
29  return false;
30 
31  if( !this->_to_load.size() )
32  return false;
33 
34  if( !this->_loading )
35  {
36  this->_loading = true;
37  this->Notify_LoadStart();
38  }
39 
40  // extract from queue
41  auto it = this->_to_load.begin();
42  DelayedLoad * load = it->first;
43  int complexity_bytes = it->second;
44  this->_to_load.erase( it );
45 
46  // load
47  load->LoadNow();
48 
49  // notify
50  this->status.items_done += 1;
51  this->status.complexity_done += complexity_bytes;
52  this->Notify_ItemLoaded( load, complexity_bytes );
53 
54  // notify loading end
55  if( this->_to_load.empty() )
56  {
57  this->status = DelayedLoadStatus();
58  this->_loading = false;
59  this->Notify_LoadFinish();
60  }
61 
62  return true;
63 }
64 
65 void DelayedLoadSystem::QueueDelayedLoad( DelayedLoad * load, int complexity_bytes )
66 {
67  if( this->_loaders.empty() )
68  {
69  load->LoadNow();
70  return;
71  }
72 
73  this->_to_load[ load ] = complexity_bytes;
74  this->status.items_total += 1;
75  this->status.complexity_total += complexity_bytes;
76 
77  this->Notify_ItemQueued( load, complexity_bytes );
78 }
79 
81 {
82  this->_to_load.erase( load );
83 }
84 
86 {
87  this->_loaders.insert( loader );
88 }
89 
91 {
92  this->_loaders.erase( loader );
93  this->_blockers.erase( loader );
94 }
95 
96 void DelayedLoadSystem::BlockLoading( DelayedLoader * loader, bool block )
97 {
98  if( !this->_loaders.count( loader ) )
99  return;
100 
101  if( block )
102  this->_blockers.insert( loader );
103  else
104  this->_blockers.erase( loader );
105 }
106 
108 {
109  return this->_loading;
110 }
111 
112 void DelayedLoadSystem::Notify_LoadStart()
113 {
114  this->log( SRC_INFO, Defs::Log::DelayedLoad, "Load start." );
115  this->_loaders.foreach( [&]( DelayedLoader * const & loader )
116  {
117  loader->LoadStart();
118  });
119 }
120 
121 void DelayedLoadSystem::Notify_LoadFinish()
122 {
123  this->log( SRC_INFO, Defs::Log::DelayedLoad, "Load finish." );
124  this->_loaders.foreach( [&]( DelayedLoader * const & loader )
125  {
126  loader->LoadFinish();
127  });
128 }
129 
130 void DelayedLoadSystem::Notify_ItemQueued( DelayedLoad const * item, int complexity_bytes )
131 {
132  this->log( SRC_INFO, Defs::Log::DelayedLoad, "Load queued for ",item->cm,"." );
133  this->_loaders.foreach( [&]( DelayedLoader * const & loader )
134  {
135  loader->ItemQueued( item, complexity_bytes );
136  });
137 }
138 
139 void DelayedLoadSystem::Notify_ItemLoaded( DelayedLoad const * item, int complexity_bytes )
140 {
141  this->log( SRC_INFO, Defs::Log::DelayedLoad, "Load performed for ",item->cm,"." );
142  this->_loaders.foreach( [&]( DelayedLoader * const & loader )
143  {
144  loader->ItemLoaded( item, complexity_bytes );
145  });
146 }
147 
149 {
150  return this->status;
151 }
152 
153 }