SDL3pp
A slim C++ wrapper for SDL3
Loading...
Searching...
No Matches
SDL3pp_properties.h
1#ifndef SDL3PP_PROPERTIES_H_
2#define SDL3PP_PROPERTIES_H_
3
4#include <SDL3/SDL_properties.h>
5#include "SDL3pp_callbackWrapper.h"
6#include "SDL3pp_error.h"
7#include "SDL3pp_strings.h"
8#include "SDL3pp_version.h"
9
10namespace SDL {
11
42// Forward decl
43struct Properties;
44
46using PropertiesID = SDL_PropertiesID;
47
48// Forward decl
49struct PropertiesRef;
50
53{
55
58 : value(value)
59 {
60 }
61
63 constexpr PropertiesParam(std::nullptr_t = nullptr)
64 : value(0)
65 {
66 }
67
69 constexpr explicit operator bool() const { return !!value; }
70
72 constexpr auto operator<=>(const PropertiesParam& other) const = default;
73
75 constexpr operator PropertiesID() const { return value; }
76};
77
78// Forward decl
79struct PropertiesLock;
80
86using PropertyType = SDL_PropertyType;
87
89 SDL_PROPERTY_TYPE_INVALID;
90
92 SDL_PROPERTY_TYPE_POINTER;
93
95 SDL_PROPERTY_TYPE_STRING;
96
98 SDL_PROPERTY_TYPE_NUMBER;
99
100constexpr PropertyType PROPERTY_TYPE_FLOAT = SDL_PROPERTY_TYPE_FLOAT;
101
103 SDL_PROPERTY_TYPE_BOOLEAN;
104
122using EnumeratePropertiesCallback = void(SDLCALL*)(void* userdata,
123 PropertiesID props,
124 const char* name);
125
143 std::function<void(PropertiesID props, const char* name)>;
144
168using CleanupPropertyCallback = void(SDLCALL*)(void* userdata, void* value);
169
194using CleanupPropertyCB = std::function<void(void* value)>;
195
206{
207 PropertiesID m_resource = 0;
208
209public:
211 constexpr Properties(std::nullptr_t = nullptr) noexcept
212 : m_resource(0)
213 {
214 }
215
223 constexpr explicit Properties(const PropertiesID resource) noexcept
224 : m_resource(resource)
225 {
226 }
227
228protected:
230 constexpr Properties(const Properties& other) noexcept = default;
231
232public:
234 constexpr Properties(Properties&& other) noexcept
235 : Properties(other.release())
236 {
237 }
238
239 constexpr Properties(const PropertiesRef& other) = delete;
240
241 constexpr Properties(PropertiesRef&& other) = delete;
242
257 static Properties Create();
258
260 ~Properties() { SDL_DestroyProperties(m_resource); }
261
263 constexpr Properties& operator=(Properties&& other) noexcept
264 {
265 std::swap(m_resource, other.m_resource);
266 return *this;
267 }
268
269protected:
271 constexpr Properties& operator=(const Properties& other) noexcept = default;
272
273public:
275 constexpr PropertiesID get() const noexcept { return m_resource; }
276
278 constexpr PropertiesID release() noexcept
279 {
280 auto r = m_resource;
281 m_resource = 0;
282 return r;
283 }
284
286 constexpr auto operator<=>(const Properties& other) const noexcept = default;
287
289 constexpr explicit operator bool() const noexcept { return !!m_resource; }
290
292 constexpr operator PropertiesParam() const noexcept { return {m_resource}; }
293
308 void Destroy();
309
327 void Copy(PropertiesParam dst);
328
350
360 void Unlock(PropertiesLock&& lock);
361
392 void* value,
394 void* userdata);
395
425 void* value,
426 CleanupPropertyCB cleanup);
427
448 void SetPointerProperty(StringParam name, void* value);
449
467 void SetStringProperty(StringParam name, StringParam value);
468
482 void SetNumberProperty(StringParam name, Sint64 value);
483
497 void SetFloatProperty(StringParam name, float value);
498
512 void SetBooleanProperty(StringParam name, bool value);
513
526 bool HasProperty(StringParam name);
527
542
573 void* GetPointerProperty(StringParam name, void* default_value);
574
596 const char* GetStringProperty(StringParam name, StringParam default_value);
597
617 Sint64 GetNumberProperty(StringParam name, Sint64 default_value);
618
638 float GetFloatProperty(StringParam name, float default_value);
639
659 bool GetBooleanProperty(StringParam name, bool default_value);
660
671 void ClearProperty(StringParam name);
672
687 void Enumerate(EnumeratePropertiesCallback callback, void* userdata);
688
702 void Enumerate(EnumeratePropertiesCB callback);
703
712};
713
716{
718
727 : Properties(resource.value)
728 {
729 }
730
738 PropertiesRef(PropertiesID resource) noexcept
739 : Properties(resource)
740 {
741 }
742
744 constexpr PropertiesRef(const PropertiesRef& other) noexcept = default;
745
748};
749
773{
774 PropertiesRef m_lock;
775
776public:
800
802 PropertiesLock(const PropertiesLock& other) = delete;
803
805 constexpr PropertiesLock(PropertiesLock&& other) noexcept
806 : m_lock(other.m_lock)
807 {
808 other.m_lock = {};
809 }
810
823
824 PropertiesLock& operator=(const PropertiesLock& other) = delete;
825
828 {
829 std::swap(m_lock, other.m_lock);
830 return *this;
831 }
832
834 constexpr operator bool() const { return bool(m_lock); }
835
845 void reset();
846
848 PropertiesRef get() { return m_lock; }
849
851 void release() { m_lock.release(); }
852};
853
854#if SDL_VERSION_ATLEAST(3, 4, 0)
855
878inline auto PROP_NAME_STRING = SDL_PROP_NAME_STRING;
879
880#endif // SDL_VERSION_ATLEAST(3, 4, 0)
881
892{
893 return {CheckError(SDL_GetGlobalProperties())};
894}
895
911{
912 return Properties(CheckError(SDL_CreateProperties()));
913}
914
916
936{
937 CheckError(SDL_CopyProperties(src, dst));
938}
939
941{
942 SDL::CopyProperties(m_resource, dst);
943}
944
967{
968 CheckError(SDL_LockProperties(props));
969}
970
971inline PropertiesLock Properties::Lock() { return {PropertiesRef(*this)}; }
972
974 : m_lock(std::move(resource))
975{
976 LockProperties(m_lock);
977}
978
991{
992 SDL_UnlockProperties(props);
993}
994
996{
997 SDL_assert_paranoid(lock.get() == *this);
998 lock.reset();
999}
1000
1002{
1003 if (!m_lock) return;
1004 UnlockProperties(m_lock);
1005 m_lock = {};
1006}
1007
1038 StringParam name,
1039 void* value,
1041 void* userdata)
1042{
1043 CheckError(
1044 SDL_SetPointerPropertyWithCleanup(props, name, value, cleanup, userdata));
1045}
1046
1076 StringParam name,
1077 void* value,
1078 CleanupPropertyCB cleanup)
1079{
1080 using Wrapper = CallbackWrapper<CleanupPropertyCB>;
1081 SDL_SetPointerPropertyWithCleanup(props,
1082 std::move(name),
1083 value,
1084 &Wrapper::CallOnce,
1085 Wrapper::Wrap(std::move(cleanup)));
1086}
1087
1089 StringParam name,
1090 void* value,
1092 void* userdata)
1093{
1095 m_resource, std::move(name), value, cleanup, userdata);
1096}
1097
1099 void* value,
1100 CleanupPropertyCB cleanup)
1101{
1103 m_resource, std::move(name), value, cleanup);
1104}
1105
1128 StringParam name,
1129 void* value)
1130{
1131 CheckError(SDL_SetPointerProperty(props, name, value));
1132}
1133
1134inline void Properties::SetPointerProperty(StringParam name, void* value)
1135{
1136 SDL::SetPointerProperty(m_resource, std::move(name), value);
1137}
1138
1158 StringParam name,
1159 StringParam value)
1160{
1161 CheckError(SDL_SetStringProperty(props, name, value));
1162}
1163
1165{
1166 SDL::SetStringProperty(m_resource, std::move(name), std::move(value));
1167}
1168
1184 StringParam name,
1185 Sint64 value)
1186{
1187 CheckError(SDL_SetNumberProperty(props, name, value));
1188}
1189
1191{
1192 SDL::SetNumberProperty(m_resource, std::move(name), value);
1193}
1194
1210 StringParam name,
1211 float value)
1212{
1213 CheckError(SDL_SetFloatProperty(props, name, value));
1214}
1215
1216inline void Properties::SetFloatProperty(StringParam name, float value)
1217{
1218 SDL::SetFloatProperty(m_resource, std::move(name), value);
1219}
1220
1236 StringParam name,
1237 bool value)
1238{
1239 CheckError(SDL_SetBooleanProperty(props, name, value));
1240}
1241
1242inline void Properties::SetBooleanProperty(StringParam name, bool value)
1243{
1244 SDL::SetBooleanProperty(m_resource, std::move(name), value);
1245}
1246
1261{
1262 return SDL_HasProperty(props, name);
1263}
1264
1266{
1267 return SDL::HasProperty(m_resource, std::move(name));
1268}
1269
1284{
1285 return SDL_GetPropertyType(props, name);
1286}
1287
1289{
1290 return SDL::GetPropertyType(m_resource, std::move(name));
1291}
1292
1325 StringParam name,
1326 void* default_value)
1327{
1328 return SDL_GetPointerProperty(props, name, default_value);
1329}
1330
1332 void* default_value)
1333{
1334 return SDL::GetPointerProperty(m_resource, std::move(name), default_value);
1335}
1336
1359inline const char* GetStringProperty(PropertiesParam props,
1360 StringParam name,
1361 StringParam default_value)
1362{
1363 return SDL_GetStringProperty(props, name, default_value);
1364}
1365
1367 StringParam default_value)
1368{
1370 m_resource, std::move(name), std::move(default_value));
1371}
1372
1394 StringParam name,
1395 Sint64 default_value)
1396{
1397 return SDL_GetNumberProperty(props, name, default_value);
1398}
1399
1401 Sint64 default_value)
1402{
1403 return SDL::GetNumberProperty(m_resource, std::move(name), default_value);
1404}
1405
1427 StringParam name,
1428 float default_value)
1429{
1430 return SDL_GetFloatProperty(props, name, default_value);
1431}
1432
1433inline float Properties::GetFloatProperty(StringParam name, float default_value)
1434{
1435 return SDL::GetFloatProperty(m_resource, std::move(name), default_value);
1436}
1437
1459 StringParam name,
1460 bool default_value)
1461{
1462 return SDL_GetBooleanProperty(props, name, default_value);
1463}
1464
1465inline bool Properties::GetBooleanProperty(StringParam name, bool default_value)
1466{
1467 return SDL::GetBooleanProperty(m_resource, std::move(name), default_value);
1468}
1469
1482{
1483 CheckError(SDL_ClearProperty(props, name));
1484}
1485
1487{
1488 SDL::ClearProperty(m_resource, std::move(name));
1489}
1490
1508 void* userdata)
1509{
1510 CheckError(SDL_EnumerateProperties(props, callback, userdata));
1511}
1512
1528 EnumeratePropertiesCB callback)
1529{
1530 return EnumerateProperties(
1531 props,
1532 [](void* userdata, PropertiesID props, const char* name) {
1533 auto& f = *static_cast<EnumeratePropertiesCB*>(userdata);
1534 f(props, name);
1535 },
1536 &callback);
1537}
1538
1540 void* userdata)
1541{
1542 SDL::EnumerateProperties(m_resource, callback, userdata);
1543}
1544
1546{
1547 SDL::EnumerateProperties(m_resource, callback);
1548}
1549
1559{
1560 Uint64 count = 0;
1561 EnumerateProperties(props, [&](auto, const char*) { count++; });
1562 return count;
1563}
1564
1566{
1567 return SDL::CountProperties(m_resource);
1568}
1569
1587{
1588 SDL_DestroyProperties(props);
1589}
1590
1592
1594
1595} // namespace SDL
1596
1597#endif /* SDL3PP_PROPERTIES_H_ */
Lock a group of properties.
Definition: SDL3pp_properties.h:773
~PropertiesLock()
Unlock a group of properties.
Definition: SDL3pp_properties.h:822
PropertiesLock(const PropertiesLock &other)=delete
Copy constructor.
PropertiesRef get()
Get the reference to locked resource.
Definition: SDL3pp_properties.h:848
void release()
Releases the lock without unlocking.
Definition: SDL3pp_properties.h:851
PropertiesLock & operator=(PropertiesLock &&other) noexcept
Assignment operator.
Definition: SDL3pp_properties.h:827
constexpr PropertiesLock(PropertiesLock &&other) noexcept
Move constructor.
Definition: SDL3pp_properties.h:805
An ID that represents a properties set.
Definition: SDL3pp_properties.h:206
constexpr Properties & operator=(const Properties &other) noexcept=default
Assignment operator.
constexpr Properties(const Properties &other) noexcept=default
Copy constructor.
constexpr Properties & operator=(Properties &&other) noexcept
Assignment operator.
Definition: SDL3pp_properties.h:263
~Properties()
Destructor.
Definition: SDL3pp_properties.h:260
constexpr Properties(const PropertiesID resource) noexcept
Constructs from PropertiesParam.
Definition: SDL3pp_properties.h:223
constexpr PropertiesID release() noexcept
Retrieves underlying PropertiesID and clear this.
Definition: SDL3pp_properties.h:278
constexpr auto operator<=>(const Properties &other) const noexcept=default
Comparison.
constexpr Properties(Properties &&other) noexcept
Move constructor.
Definition: SDL3pp_properties.h:234
constexpr Properties(std::nullptr_t=nullptr) noexcept
Default ctor.
Definition: SDL3pp_properties.h:211
constexpr PropertiesID get() const noexcept
Retrieves underlying PropertiesID.
Definition: SDL3pp_properties.h:275
Helpers to use C++ strings parameters.
Definition: SDL3pp_strings.h:43
#define SDL_assert_paranoid(condition)
An assertion test that is performed only when built with paranoid settings.
Definition: SDL3pp_assert.h:383
constexpr void CheckError(bool result)
Check and throw if returned value from SDL is an error.
Definition: SDL3pp_error.h:197
constexpr PropertyType PROPERTY_TYPE_BOOLEAN
BOOLEAN.
Definition: SDL3pp_properties.h:102
void UnlockProperties(PropertiesParam props)
Unlock a group of properties.
Definition: SDL3pp_properties.h:990
PropertiesLock(PropertiesRef resource)
Lock a group of properties.
Definition: SDL3pp_properties.h:973
static Properties Create()
Create a group of properties.
Definition: SDL3pp_properties.h:915
Sint64 GetNumberProperty(StringParam name, Sint64 default_value)
Get a number property from a group of properties.
Definition: SDL3pp_properties.h:1400
std::function< void(void *value)> CleanupPropertyCB
A callback used to free resources when a property is deleted.
Definition: SDL3pp_properties.h:194
Sint64 GetNumberProperty(PropertiesParam props, StringParam name, Sint64 default_value)
Get a number property from a group of properties.
Definition: SDL3pp_properties.h:1393
void SetBooleanProperty(PropertiesParam props, StringParam name, bool value)
Set a boolean property in a group of properties.
Definition: SDL3pp_properties.h:1235
void LockProperties(PropertiesParam props)
Lock a group of properties.
Definition: SDL3pp_properties.h:966
SDL_PropertyType PropertyType
SDL property type.
Definition: SDL3pp_properties.h:86
void SetFloatProperty(StringParam name, float value)
Set a floating point property in a group of properties.
Definition: SDL3pp_properties.h:1216
void SetPointerPropertyWithCleanup(StringParam name, void *value, CleanupPropertyCallback cleanup, void *userdata)
Set a pointer property in a group of properties with a cleanup function that is called when the prope...
Definition: SDL3pp_properties.h:1088
float GetFloatProperty(StringParam name, float default_value)
Get a floating point property from a group of properties.
Definition: SDL3pp_properties.h:1433
PropertiesLock Lock()
Lock a group of properties.
Definition: SDL3pp_properties.h:971
void SetNumberProperty(StringParam name, Sint64 value)
Set an integer property in a group of properties.
Definition: SDL3pp_properties.h:1190
Uint64 CountProperties(PropertiesParam props)
Returns the number of properties this has.
Definition: SDL3pp_properties.h:1558
void Unlock(PropertiesLock &&lock)
Unlock a group of properties.
Definition: SDL3pp_properties.h:995
const char * GetStringProperty(StringParam name, StringParam default_value)
Get a string property from a group of properties.
Definition: SDL3pp_properties.h:1366
bool HasProperty(PropertiesParam props, StringParam name)
Return whether a property exists in a group of properties.
Definition: SDL3pp_properties.h:1260
float GetFloatProperty(PropertiesParam props, StringParam name, float default_value)
Get a floating point property from a group of properties.
Definition: SDL3pp_properties.h:1426
void ClearProperty(PropertiesParam props, StringParam name)
Clear a property from a group of properties.
Definition: SDL3pp_properties.h:1481
bool GetBooleanProperty(StringParam name, bool default_value)
Get a boolean property from a group of properties.
Definition: SDL3pp_properties.h:1465
void SetStringProperty(StringParam name, StringParam value)
Set a string property in a group of properties.
Definition: SDL3pp_properties.h:1164
std::function< void(PropertiesID props, const char *name)> EnumeratePropertiesCB
A callback used to enumerate all the properties in a group of properties.
Definition: SDL3pp_properties.h:143
void Copy(PropertiesParam dst)
Copy a group of properties.
Definition: SDL3pp_properties.h:940
Uint64 GetCount()
Returns the number of properties this has.
Definition: SDL3pp_properties.h:1565
void * GetPointerProperty(PropertiesParam props, StringParam name, void *default_value)
Get a pointer property from a group of properties.
Definition: SDL3pp_properties.h:1324
PropertyType GetPropertyType(PropertiesParam props, StringParam name)
Get the type of a property in a group of properties.
Definition: SDL3pp_properties.h:1283
bool HasProperty(StringParam name)
Return whether a property exists in a group of properties.
Definition: SDL3pp_properties.h:1265
void * GetPointerProperty(StringParam name, void *default_value)
Get a pointer property from a group of properties.
Definition: SDL3pp_properties.h:1331
void SetPointerPropertyWithCleanup(PropertiesParam props, StringParam name, void *value, CleanupPropertyCallback cleanup, void *userdata)
Set a pointer property in a group of properties with a cleanup function that is called when the prope...
Definition: SDL3pp_properties.h:1037
void SetBooleanProperty(StringParam name, bool value)
Set a boolean property in a group of properties.
Definition: SDL3pp_properties.h:1242
void CopyProperties(PropertiesParam src, PropertiesParam dst)
Copy a group of properties.
Definition: SDL3pp_properties.h:935
Properties CreateProperties()
Create a group of properties.
Definition: SDL3pp_properties.h:910
constexpr PropertyType PROPERTY_TYPE_NUMBER
NUMBER.
Definition: SDL3pp_properties.h:97
constexpr PropertyType PROPERTY_TYPE_POINTER
POINTER.
Definition: SDL3pp_properties.h:91
auto PROP_NAME_STRING
A generic property for naming things.
Definition: SDL3pp_properties.h:878
void SetPointerProperty(PropertiesParam props, StringParam name, void *value)
Set a pointer property in a group of properties.
Definition: SDL3pp_properties.h:1127
void(SDLCALL *)(void *userdata, void *value) CleanupPropertyCallback
A callback used to free resources when a property is deleted.
Definition: SDL3pp_properties.h:168
const char * GetStringProperty(PropertiesParam props, StringParam name, StringParam default_value)
Get a string property from a group of properties.
Definition: SDL3pp_properties.h:1359
constexpr PropertyType PROPERTY_TYPE_INVALID
INVALID.
Definition: SDL3pp_properties.h:88
constexpr PropertyType PROPERTY_TYPE_FLOAT
FLOAT.
Definition: SDL3pp_properties.h:100
void Enumerate(EnumeratePropertiesCallback callback, void *userdata)
Enumerate the properties contained in a group of properties.
Definition: SDL3pp_properties.h:1539
void SetNumberProperty(PropertiesParam props, StringParam name, Sint64 value)
Set an integer property in a group of properties.
Definition: SDL3pp_properties.h:1183
constexpr PropertyType PROPERTY_TYPE_STRING
STRING.
Definition: SDL3pp_properties.h:94
PropertyType GetPropertyType(StringParam name)
Get the type of a property in a group of properties.
Definition: SDL3pp_properties.h:1288
void SetFloatProperty(PropertiesParam props, StringParam name, float value)
Set a floating point property in a group of properties.
Definition: SDL3pp_properties.h:1209
void SetPointerProperty(StringParam name, void *value)
Set a pointer property in a group of properties.
Definition: SDL3pp_properties.h:1134
void Destroy()
Destroy a group of properties.
Definition: SDL3pp_properties.h:1591
void SetStringProperty(PropertiesParam props, StringParam name, StringParam value)
Set a string property in a group of properties.
Definition: SDL3pp_properties.h:1157
void reset()
Unlock a group of properties.
Definition: SDL3pp_properties.h:1001
void(SDLCALL *)(void *userdata, PropertiesID props, const char *name) EnumeratePropertiesCallback
A callback used to enumerate all the properties in a group of properties.
Definition: SDL3pp_properties.h:124
SDL_PropertiesID PropertiesID
Alias to raw representation for Properties.
Definition: SDL3pp_properties.h:46
bool GetBooleanProperty(PropertiesParam props, StringParam name, bool default_value)
Get a boolean property from a group of properties.
Definition: SDL3pp_properties.h:1458
void ClearProperty(StringParam name)
Clear a property from a group of properties.
Definition: SDL3pp_properties.h:1486
void EnumerateProperties(PropertiesParam props, EnumeratePropertiesCallback callback, void *userdata)
Enumerate the properties contained in a group of properties.
Definition: SDL3pp_properties.h:1506
void DestroyProperties(PropertiesID props)
Destroy a group of properties.
Definition: SDL3pp_properties.h:1586
PropertiesRef GetGlobalProperties()
Get the global SDL properties.
Definition: SDL3pp_properties.h:891
::Uint64 Uint64
An unsigned 64-bit integer type.
Definition: SDL3pp_stdinc.h:371
::Sint64 Sint64
A signed 64-bit integer type.
Definition: SDL3pp_stdinc.h:356
Main include header for the SDL3pp library.
Definition: SDL3pp_callbackWrapper.h:20
Safely wrap Properties for non owning parameters.
Definition: SDL3pp_properties.h:53
constexpr auto operator<=>(const PropertiesParam &other) const =default
Comparison.
constexpr PropertiesParam(PropertiesID value)
Constructs from PropertiesID.
Definition: SDL3pp_properties.h:57
constexpr PropertiesParam(std::nullptr_t=nullptr)
Constructs null/invalid.
Definition: SDL3pp_properties.h:63
PropertiesID value
parameter's PropertiesID
Definition: SDL3pp_properties.h:54
Semi-safe reference for Properties.
Definition: SDL3pp_properties.h:716
PropertiesRef(PropertiesParam resource) noexcept
Constructs from PropertiesParam.
Definition: SDL3pp_properties.h:726
constexpr PropertiesRef(const PropertiesRef &other) noexcept=default
Copy constructor.
~PropertiesRef()
Destructor.
Definition: SDL3pp_properties.h:747
PropertiesRef(PropertiesID resource) noexcept
Constructs from PropertiesParam.
Definition: SDL3pp_properties.h:738