3/////////////////////////////////////////////////
4template <typename Resource, typename Identifier>
5template <typename... Args>
6bool ResourcesHolder <Resource, Identifier>::load (
8 const std::string& filename,
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)...)) {
19 // store file name for reload
20 m_filesPath.at (id).push_back (filename);
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)...)) {
31 if (!newRes->loadFromFile (filename, std::forward <Args> (args)...)) {
36 holdResource (std::move (newRes), id, filename);
40/////////////////////////////////////////////////
41template <typename Resource, typename Identifier>
42template <typename... Args>
43bool ResourcesHolder<Resource, Identifier>::reload (
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)...);
57 return res->second->loadFromFile (file, std::forward <Args> (args)...);
61 spdlog::error ("ResourcesHolder::reload (): id {} was not previously loaded", id);
66/////////////////////////////////////////////////
67template <typename Resource, typename Identifier>
68const Resource& ResourcesHolder <Resource, Identifier>::get (const Identifier& id) const
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.");
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);
80 spdlog::error ("ResourcesHolder::get (): can't find resource {}, will return first element", id);
81 return *(std::begin (m_resources)->second);
84/////////////////////////////////////////////////
85template <typename Resource, typename Identifier>
86Resource& ResourcesHolder <Resource, Identifier>::get (const Identifier& id)
88 return const_cast <Resource&> (std::as_const (*this).get (id));
92/////////////////////////////////////////////////
93template <typename Resource, typename Identifier>
94void ResourcesHolder <Resource, Identifier>::holdResource (
95 std::unique_ptr <Resource>&& resource,
97 const std::string& filename)
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);