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 = SDL_EnumeratePropertiesCallback;
119
137 std::function<void(PropertiesID props, const char* name)>;
138
162using CleanupPropertyCallback = SDL_CleanupPropertyCallback;
163
187using CleanupPropertyCB = std::function<void(void*)>;
188
199{
200 PropertiesID m_resource = 0;
201
202public:
204 constexpr Properties() = default;
205
213 constexpr explicit Properties(const PropertiesID resource)
214 : m_resource(resource)
215 {
216 }
217
219 constexpr Properties(const Properties& other) = delete;
220
222 constexpr Properties(Properties&& other)
223 : Properties(other.release())
224 {
225 }
226
227 constexpr Properties(const PropertiesRef& other) = delete;
228
229 constexpr Properties(PropertiesRef&& other) = delete;
230
245 static Properties Create();
246
248 ~Properties() { SDL_DestroyProperties(m_resource); }
249
252 {
253 std::swap(m_resource, other.m_resource);
254 return *this;
255 }
256
258 constexpr PropertiesID get() const { return m_resource; }
259
262 {
263 auto r = m_resource;
264 m_resource = 0;
265 return r;
266 }
267
269 constexpr auto operator<=>(const Properties& other) const = default;
270
272 constexpr bool operator==(std::nullptr_t _) const { return !m_resource; }
273
275 constexpr explicit operator bool() const { return !!m_resource; }
276
278 constexpr operator PropertiesParam() const { return {m_resource}; }
279
294 void Destroy();
295
311 void Copy(PropertiesParam dst);
312
333 void Lock();
334
345 void Unlock();
346
377 void* value,
379 void* userdata);
380
410 void* value,
411 CleanupPropertyCB cleanup);
412
433 void SetPointerProperty(StringParam name, void* value);
434
452 void SetStringProperty(StringParam name, StringParam value);
453
467 void SetNumberProperty(StringParam name, Sint64 value);
468
482 void SetFloatProperty(StringParam name, float value);
483
497 void SetBooleanProperty(StringParam name, bool value);
498
511 bool HasProperty(StringParam name);
512
527
558 void* GetPointerProperty(StringParam name, void* default_value);
559
581 const char* GetStringProperty(StringParam name, StringParam default_value);
582
602 Sint64 GetNumberProperty(StringParam name, Sint64 default_value);
603
623 float GetFloatProperty(StringParam name, float default_value);
624
644 bool GetBooleanProperty(StringParam name, bool default_value);
645
656 void ClearProperty(StringParam name);
657
672 void Enumerate(EnumeratePropertiesCallback callback, void* userdata);
673
687 void Enumerate(EnumeratePropertiesCB callback);
688
697};
698
701{
710 : Properties(resource.value)
711 {
712 }
713
716 : Properties(other.get())
717 {
718 }
719
722};
723
733{
734 return {CheckError(SDL_GetGlobalProperties())};
735}
736
752{
753 return Properties(CheckError(SDL_CreateProperties()));
754}
755
757
775{
776 CheckError(SDL_CopyProperties(src, dst));
777}
778
780{
781 SDL::CopyProperties(m_resource, dst);
782}
783
806{
807 CheckError(SDL_LockProperties(props));
808}
809
810inline void Properties::Lock() { SDL::LockProperties(m_resource); }
811
824{
825 SDL_UnlockProperties(props);
826}
827
828inline void Properties::Unlock() { SDL::UnlockProperties(m_resource); }
829
860 StringParam name,
861 void* value,
863 void* userdata)
864{
866 SDL_SetPointerPropertyWithCleanup(props, name, value, cleanup, userdata));
867}
868
898 StringParam name,
899 void* value,
900 CleanupPropertyCB cleanup)
901{
903 SDL_SetPointerPropertyWithCleanup(props,
904 std::move(name),
905 value,
906 &Wrapper::CallOnce,
907 Wrapper::Wrap(std::move(cleanup)));
908}
909
911 StringParam name,
912 void* value,
914 void* userdata)
915{
917 m_resource, std::move(name), value, cleanup, userdata);
918}
919
921 void* value,
922 CleanupPropertyCB cleanup)
923{
925 m_resource, std::move(name), value, cleanup);
926}
927
950 StringParam name,
951 void* value)
952{
953 CheckError(SDL_SetPointerProperty(props, name, value));
954}
955
956inline void Properties::SetPointerProperty(StringParam name, void* value)
957{
958 SDL::SetPointerProperty(m_resource, std::move(name), value);
959}
960
980 StringParam name,
981 StringParam value)
982{
983 CheckError(SDL_SetStringProperty(props, name, value));
984}
985
987{
988 SDL::SetStringProperty(m_resource, std::move(name), std::move(value));
989}
990
1006 StringParam name,
1007 Sint64 value)
1008{
1009 CheckError(SDL_SetNumberProperty(props, name, value));
1010}
1011
1013{
1014 SDL::SetNumberProperty(m_resource, std::move(name), value);
1015}
1016
1032 StringParam name,
1033 float value)
1034{
1035 CheckError(SDL_SetFloatProperty(props, name, value));
1036}
1037
1038inline void Properties::SetFloatProperty(StringParam name, float value)
1039{
1040 SDL::SetFloatProperty(m_resource, std::move(name), value);
1041}
1042
1058 StringParam name,
1059 bool value)
1060{
1061 CheckError(SDL_SetBooleanProperty(props, name, value));
1062}
1063
1064inline void Properties::SetBooleanProperty(StringParam name, bool value)
1065{
1066 SDL::SetBooleanProperty(m_resource, std::move(name), value);
1067}
1068
1083{
1084 return SDL_HasProperty(props, name);
1085}
1086
1088{
1089 return SDL::HasProperty(m_resource, std::move(name));
1090}
1091
1107{
1108 return SDL_GetPropertyType(props, name);
1109}
1110
1112{
1113 return SDL::GetPropertyType(m_resource, std::move(name));
1114}
1115
1148 StringParam name,
1149 void* default_value)
1150{
1151 return SDL_GetPointerProperty(props, name, default_value);
1152}
1153
1155 void* default_value)
1156{
1157 return SDL::GetPointerProperty(m_resource, std::move(name), default_value);
1158}
1159
1182inline const char* GetStringProperty(PropertiesParam props,
1183 StringParam name,
1184 StringParam default_value)
1185{
1186 return SDL_GetStringProperty(props, name, default_value);
1187}
1188
1190 StringParam default_value)
1191{
1193 m_resource, std::move(name), std::move(default_value));
1194}
1195
1217 StringParam name,
1218 Sint64 default_value)
1219{
1220 return SDL_GetNumberProperty(props, name, default_value);
1221}
1222
1224 Sint64 default_value)
1225{
1226 return SDL::GetNumberProperty(m_resource, std::move(name), default_value);
1227}
1228
1250 StringParam name,
1251 float default_value)
1252{
1253 return SDL_GetFloatProperty(props, name, default_value);
1254}
1255
1256inline float Properties::GetFloatProperty(StringParam name, float default_value)
1257{
1258 return SDL::GetFloatProperty(m_resource, std::move(name), default_value);
1259}
1260
1282 StringParam name,
1283 bool default_value)
1284{
1285 return SDL_GetBooleanProperty(props, name, default_value);
1286}
1287
1288inline bool Properties::GetBooleanProperty(StringParam name, bool default_value)
1289{
1290 return SDL::GetBooleanProperty(m_resource, std::move(name), default_value);
1291}
1292
1305{
1306 CheckError(SDL_ClearProperty(props, name));
1307}
1308
1310{
1311 SDL::ClearProperty(m_resource, std::move(name));
1312}
1313
1331 void* userdata)
1332{
1333 CheckError(SDL_EnumerateProperties(props, callback, userdata));
1334}
1335
1351 EnumeratePropertiesCB callback)
1352{
1353 return EnumerateProperties(
1354 props,
1355 [](void* userdata, PropertiesID props, const char* name) {
1356 auto& f = *static_cast<EnumeratePropertiesCB*>(userdata);
1357 f(props, name);
1358 },
1359 &callback);
1360}
1361
1363 void* userdata)
1364{
1365 SDL::EnumerateProperties(m_resource, callback, userdata);
1366}
1367
1369{
1370 SDL::EnumerateProperties(m_resource, callback);
1371}
1372
1382{
1383 Uint64 count = 0;
1384 EnumerateProperties(props, [&](auto, const char*) { count++; });
1385 return count;
1386}
1387
1389{
1390 return SDL::CountProperties(m_resource);
1391}
1392
1410{
1411 SDL_DestroyProperties(props);
1412}
1413
1415
1417
1418} // namespace SDL
1419
1420#endif /* SDL3PP_PROPERTIES_H_ */
SDL properties ID.
Definition: SDL3pp_properties.h:199
constexpr Properties(const Properties &other)=delete
Copy constructor.
constexpr PropertiesID get() const
Retrieves underlying PropertiesID.
Definition: SDL3pp_properties.h:258
Properties & operator=(Properties other)
Assignment operator.
Definition: SDL3pp_properties.h:251
constexpr auto operator<=>(const Properties &other) const =default
Comparison.
constexpr Properties(const PropertiesID resource)
Constructs from PropertiesParam.
Definition: SDL3pp_properties.h:213
constexpr Properties(Properties &&other)
Move constructor.
Definition: SDL3pp_properties.h:222
~Properties()
Destructor.
Definition: SDL3pp_properties.h:248
constexpr bool operator==(std::nullptr_t _) const
Comparison.
Definition: SDL3pp_properties.h:272
constexpr PropertiesID release()
Retrieves underlying PropertiesID and clear this.
Definition: SDL3pp_properties.h:261
constexpr Properties()=default
Default ctor.
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:198
constexpr PropertyType PROPERTY_TYPE_BOOLEAN
BOOLEAN.
Definition: SDL3pp_properties.h:98
void UnlockProperties(PropertiesParam props)
Unlock a group of properties.
Definition: SDL3pp_properties.h:823
static Properties Create()
Create a group of properties.
Definition: SDL3pp_properties.h:756
Sint64 GetNumberProperty(StringParam name, Sint64 default_value)
Get a number property from a group of properties.
Definition: SDL3pp_properties.h:1223
Sint64 GetNumberProperty(PropertiesParam props, StringParam name, Sint64 default_value)
Get a number property from a group of properties.
Definition: SDL3pp_properties.h:1216
void SetBooleanProperty(PropertiesParam props, StringParam name, bool value)
Set a boolean property in a group of properties.
Definition: SDL3pp_properties.h:1057
void LockProperties(PropertiesParam props)
Lock a group of properties.
Definition: SDL3pp_properties.h:805
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:1038
void Lock()
Lock a group of properties.
Definition: SDL3pp_properties.h:810
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:910
float GetFloatProperty(StringParam name, float default_value)
Get a floating point property from a group of properties.
Definition: SDL3pp_properties.h:1256
void SetNumberProperty(StringParam name, Sint64 value)
Set an integer property in a group of properties.
Definition: SDL3pp_properties.h:1012
void Unlock()
Unlock a group of properties.
Definition: SDL3pp_properties.h:828
Uint64 CountProperties(PropertiesParam props)
Returns the number of properties this has.
Definition: SDL3pp_properties.h:1381
const char * GetStringProperty(StringParam name, StringParam default_value)
Get a string property from a group of properties.
Definition: SDL3pp_properties.h:1189
bool HasProperty(PropertiesParam props, StringParam name)
Return whether a property exists in a group of properties.
Definition: SDL3pp_properties.h:1082
float GetFloatProperty(PropertiesParam props, StringParam name, float default_value)
Get a floating point property from a group of properties.
Definition: SDL3pp_properties.h:1249
void ClearProperty(PropertiesParam props, StringParam name)
Clear a property from a group of properties.
Definition: SDL3pp_properties.h:1304
bool GetBooleanProperty(StringParam name, bool default_value)
Get a boolean property from a group of properties.
Definition: SDL3pp_properties.h:1288
void SetStringProperty(StringParam name, StringParam value)
Set a string property in a group of properties.
Definition: SDL3pp_properties.h:986
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:137
void Copy(PropertiesParam dst)
Copy a group of properties.
Definition: SDL3pp_properties.h:779
SDL_EnumeratePropertiesCallback EnumeratePropertiesCallback
A callback used to enumerate all the properties in a group of properties.
Definition: SDL3pp_properties.h:118
Uint64 GetCount()
Returns the number of properties this has.
Definition: SDL3pp_properties.h:1388
void * GetPointerProperty(PropertiesParam props, StringParam name, void *default_value)
Get a pointer property from a group of properties.
Definition: SDL3pp_properties.h:1147
PropertyType GetPropertyType(PropertiesParam props, StringParam name)
Get the type of a property in a group of properties.
Definition: SDL3pp_properties.h:1106
bool HasProperty(StringParam name)
Return whether a property exists in a group of properties.
Definition: SDL3pp_properties.h:1087
void * GetPointerProperty(StringParam name, void *default_value)
Get a pointer property from a group of properties.
Definition: SDL3pp_properties.h:1154
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:859
void SetBooleanProperty(StringParam name, bool value)
Set a boolean property in a group of properties.
Definition: SDL3pp_properties.h:1064
void CopyProperties(PropertiesParam src, PropertiesParam dst)
Copy a group of properties.
Definition: SDL3pp_properties.h:774
Properties CreateProperties()
Create a group of properties.
Definition: SDL3pp_properties.h:751
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:949
const char * GetStringProperty(PropertiesParam props, StringParam name, StringParam default_value)
Get a string property from a group of properties.
Definition: SDL3pp_properties.h:1182
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:1362
void SetNumberProperty(PropertiesParam props, StringParam name, Sint64 value)
Set an integer property in a group of properties.
Definition: SDL3pp_properties.h:1005
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:1111
void SetFloatProperty(PropertiesParam props, StringParam name, float value)
Set a floating point property in a group of properties.
Definition: SDL3pp_properties.h:1031
std::function< void(void *)> CleanupPropertyCB
A callback used to free resources when a property is deleted.
Definition: SDL3pp_properties.h:187
void SetPointerProperty(StringParam name, void *value)
Set a pointer property in a group of properties.
Definition: SDL3pp_properties.h:956
void Destroy()
Destroy a group of properties.
Definition: SDL3pp_properties.h:1414
void SetStringProperty(PropertiesParam props, StringParam name, StringParam value)
Set a string property in a group of properties.
Definition: SDL3pp_properties.h:979
SDL_CleanupPropertyCallback CleanupPropertyCallback
A callback used to free resources when a property is deleted.
Definition: SDL3pp_properties.h:162
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:1281
void ClearProperty(StringParam name)
Clear a property from a group of properties.
Definition: SDL3pp_properties.h:1309
void EnumerateProperties(PropertiesParam props, EnumeratePropertiesCallback callback, void *userdata)
Enumerate the properties contained in a group of properties.
Definition: SDL3pp_properties.h:1329
void DestroyProperties(PropertiesID props)
Destroy a group of properties.
Definition: SDL3pp_properties.h:1409
PropertiesRef GetGlobalProperties()
Get the global SDL properties.
Definition: SDL3pp_properties.h:732
::Uint64 Uint64
An unsigned 64-bit integer type.
Definition: SDL3pp_stdinc.h:340
::Sint64 Sint64
A signed 64-bit integer type.
Definition: SDL3pp_stdinc.h:325
Main include header for the SDL3pp library.
Definition: SDL3pp_callbackWrapper.h:66
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:701
PropertiesRef(PropertiesParam resource)
Constructs from PropertiesParam.
Definition: SDL3pp_properties.h:709
PropertiesRef(const PropertiesRef &other)
Copy constructor.
Definition: SDL3pp_properties.h:715
~PropertiesRef()
Destructor.
Definition: SDL3pp_properties.h:721