refactor: fase 5 — singletons a std::unique_ptr (elimina new/delete manual)

5 singletons afectats: Audio, Screen, Director, Resource::Cache, Resource::List.

- static T* instance → static std::unique_ptr<T> instance
- init(): new T() adoptat immediatament per unique_ptr (ownership RAII)
- destroy(): instance.reset() (sense delete manual)
- get(): retorna instance.get()
- Destructors moguts a public perquè std::default_delete hi pugui accedir
  (ctors privats + copy/move deleted → encapsulació efectiva mantinguda)

Ordre de destrucció preservat: SDL_AppQuit segueix cridant destroy() en
l'ordre invers a init() — la RAII automàtica no s'activa fins al final
del programa (LIFO de variables static).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-18 14:02:01 +02:00
parent 5e57034a38
commit c6e37af7d1
10 changed files with 37 additions and 42 deletions

View File

@@ -3,6 +3,7 @@
#include <SDL3/SDL.h>
#include <cstddef>
#include <memory>
#include <string>
#include <vector>
@@ -20,6 +21,7 @@ namespace Resource {
static void destroy();
static auto get() -> Cache*;
~Cache() = default;
Cache(const Cache&) = delete;
auto operator=(const Cache&) -> Cache& = delete;
@@ -39,7 +41,6 @@ namespace Resource {
private:
Cache() = default;
~Cache() = default;
enum class LoadStage {
MUSICS,
@@ -66,7 +67,7 @@ namespace Resource {
int loaded_count_{0};
std::string current_loading_name_;
static Cache* instance;
static std::unique_ptr<Cache> instance;
};
} // namespace Resource