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_resource.h"
8#include "SDL3pp_strings.h"
9#include "SDL3pp_version.h"
10
11namespace SDL {
12
38
39// Forward decl
40struct PropertiesBase;
41
42// Forward decl
43struct Properties;
44
46using PropertiesID = SDL_PropertiesID;
47
54
55// Forward decl
56struct PropertiesLock;
57
63using PropertyType = SDL_PropertyType;
64
66 SDL_PROPERTY_TYPE_INVALID;
67
69 SDL_PROPERTY_TYPE_POINTER;
70
72 SDL_PROPERTY_TYPE_STRING;
73
75 SDL_PROPERTY_TYPE_NUMBER;
76
77constexpr PropertyType PROPERTY_TYPE_FLOAT = SDL_PROPERTY_TYPE_FLOAT;
78
80 SDL_PROPERTY_TYPE_BOOLEAN;
81
99using EnumeratePropertiesCallback = void(SDLCALL*)(void* userdata,
100 PropertiesID props,
101 const char* name);
102
122 std::function<void(PropertiesID props, const char* name)>;
123
146using CleanupPropertyCallback = void(SDLCALL*)(void* userdata, void* value);
147
171using CleanupPropertyCB = std::function<void(void* value)>;
172
178struct PropertiesBase : ResourceBaseT<PropertiesID>
179{
181
196 void Destroy();
197
215 void Copy(PropertiesRef dst);
216
238
248 void Unlock(PropertiesLock&& lock);
249
279 void* value,
281 void* userdata);
282
311 void* value,
312 CleanupPropertyCB cleanup);
313
334 void SetPointerProperty(StringParam name, void* value);
335
353 void SetStringProperty(StringParam name, StringParam value);
354
368 void SetNumberProperty(StringParam name, Sint64 value);
369
383 void SetFloatProperty(StringParam name, float value);
384
398 void SetBooleanProperty(StringParam name, bool value);
399
412 bool HasProperty(StringParam name);
413
428
458 void* GetPointerProperty(StringParam name, void* default_value);
459
480 const char* GetStringProperty(StringParam name, StringParam default_value);
481
501 Sint64 GetNumberProperty(StringParam name, Sint64 default_value);
502
522 float GetFloatProperty(StringParam name, float default_value);
523
543 bool GetBooleanProperty(StringParam name, bool default_value);
544
555 void ClearProperty(StringParam name);
556
571 void Enumerate(EnumeratePropertiesCallback callback, void* userdata);
572
586 void Enumerate(EnumeratePropertiesCB callback);
587
596};
597
609{
610 using PropertiesBase::PropertiesBase;
611
619 constexpr explicit Properties(PropertiesID resource) noexcept
620 : PropertiesBase(resource)
621 {
622 }
623
625 constexpr Properties(Properties&& other) noexcept
626 : Properties(other.release())
627 {
628 }
629
645 Properties();
646
648 ~Properties() { SDL_DestroyProperties(get()); }
649
651 constexpr Properties& operator=(Properties&& other) noexcept
652 {
653 swap(*this, other);
654 return *this;
655 }
656};
657
681{
682 PropertiesRef m_lock;
683
684public:
708
710 PropertiesLock(const PropertiesLock& other) = delete;
711
714 : m_lock(std::move(other.m_lock))
715 {
716 }
717
728
729 PropertiesLock& operator=(const PropertiesLock& other) = delete;
730
733 {
734 std::swap(m_lock, other.m_lock);
735 return *this;
736 }
737
739 constexpr operator bool() const { return bool(m_lock); }
740
750 void reset();
751
753 PropertiesRef resource() const { return m_lock; }
754
756 void release() { m_lock.release(); }
757};
758
759#if SDL_VERSION_ATLEAST(3, 4, 0)
760
783constexpr const char* PROP_NAME_STRING = SDL_PROP_NAME_STRING;
784
785#endif // SDL_VERSION_ATLEAST(3, 4, 0)
786
799{
800 return {CheckError(SDL_GetGlobalProperties())};
801}
802
819
821 : Properties(CheckError(SDL_CreateProperties()))
822{
823}
824
844{
845 CheckError(SDL_CopyProperties(src, dst));
846}
847
849{
850 SDL::CopyProperties(get(), dst);
851}
852
875{
876 CheckError(SDL_LockProperties(props));
877}
878
880
882 : m_lock(std::move(resource))
883{
884 LockProperties(m_lock);
885}
886
899{
900 SDL_UnlockProperties(props);
901}
902
904{
905 SDL_assert_paranoid(lock.resource() == *this);
906 std::move(lock).reset();
907}
908
910{
911 if (!m_lock) return;
912 UnlockProperties(m_lock);
913 m_lock = {};
914}
915
946 StringParam name,
947 void* value,
949 void* userdata)
950{
952 SDL_SetPointerPropertyWithCleanup(props, name, value, cleanup, userdata));
953}
954
984 StringParam name,
985 void* value,
986 CleanupPropertyCB cleanup)
987{
989 SDL_SetPointerPropertyWithCleanup(props,
990 std::move(name),
991 value,
992 &Wrapper::CallOnce,
993 Wrapper::Wrap(std::move(cleanup)));
994}
995
997 StringParam name,
998 void* value,
1000 void* userdata)
1001{
1003 get(), std::move(name), value, cleanup, userdata);
1004}
1005
1007 StringParam name,
1008 void* value,
1009 CleanupPropertyCB cleanup)
1010{
1012 get(), std::move(name), value, std::move(cleanup));
1013}
1014
1037 StringParam name,
1038 void* value)
1039{
1040 CheckError(SDL_SetPointerProperty(props, name, value));
1041}
1042
1044{
1045 SDL::SetPointerProperty(get(), std::move(name), value);
1046}
1047
1067 StringParam name,
1068 StringParam value)
1069{
1070 CheckError(SDL_SetStringProperty(props, name, value));
1071}
1072
1074 StringParam value)
1075{
1076 SDL::SetStringProperty(get(), std::move(name), std::move(value));
1077}
1078
1094 StringParam name,
1095 Sint64 value)
1096{
1097 CheckError(SDL_SetNumberProperty(props, name, value));
1098}
1099
1101{
1102 SDL::SetNumberProperty(get(), std::move(name), value);
1103}
1104
1119inline void SetFloatProperty(PropertiesRef props, StringParam name, float value)
1120{
1121 CheckError(SDL_SetFloatProperty(props, name, value));
1122}
1123
1124inline void PropertiesBase::SetFloatProperty(StringParam name, float value)
1125{
1126 SDL::SetFloatProperty(get(), std::move(name), value);
1127}
1128
1144 StringParam name,
1145 bool value)
1146{
1147 CheckError(SDL_SetBooleanProperty(props, name, value));
1148}
1149
1151{
1152 SDL::SetBooleanProperty(get(), std::move(name), value);
1153}
1154
1168inline bool HasProperty(PropertiesRef props, StringParam name)
1169{
1170 return SDL_HasProperty(props, name);
1171}
1172
1174{
1175 return SDL::HasProperty(get(), std::move(name));
1176}
1177
1192{
1193 return SDL_GetPropertyType(props, name);
1194}
1195
1197{
1198 return SDL::GetPropertyType(get(), std::move(name));
1199}
1200
1232 StringParam name,
1233 void* default_value)
1234{
1235 return SDL_GetPointerProperty(props, name, default_value);
1236}
1237
1239 void* default_value)
1240{
1241 return SDL::GetPointerProperty(get(), std::move(name), default_value);
1242}
1243
1265inline const char* GetStringProperty(PropertiesRef props,
1266 StringParam name,
1267 StringParam default_value)
1268{
1269 return SDL_GetStringProperty(props, name, default_value);
1270}
1271
1273 StringParam default_value)
1274{
1276 get(), std::move(name), std::move(default_value));
1277}
1278
1300 StringParam name,
1301 Sint64 default_value)
1302{
1303 return SDL_GetNumberProperty(props, name, default_value);
1304}
1305
1307 Sint64 default_value)
1308{
1309 return SDL::GetNumberProperty(get(), std::move(name), default_value);
1310}
1311
1333 StringParam name,
1334 float default_value)
1335{
1336 return SDL_GetFloatProperty(props, name, default_value);
1337}
1338
1340 float default_value)
1341{
1342 return SDL::GetFloatProperty(get(), std::move(name), default_value);
1343}
1344
1366 StringParam name,
1367 bool default_value)
1368{
1369 return SDL_GetBooleanProperty(props, name, default_value);
1370}
1371
1373 bool default_value)
1374{
1375 return SDL::GetBooleanProperty(get(), std::move(name), default_value);
1376}
1377
1390{
1391 CheckError(SDL_ClearProperty(props, name));
1392}
1393
1395{
1396 SDL::ClearProperty(get(), std::move(name));
1397}
1398
1416 void* userdata)
1417{
1418 CheckError(SDL_EnumerateProperties(props, callback, userdata));
1419}
1420
1436 EnumeratePropertiesCB callback)
1437{
1438 return EnumerateProperties(
1439 props,
1440 [](void* userdata, PropertiesID props, const char* name) {
1441 auto& f = *static_cast<const EnumeratePropertiesCB*>(userdata);
1442 f(props, name);
1443 },
1444 &callback);
1445}
1446
1448 void* userdata)
1449{
1450 SDL::EnumerateProperties(get(), callback, userdata);
1451}
1452
1454{
1455 SDL::EnumerateProperties(get(), std::move(callback));
1456}
1457
1467{
1468 Uint64 count = 0;
1469 EnumerateProperties(props, [&count](auto, const char*) { count++; });
1470 return count;
1471}
1472
1474
1492{
1493 SDL_DestroyProperties(props);
1494}
1495
1497
1499
1510
1511} // namespace SDL
1512
1513#endif /* SDL3PP_PROPERTIES_H_ */
Lock a group of properties.
Definition SDL3pp_properties.h:681
~PropertiesLock()
Unlock a group of properties.
Definition SDL3pp_properties.h:727
PropertiesLock(const PropertiesLock &other)=delete
Copy constructor.
void release()
Releases the lock without unlocking.
Definition SDL3pp_properties.h:756
PropertiesRef resource() const
Get the reference to locked resource.
Definition SDL3pp_properties.h:753
PropertiesLock(PropertiesLock &&other) noexcept
Move constructor.
Definition SDL3pp_properties.h:713
PropertiesLock & operator=(PropertiesLock &&other) noexcept
Assignment operator.
Definition SDL3pp_properties.h:732
constexpr RawPointer release() noexcept
Definition SDL3pp_resource.h:57
friend constexpr void swap(ResourceBaseT &lhs, ResourceBaseT &rhs) noexcept
Definition SDL3pp_resource.h:65
constexpr RawPointer get() const noexcept
Definition SDL3pp_resource.h:54
constexpr ResourceBaseT()=default
Default constructor, creates null/invalid resource.
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:79
PropertiesLock(PropertiesRef resource)
Lock a group of properties.
Definition SDL3pp_properties.h:881
void ClearProperty(PropertiesRef props, StringParam name)
Clear a property from a group of properties.
Definition SDL3pp_properties.h:1389
std::function< void(void *value)> CleanupPropertyCB
A callback used to free resources when a property is deleted.
Definition SDL3pp_properties.h:171
Uint64 CountProperties(PropertiesRef props)
Returns the number of properties this has.
Definition SDL3pp_properties.h:1466
void SetNumberProperty(StringParam name, Sint64 value)
Set an integer property in a group of properties.
Definition SDL3pp_properties.h:1100
bool HasProperty(StringParam name)
Return whether a property exists in a group of properties.
Definition SDL3pp_properties.h:1173
bool GetBooleanProperty(PropertiesRef props, StringParam name, bool default_value)
Get a boolean property from a group of properties.
Definition SDL3pp_properties.h:1365
const char * GetStringProperty(StringParam name, StringParam default_value)
Get a string property from a group of properties.
Definition SDL3pp_properties.h:1272
void(SDLCALL *)(void *userdata, void *value) CleanupPropertyCallback
A callback used to free resources when a property is deleted.
Definition SDL3pp_properties.h:146
constexpr const char * PROP_NAME_STRING
A generic property for naming things.
Definition SDL3pp_properties.h:783
void LockProperties(PropertiesRef props)
Lock a group of properties.
Definition SDL3pp_properties.h:874
const char * GetStringProperty(PropertiesRef props, StringParam name, StringParam default_value)
Get a string property from a group of properties.
Definition SDL3pp_properties.h:1265
void EnumerateProperties(PropertiesRef props, EnumeratePropertiesCallback callback, void *userdata)
Enumerate the properties contained in a group of properties.
Definition SDL3pp_properties.h:1414
void * GetPointerProperty(PropertiesRef props, StringParam name, void *default_value)
Get a pointer property from a group of properties.
Definition SDL3pp_properties.h:1231
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:945
void SetFloatProperty(PropertiesRef props, StringParam name, float value)
Set a floating point property in a group of properties.
Definition SDL3pp_properties.h:1119
PropertiesLock Lock()
Lock a group of properties.
Definition SDL3pp_properties.h:879
bool GetBooleanProperty(StringParam name, bool default_value)
Get a boolean property from a group of properties.
Definition SDL3pp_properties.h:1372
void SetBooleanProperty(PropertiesRef props, StringParam name, bool value)
Set a boolean property in a group of properties.
Definition SDL3pp_properties.h:1143
Properties()
Create a group of properties.
Definition SDL3pp_properties.h:820
void Enumerate(EnumeratePropertiesCallback callback, void *userdata)
Enumerate the properties contained in a group of properties.
Definition SDL3pp_properties.h:1447
Properties CreateProperties()
Create a group of properties.
Definition SDL3pp_properties.h:818
void SetNumberProperty(PropertiesRef props, StringParam name, Sint64 value)
Set an integer property in a group of properties.
Definition SDL3pp_properties.h:1093
constexpr PropertyType PROPERTY_TYPE_NUMBER
NUMBER.
Definition SDL3pp_properties.h:74
void CopyProperties(PropertiesRef src, PropertiesRef dst)
Copy a group of properties.
Definition SDL3pp_properties.h:843
constexpr PropertyType PROPERTY_TYPE_POINTER
POINTER.
Definition SDL3pp_properties.h:68
void SetStringProperty(PropertiesRef props, StringParam name, StringParam value)
Set a string property in a group of properties.
Definition SDL3pp_properties.h:1066
bool HasProperty(PropertiesRef props, StringParam name)
Return whether a property exists in a group of properties.
Definition SDL3pp_properties.h:1168
PropertyType GetPropertyType(PropertiesRef props, StringParam name)
Get the type of a property in a group of properties.
Definition SDL3pp_properties.h:1191
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:121
void ClearProperty(StringParam name)
Clear a property from a group of properties.
Definition SDL3pp_properties.h:1394
float GetFloatProperty(PropertiesRef props, StringParam name, float default_value)
Get a floating point property from a group of properties.
Definition SDL3pp_properties.h:1332
constexpr PropertyType PROPERTY_TYPE_INVALID
INVALID.
Definition SDL3pp_properties.h:65
void * GetPointerProperty(StringParam name, void *default_value)
Get a pointer property from a group of properties.
Definition SDL3pp_properties.h:1238
void UnlockProperties(PropertiesRef props)
Unlock a group of properties.
Definition SDL3pp_properties.h:898
constexpr PropertyType PROPERTY_TYPE_FLOAT
FLOAT.
Definition SDL3pp_properties.h:77
constexpr PropertyType PROPERTY_TYPE_STRING
STRING.
Definition SDL3pp_properties.h:71
Uint64 GetCount()
Returns the number of properties this has.
Definition SDL3pp_properties.h:1473
void Destroy()
Destroy a group of properties.
Definition SDL3pp_properties.h:1496
float GetFloatProperty(StringParam name, float default_value)
Get a floating point property from a group of properties.
Definition SDL3pp_properties.h:1339
void Copy(PropertiesRef dst)
Copy a group of properties.
Definition SDL3pp_properties.h:848
SDL_PropertiesID PropertiesID
Alias to raw representation for Properties.
Definition SDL3pp_properties.h:46
void SetPointerProperty(PropertiesRef props, StringParam name, void *value)
Set a pointer property in a group of properties.
Definition SDL3pp_properties.h:1036
PropertyType GetPropertyType(StringParam name)
Get the type of a property in a group of properties.
Definition SDL3pp_properties.h:1196
void reset()
Unlock a group of properties.
Definition SDL3pp_properties.h:909
void SetBooleanProperty(StringParam name, bool value)
Set a boolean property in a group of properties.
Definition SDL3pp_properties.h:1150
SDL_PropertyType PropertyType
SDL property type.
Definition SDL3pp_properties.h:63
void SetFloatProperty(StringParam name, float value)
Set a floating point property in a group of properties.
Definition SDL3pp_properties.h:1124
void Unlock(PropertiesLock &&lock)
Unlock a group of properties.
Definition SDL3pp_properties.h:903
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:996
void SetPointerProperty(StringParam name, void *value)
Set a pointer property in a group of properties.
Definition SDL3pp_properties.h:1043
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:99
ResourceRefT< PropertiesBase > PropertiesRef
Reference for Properties.
Definition SDL3pp_properties.h:53
void SetStringProperty(StringParam name, StringParam value)
Set a string property in a group of properties.
Definition SDL3pp_properties.h:1073
Sint64 GetNumberProperty(StringParam name, Sint64 default_value)
Get a number property from a group of properties.
Definition SDL3pp_properties.h:1306
void DestroyProperties(PropertiesID props)
Destroy a group of properties.
Definition SDL3pp_properties.h:1491
Sint64 GetNumberProperty(PropertiesRef props, StringParam name, Sint64 default_value)
Get a number property from a group of properties.
Definition SDL3pp_properties.h:1299
PropertiesRef GetGlobalProperties()
Get the global SDL properties.
Definition SDL3pp_properties.h:798
::Sint64 Sint64
A signed 64-bit integer type.
Definition SDL3pp_stdinc.h:311
::Uint64 Uint64
An unsigned 64-bit integer type.
Definition SDL3pp_stdinc.h:326
Main include header for the SDL3pp library.
Definition SDL3pp_callbackWrapper.h:20
Base class to Properties.
Definition SDL3pp_properties.h:179
constexpr ResourceBaseT()=default
Default constructor, creates null/invalid resource.
An ID that represents a properties set.
Definition SDL3pp_properties.h:609
constexpr Properties & operator=(Properties &&other) noexcept
Assignment operator.
Definition SDL3pp_properties.h:651
~Properties()
Destructor.
Definition SDL3pp_properties.h:648
constexpr Properties(PropertiesID resource) noexcept
Constructs from raw Properties.
Definition SDL3pp_properties.h:619
constexpr Properties(Properties &&other) noexcept
Move constructor.
Definition SDL3pp_properties.h:625
A non-owning reference wrapper for a given resource.
Definition SDL3pp_resource.h:93