HoverRace  2.0
Loader.h
Go to the documentation of this file.
1 
2 // Loader.h
3 //
4 // Copyright (c) 2014, 2015 Michael Imamura.
5 //
6 // Licensed under GrokkSoft HoverRace SourceCode License v1.0(the "License");
7 // you may not use this file except in compliance with the License.
8 //
9 // A copy of the license should have been attached to the package from which
10 // you have taken this file. If you can not find the license you can not use
11 // this file.
12 //
13 //
14 // The author makes no representations about the suitability of
15 // this software for any purpose. It is provided "as is" "AS IS",
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
17 // implied.
18 //
19 // See the License for the specific language governing permissions
20 // and limitations under the License.
21 
22 #pragma once
23 
24 #include <queue>
25 
26 #include "Log.h"
27 
28 #if defined(_WIN32) && defined(HR_ENGINE_SHARED)
29 # ifdef MR_ENGINE
30 # define MR_DllDeclare __declspec( dllexport )
31 # else
32 # define MR_DllDeclare __declspec( dllimport )
33 # endif
34 #else
35 # define MR_DllDeclare
36 #endif
37 
38 namespace HoverRace {
39 namespace Util {
40 
45 class Loader
46 {
47 public:
48  Loader() { }
49  ~Loader() { }
50 
51 public:
52  bool IsEmpty() const { return loaders.empty(); }
53 
54 public:
55  using finishedLoadingSignal_t = boost::signals2::signal<void()>;
56 
63  {
64  return finishedLoadingSignal;
65  }
66 
68  {
70  }
71 
72 private:
73  using loader_t = std::pair<std::string, std::function<void()>>;
74 public:
81  template<class Fn>
82  void AddLoader(const std::string &s, Fn fn)
83  {
84  loaders.emplace(s, fn);
85  }
86 
91  template<class Fn>
92  void AddLoader(Fn fn)
93  {
94  std::ostringstream oss;
95  oss << "Loader " << loaders.size();
96  loaders.emplace(oss.str(), fn);
97  }
98 
99 public:
111  bool LoadNext()
112  {
113  if (loaders.empty()) {
114  return false;
115  }
116 
117  auto &loader = loaders.front();
118  HR_LOG(info) << "Loading: " << loader.first;
119  loader.second();
120  loaders.pop();
121 
122  return !loaders.empty();
123  }
124 
125 private:
126  std::queue<loader_t> loaders;
128 };
129 
130 } // namespace Util
131 } // namespace HoverRace
132 
133 #undef MR_DllDeclare
void AddLoader(Fn fn)
Add a new unnamed loader.
Definition: Loader.h:92
std::queue< loader_t > loaders
Definition: Loader.h:126
SDL_RendererInfo info
Definition: SdlDisplay.cpp:253
void AddLoader(const std::string &s, Fn fn)
Add a new named loader.
Definition: Loader.h:82
bool IsEmpty() const
Definition: Loader.h:52
void FireFinishedLoadingSignal()
Definition: Loader.h:67
Queue of resource loaders.
Definition: Loader.h:45
~Loader()
Definition: Loader.h:49
Loader()
Definition: Loader.h:48
#define HR_LOG(lvl)
Alias for BOOST_LOG_TRIVIAL.
Definition: Log.h:45
bool LoadNext()
Load the next item.
Definition: Loader.h:111
finishedLoadingSignal_t finishedLoadingSignal
Definition: Loader.h:127
std::pair< std::string, std::function< void()>> loader_t
Definition: Loader.h:73
finishedLoadingSignal_t & GetFinishedLoadingSignal()
Fired when all resources have been loaded and the loading scene is shutting down. ...
Definition: Loader.h:62
Definition: Announcement.h:24
boost::signals2::signal< void()> finishedLoadingSignal_t
Definition: Loader.h:55