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
41
42// Forward decl
43struct Properties;
44
46using PropertiesID = SDL_PropertiesID;
47
48// Forward decl
49struct PropertiesRef;
50
51// Forward decl
52struct PropertiesLock;
53
59using PropertyType = SDL_PropertyType;
60
62 SDL_PROPERTY_TYPE_INVALID;
63
65 SDL_PROPERTY_TYPE_POINTER;
66
68 SDL_PROPERTY_TYPE_STRING;
69
71 SDL_PROPERTY_TYPE_NUMBER;
72
73constexpr PropertyType PROPERTY_TYPE_FLOAT = SDL_PROPERTY_TYPE_FLOAT;
74
76 SDL_PROPERTY_TYPE_BOOLEAN;
77
95using EnumeratePropertiesCallback = void(SDLCALL*)(void* userdata,
96 PropertiesID props,
97 const char* name);
98
116 std::function<void(PropertiesID props, const char* name)>;
117
141using CleanupPropertyCallback = void(SDLCALL*)(void* userdata, void* value);
142
167using CleanupPropertyCB = std::function<void(void* value)>;
168
179{
180 PropertiesID m_resource = 0;
181
182public:
184 constexpr Properties(std::nullptr_t = nullptr) noexcept
185 : m_resource(0)
186 {
187 }
188
196 constexpr explicit Properties(PropertiesID resource) noexcept
197 : m_resource(resource)
198 {
199 }
200
202 constexpr Properties(const Properties& other) noexcept = delete;
203
205 constexpr Properties(Properties&& other) noexcept
206 : Properties(other.release())
207 {
208 }
209
210 constexpr Properties(const PropertiesRef& other) = delete;
211
212 constexpr Properties(PropertiesRef&& other) = delete;
213
229 static Properties Create();
230
232 ~Properties() { SDL_DestroyProperties(m_resource); }
233
235 constexpr Properties& operator=(Properties&& other) noexcept
236 {
237 std::swap(m_resource, other.m_resource);
238 return *this;
239 }
240
242 Properties& operator=(const Properties& other) = delete;
243
245 constexpr PropertiesID get() const noexcept { return m_resource; }
246
248 constexpr PropertiesID release() noexcept
249 {
250 auto r = m_resource;
251 m_resource = 0;
252 return r;
253 }
254
256 constexpr auto operator<=>(const Properties& other) const noexcept = default;
257
259 constexpr explicit operator bool() const noexcept { return !!m_resource; }
260
275 void Destroy();
276
294 void Copy(PropertiesRef dst);
295
317
327 void Unlock(PropertiesLock&& lock);
328
359 void* value,
361 void* userdata);
362
392 void* value,
393 CleanupPropertyCB cleanup);
394
415 void SetPointerProperty(StringParam name, void* value);
416
434 void SetStringProperty(StringParam name, StringParam value);
435
449 void SetNumberProperty(StringParam name, Sint64 value);
450
464 void SetFloatProperty(StringParam name, float value);
465
479 void SetBooleanProperty(StringParam name, bool value);
480
493 bool HasProperty(StringParam name);
494
509
540 void* GetPointerProperty(StringParam name, void* default_value);
541
563 const char* GetStringProperty(StringParam name, StringParam default_value);
564
584 Sint64 GetNumberProperty(StringParam name, Sint64 default_value);
585
605 float GetFloatProperty(StringParam name, float default_value);
606
626 bool GetBooleanProperty(StringParam name, bool default_value);
627
638 void ClearProperty(StringParam name);
639
654 void Enumerate(EnumeratePropertiesCallback callback, void* userdata);
655
669 void Enumerate(EnumeratePropertiesCB callback);
670
679};
680
687{
689
697 constexpr PropertiesRef(PropertiesID resource) noexcept
698 : Properties(resource)
699 {
700 }
701
709 constexpr PropertiesRef(const Properties& resource) noexcept
710 : Properties(resource.get())
711 {
712 }
713
721 constexpr PropertiesRef(Properties&& resource) noexcept
722 : Properties(std::move(resource).release())
723 {
724 }
725
727 constexpr PropertiesRef(const PropertiesRef& other) noexcept
728 : Properties(other.get())
729 {
730 }
731
733 constexpr PropertiesRef(PropertiesRef&& other) noexcept
734 : Properties(other.get())
735 {
736 }
737
740
742 PropertiesRef& operator=(const PropertiesRef& other) noexcept
743 {
744 release();
745 Properties::operator=(Properties(other.get()));
746 return *this;
747 }
748
750 constexpr operator PropertiesID() const noexcept { return get(); }
751};
752
776{
777 PropertiesRef m_lock;
778
779public:
803
805 PropertiesLock(const PropertiesLock& other) = delete;
806
809 : m_lock(std::move(other.m_lock))
810 {
811 }
812
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 resource() const { return m_lock; }
849
851 void release() { m_lock.release(); }
852};
853
854#if SDL_VERSION_ATLEAST(3, 4, 0)
855
878constexpr const char* PROP_NAME_STRING = SDL_PROP_NAME_STRING;
879
880#endif // SDL_VERSION_ATLEAST(3, 4, 0)
881
894{
895 return {CheckError(SDL_GetGlobalProperties())};
896}
897
914{
915 return Properties(CheckError(SDL_CreateProperties()));
916}
917
919
939{
940 CheckError(SDL_CopyProperties(src, dst));
941}
942
944{
945 SDL::CopyProperties(m_resource, dst);
946}
947
970{
971 CheckError(SDL_LockProperties(props));
972}
973
974inline PropertiesLock Properties::Lock() { return {PropertiesRef(*this)}; }
975
977 : m_lock(std::move(resource))
978{
979 LockProperties(m_lock);
980}
981
994{
995 SDL_UnlockProperties(props);
996}
997
999{
1000 SDL_assert_paranoid(lock.resource() == *this);
1001 std::move(lock).reset();
1002}
1003
1005{
1006 if (!m_lock) return;
1007 UnlockProperties(m_lock);
1008 m_lock = {};
1009}
1010
1041 StringParam name,
1042 void* value,
1044 void* userdata)
1045{
1046 CheckError(
1047 SDL_SetPointerPropertyWithCleanup(props, name, value, cleanup, userdata));
1048}
1049
1079 StringParam name,
1080 void* value,
1081 CleanupPropertyCB cleanup)
1082{
1083 using Wrapper = CallbackWrapper<CleanupPropertyCB>;
1084 SDL_SetPointerPropertyWithCleanup(props,
1085 std::move(name),
1086 value,
1087 &Wrapper::CallOnce,
1088 Wrapper::Wrap(std::move(cleanup)));
1089}
1090
1092 StringParam name,
1093 void* value,
1095 void* userdata)
1096{
1098 m_resource, std::move(name), value, cleanup, userdata);
1099}
1100
1102 void* value,
1103 CleanupPropertyCB cleanup)
1104{
1106 m_resource, std::move(name), value, std::move(cleanup));
1107}
1108
1131 StringParam name,
1132 void* value)
1133{
1134 CheckError(SDL_SetPointerProperty(props, name, value));
1135}
1136
1137inline void Properties::SetPointerProperty(StringParam name, void* value)
1138{
1139 SDL::SetPointerProperty(m_resource, std::move(name), value);
1140}
1141
1161 StringParam name,
1162 StringParam value)
1163{
1164 CheckError(SDL_SetStringProperty(props, name, value));
1165}
1166
1168{
1169 SDL::SetStringProperty(m_resource, std::move(name), std::move(value));
1170}
1171
1187 StringParam name,
1188 Sint64 value)
1189{
1190 CheckError(SDL_SetNumberProperty(props, name, value));
1191}
1192
1194{
1195 SDL::SetNumberProperty(m_resource, std::move(name), value);
1196}
1197
1212inline void SetFloatProperty(PropertiesRef props, StringParam name, float value)
1213{
1214 CheckError(SDL_SetFloatProperty(props, name, value));
1215}
1216
1217inline void Properties::SetFloatProperty(StringParam name, float value)
1218{
1219 SDL::SetFloatProperty(m_resource, std::move(name), value);
1220}
1221
1237 StringParam name,
1238 bool value)
1239{
1240 CheckError(SDL_SetBooleanProperty(props, name, value));
1241}
1242
1243inline void Properties::SetBooleanProperty(StringParam name, bool value)
1244{
1245 SDL::SetBooleanProperty(m_resource, std::move(name), value);
1246}
1247
1261inline bool HasProperty(PropertiesRef props, StringParam name)
1262{
1263 return SDL_HasProperty(props, name);
1264}
1265
1267{
1268 return SDL::HasProperty(m_resource, std::move(name));
1269}
1270
1285{
1286 return SDL_GetPropertyType(props, name);
1287}
1288
1290{
1291 return SDL::GetPropertyType(m_resource, std::move(name));
1292}
1293
1326 StringParam name,
1327 void* default_value)
1328{
1329 return SDL_GetPointerProperty(props, name, default_value);
1330}
1331
1333 void* default_value)
1334{
1335 return SDL::GetPointerProperty(m_resource, std::move(name), default_value);
1336}
1337
1360inline const char* GetStringProperty(PropertiesRef props,
1361 StringParam name,
1362 StringParam default_value)
1363{
1364 return SDL_GetStringProperty(props, name, default_value);
1365}
1366
1368 StringParam default_value)
1369{
1371 m_resource, std::move(name), std::move(default_value));
1372}
1373
1395 StringParam name,
1396 Sint64 default_value)
1397{
1398 return SDL_GetNumberProperty(props, name, default_value);
1399}
1400
1402 Sint64 default_value)
1403{
1404 return SDL::GetNumberProperty(m_resource, std::move(name), default_value);
1405}
1406
1428 StringParam name,
1429 float default_value)
1430{
1431 return SDL_GetFloatProperty(props, name, default_value);
1432}
1433
1434inline float Properties::GetFloatProperty(StringParam name, float default_value)
1435{
1436 return SDL::GetFloatProperty(m_resource, std::move(name), default_value);
1437}
1438
1460 StringParam name,
1461 bool default_value)
1462{
1463 return SDL_GetBooleanProperty(props, name, default_value);
1464}
1465
1466inline bool Properties::GetBooleanProperty(StringParam name, bool default_value)
1467{
1468 return SDL::GetBooleanProperty(m_resource, std::move(name), default_value);
1469}
1470
1483{
1484 CheckError(SDL_ClearProperty(props, name));
1485}
1486
1488{
1489 SDL::ClearProperty(m_resource, std::move(name));
1490}
1491
1509 void* userdata)
1510{
1511 CheckError(SDL_EnumerateProperties(props, callback, userdata));
1512}
1513
1529 EnumeratePropertiesCB callback)
1530{
1531 return EnumerateProperties(
1532 props,
1533 [](void* userdata, PropertiesID props, const char* name) {
1534 auto& f = *static_cast<const EnumeratePropertiesCB*>(userdata);
1535 f(props, name);
1536 },
1537 &callback);
1538}
1539
1541 void* userdata)
1542{
1543 SDL::EnumerateProperties(m_resource, callback, userdata);
1544}
1545
1547{
1548 SDL::EnumerateProperties(m_resource, std::move(callback));
1549}
1550
1560{
1561 Uint64 count = 0;
1562 EnumerateProperties(props, [&count](auto, const char*) { count++; });
1563 return count;
1564}
1565
1567{
1568 return SDL::CountProperties(m_resource);
1569}
1570
1588{
1589 SDL_DestroyProperties(props);
1590}
1591
1593
1595
1596} // namespace SDL
1597
1598#endif /* SDL3PP_PROPERTIES_H_ */
Lock a group of properties.
Definition SDL3pp_properties.h:776
~PropertiesLock()
Unlock a group of properties.
Definition SDL3pp_properties.h:822
PropertiesLock(const PropertiesLock &other)=delete
Copy constructor.
void release()
Releases the lock without unlocking.
Definition SDL3pp_properties.h:851
PropertiesRef resource() const
Get the reference to locked resource.
Definition SDL3pp_properties.h:848
PropertiesLock(PropertiesLock &&other) noexcept
Move constructor.
Definition SDL3pp_properties.h:808
PropertiesLock & operator=(PropertiesLock &&other) noexcept
Assignment operator.
Definition SDL3pp_properties.h:827
An ID that represents a properties set.
Definition SDL3pp_properties.h:179
constexpr Properties & operator=(Properties &&other) noexcept
Assignment operator.
Definition SDL3pp_properties.h:235
~Properties()
Destructor.
Definition SDL3pp_properties.h:232
constexpr Properties(const Properties &other) noexcept=delete
Copy constructor.
constexpr PropertiesID release() noexcept
Retrieves underlying PropertiesID and clear this.
Definition SDL3pp_properties.h:248
constexpr Properties(PropertiesID resource) noexcept
Constructs from raw Properties.
Definition SDL3pp_properties.h:196
constexpr auto operator<=>(const Properties &other) const noexcept=default
Comparison.
constexpr Properties(Properties &&other) noexcept
Move constructor.
Definition SDL3pp_properties.h:205
constexpr Properties(std::nullptr_t=nullptr) noexcept
Default ctor.
Definition SDL3pp_properties.h:184
constexpr PropertiesID get() const noexcept
Retrieves underlying PropertiesID.
Definition SDL3pp_properties.h:245
Properties & operator=(const Properties &other)=delete
Assignment operator.
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:199
constexpr PropertyType PROPERTY_TYPE_BOOLEAN
BOOLEAN.
Definition SDL3pp_properties.h:75
PropertiesLock(PropertiesRef resource)
Lock a group of properties.
Definition SDL3pp_properties.h:976
static Properties Create()
Create a group of properties.
Definition SDL3pp_properties.h:918
Sint64 GetNumberProperty(StringParam name, Sint64 default_value)
Get a number property from a group of properties.
Definition SDL3pp_properties.h:1401
void ClearProperty(PropertiesRef props, StringParam name)
Clear a property from a group of properties.
Definition SDL3pp_properties.h:1482
std::function< void(void *value)> CleanupPropertyCB
A callback used to free resources when a property is deleted.
Definition SDL3pp_properties.h:167
Uint64 CountProperties(PropertiesRef props)
Returns the number of properties this has.
Definition SDL3pp_properties.h:1559
void SetFloatProperty(StringParam name, float value)
Set a floating point property in a group of properties.
Definition SDL3pp_properties.h:1217
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:1091
float GetFloatProperty(StringParam name, float default_value)
Get a floating point property from a group of properties.
Definition SDL3pp_properties.h:1434
PropertiesLock Lock()
Lock a group of properties.
Definition SDL3pp_properties.h:974
bool GetBooleanProperty(PropertiesRef props, StringParam name, bool default_value)
Get a boolean property from a group of properties.
Definition SDL3pp_properties.h:1459
void SetNumberProperty(StringParam name, Sint64 value)
Set an integer property in a group of properties.
Definition SDL3pp_properties.h:1193
void(SDLCALL *)(void *userdata, void *value) CleanupPropertyCallback
A callback used to free resources when a property is deleted.
Definition SDL3pp_properties.h:141
constexpr const char * PROP_NAME_STRING
A generic property for naming things.
Definition SDL3pp_properties.h:878
void Unlock(PropertiesLock &&lock)
Unlock a group of properties.
Definition SDL3pp_properties.h:998
const char * GetStringProperty(StringParam name, StringParam default_value)
Get a string property from a group of properties.
Definition SDL3pp_properties.h:1367
void LockProperties(PropertiesRef props)
Lock a group of properties.
Definition SDL3pp_properties.h:969
const char * GetStringProperty(PropertiesRef props, StringParam name, StringParam default_value)
Get a string property from a group of properties.
Definition SDL3pp_properties.h:1360
void EnumerateProperties(PropertiesRef props, EnumeratePropertiesCallback callback, void *userdata)
Enumerate the properties contained in a group of properties.
Definition SDL3pp_properties.h:1507
bool GetBooleanProperty(StringParam name, bool default_value)
Get a boolean property from a group of properties.
Definition SDL3pp_properties.h:1466
void SetStringProperty(StringParam name, StringParam value)
Set a string property in a group of properties.
Definition SDL3pp_properties.h:1167
void * GetPointerProperty(PropertiesRef props, StringParam name, void *default_value)
Get a pointer property from a group of properties.
Definition SDL3pp_properties.h:1325
void SetPointerPropertyWithCleanup(PropertiesRef 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:1040
void SetFloatProperty(PropertiesRef props, StringParam name, float value)
Set a floating point property in a group of properties.
Definition SDL3pp_properties.h:1212
Uint64 GetCount()
Returns the number of properties this has.
Definition SDL3pp_properties.h:1566
void SetBooleanProperty(PropertiesRef props, StringParam name, bool value)
Set a boolean property in a group of properties.
Definition SDL3pp_properties.h:1236
bool HasProperty(StringParam name)
Return whether a property exists in a group of properties.
Definition SDL3pp_properties.h:1266
void * GetPointerProperty(StringParam name, void *default_value)
Get a pointer property from a group of properties.
Definition SDL3pp_properties.h:1332
void SetBooleanProperty(StringParam name, bool value)
Set a boolean property in a group of properties.
Definition SDL3pp_properties.h:1243
Properties CreateProperties()
Create a group of properties.
Definition SDL3pp_properties.h:913
void SetNumberProperty(PropertiesRef props, StringParam name, Sint64 value)
Set an integer property in a group of properties.
Definition SDL3pp_properties.h:1186
constexpr PropertyType PROPERTY_TYPE_NUMBER
NUMBER.
Definition SDL3pp_properties.h:70
void CopyProperties(PropertiesRef src, PropertiesRef dst)
Copy a group of properties.
Definition SDL3pp_properties.h:938
constexpr PropertyType PROPERTY_TYPE_POINTER
POINTER.
Definition SDL3pp_properties.h:64
void SetStringProperty(PropertiesRef props, StringParam name, StringParam value)
Set a string property in a group of properties.
Definition SDL3pp_properties.h:1160
void Copy(PropertiesRef dst)
Copy a group of properties.
Definition SDL3pp_properties.h:943
bool HasProperty(PropertiesRef props, StringParam name)
Return whether a property exists in a group of properties.
Definition SDL3pp_properties.h:1261
PropertyType GetPropertyType(PropertiesRef props, StringParam name)
Get the type of a property in a group of properties.
Definition SDL3pp_properties.h:1284
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:115
float GetFloatProperty(PropertiesRef props, StringParam name, float default_value)
Get a floating point property from a group of properties.
Definition SDL3pp_properties.h:1427
constexpr PropertyType PROPERTY_TYPE_INVALID
INVALID.
Definition SDL3pp_properties.h:61
void UnlockProperties(PropertiesRef props)
Unlock a group of properties.
Definition SDL3pp_properties.h:993
constexpr PropertyType PROPERTY_TYPE_FLOAT
FLOAT.
Definition SDL3pp_properties.h:73
void Enumerate(EnumeratePropertiesCallback callback, void *userdata)
Enumerate the properties contained in a group of properties.
Definition SDL3pp_properties.h:1540
constexpr PropertyType PROPERTY_TYPE_STRING
STRING.
Definition SDL3pp_properties.h:67
PropertyType GetPropertyType(StringParam name)
Get the type of a property in a group of properties.
Definition SDL3pp_properties.h:1289
SDL_PropertiesID PropertiesID
Alias to raw representation for Properties.
Definition SDL3pp_properties.h:46
void SetPointerProperty(StringParam name, void *value)
Set a pointer property in a group of properties.
Definition SDL3pp_properties.h:1137
void SetPointerProperty(PropertiesRef props, StringParam name, void *value)
Set a pointer property in a group of properties.
Definition SDL3pp_properties.h:1130
void Destroy()
Destroy a group of properties.
Definition SDL3pp_properties.h:1592
void reset()
Unlock a group of properties.
Definition SDL3pp_properties.h:1004
SDL_PropertyType PropertyType
SDL property type.
Definition SDL3pp_properties.h:59
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:95
void ClearProperty(StringParam name)
Clear a property from a group of properties.
Definition SDL3pp_properties.h:1487
void DestroyProperties(PropertiesID props)
Destroy a group of properties.
Definition SDL3pp_properties.h:1587
Sint64 GetNumberProperty(PropertiesRef props, StringParam name, Sint64 default_value)
Get a number property from a group of properties.
Definition SDL3pp_properties.h:1394
PropertiesRef GetGlobalProperties()
Get the global SDL properties.
Definition SDL3pp_properties.h:893
::Sint64 Sint64
A signed 64-bit integer type.
Definition SDL3pp_stdinc.h:296
::Uint64 Uint64
An unsigned 64-bit integer type.
Definition SDL3pp_stdinc.h:311
Main include header for the SDL3pp library.
Definition SDL3pp_callbackWrapper.h:20
Reference for Properties.
Definition SDL3pp_properties.h:687
constexpr PropertiesRef(PropertiesRef &&other) noexcept
Move constructor.
Definition SDL3pp_properties.h:733
constexpr PropertiesRef(Properties &&resource) noexcept
Constructs from Properties.
Definition SDL3pp_properties.h:721
constexpr PropertiesRef(const Properties &resource) noexcept
Constructs from Properties.
Definition SDL3pp_properties.h:709
constexpr PropertiesRef(const PropertiesRef &other) noexcept
Copy constructor.
Definition SDL3pp_properties.h:727
constexpr Properties(std::nullptr_t=nullptr) noexcept
Default ctor.
Definition SDL3pp_properties.h:184
PropertiesRef & operator=(const PropertiesRef &other) noexcept
Assignment operator.
Definition SDL3pp_properties.h:742
~PropertiesRef()
Destructor.
Definition SDL3pp_properties.h:739
constexpr PropertiesRef(PropertiesID resource) noexcept
Constructs from raw Properties.
Definition SDL3pp_properties.h:697