GA::kit v0.3
G&A's in-house C++ application framework
Loading...
Searching...
No Matches
resource.h
Go to the documentation of this file.
1#pragma once
2#include "ga/defines.h"
3#include <iostream>
4#include <map>
5#include <memory>
6#include <vector>
7#include <unordered_map>
8
9namespace ga {
10
11template <class ResourceT, class... Args>
12bool loadResource( ResourceT& r, Args... args )
13{
14 return load( r, args... );
15}
16
28template <class ResourceT>
30{
31public:
32 // retrieve a Resource from the Cache by name, return nullptr if not found
33 std::shared_ptr<ResourceT> get( const std::string& name )
34 {
35 auto it = cache.find( name );
36 return it != cache.end() ? it->second : nullptr;
37 }
38
39 // load a Resource into the Cache from disk, return nullptr on failure
40 template <class... Args>
41 std::shared_ptr<ResourceT> load( const std::string& name, Args... args )
42 {
43 auto rPtr = std::make_shared<ResourceT>();
44 if ( ga::loadResource( *rPtr.get(), args... ) ) {
45 cache[name] = std::move( rPtr );
46 return cache[name];
47 }
48 return nullptr;
49 }
50
52 //bool add( ResourceT&& resource, bool overwrite = true )
53 //{
54 // if ( !overwrite && cache.find( resource.name ) != cache.end() ) {
55 // return false;
56 // }
57 // cache[name] = std::move( resource );
58 // return true;
59 //}
60
61 // get the names / keys of the cache
62 std::vector<std::string> getNames()
63 {
64 std::vector<std::string> names;
65 names.reserve( cache.size() );
66 for ( auto& el : cache ) {
67 names.push_back( el.first );
68 }
69 return names;
70 }
71
72 std::unordered_map<std::string, std::shared_ptr<ResourceT>> cache;
73};
74
75} // namespace ga
resource cache class, represents a container for a resource type organized by name (string)
Definition: resource.h:30
std::unordered_map< std::string, std::shared_ptr< ResourceT > > cache
Definition: resource.h:72
std::shared_ptr< ResourceT > get(const std::string &name)
Definition: resource.h:33
std::shared_ptr< ResourceT > load(const std::string &name, Args... args)
Definition: resource.h:41
std::vector< std::string > getNames()
Definition: resource.h:62
Definition: color.h:9
bool loadResource(ResourceT &r, Args... args)
Definition: resource.h:12