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
9namespace SDL {
10
41// Forward decl
42struct Properties;
43
45using PropertiesID = SDL_PropertiesID;
46
47// Forward decl
48struct PropertiesRef;
49
52{
54
57 : value(value)
58 {
59 }
60
62 constexpr PropertiesParam(std::nullptr_t _ = nullptr)
63 : value(0)
64 {
65 }
66
68 constexpr explicit operator bool() const { return !!value; }
69
71 constexpr auto operator<=>(const PropertiesParam& other) const = default;
72
74 constexpr operator PropertiesID() const { return value; }
75};
76
82using PropertyType = SDL_PropertyType;
83
85 SDL_PROPERTY_TYPE_INVALID;
86
88 SDL_PROPERTY_TYPE_POINTER;
89
91 SDL_PROPERTY_TYPE_STRING;
92
94 SDL_PROPERTY_TYPE_NUMBER;
95
96constexpr PropertyType PROPERTY_TYPE_FLOAT = SDL_PROPERTY_TYPE_FLOAT;
97
99 SDL_PROPERTY_TYPE_BOOLEAN;
100
118using EnumeratePropertiesCallback = void(SDLCALL*)(void* userdata,
119 PropertiesID props,
120 const char* name);
121
139 std::function<void(PropertiesID props, const char* name)>;
140
164using CleanupPropertyCallback = void(SDLCALL*)(void* userdata, void* value);
165
190using CleanupPropertyCB = std::function<void(void* value)>;
191
202{
203 PropertiesID m_resource = 0;
204
205public:
207 constexpr Properties(std::nullptr_t = nullptr) noexcept
208 : m_resource(0)
209 {
210 }
211
219 constexpr explicit Properties(const PropertiesID resource) noexcept
220 : m_resource(resource)
221 {
222 }
223
225 constexpr Properties(const Properties& other) = delete;
226
228 constexpr Properties(Properties&& other) noexcept
229 : Properties(other.release())
230 {
231 }
232
233 constexpr Properties(const PropertiesRef& other) = delete;
234
235 constexpr Properties(PropertiesRef&& other) = delete;
236
251 static Properties Create();
252
254 ~Properties() { SDL_DestroyProperties(m_resource); }
255
257 constexpr Properties& operator=(Properties&& other) noexcept
258 {
259 std::swap(m_resource, other.m_resource);
260 return *this;
261 }
262
263protected:
265 constexpr Properties& operator=(const Properties& other) noexcept = default;
266
267public:
269 constexpr PropertiesID get() const noexcept { return m_resource; }
270
272 constexpr PropertiesID release() noexcept
273 {
274 auto r = m_resource;
275 m_resource = 0;
276 return r;
277 }
278
280 constexpr auto operator<=>(const Properties& other) const noexcept = default;
281
283 constexpr explicit operator bool() const noexcept { return !!m_resource; }
284
286 constexpr operator PropertiesParam() const noexcept { return {m_resource}; }
287
302 void Destroy();
303
319 void Copy(PropertiesParam dst);
320
341 void Lock();
342
352 void Unlock();
353
384 void* value,
386 void* userdata);
387
417 void* value,
418 CleanupPropertyCB cleanup);
419
440 void SetPointerProperty(StringParam name, void* value);
441
459 void SetStringProperty(StringParam name, StringParam value);
460
474 void SetNumberProperty(StringParam name, Sint64 value);
475
489 void SetFloatProperty(StringParam name, float value);
490
504 void SetBooleanProperty(StringParam name, bool value);
505
518 bool HasProperty(StringParam name);
519
534
565 void* GetPointerProperty(StringParam name, void* default_value);
566
588 const char* GetStringProperty(StringParam name, StringParam default_value);
589
609 Sint64 GetNumberProperty(StringParam name, Sint64 default_value);
610
630 float GetFloatProperty(StringParam name, float default_value);
631
651 bool GetBooleanProperty(StringParam name, bool default_value);
652
663 void ClearProperty(StringParam name);
664
679 void Enumerate(EnumeratePropertiesCallback callback, void* userdata);
680
694 void Enumerate(EnumeratePropertiesCB callback);
695
704};
705
708{
710
719 : Properties(resource.value)
720 {
721 }
722
730 PropertiesRef(PropertiesID resource) noexcept
731 : Properties(resource)
732 {
733 }
734
736 PropertiesRef(const PropertiesRef& other) noexcept
737 : Properties(other.get())
738 {
739 }
740
743};
744
755{
756 return {CheckError(SDL_GetGlobalProperties())};
757}
758
774{
775 return Properties(CheckError(SDL_CreateProperties()));
776}
777
779
797{
798 CheckError(SDL_CopyProperties(src, dst));
799}
800
802{
803 SDL::CopyProperties(m_resource, dst);
804}
805
828{
829 CheckError(SDL_LockProperties(props));
830}
831
832inline void Properties::Lock() { SDL::LockProperties(m_resource); }
833
846{
847 SDL_UnlockProperties(props);
848}
849
850inline void Properties::Unlock() { SDL::UnlockProperties(m_resource); }
851
882 StringParam name,
883 void* value,
885 void* userdata)
886{
888 SDL_SetPointerPropertyWithCleanup(props, name, value, cleanup, userdata));
889}
890
920 StringParam name,
921 void* value,
922 CleanupPropertyCB cleanup)
923{
925 SDL_SetPointerPropertyWithCleanup(props,
926 std::move(name),
927 value,
928 &Wrapper::CallOnce,
929 Wrapper::Wrap(std::move(cleanup)));
930}
931
933 StringParam name,
934 void* value,
936 void* userdata)
937{
939 m_resource, std::move(name), value, cleanup, userdata);
940}
941
943 void* value,
944 CleanupPropertyCB cleanup)
945{
947 m_resource, std::move(name), value, cleanup);
948}
949
972 StringParam name,
973 void* value)
974{
975 CheckError(SDL_SetPointerProperty(props, name, value));
976}
977
978inline void Properties::SetPointerProperty(StringParam name, void* value)
979{
980 SDL::SetPointerProperty(m_resource, std::move(name), value);
981}
982
1002 StringParam name,
1003 StringParam value)
1004{
1005 CheckError(SDL_SetStringProperty(props, name, value));
1006}
1007
1009{
1010 SDL::SetStringProperty(m_resource, std::move(name), std::move(value));
1011}
1012
1028 StringParam name,
1029 Sint64 value)
1030{
1031 CheckError(SDL_SetNumberProperty(props, name, value));
1032}
1033
1035{
1036 SDL::SetNumberProperty(m_resource, std::move(name), value);
1037}
1038
1054 StringParam name,
1055 float value)
1056{
1057 CheckError(SDL_SetFloatProperty(props, name, value));
1058}
1059
1060inline void Properties::SetFloatProperty(StringParam name, float value)
1061{
1062 SDL::SetFloatProperty(m_resource, std::move(name), value);
1063}
1064
1080 StringParam name,
1081 bool value)
1082{
1083 CheckError(SDL_SetBooleanProperty(props, name, value));
1084}
1085
1086inline void Properties::SetBooleanProperty(StringParam name, bool value)
1087{
1088 SDL::SetBooleanProperty(m_resource, std::move(name), value);
1089}
1090
1105{
1106 return SDL_HasProperty(props, name);
1107}
1108
1110{
1111 return SDL::HasProperty(m_resource, std::move(name));
1112}
1113
1128{
1129 return SDL_GetPropertyType(props, name);
1130}
1131
1133{
1134 return SDL::GetPropertyType(m_resource, std::move(name));
1135}
1136
1169 StringParam name,
1170 void* default_value)
1171{
1172 return SDL_GetPointerProperty(props, name, default_value);
1173}
1174
1176 void* default_value)
1177{
1178 return SDL::GetPointerProperty(m_resource, std::move(name), default_value);
1179}
1180
1203inline const char* GetStringProperty(PropertiesParam props,
1204 StringParam name,
1205 StringParam default_value)
1206{
1207 return SDL_GetStringProperty(props, name, default_value);
1208}
1209
1211 StringParam default_value)
1212{
1214 m_resource, std::move(name), std::move(default_value));
1215}
1216
1238 StringParam name,
1239 Sint64 default_value)
1240{
1241 return SDL_GetNumberProperty(props, name, default_value);
1242}
1243
1245 Sint64 default_value)
1246{
1247 return SDL::GetNumberProperty(m_resource, std::move(name), default_value);
1248}
1249
1271 StringParam name,
1272 float default_value)
1273{
1274 return SDL_GetFloatProperty(props, name, default_value);
1275}
1276
1277inline float Properties::GetFloatProperty(StringParam name, float default_value)
1278{
1279 return SDL::GetFloatProperty(m_resource, std::move(name), default_value);
1280}
1281
1303 StringParam name,
1304 bool default_value)
1305{
1306 return SDL_GetBooleanProperty(props, name, default_value);
1307}
1308
1309inline bool Properties::GetBooleanProperty(StringParam name, bool default_value)
1310{
1311 return SDL::GetBooleanProperty(m_resource, std::move(name), default_value);
1312}
1313
1326{
1327 CheckError(SDL_ClearProperty(props, name));
1328}
1329
1331{
1332 SDL::ClearProperty(m_resource, std::move(name));
1333}
1334
1352 void* userdata)
1353{
1354 CheckError(SDL_EnumerateProperties(props, callback, userdata));
1355}
1356
1372 EnumeratePropertiesCB callback)
1373{
1374 return EnumerateProperties(
1375 props,
1376 [](void* userdata, PropertiesID props, const char* name) {
1377 auto& f = *static_cast<EnumeratePropertiesCB*>(userdata);
1378 f(props, name);
1379 },
1380 &callback);
1381}
1382
1384 void* userdata)
1385{
1386 SDL::EnumerateProperties(m_resource, callback, userdata);
1387}
1388
1390{
1391 SDL::EnumerateProperties(m_resource, callback);
1392}
1393
1403{
1404 Uint64 count = 0;
1405 EnumerateProperties(props, [&](auto, const char*) { count++; });
1406 return count;
1407}
1408
1410{
1411 return SDL::CountProperties(m_resource);
1412}
1413
1431{
1432 SDL_DestroyProperties(props);
1433}
1434
1436
1438
1439} // namespace SDL
1440
1441#endif /* SDL3PP_PROPERTIES_H_ */
SDL properties ID.
Definition: SDL3pp_properties.h:202
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:257
~Properties()
Destructor.
Definition: SDL3pp_properties.h:254
constexpr Properties(const PropertiesID resource) noexcept
Constructs from PropertiesParam.
Definition: SDL3pp_properties.h:219
constexpr PropertiesID release() noexcept
Retrieves underlying PropertiesID and clear this.
Definition: SDL3pp_properties.h:272
constexpr auto operator<=>(const Properties &other) const noexcept=default
Comparison.
constexpr Properties(Properties &&other) noexcept
Move constructor.
Definition: SDL3pp_properties.h:228
constexpr Properties(std::nullptr_t=nullptr) noexcept
Default ctor.
Definition: SDL3pp_properties.h:207
constexpr PropertiesID get() const noexcept
Retrieves underlying PropertiesID.
Definition: SDL3pp_properties.h:269
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:98
void UnlockProperties(PropertiesParam props)
Unlock a group of properties.
Definition: SDL3pp_properties.h:845
static Properties Create()
Create a group of properties.
Definition: SDL3pp_properties.h:778
Sint64 GetNumberProperty(StringParam name, Sint64 default_value)
Get a number property from a group of properties.
Definition: SDL3pp_properties.h:1244
std::function< void(void *value)> CleanupPropertyCB
A callback used to free resources when a property is deleted.
Definition: SDL3pp_properties.h:190
Sint64 GetNumberProperty(PropertiesParam props, StringParam name, Sint64 default_value)
Get a number property from a group of properties.
Definition: SDL3pp_properties.h:1237
void SetBooleanProperty(PropertiesParam props, StringParam name, bool value)
Set a boolean property in a group of properties.
Definition: SDL3pp_properties.h:1079
void LockProperties(PropertiesParam props)
Lock a group of properties.
Definition: SDL3pp_properties.h:827
SDL_PropertyType PropertyType
SDL property type.
Definition: SDL3pp_properties.h:82
void SetFloatProperty(StringParam name, float value)
Set a floating point property in a group of properties.
Definition: SDL3pp_properties.h:1060
void Lock()
Lock a group of properties.
Definition: SDL3pp_properties.h:832
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:932
float GetFloatProperty(StringParam name, float default_value)
Get a floating point property from a group of properties.
Definition: SDL3pp_properties.h:1277
void SetNumberProperty(StringParam name, Sint64 value)
Set an integer property in a group of properties.
Definition: SDL3pp_properties.h:1034
void Unlock()
Unlock a group of properties.
Definition: SDL3pp_properties.h:850
Uint64 CountProperties(PropertiesParam props)
Returns the number of properties this has.
Definition: SDL3pp_properties.h:1402
const char * GetStringProperty(StringParam name, StringParam default_value)
Get a string property from a group of properties.
Definition: SDL3pp_properties.h:1210
bool HasProperty(PropertiesParam props, StringParam name)
Return whether a property exists in a group of properties.
Definition: SDL3pp_properties.h:1104
float GetFloatProperty(PropertiesParam props, StringParam name, float default_value)
Get a floating point property from a group of properties.
Definition: SDL3pp_properties.h:1270
void ClearProperty(PropertiesParam props, StringParam name)
Clear a property from a group of properties.
Definition: SDL3pp_properties.h:1325
bool GetBooleanProperty(StringParam name, bool default_value)
Get a boolean property from a group of properties.
Definition: SDL3pp_properties.h:1309
void SetStringProperty(StringParam name, StringParam value)
Set a string property in a group of properties.
Definition: SDL3pp_properties.h:1008
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:139
void Copy(PropertiesParam dst)
Copy a group of properties.
Definition: SDL3pp_properties.h:801
Uint64 GetCount()
Returns the number of properties this has.
Definition: SDL3pp_properties.h:1409
void * GetPointerProperty(PropertiesParam props, StringParam name, void *default_value)
Get a pointer property from a group of properties.
Definition: SDL3pp_properties.h:1168
PropertyType GetPropertyType(PropertiesParam props, StringParam name)
Get the type of a property in a group of properties.
Definition: SDL3pp_properties.h:1127
bool HasProperty(StringParam name)
Return whether a property exists in a group of properties.
Definition: SDL3pp_properties.h:1109
void * GetPointerProperty(StringParam name, void *default_value)
Get a pointer property from a group of properties.
Definition: SDL3pp_properties.h:1175
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:881
void SetBooleanProperty(StringParam name, bool value)
Set a boolean property in a group of properties.
Definition: SDL3pp_properties.h:1086
void CopyProperties(PropertiesParam src, PropertiesParam dst)
Copy a group of properties.
Definition: SDL3pp_properties.h:796
Properties CreateProperties()
Create a group of properties.
Definition: SDL3pp_properties.h:773
constexpr PropertyType PROPERTY_TYPE_NUMBER
NUMBER.
Definition: SDL3pp_properties.h:93
constexpr PropertyType PROPERTY_TYPE_POINTER
POINTER.
Definition: SDL3pp_properties.h:87
void SetPointerProperty(PropertiesParam props, StringParam name, void *value)
Set a pointer property in a group of properties.
Definition: SDL3pp_properties.h:971
void(SDLCALL *)(void *userdata, void *value) CleanupPropertyCallback
A callback used to free resources when a property is deleted.
Definition: SDL3pp_properties.h:164
const char * GetStringProperty(PropertiesParam props, StringParam name, StringParam default_value)
Get a string property from a group of properties.
Definition: SDL3pp_properties.h:1203
constexpr PropertyType PROPERTY_TYPE_INVALID
INVALID.
Definition: SDL3pp_properties.h:84
constexpr PropertyType PROPERTY_TYPE_FLOAT
FLOAT.
Definition: SDL3pp_properties.h:96
void Enumerate(EnumeratePropertiesCallback callback, void *userdata)
Enumerate the properties contained in a group of properties.
Definition: SDL3pp_properties.h:1383
void SetNumberProperty(PropertiesParam props, StringParam name, Sint64 value)
Set an integer property in a group of properties.
Definition: SDL3pp_properties.h:1027
constexpr PropertyType PROPERTY_TYPE_STRING
STRING.
Definition: SDL3pp_properties.h:90
PropertyType GetPropertyType(StringParam name)
Get the type of a property in a group of properties.
Definition: SDL3pp_properties.h:1132
void SetFloatProperty(PropertiesParam props, StringParam name, float value)
Set a floating point property in a group of properties.
Definition: SDL3pp_properties.h:1053
void SetPointerProperty(StringParam name, void *value)
Set a pointer property in a group of properties.
Definition: SDL3pp_properties.h:978
void Destroy()
Destroy a group of properties.
Definition: SDL3pp_properties.h:1435
void SetStringProperty(PropertiesParam props, StringParam name, StringParam value)
Set a string property in 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:120
SDL_PropertiesID PropertiesID
Alias to raw representation for Properties.
Definition: SDL3pp_properties.h:45
bool GetBooleanProperty(PropertiesParam props, StringParam name, bool default_value)
Get a boolean property from a group of properties.
Definition: SDL3pp_properties.h:1302
void ClearProperty(StringParam name)
Clear a property from a group of properties.
Definition: SDL3pp_properties.h:1330
void EnumerateProperties(PropertiesParam props, EnumeratePropertiesCallback callback, void *userdata)
Enumerate the properties contained in a group of properties.
Definition: SDL3pp_properties.h:1350
void DestroyProperties(PropertiesID props)
Destroy a group of properties.
Definition: SDL3pp_properties.h:1430
PropertiesRef GetGlobalProperties()
Get the global SDL properties.
Definition: SDL3pp_properties.h:754
::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:52
constexpr PropertiesParam(std::nullptr_t _=nullptr)
Constructs null/invalid.
Definition: SDL3pp_properties.h:62
constexpr auto operator<=>(const PropertiesParam &other) const =default
Comparison.
constexpr PropertiesParam(PropertiesID value)
Constructs from PropertiesID.
Definition: SDL3pp_properties.h:56
PropertiesID value
parameter's PropertiesID
Definition: SDL3pp_properties.h:53
Semi-safe reference for Properties.
Definition: SDL3pp_properties.h:708
PropertiesRef(const PropertiesRef &other) noexcept
Copy constructor.
Definition: SDL3pp_properties.h:736
PropertiesRef(PropertiesParam resource) noexcept
Constructs from PropertiesParam.
Definition: SDL3pp_properties.h:718
~PropertiesRef()
Destructor.
Definition: SDL3pp_properties.h:742
PropertiesRef(PropertiesID resource) noexcept
Constructs from PropertiesParam.
Definition: SDL3pp_properties.h:730