-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into string_view
- Loading branch information
Showing
16 changed files
with
174 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright 2024 Jan Niklas Hasse <jhasse@bixense.com> | ||
// For conditions of distribution and use, see copyright notice in LICENSE.txt | ||
#include "TextureCache.hpp" | ||
|
||
#include <cassert> | ||
|
||
namespace jngl { | ||
|
||
std::shared_ptr<Texture> TextureCache::get(std::string_view filename) { | ||
#if __cpp_lib_generic_unordered_lookup | ||
auto it = textures.find(filename); | ||
#else | ||
auto it = textures.find(std::string(filename)); | ||
#endif | ||
if (it == textures.end()) { | ||
return nullptr; | ||
} | ||
return it->second; | ||
} | ||
|
||
void TextureCache::insert(std::string_view filename, std::shared_ptr<Texture> texture) { | ||
auto result [[maybe_unused]] = textures.emplace(filename, std::move(texture)); | ||
assert(result.second); | ||
} | ||
|
||
void TextureCache::remove(std::string_view filename) { | ||
#if __cpp_lib_generic_unordered_lookup | ||
auto it = textures.find(filename); | ||
#else | ||
auto it = textures.find(std::string(filename)); | ||
#endif | ||
if (it != textures.end()) { | ||
textures.erase(it); | ||
} | ||
} | ||
|
||
} // namespace jngl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright 2024 Jan Niklas Hasse <jhasse@bixense.com> | ||
// For conditions of distribution and use, see copyright notice in LICENSE.txt | ||
#pragma once | ||
|
||
#include "jngl/Singleton.hpp" | ||
|
||
#include <memory> | ||
|
||
namespace jngl { | ||
|
||
class Texture; | ||
|
||
class TextureCache : public Singleton<TextureCache> { | ||
public: | ||
std::shared_ptr<Texture> get(std::string_view filename); | ||
void insert(std::string_view filename, std::shared_ptr<Texture>); | ||
void remove(std::string_view filename); | ||
|
||
private: | ||
// https://www.cppstories.com/2021/heterogeneous-access-cpp20/ | ||
struct string_hash { | ||
using is_transparent = void; | ||
[[nodiscard]] size_t operator()(const char* txt) const { | ||
return std::hash<std::string_view>{}(txt); | ||
} | ||
[[nodiscard]] size_t operator()(std::string_view txt) const { | ||
return std::hash<std::string_view>{}(txt); | ||
} | ||
[[nodiscard]] size_t operator()(const std::string& txt) const { | ||
return std::hash<std::string>{}(txt); | ||
} | ||
}; | ||
std::unordered_map<std::string, std::shared_ptr<Texture>, string_hash, std::equal_to<>> | ||
textures; | ||
}; | ||
|
||
} // namespace jngl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.