Loading [MathJax]/extensions/MathZoom.js
Cytopia  0.3
A city building simulation game
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ResourceManager Class Reference

#include <ResourceManager.hxx>

+ Inheritance diagram for ResourceManager:
+ Collaboration diagram for ResourceManager:

Classes

struct  AgeIterator
 
struct  resource_type
 
struct  ResourceItem
 

Public Member Functions

 ResourceManager ()
 Creates the ResourceManager. More...
 
template<typename ResourceID >
resource_type< ResourceID >::type get (const ResourceID &)
 Fetches and return a Soundtrack. More...
 
void prune ()
 Cleans up the cache of unused resources. More...
 

Private Types

using MAX_RESOURCE_BYTES = Constant< 150000000 >
 

Private Attributes

uint32_t m_Age
 
uint32_t m_CacheSize
 

Additional Inherited Members

- Static Public Member Functions inherited from Singleton< ResourceManager >
static ResourceManagerinstance (void)
 Get an instance of the singleton. More...
 
- Protected Member Functions inherited from Singleton< ResourceManager >
 Singleton () noexcept=default
 
 ~Singleton () noexcept=default
 

Detailed Description

Definition at line 16 of file ResourceManager.hxx.

Member Typedef Documentation

◆ MAX_RESOURCE_BYTES

using ResourceManager::MAX_RESOURCE_BYTES = Constant<150000000>
private

Definition at line 36 of file ResourceManager.hxx.

Constructor & Destructor Documentation

◆ ResourceManager()

ResourceManager::ResourceManager ( )

Creates the ResourceManager.

Exceptions
ConfigurationErrorwhen loading configuration results in an error

Definition at line 36 of file ResourceManager.cxx.

36  : m_Age(0), m_CacheSize(0)
37 {
38 #ifdef USE_AUDIO
39  std::string jsonFileContent = fs::readFileAsString(Settings::instance().audioConfigJSONFile.get());
40  json config_json = json::parse(jsonFileContent, nullptr, false);
41 
42  // check if json file can be parsed
43  if (config_json.is_discarded())
44  throw ConfigurationError(TRACE_INFO "Error parsing JSON File " + Settings::instance().audioConfigJSONFile.get());
45 
46  m_audioConfig = config_json;
47 #endif // USE_AUDIO
48 }
+ Here is the call graph for this function:

Member Function Documentation

◆ get()

template<typename ResourceID >
ResourceManager::resource_type< ResourceID >::type ResourceManager::get ( const ResourceID &  id)

Fetches and return a Soundtrack.

Parameters
ResourceIDthe id of the resource
Exceptions
AudioErrorwhen loading the file results in an error

Definition at line 14 of file ResourceManager.inl.hxx.

15 {
16  fetch(id);
17 #ifdef USE_AUDIO
18  if constexpr (std::is_same_v<ResourceID, SoundtrackID>)
19  return m_soundtracks.at(id).resource;
20 #endif // USE_AUDIO
21 }
+ Here is the caller graph for this function:

◆ prune()

void ResourceManager::prune ( )

Cleans up the cache of unused resources.

Definition at line 171 of file ResourceManager.cxx.

172 {
173 #ifdef USE_AUDIO
174  size_t total_size = 0;
175  total_size += m_soundtracks.size();
176  /* We evict a half of kept resources */
177  total_size /= 2;
178  std::vector<uint32_t> all_ages;
179  using AgeIt = AgeIterator<Mapping<SoundtrackID, SoundtrackResource>::iterator>;
180  all_ages.insert<AgeIt>(all_ages.cend(), AgeIterator{m_soundtracks.begin()}, AgeIterator{m_soundtracks.end()});
181  std::nth_element(all_ages.begin(), all_ages.begin() + total_size, all_ages.end());
182  uint32_t median = all_ages[total_size];
183  for (auto it = m_soundtracks.begin(); it != m_soundtracks.end();)
184  {
185  if (it->second.age < median and !it->second.resource->isPlaying)
186  {
187  int sizeBytes = 0;
188  alGetBufferi(it->second.resource->buffer, AL_SIZE, &sizeBytes);
189  m_CacheSize -= sizeof(DecodedAudioData) + sizeof(Soundtrack) + sizeof(SoundtrackUPtr) + sizeBytes;
190  it = m_soundtracks.erase(it);
191  }
192  else
193  {
194  it->second.age = 0;
195  it++;
196  }
197  }
198 #endif // USE_AUDIO
199  m_Age = 0;
200  LOG(LOG_INFO) << "After eviction, cache is " << (m_CacheSize / 1000000) << "MB";
201 }

Member Data Documentation

◆ m_Age

uint32_t ResourceManager::m_Age
private

Definition at line 32 of file ResourceManager.hxx.

◆ m_CacheSize

uint32_t ResourceManager::m_CacheSize
private

Definition at line 33 of file ResourceManager.hxx.


The documentation for this class was generated from the following files:
TRACE_INFO
#define TRACE_INFO
Definition: Exception.hxx:12
ConfigurationError
A configuration error.
Definition: Exception.hxx:36
LOG
Definition: LOG.hxx:32
LOG_INFO
@ LOG_INFO
Definition: LOG.hxx:25
SoundtrackUPtr
std::unique_ptr< Soundtrack > SoundtrackUPtr
Definition: Soundtrack.hxx:98
ResourceManager::m_Age
uint32_t m_Age
Definition: ResourceManager.hxx:32
ResourceManager::m_CacheSize
uint32_t m_CacheSize
Definition: ResourceManager.hxx:33
readFileAsString
std::string readFileAsString(const std::string &fileName, bool binaryMode)
Read contents from a file as string.
Definition: Filesystem.cxx:12
DecodedAudioData
Container for raw pcm data that read from .ogg sound file.
Definition: Soundtrack.hxx:25
Singleton< Settings >::instance
static Settings & instance(void)
Get an instance of the singleton.
Definition: Singleton.hxx:15
Soundtrack
Definition: Soundtrack.hxx:32
json
nlohmann::json json
Definition: Settings.hxx:12
string
std::string string
Definition: AudioConfig.hxx:14