game_launcher 1.1.0
Modern Linux game launcher with Steam / Proton / Wine integration
Loading...
Searching...
No Matches
image_service.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// ==============================================================================
3// core/services/image_service.h
4// ==============================================================================
5// Abstract image service interface for UI-agnostic image loading
6// ==============================================================================
7// This provides an abstraction layer for image loading operations.
8// The Qt UI layer provides the concrete implementation using QPixmap.
9//
10// This follows the same pattern as NotificationService - business logic
11// can request image operations without depending on graphics APIs.
12// ==============================================================================
13
14#pragma once
15
16#include <memory>
17#include <string>
18
19namespace core
20{
21
22// ==============================================================================
23// IImageService Interface
24// ==============================================================================
25
26/**
27 * @class IImageService
28 * @brief Abstract interface for image loading operations
29 *
30 * The Qt UI layer implements this interface using QPixmap for image loading.
31 * The business logic layer uses this interface without knowing about
32 * the underlying graphics API.
33 */
35{
36 public:
37 virtual ~IImageService() = default;
38
39 /**
40 * @brief Load an image from disk
41 * @param path Path to the image file
42 * @param width Output parameter for image width (optional)
43 * @param height Output parameter for image height (optional)
44 * @return true if loading succeeded, false otherwise
45 *
46 * Implementations should cache the loaded image for subsequent calls.
47 */
48 virtual bool loadImage(const std::string& path, int* width = nullptr,
49 int* height = nullptr) = 0;
50
51 /**
52 * @brief Check if an image is already loaded/cached
53 * @param path Path to check
54 * @return true if image is cached
55 */
56 virtual bool isImageLoaded(const std::string& path) const = 0;
57
58 /**
59 * @brief Unload a specific image and free resources
60 * @param path Path of the image to unload
61 */
62 virtual void unloadImage(const std::string& path) = 0;
63
64 /**
65 * @brief Clear all cached images and free resources
66 */
67 virtual void clearCache() = 0;
68
69 /**
70 * @brief Get dimensions of a loaded image
71 * @param path Path to the image
72 * @param width Output parameter for width
73 * @param height Output parameter for height
74 * @return true if image is loaded and dimensions retrieved
75 */
76 virtual bool getImageDimensions(const std::string& path, int* width, int* height) const = 0;
77};
78
79// ==============================================================================
80// ImageService Singleton Accessor
81// ==============================================================================
82
83/**
84 * @class ImageService
85 * @brief Singleton accessor for the image service
86 *
87 * Usage:
88 * 1. UI layer creates concrete implementation at startup
89 * 2. UI layer calls ImageService::setInstance(implementation)
90 * 3. Business logic calls ImageService::instance()->loadImage(...)
91 */
93{
94 public:
95 /**
96 * @brief Set the image service implementation
97 * @param service Shared pointer to the implementation
98 *
99 * Must be called by UI layer before any image operations.
100 */
101 static void setInstance(std::shared_ptr<IImageService> service);
102
103 /**
104 * @brief Get the image service instance
105 * @return shared_ptr to the image service, empty if not set.
106 *
107 * Returns a shared_ptr (rather than a raw pointer) so the caller's
108 * copy keeps the underlying service alive for the duration of
109 * their call — even if another thread races setInstance(nullptr)
110 * at QApplication shutdown. Caller pattern:
111 * auto svc = ImageService::instance();
112 * if (svc) svc->loadImage(...);
113 */
114 static std::shared_ptr<IImageService> instance();
115
116 /**
117 * @brief Check if an image service has been set
118 * @return true if service is available
119 */
120 static bool hasInstance();
121
122 private:
123 static std::shared_ptr<IImageService> instance_;
124};
125
126// ==============================================================================
127// NullImageService - Safe default when no UI is present
128// ==============================================================================
129
130/**
131 * @class NullImageService
132 * @brief No-op implementation for headless/testing scenarios
133 */
135{
136 public:
137 bool loadImage(const std::string& /*path*/, int* width = nullptr,
138 int* height = nullptr) override
139 {
140 if (width)
141 *width = 0;
142 if (height)
143 *height = 0;
144 return false;
145 }
146
147 bool isImageLoaded(const std::string& /*path*/) const override
148 {
149 return false;
150 }
151
152 void unloadImage(const std::string& /*path*/) override {}
153
154 void clearCache() override {}
155
156 bool getImageDimensions(const std::string& /*path*/, int* width, int* height) const override
157 {
158 if (width)
159 *width = 0;
160 if (height)
161 *height = 0;
162 return false;
163 }
164};
165
166} // namespace core
Abstract interface for image loading operations.
virtual bool getImageDimensions(const std::string &path, int *width, int *height) const =0
Get dimensions of a loaded image.
virtual void unloadImage(const std::string &path)=0
Unload a specific image and free resources.
virtual bool isImageLoaded(const std::string &path) const =0
Check if an image is already loaded/cached.
virtual ~IImageService()=default
virtual void clearCache()=0
Clear all cached images and free resources.
virtual bool loadImage(const std::string &path, int *width=nullptr, int *height=nullptr)=0
Load an image from disk.
Singleton accessor for the image service.
static bool hasInstance()
Check if an image service has been set.
static std::shared_ptr< IImageService > instance()
Get the image service instance.
static void setInstance(std::shared_ptr< IImageService > service)
Set the image service implementation.
No-op implementation for headless/testing scenarios.
void unloadImage(const std::string &) override
Unload a specific image and free resources.
bool isImageLoaded(const std::string &) const override
Check if an image is already loaded/cached.
bool loadImage(const std::string &, int *width=nullptr, int *height=nullptr) override
Load an image from disk.
void clearCache() override
Clear all cached images and free resources.
bool getImageDimensions(const std::string &, int *width, int *height) const override
Get dimensions of a loaded image.