SDL3pp
A slim C++ wrapper for SDL3
Loading...
Searching...
No Matches
SDL3pp_storage.h
1#ifndef SDL3PP_STORAGE_H_
2#define SDL3PP_STORAGE_H_
3
4#include <SDL3/SDL_storage.h>
5#include "SDL3pp_filesystem.h"
6#include "SDL3pp_properties.h"
7#include "SDL3pp_stdinc.h"
8
9namespace SDL {
10
226// Forward decl
227struct Storage;
228
230using StorageRaw = SDL_Storage*;
231
232// Forward decl
233struct StorageRef;
234
237{
239
242 : value(value)
243 {
244 }
245
247 constexpr StorageParam(std::nullptr_t = nullptr)
248 : value(nullptr)
249 {
250 }
251
253 constexpr explicit operator bool() const { return !!value; }
254
256 constexpr auto operator<=>(const StorageParam& other) const = default;
257
259 constexpr operator StorageRaw() const { return value; }
260};
261
278using StorageInterface = SDL_StorageInterface;
279
292{
293 StorageRaw m_resource = nullptr;
294
295public:
297 constexpr Storage(std::nullptr_t = nullptr) noexcept
298 : m_resource(0)
299 {
300 }
301
309 constexpr explicit Storage(const StorageRaw resource) noexcept
310 : m_resource(resource)
311 {
312 }
313
314protected:
316 constexpr Storage(const Storage& other) noexcept = default;
317
318public:
320 constexpr Storage(Storage&& other) noexcept
321 : Storage(other.release())
322 {
323 }
324
325 constexpr Storage(const StorageRef& other) = delete;
326
327 constexpr Storage(StorageRef&& other) = delete;
328
349 : m_resource(CheckError(SDL_OpenTitleStorage(override, props)))
350 {
351 }
352
378 : m_resource(CheckError(SDL_OpenUserStorage(org, app, props)))
379 {
380 }
381
405 : m_resource(CheckError(SDL_OpenFileStorage(path)))
406 {
407 }
408
436 Storage(const StorageInterface& iface, void* userdata)
437 : m_resource(CheckError(SDL_OpenStorage(&iface, userdata)))
438 {
439 }
440
442 ~Storage() { CheckError(SDL_CloseStorage(m_resource)); }
443
445 constexpr Storage& operator=(Storage&& other) noexcept
446 {
447 std::swap(m_resource, other.m_resource);
448 return *this;
449 }
450
451protected:
453 constexpr Storage& operator=(const Storage& other) noexcept = default;
454
455public:
457 constexpr StorageRaw get() const noexcept { return m_resource; }
458
460 constexpr StorageRaw release() noexcept
461 {
462 auto r = m_resource;
463 m_resource = nullptr;
464 return r;
465 }
466
468 constexpr auto operator<=>(const Storage& other) const noexcept = default;
469
471 constexpr explicit operator bool() const noexcept { return !!m_resource; }
472
474 constexpr operator StorageParam() const noexcept { return {m_resource}; }
475
488 bool Close();
489
502 bool Ready();
503
516 std::optional<Uint64> GetFileSize(StringParam path);
517
537 bool ReadFile(StringParam path, TargetBytes destination);
538
557 std::string ReadFile(StringParam path);
558
577 template<class T>
578 std::vector<T> ReadFileAs(StringParam path);
579
593 void WriteFile(StringParam path, SourceBytes source);
594
605 void CreateDirectory(StringParam path);
606
634 void* userdata);
635
660 std::vector<Path> EnumerateDirectory(StringParam path);
661
687
698 void RemovePath(StringParam path);
699
711 void RenamePath(StringParam oldpath, StringParam newpath);
712
724 void CopyFile(StringParam oldpath, StringParam newpath);
725
738
750
785 StringParam pattern,
786 GlobFlags flags);
787};
788
791{
792 using Storage::Storage;
793
801 StorageRef(StorageParam resource) noexcept
802 : Storage(resource.value)
803 {
804 }
805
813 StorageRef(StorageRaw resource) noexcept
814 : Storage(resource)
815 {
816 }
817
819 constexpr StorageRef(const StorageRef& other) noexcept = default;
820
823};
824
845{
846 return Storage(std::move(override), props);
847}
848
874 StringParam app,
875 PropertiesParam props)
876{
877 return Storage(std::move(org), std::move(app), props);
878}
879
903{
904 return Storage(std::move(path));
905}
906
933inline Storage OpenStorage(const StorageInterface& iface, void* userdata)
934{
935 return Storage(iface, userdata);
936}
937
954inline bool CloseStorage(StorageRaw storage)
955{
956 return SDL_CloseStorage(storage);
957}
958
959inline bool Storage::Close() { return CloseStorage(release()); }
960
974inline bool StorageReady(StorageParam storage)
975{
976 return SDL_StorageReady(storage);
977}
978
979inline bool Storage::Ready() { return SDL::StorageReady(m_resource); }
980
994inline std::optional<Uint64> GetStorageFileSize(StorageParam storage,
995 StringParam path)
996{
997 if (Uint64 length; SDL_GetStorageFileSize(storage, path, &length)) {
998 return length;
999 }
1000 return {};
1001}
1002
1003inline std::optional<Uint64> Storage::GetFileSize(StringParam path)
1004{
1005 return SDL::GetStorageFileSize(m_resource, std::move(path));
1006}
1007
1028inline bool ReadStorageFile(StorageParam storage,
1029 StringParam path,
1030 TargetBytes destination)
1031{
1032 return SDL_ReadStorageFile(
1033 storage, path, destination.data(), destination.size_bytes());
1034}
1035
1051inline std::string ReadStorageFile(StorageParam storage, StringParam path)
1052{
1053 auto sz = GetStorageFileSize(storage, path.c_str());
1054 if (!sz || *sz == 0) return {};
1055 std::string buffer(*sz, 0);
1056 CheckError(ReadStorageFile(storage, std::move(path), buffer));
1057 return buffer;
1058}
1059
1060inline bool Storage::ReadFile(StringParam path, TargetBytes destination)
1061{
1062 return SDL::ReadStorageFile(
1063 m_resource, std::move(path), std::move(destination));
1064}
1065
1066inline std::string Storage::ReadFile(StringParam path)
1067{
1068 return SDL::ReadStorageFile(m_resource, std::move(path));
1069}
1070
1086template<class T>
1087inline std::vector<T> ReadStorageFileAs(StorageParam storage, StringParam path)
1088{
1089 auto sz = GetStorageFileSize(storage, path.c_str());
1090 if (!sz || *sz == 0) return {};
1091 std::vector<T> buffer(*sz / sizeof(T) + (*sz % sizeof(T) ? 1 : 0), 0);
1092 CheckError(ReadFile(std::move(path), {buffer.data(), *sz}));
1093 return buffer;
1094}
1095
1096template<class T>
1097inline std::vector<T> Storage::ReadFileAs(StringParam path)
1098{
1099 return SDL::ReadStorageFileAs<T>(m_resource, std::move(path));
1100}
1101
1116inline void WriteStorageFile(StorageParam storage,
1117 StringParam path,
1118 SourceBytes source)
1119{
1120 CheckError(
1121 SDL_WriteStorageFile(storage, path, source.data(), source.size_bytes()));
1122}
1123
1125{
1126 SDL::WriteStorageFile(m_resource, std::move(path), std::move(source));
1127}
1128
1141{
1142 CheckError(SDL_CreateStorageDirectory(storage, path));
1143}
1144
1146{
1147 SDL::CreateStorageDirectory(m_resource, std::move(path));
1148}
1149
1175 StringParam path,
1177 void* userdata)
1178{
1179 CheckError(SDL_EnumerateStorageDirectory(storage, path, callback, userdata));
1180}
1181
1206 StringParam path,
1207 EnumerateDirectoryCB callback)
1208{
1210 storage,
1211 std::move(path),
1212 [](void* userdata, const char* dirname, const char* fname) {
1213 auto& cb = *static_cast<EnumerateDirectoryCB*>(userdata);
1214 return cb(dirname, fname);
1215 },
1216 &callback);
1217}
1218
1242inline std::vector<Path> EnumerateStorageDirectory(StorageParam storage,
1243 StringParam path)
1244{
1245 std::vector<Path> r;
1246 EnumerateDirectory(std::move(path), [&](const char*, const char* fname) {
1247 r.emplace_back(fname);
1248 return ENUM_CONTINUE;
1249 });
1250 return r;
1251}
1252
1255 void* userdata)
1256{
1258 m_resource, std::move(path), callback, userdata);
1259}
1260
1261inline std::vector<Path> Storage::EnumerateDirectory(StringParam path)
1262{
1263 return SDL::EnumerateStorageDirectory(m_resource, std::move(path));
1264}
1265
1267 EnumerateDirectoryCB callback)
1268{
1269 SDL::EnumerateStorageDirectory(m_resource, std::move(path), callback);
1270}
1271
1284{
1285 CheckError(SDL_RemoveStoragePath(storage, path));
1286}
1287
1289{
1290 SDL::RemoveStoragePath(m_resource, std::move(path));
1291}
1292
1306 StringParam oldpath,
1307 StringParam newpath)
1308{
1309 CheckError(SDL_RenameStoragePath(storage, oldpath, newpath));
1310}
1311
1312inline void Storage::RenamePath(StringParam oldpath, StringParam newpath)
1313{
1314 SDL::RenameStoragePath(m_resource, std::move(oldpath), std::move(newpath));
1315}
1316
1329inline void CopyStorageFile(StorageParam storage,
1330 StringParam oldpath,
1331 StringParam newpath)
1332{
1333 CheckError(SDL_CopyStorageFile(storage, oldpath, newpath));
1334}
1335
1336inline void Storage::CopyFile(StringParam oldpath, StringParam newpath)
1337{
1338 SDL::CopyStorageFile(m_resource, std::move(oldpath), std::move(newpath));
1339}
1340
1354{
1355 if (PathInfo info; SDL_GetStoragePathInfo(storage, path, &info)) {
1356 return info;
1357 }
1358 return {};
1359}
1360
1362{
1363 return SDL::GetStoragePathInfo(m_resource, std::move(path));
1364}
1365
1378{
1379 return SDL_GetStorageSpaceRemaining(storage);
1380}
1381
1383{
1384 return SDL::GetStorageSpaceRemaining(m_resource);
1385}
1386
1421 StringParam path,
1422 StringParam pattern,
1423 GlobFlags flags)
1424{
1425 int count;
1426 auto data =
1427 CheckError(SDL_GlobStorageDirectory(storage, path, pattern, flags, &count));
1428 return OwnArray<char*>{data, size_t(count)};
1429}
1430
1432 StringParam pattern,
1433 GlobFlags flags)
1434{
1436 m_resource, std::move(path), std::move(pattern), flags);
1437}
1438
1440
1441} // namespace SDL
1442
1443#endif /* SDL3PP_STORAGE_H_ */
Base class for SDL memory allocated array wrap.
Definition: SDL3pp_ownPtr.h:44
Source byte stream.
Definition: SDL3pp_strings.h:240
constexpr size_t size_bytes() const
Retrieves contained size in bytes.
Definition: SDL3pp_strings.h:304
constexpr const char * data() const
Retrieves contained data.
Definition: SDL3pp_strings.h:307
An abstract interface for filesystem access.
Definition: SDL3pp_storage.h:292
constexpr auto operator<=>(const Storage &other) const noexcept=default
Comparison.
Storage(StringParam org, StringParam app, PropertiesParam props)
Opens up a container for a user's unique read/write filesystem.
Definition: SDL3pp_storage.h:377
constexpr Storage & operator=(const Storage &other) noexcept=default
Assignment operator.
Storage(StringParam path)
Opens up a container for local filesystem storage.
Definition: SDL3pp_storage.h:404
constexpr Storage(std::nullptr_t=nullptr) noexcept
Default ctor.
Definition: SDL3pp_storage.h:297
constexpr StorageRaw release() noexcept
Retrieves underlying StorageRaw and clear this.
Definition: SDL3pp_storage.h:460
~Storage()
Destructor.
Definition: SDL3pp_storage.h:442
Storage(const StorageInterface &iface, void *userdata)
Opens up a container using a client-provided storage interface.
Definition: SDL3pp_storage.h:436
constexpr Storage(Storage &&other) noexcept
Move constructor.
Definition: SDL3pp_storage.h:320
constexpr Storage(const Storage &other) noexcept=default
Copy constructor.
Storage(StringParam override, PropertiesParam props)
Opens up a read-only container for the application's filesystem.
Definition: SDL3pp_storage.h:348
constexpr Storage & operator=(Storage &&other) noexcept
Assignment operator.
Definition: SDL3pp_storage.h:445
constexpr Storage(const StorageRaw resource) noexcept
Constructs from StorageParam.
Definition: SDL3pp_storage.h:309
constexpr StorageRaw get() const noexcept
Retrieves underlying StorageRaw.
Definition: SDL3pp_storage.h:457
Helpers to use C++ strings parameters.
Definition: SDL3pp_strings.h:43
constexpr const char * c_str() const
Converts to a null terminated C string.
Definition: SDL3pp_strings.h:124
Target byte stream.
Definition: SDL3pp_strings.h:326
constexpr char * data() const
Retrieves contained data.
Definition: SDL3pp_strings.h:412
constexpr size_t size_bytes() const
Retrieves contained size in bytes.
Definition: SDL3pp_strings.h:409
constexpr void CheckError(bool result)
Check and throw if returned value from SDL is an error.
Definition: SDL3pp_error.h:197
std::function< EnumerationResult(const char *dirname, const char *fname)> EnumerateDirectoryCB
Callback for directory enumeration.
Definition: SDL3pp_filesystem.h:488
constexpr EnumerationResult ENUM_CONTINUE
Value that requests that enumeration continue.
Definition: SDL3pp_filesystem.h:426
EnumerationResult(SDLCALL *)(void *userdata, const char *dirname, const char *fname) EnumerateDirectoryCallback
Callback for directory enumeration.
Definition: SDL3pp_filesystem.h:460
void EnumerateDirectory(StringParam path, EnumerateDirectoryCallback callback, void *userdata)
Enumerate a directory through a callback function.
Definition: SDL3pp_filesystem.h:510
Uint32 GlobFlags
Flags for path matching.
Definition: SDL3pp_filesystem.h:392
::Uint64 Uint64
An unsigned 64-bit integer type.
Definition: SDL3pp_stdinc.h:371
void CreateDirectory(StringParam path)
Create a directory in a writable storage container.
Definition: SDL3pp_storage.h:1145
std::optional< Uint64 > GetStorageFileSize(StorageParam storage, StringParam path)
Query the size of a file within a storage container.
Definition: SDL3pp_storage.h:994
Storage OpenUserStorage(StringParam org, StringParam app, PropertiesParam props)
Opens up a container for a user's unique read/write filesystem.
Definition: SDL3pp_storage.h:873
Storage OpenTitleStorage(StringParam override, PropertiesParam props)
Opens up a read-only container for the application's filesystem.
Definition: SDL3pp_storage.h:844
OwnArray< char * > GlobStorageDirectory(StorageParam storage, StringParam path, StringParam pattern, GlobFlags flags)
Enumerate a directory tree, filtered by pattern, and return a list.
Definition: SDL3pp_storage.h:1420
SDL_StorageInterface StorageInterface
Function interface for Storage.
Definition: SDL3pp_storage.h:278
bool ReadStorageFile(StorageParam storage, StringParam path, TargetBytes destination)
Synchronously read a file from a storage container into a client-provided buffer.
Definition: SDL3pp_storage.h:1028
PathInfo GetPathInfo(StringParam path)
Get information about a filesystem path in a storage container.
Definition: SDL3pp_storage.h:1361
void RenameStoragePath(StorageParam storage, StringParam oldpath, StringParam newpath)
Rename a file or directory in a writable storage container.
Definition: SDL3pp_storage.h:1305
Storage OpenStorage(const StorageInterface &iface, void *userdata)
Opens up a container using a client-provided storage interface.
Definition: SDL3pp_storage.h:933
void WriteFile(StringParam path, SourceBytes source)
Synchronously write a file from client memory into a storage container.
Definition: SDL3pp_storage.h:1124
Uint64 GetSpaceRemaining()
Queries the remaining space in a storage container.
Definition: SDL3pp_storage.h:1382
OwnArray< char * > GlobDirectory(StringParam path, StringParam pattern, GlobFlags flags)
Enumerate a directory tree, filtered by pattern, and return a list.
Definition: SDL3pp_storage.h:1431
std::vector< T > ReadFileAs(StringParam path)
Synchronously read a file from a storage container into a client-provided buffer.
Definition: SDL3pp_storage.h:1097
SDL_Storage * StorageRaw
Alias to raw representation for Storage.
Definition: SDL3pp_storage.h:230
bool StorageReady(StorageParam storage)
Checks if the storage container is ready to use.
Definition: SDL3pp_storage.h:974
bool ReadFile(StringParam path, TargetBytes destination)
Synchronously read a file from a storage container into a client-provided buffer.
Definition: SDL3pp_storage.h:1060
std::optional< Uint64 > GetFileSize(StringParam path)
Query the size of a file within a storage container.
Definition: SDL3pp_storage.h:1003
void RemoveStoragePath(StorageParam storage, StringParam path)
Remove a file or an empty directory in a writable storage container.
Definition: SDL3pp_storage.h:1283
bool Close()
Closes and frees a storage container.
Definition: SDL3pp_storage.h:959
void RemovePath(StringParam path)
Remove a file or an empty directory in a writable storage container.
Definition: SDL3pp_storage.h:1288
void EnumerateStorageDirectory(StorageParam storage, StringParam path, EnumerateDirectoryCallback callback, void *userdata)
Enumerate a directory in a storage container through a callback function.
Definition: SDL3pp_storage.h:1174
bool Ready()
Checks if the storage container is ready to use.
Definition: SDL3pp_storage.h:979
std::vector< T > ReadStorageFileAs(StorageParam storage, StringParam path)
Synchronously read a file from a storage container into a client-provided buffer.
Definition: SDL3pp_storage.h:1087
void CopyStorageFile(StorageParam storage, StringParam oldpath, StringParam newpath)
Copy a file in a writable storage container.
Definition: SDL3pp_storage.h:1329
void RenamePath(StringParam oldpath, StringParam newpath)
Rename a file or directory in a writable storage container.
Definition: SDL3pp_storage.h:1312
Storage OpenFileStorage(StringParam path)
Opens up a container for local filesystem storage.
Definition: SDL3pp_storage.h:902
void WriteStorageFile(StorageParam storage, StringParam path, SourceBytes source)
Synchronously write a file from client memory into a storage container.
Definition: SDL3pp_storage.h:1116
PathInfo GetStoragePathInfo(StorageParam storage, StringParam path)
Get information about a filesystem path in a storage container.
Definition: SDL3pp_storage.h:1353
void CreateStorageDirectory(StorageParam storage, StringParam path)
Create a directory in a writable storage container.
Definition: SDL3pp_storage.h:1140
void CopyFile(StringParam oldpath, StringParam newpath)
Copy a file in a writable storage container.
Definition: SDL3pp_storage.h:1336
Uint64 GetStorageSpaceRemaining(StorageParam storage)
Queries the remaining space in a storage container.
Definition: SDL3pp_storage.h:1377
bool CloseStorage(StorageRaw storage)
Closes and frees a storage container.
Definition: SDL3pp_storage.h:954
void EnumerateDirectory(StringParam path, EnumerateDirectoryCallback callback, void *userdata)
Enumerate a directory in a storage container through a callback function.
Definition: SDL3pp_storage.h:1253
Main include header for the SDL3pp library.
Information about a path on the filesystem.
Definition: SDL3pp_filesystem.h:352
Safely wrap Properties for non owning parameters.
Definition: SDL3pp_properties.h:53
Safely wrap Storage for non owning parameters.
Definition: SDL3pp_storage.h:237
constexpr StorageParam(StorageRaw value)
Constructs from StorageRaw.
Definition: SDL3pp_storage.h:241
constexpr StorageParam(std::nullptr_t=nullptr)
Constructs null/invalid.
Definition: SDL3pp_storage.h:247
constexpr auto operator<=>(const StorageParam &other) const =default
Comparison.
StorageRaw value
parameter's StorageRaw
Definition: SDL3pp_storage.h:238
Semi-safe reference for Storage.
Definition: SDL3pp_storage.h:791
constexpr StorageRef(const StorageRef &other) noexcept=default
Copy constructor.
StorageRef(StorageRaw resource) noexcept
Constructs from StorageParam.
Definition: SDL3pp_storage.h:813
~StorageRef()
Destructor.
Definition: SDL3pp_storage.h:822
StorageRef(StorageParam resource) noexcept
Constructs from StorageParam.
Definition: SDL3pp_storage.h:801