The ImageResolver class is responsible for loading and caching images fr.
It stores loaded images in memory to avoid reloading them multiple times, improving performance.
namespace ecs {
/**
* An image loader that caches images
*/
class ImageResolver {
public:
ImageResolver(const std::string &);
~ImageResolver();
/**
* @brief Get the Image content
* @param path The path to the image
* @param reload If the image should be reloaded (default: false)
*
* @return The image content
*/
std::string getImage(const std::string &, bool = false);
protected:
private:
std::string _basePath;
std::unordered_map<std::string, std::string> _cache;
};
} // namespace ecs
Public Members:
- Constructor:
ImageResolver(const std::string &basePath)
- Initializes the image resolver with a base path for image loading.
- Parameters:
basePath: A std::string representing the base directory path where the images are located.
- Destructor:
~ImageResolver()
- Default destructor for cleaning up the
ImageResolver. It ensures proper memory management.
std::string getImage(const std::string &path, bool reload = false)
- Retrieves the image content from the specified path.
- Parameters:
path: A std::string representing the relative path of the image to load (appended to _basePath).
reload: A bool flag that determines whether to force a reload of the image. Default is false.
- Returns:
- The content of the image as a
std::string.
- Behavior:
- If
reload is true, the image is reloaded from disk, bypassing the cache.
- If
reload is false, the method first checks if the image is already cached. If so, it returns the cached image. Otherwise, it loads the image from disk and caches it for future use.
Private Members:
std::string _basePath
- The base directory path where image files are located. All relative paths for images are appended to this base path.
std::unordered_map<std::string, std::string> _cache
- A cache of loaded images where the key is the image path (relative to
_basePath) and the value is the content of the image as a std::string.
- This cache helps to avoid redundant file reads by storing images in memory once they are loaded.
namespace ecs {
ImageResolver::ImageResolver(const std::string &path) : _basePath(path) {}
ImageResolver::~ImageResolver() {}
std::string readFile(const std::string &path)
{
std::ifstream file;
std::string s;
file.open(path);
if (file.is_open()) {
std::string line;
while (getline(file, line)) {
s += line;
s += "\\n";
}
file.close();
}
return s;
}
std::string ImageResolver::getImage(const std::string &path, bool reload)
{
if (reload) {
_cache[path] = readFile(_basePath + path);
return _cache.at(path);
}
try {
return _cache.at(path);
} catch (const std::out_of_range &e) {
std::string s = readFile(_basePath + path);
if (!s.empty())
_cache[path] = s;
return s;
}
}
} // namespace ecs
Key Methods:
getImage()
- Caching Behavior:
- If an image is requested and already exists in the cache, it is returned directly from memory.
- If the image is not in the cache, it is loaded from disk and added to the cache for future requests.
- Reloading Behavior:
- By setting the
reload flag to true, you can force the image to be reloaded from disk even if it is already cached. This is useful if the image file might have changed since it was first loaded.
Example Use Case: