Smolgui
Immediate gui library based on SFML
Loading...
Searching...
No Matches
ResourcesHolder.tpp
Go to the documentation of this file.
1namespace sgui
2{
3/////////////////////////////////////////////////
4template <typename Resource, typename Identifier>
5template <typename... Args>
6bool ResourcesHolder <Resource, Identifier>::load (
7 const Identifier& id,
8 const std::string& filename,
9 Args&&... args)
10{
11 // For Layout or TextContainer, data can be stored in separate files
12 if constexpr (std::is_same_v <Resource, Layout> || std::is_same_v <Resource, TextContainer>) {
13 // add data to the previously loaded ones
14 const auto res = m_resources.find (id);
15 if (res != std::end (m_resources)) {
16 if (!res->second->loadFromFile (filename, std::forward <Args> (args)...)) {
17 return false;
18 }
19 // store file name for reload
20 m_filesPath.at (id).push_back (filename);
21 return true;
22 }
23 }
24 // For all other type, information are stored in one file
25 auto newRes = std::make_unique <Resource> ();
26 if constexpr (std::is_same_v <Resource, sf::Font>) {
27 if (!newRes->openFromFile (filename, std::forward <Args> (args)...)) {
28 return false;
29 }
30 } else {
31 if (!newRes->loadFromFile (filename, std::forward <Args> (args)...)) {
32 return false;
33 }
34 }
35 // hold it
36 holdResource (std::move (newRes), id, filename);
37 return true;
38}
39
40/////////////////////////////////////////////////
41template <typename Resource, typename Identifier>
42template <typename... Args>
43bool ResourcesHolder<Resource, Identifier>::reload (
44 const Identifier& id,
45 Args&&... args)
46{
47 // get previously loaded resource
48 const auto res = m_resources.find (id);
49 const auto filesPath = m_filesPath.find (id);
50 // reload it from all its files
51 if (filesPath != std::end (m_filesPath) && res != std::end (m_resources)) {
52 res->second = std::move (std::make_unique <Resource> ());
53 for (const auto& file : filesPath->second) {
54 if constexpr (std::is_same_v <Resource, sf::Font>) {
55 return res->second->openFromFile (file, std::forward <Args> (args)...);
56 } else {
57 return res->second->loadFromFile (file, std::forward <Args> (args)...);
58 }
59 }
60 } else {
61 spdlog::error ("ResourcesHolder::reload (): id {} was not previously loaded", id);
62 }
63 return false;
64}
65
66/////////////////////////////////////////////////
67template <typename Resource, typename Identifier>
68const Resource& ResourcesHolder <Resource, Identifier>::get (const Identifier& id) const
69{
70 // Code crash if user forgot to load a resource
71 if (m_resources.empty ()) {
72 spdlog::error ("ResourcesHolder::get (): you tryied to access element in an empty resource holder.");
73 assert (false);
74 }
75 // Return asked resource OR the first one stored if it doesn't exist
76 const auto resource = m_resources.find (id);
77 if (resource != std::end (m_resources)) {
78 return *(resource->second);
79 }
80 spdlog::error ("ResourcesHolder::get (): can't find resource {}, will return first element", id);
81 return *(std::begin (m_resources)->second);
82}
83
84/////////////////////////////////////////////////
85template <typename Resource, typename Identifier>
86Resource& ResourcesHolder <Resource, Identifier>::get (const Identifier& id)
87{
88 return const_cast <Resource&> (std::as_const (*this).get (id));
89}
90
91
92/////////////////////////////////////////////////
93template <typename Resource, typename Identifier>
94void ResourcesHolder <Resource, Identifier>::holdResource (
95 std::unique_ptr <Resource>&& resource,
96 const Identifier& id,
97 const std::string& filename)
98{
99 // Store resource in the container
100 const auto res = m_resources.insert (std::make_pair (id, std::move (resource)));
101 const auto files = std::vector <std::string> { filename };
102 const auto path = m_filesPath.insert (std::make_pair (id, files));
103 // this code should never be executed in theory...
104 if (!res.second || !path.second) {
105 spdlog::error ("ResourcesHolder::holdResource (): failed to insert resource {} and its path. Is id unique ?", id);
106 assert (false);
107 }
108}
109
110} // namespace sgui