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
83using PropertyType = SDL_PropertyType;
84
86 SDL_PROPERTY_TYPE_INVALID;
87
89 SDL_PROPERTY_TYPE_POINTER;
90
92 SDL_PROPERTY_TYPE_STRING;
93
95 SDL_PROPERTY_TYPE_NUMBER;
96
97constexpr PropertyType PROPERTY_TYPE_FLOAT = SDL_PROPERTY_TYPE_FLOAT;
98
100 SDL_PROPERTY_TYPE_BOOLEAN;
101
119using EnumeratePropertiesCallback = void(SDLCALL*)(void* userdata,
120 PropertiesID props,
121 const char* name);
122
140 std::function<void(PropertiesID props, const char* name)>;
141
165using CleanupPropertyCallback = void(SDLCALL*)(void* userdata, void* value);
166
191using CleanupPropertyCB = std::function<void(void* value)>;
192
203{
204 PropertiesID m_resource = 0;
205
206public:
208 constexpr Properties(std::nullptr_t = nullptr) noexcept
209 : m_resource(0)
210 {
211 }
212
220 constexpr explicit Properties(const PropertiesID resource) noexcept
221 : m_resource(resource)
222 {
223 }
224
226 constexpr Properties(const Properties& other) = delete;
227
229 constexpr Properties(Properties&& other) noexcept
230 : Properties(other.release())
231 {
232 }
233
234 constexpr Properties(const PropertiesRef& other) = delete;
235
236 constexpr Properties(PropertiesRef&& other) = delete;
237
252 static Properties Create();
253
255 ~Properties() { SDL_DestroyProperties(m_resource); }
256
258 constexpr Properties& operator=(Properties&& other) noexcept
259 {
260 std::swap(m_resource, other.m_resource);
261 return *this;
262 }
263
264protected:
266 constexpr Properties& operator=(const Properties& other) noexcept = default;
267
268public:
270 constexpr PropertiesID get() const noexcept { return m_resource; }
271
273 constexpr PropertiesID release() noexcept
274 {
275 auto r = m_resource;
276 m_resource = 0;
277 return r;
278 }
279
281 constexpr auto operator<=>(const Properties& other) const noexcept = default;
282
284 constexpr explicit operator bool() const noexcept { return !!m_resource; }
285
287 constexpr operator PropertiesParam() const noexcept { return {m_resource}; }
288
303 void Destroy();
304
322 void Copy(PropertiesParam dst);
323
344 void Lock();
345
355 void Unlock();
356
387 void* value,
389 void* userdata);
390
420 void* value,
421 CleanupPropertyCB cleanup);
422
443 void SetPointerProperty(StringParam name, void* value);
444
462 void SetStringProperty(StringParam name, StringParam value);
463
477 void SetNumberProperty(StringParam name, Sint64 value);
478
492 void SetFloatProperty(StringParam name, float value);
493
507 void SetBooleanProperty(StringParam name, bool value);
508
521 bool HasProperty(StringParam name);
522
537
568 void* GetPointerProperty(StringParam name, void* default_value);
569
591 const char* GetStringProperty(StringParam name, StringParam default_value);
592
612 Sint64 GetNumberProperty(StringParam name, Sint64 default_value);
613
633 float GetFloatProperty(StringParam name, float default_value);
634
654 bool GetBooleanProperty(StringParam name, bool default_value);
655
666 void ClearProperty(StringParam name);
667
682 void Enumerate(EnumeratePropertiesCallback callback, void* userdata);
683
697 void Enumerate(EnumeratePropertiesCB callback);
698
707};
708
711{
713
722 : Properties(resource.value)
723 {
724 }
725
733 PropertiesRef(PropertiesID resource) noexcept
734 : Properties(resource)
735 {
736 }
737
739 PropertiesRef(const PropertiesRef& other) noexcept
740 : Properties(other.get())
741 {
742 }
743
746};
747
748#if SDL_VERSION_ATLEAST(3, 3, 6)
749
772inline auto PROP_NAME_STRING = SDL_PROP_NAME_STRING;
773
774#endif // SDL_VERSION_ATLEAST(3, 3, 6)
775
786{
787 return {CheckError(SDL_GetGlobalProperties())};
788}
789
805{
806 return Properties(CheckError(SDL_CreateProperties()));
807}
808
810
830{
831 CheckError(SDL_CopyProperties(src, dst));
832}
833
835{
836 SDL::CopyProperties(m_resource, dst);
837}
838
861{
862 CheckError(SDL_LockProperties(props));
863}
864
865inline void Properties::Lock() { SDL::LockProperties(m_resource); }
866
879{
880 SDL_UnlockProperties(props);
881}
882
883inline void Properties::Unlock() { SDL::UnlockProperties(m_resource); }
884
915 StringParam name,
916 void* value,
918 void* userdata)
919{
921 SDL_SetPointerPropertyWithCleanup(props, name, value, cleanup, userdata));
922}
923
953 StringParam name,
954 void* value,
955 CleanupPropertyCB cleanup)
956{
958 SDL_SetPointerPropertyWithCleanup(props,
959 std::move(name),
960 value,
961 &Wrapper::CallOnce,
962 Wrapper::Wrap(std::move(cleanup)));
963}
964
966 StringParam name,
967 void* value,
969 void* userdata)
970{
972 m_resource, std::move(name), value, cleanup, userdata);
973}
974
976 void* value,
977 CleanupPropertyCB cleanup)
978{
980 m_resource, std::move(name), value, cleanup);
981}
982
1005 StringParam name,
1006 void* value)
1007{
1008 CheckError(SDL_SetPointerProperty(props, name, value));
1009}
1010
1011inline void Properties::SetPointerProperty(StringParam name, void* value)
1012{
1013 SDL::SetPointerProperty(m_resource, std::move(name), value);
1014}
1015
1035 StringParam name,
1036 StringParam value)
1037{
1038 CheckError(SDL_SetStringProperty(props, name, value));
1039}
1040
1042{
1043 SDL::SetStringProperty(m_resource, std::move(name), std::move(value));
1044}
1045
1061 StringParam name,
1062 Sint64 value)
1063{
1064 CheckError(SDL_SetNumberProperty(props, name, value));
1065}
1066
1068{
1069 SDL::SetNumberProperty(m_resource, std::move(name), value);
1070}
1071
1087 StringParam name,
1088 float value)
1089{
1090 CheckError(SDL_SetFloatProperty(props, name, value));
1091}
1092
1093inline void Properties::SetFloatProperty(StringParam name, float value)
1094{
1095 SDL::SetFloatProperty(m_resource, std::move(name), value);
1096}
1097
1113 StringParam name,
1114 bool value)
1115{
1116 CheckError(SDL_SetBooleanProperty(props, name, value));
1117}
1118
1119inline void Properties::SetBooleanProperty(StringParam name, bool value)
1120{
1121 SDL::SetBooleanProperty(m_resource, std::move(name), value);
1122}
1123
1138{
1139 return SDL_HasProperty(props, name);
1140}
1141
1143{
1144 return SDL::HasProperty(m_resource, std::move(name));
1145}
1146
1161{
1162 return SDL_GetPropertyType(props, name);
1163}
1164
1166{
1167 return SDL::GetPropertyType(m_resource, std::move(name));
1168}
1169
1202 StringParam name,
1203 void* default_value)
1204{
1205 return SDL_GetPointerProperty(props, name, default_value);
1206}
1207
1209 void* default_value)
1210{
1211 return SDL::GetPointerProperty(m_resource, std::move(name), default_value);
1212}
1213
1236inline const char* GetStringProperty(PropertiesParam props,
1237 StringParam name,
1238 StringParam default_value)
1239{
1240 return SDL_GetStringProperty(props, name, default_value);
1241}
1242
1244 StringParam default_value)
1245{
1247 m_resource, std::move(name), std::move(default_value));
1248}
1249
1271 StringParam name,
1272 Sint64 default_value)
1273{
1274 return SDL_GetNumberProperty(props, name, default_value);
1275}
1276
1278 Sint64 default_value)
1279{
1280 return SDL::GetNumberProperty(m_resource, std::move(name), default_value);
1281}
1282
1304 StringParam name,
1305 float default_value)
1306{
1307 return SDL_GetFloatProperty(props, name, default_value);
1308}
1309
1310inline float Properties::GetFloatProperty(StringParam name, float default_value)
1311{
1312 return SDL::GetFloatProperty(m_resource, std::move(name), default_value);
1313}
1314
1336 StringParam name,
1337 bool default_value)
1338{
1339 return SDL_GetBooleanProperty(props, name, default_value);
1340}
1341
1342inline bool Properties::GetBooleanProperty(StringParam name, bool default_value)
1343{
1344 return SDL::GetBooleanProperty(m_resource, std::move(name), default_value);
1345}
1346
1359{
1360 CheckError(SDL_ClearProperty(props, name));
1361}
1362
1364{
1365 SDL::ClearProperty(m_resource, std::move(name));
1366}
1367
1385 void* userdata)
1386{
1387 CheckError(SDL_EnumerateProperties(props, callback, userdata));
1388}
1389
1405 EnumeratePropertiesCB callback)
1406{
1407 return EnumerateProperties(
1408 props,
1409 [](void* userdata, PropertiesID props, const char* name) {
1410 auto& f = *static_cast<EnumeratePropertiesCB*>(userdata);
1411 f(props, name);
1412 },
1413 &callback);
1414}
1415
1417 void* userdata)
1418{
1419 SDL::EnumerateProperties(m_resource, callback, userdata);
1420}
1421
1423{
1424 SDL::EnumerateProperties(m_resource, callback);
1425}
1426
1436{
1437 Uint64 count = 0;
1438 EnumerateProperties(props, [&](auto, const char*) { count++; });
1439 return count;
1440}
1441
1443{
1444 return SDL::CountProperties(m_resource);
1445}
1446
1464{
1465 SDL_DestroyProperties(props);
1466}
1467
1469
1471
1472} // namespace SDL
1473
1474#endif /* SDL3PP_PROPERTIES_H_ */
An ID that represents a properties set.
Definition: SDL3pp_properties.h:203
constexpr Properties(const Properties &other)=delete
Copy constructor.
constexpr Properties & operator=(const Properties &other) noexcept=default
Assignment operator.
constexpr Properties & operator=(Properties &&other) noexcept
Assignment operator.
Definition: SDL3pp_properties.h:258
~Properties()
Destructor.
Definition: SDL3pp_properties.h:255
constexpr Properties(const PropertiesID resource) noexcept
Constructs from PropertiesParam.
Definition: SDL3pp_properties.h:220
constexpr PropertiesID release() noexcept
Retrieves underlying PropertiesID and clear this.
Definition: SDL3pp_properties.h:273
constexpr auto operator<=>(const Properties &other) const noexcept=default
Comparison.
constexpr Properties(Properties &&other) noexcept
Move constructor.
Definition: SDL3pp_properties.h:229
constexpr Properties(std::nullptr_t=nullptr) noexcept
Default ctor.
Definition: SDL3pp_properties.h:208
constexpr PropertiesID get() const noexcept
Retrieves underlying PropertiesID.
Definition: SDL3pp_properties.h:270
Helpers to use C++ strings parameters.
Definition: SDL3pp_strings.h:43
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:99
void UnlockProperties(PropertiesParam props)
Unlock a group of properties.
Definition: SDL3pp_properties.h:878
static Properties Create()
Create a group of properties.
Definition: SDL3pp_properties.h:809
Sint64 GetNumberProperty(StringParam name, Sint64 default_value)
Get a number property from a group of properties.
Definition: SDL3pp_properties.h:1277
std::function< void(void *value)> CleanupPropertyCB
A callback used to free resources when a property is deleted.
Definition: SDL3pp_properties.h:191
Sint64 GetNumberProperty(PropertiesParam props, StringParam name, Sint64 default_value)
Get a number property from a group of properties.
Definition: SDL3pp_properties.h:1270
void SetBooleanProperty(PropertiesParam props, StringParam name, bool value)
Set a boolean property in a group of properties.
Definition: SDL3pp_properties.h:1112
void LockProperties(PropertiesParam props)
Lock a group of properties.
Definition: SDL3pp_properties.h:860
SDL_PropertyType PropertyType
SDL property type.
Definition: SDL3pp_properties.h:83
void SetFloatProperty(StringParam name, float value)
Set a floating point property in a group of properties.
Definition: SDL3pp_properties.h:1093
void Lock()
Lock a group of properties.
Definition: SDL3pp_properties.h:865
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:965
float GetFloatProperty(StringParam name, float default_value)
Get a floating point property from a group of properties.
Definition: SDL3pp_properties.h:1310
void SetNumberProperty(StringParam name, Sint64 value)
Set an integer property in a group of properties.
Definition: SDL3pp_properties.h:1067
void Unlock()
Unlock a group of properties.
Definition: SDL3pp_properties.h:883
Uint64 CountProperties(PropertiesParam props)
Returns the number of properties this has.
Definition: SDL3pp_properties.h:1435
const char * GetStringProperty(StringParam name, StringParam default_value)
Get a string property from a group of properties.
Definition: SDL3pp_properties.h:1243
bool HasProperty(PropertiesParam props, StringParam name)
Return whether a property exists in a group of properties.
Definition: SDL3pp_properties.h:1137
float GetFloatProperty(PropertiesParam props, StringParam name, float default_value)
Get a floating point property from a group of properties.
Definition: SDL3pp_properties.h:1303
void ClearProperty(PropertiesParam props, StringParam name)
Clear a property from a group of properties.
Definition: SDL3pp_properties.h:1358
bool GetBooleanProperty(StringParam name, bool default_value)
Get a boolean property from a group of properties.
Definition: SDL3pp_properties.h:1342
void SetStringProperty(StringParam name, StringParam value)
Set a string property in a group of properties.
Definition: SDL3pp_properties.h:1041
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:140
void Copy(PropertiesParam dst)
Copy a group of properties.
Definition: SDL3pp_properties.h:834
Uint64 GetCount()
Returns the number of properties this has.
Definition: SDL3pp_properties.h:1442
void * GetPointerProperty(PropertiesParam props, StringParam name, void *default_value)
Get a pointer property from a group of properties.
Definition: SDL3pp_properties.h:1201
PropertyType GetPropertyType(PropertiesParam props, StringParam name)
Get the type of a property in a group of properties.
Definition: SDL3pp_properties.h:1160
bool HasProperty(StringParam name)
Return whether a property exists in a group of properties.
Definition: SDL3pp_properties.h:1142
void * GetPointerProperty(StringParam name, void *default_value)
Get a pointer property from a group of properties.
Definition: SDL3pp_properties.h:1208
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:914
void SetBooleanProperty(StringParam name, bool value)
Set a boolean property in a group of properties.
Definition: SDL3pp_properties.h:1119
void CopyProperties(PropertiesParam src, PropertiesParam dst)
Copy a group of properties.
Definition: SDL3pp_properties.h:829
Properties CreateProperties()
Create a group of properties.
Definition: SDL3pp_properties.h:804
constexpr PropertyType PROPERTY_TYPE_NUMBER
NUMBER.
Definition: SDL3pp_properties.h:94
constexpr PropertyType PROPERTY_TYPE_POINTER
POINTER.
Definition: SDL3pp_properties.h:88
auto PROP_NAME_STRING
A generic property for naming things.
Definition: SDL3pp_properties.h:772
void SetPointerProperty(PropertiesParam props, StringParam name, void *value)
Set a pointer property in a group of properties.
Definition: SDL3pp_properties.h:1004
void(SDLCALL *)(void *userdata, void *value) CleanupPropertyCallback
A callback used to free resources when a property is deleted.
Definition: SDL3pp_properties.h:165
const char * GetStringProperty(PropertiesParam props, StringParam name, StringParam default_value)
Get a string property from a group of properties.
Definition: SDL3pp_properties.h:1236
constexpr PropertyType PROPERTY_TYPE_INVALID
INVALID.
Definition: SDL3pp_properties.h:85
constexpr PropertyType PROPERTY_TYPE_FLOAT
FLOAT.
Definition: SDL3pp_properties.h:97
void Enumerate(EnumeratePropertiesCallback callback, void *userdata)
Enumerate the properties contained in a group of properties.
Definition: SDL3pp_properties.h:1416
void SetNumberProperty(PropertiesParam props, StringParam name, Sint64 value)
Set an integer property in a group of properties.
Definition: SDL3pp_properties.h:1060
constexpr PropertyType PROPERTY_TYPE_STRING
STRING.
Definition: SDL3pp_properties.h:91
PropertyType GetPropertyType(StringParam name)
Get the type of a property in a group of properties.
Definition: SDL3pp_properties.h:1165
void SetFloatProperty(PropertiesParam props, StringParam name, float value)
Set a floating point property in a group of properties.
Definition: SDL3pp_properties.h:1086
void SetPointerProperty(StringParam name, void *value)
Set a pointer property in a group of properties.
Definition: SDL3pp_properties.h:1011
void Destroy()
Destroy a group of properties.
Definition: SDL3pp_properties.h:1468
void SetStringProperty(PropertiesParam props, StringParam name, StringParam value)
Set a string property in a group of properties.
Definition: SDL3pp_properties.h:1034
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:121
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:1335
void ClearProperty(StringParam name)
Clear a property from a group of properties.
Definition: SDL3pp_properties.h:1363
void EnumerateProperties(PropertiesParam props, EnumeratePropertiesCallback callback, void *userdata)
Enumerate the properties contained in a group of properties.
Definition: SDL3pp_properties.h:1383
void DestroyProperties(PropertiesID props)
Destroy a group of properties.
Definition: SDL3pp_properties.h:1463
PropertiesRef GetGlobalProperties()
Get the global SDL properties.
Definition: SDL3pp_properties.h:785
::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 PropertiesParam(std::nullptr_t _=nullptr)
Constructs null/invalid.
Definition: SDL3pp_properties.h:63
constexpr auto operator<=>(const PropertiesParam &other) const =default
Comparison.
constexpr PropertiesParam(PropertiesID value)
Constructs from PropertiesID.
Definition: SDL3pp_properties.h:57
PropertiesID value
parameter's PropertiesID
Definition: SDL3pp_properties.h:54
Semi-safe reference for Properties.
Definition: SDL3pp_properties.h:711
PropertiesRef(const PropertiesRef &other) noexcept
Copy constructor.
Definition: SDL3pp_properties.h:739
PropertiesRef(PropertiesParam resource) noexcept
Constructs from PropertiesParam.
Definition: SDL3pp_properties.h:721
~PropertiesRef()
Destructor.
Definition: SDL3pp_properties.h:745
PropertiesRef(PropertiesID resource) noexcept
Constructs from PropertiesParam.
Definition: SDL3pp_properties.h:733