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_error.h"
6#include "SDL3pp_stdinc.h"
7
8namespace SDL {
9
45using PropertyType = SDL_PropertyType;
46
74using CleanupPropertyCallback = SDL_CleanupPropertyCallback;
75
85using CleanupPropertyCB = std::function<void(void*)>;
86
88
110using EnumeratePropertiesCallback = SDL_EnumeratePropertiesCallback;
111
112// Forward decl
113struct PropertiesRef;
114
136 std::function<void(PropertiesRef props, const char* name)>;
137
139
140// Forward decl
141struct PropertiesLock;
142
143// Forward decl
144struct PropertiesBase;
145
146// Forward decl
147struct Properties;
148
150 SDL_PROPERTY_TYPE_INVALID;
151
153 SDL_PROPERTY_TYPE_POINTER;
154
156 SDL_PROPERTY_TYPE_STRING;
157
159 SDL_PROPERTY_TYPE_NUMBER;
160
161constexpr PropertyType PROPERTY_TYPE_FLOAT = SDL_PROPERTY_TYPE_FLOAT;
162
164 SDL_PROPERTY_TYPE_BOOLEAN;
165
202struct PropertiesBase : Resource<SDL_PropertiesID>
203{
204 using Resource::Resource;
205
222 {
223 CheckError(SDL_CopyProperties(get(), dst.get()));
224 }
225
248
274 void* value,
275 CleanupPropertyCB cleanup)
276 {
278 SetPointerWithCleanup(std::move(name),
279 value,
280 &Wrapper::CallOnce,
281 Wrapper::Wrap(std::move(cleanup)));
282 }
283
313 void* value,
315 void* userdata)
316 {
318 SDL_SetPointerPropertyWithCleanup(get(), name, value, cleanup, userdata));
319 }
320
341 void SetPointer(StringParam name, void* value)
342 {
343 CheckError(SDL_SetPointerProperty(get(), name, value));
344 }
345
364 {
365 CheckError(SDL_SetStringProperty(get(), name, value));
366 }
367
381 void SetNumber(StringParam name, Sint64 value)
382 {
383 CheckError(SDL_SetNumberProperty(get(), name, value));
384 }
385
399 void SetFloat(StringParam name, float value)
400 {
401 CheckError(SDL_SetFloatProperty(get(), name, value));
402 }
403
417 void SetBoolean(StringParam name, bool value)
418 {
419 CheckError(SDL_SetBooleanProperty(get(), name, value));
420 }
421
434 bool Has(StringParam name) const { return SDL_HasProperty(get(), name); }
435
450 {
451 return SDL_GetPropertyType(get(), name);
452 }
453
487 void* GetPointer(StringParam name, void* default_value) const
488 {
489 return SDL_GetPointerProperty(get(), name, default_value);
490 }
491
521 const char* GetString(StringParam name, StringParam default_value) const
522 {
523 return SDL_GetStringProperty(get(), name, default_value);
524 }
525
550 Sint64 GetNumber(StringParam name, Sint64 default_value) const
551 {
552 return SDL_GetNumberProperty(get(), name, default_value);
553 }
554
579 float GetFloat(StringParam name, float default_value) const
580 {
581 return SDL_GetFloatProperty(get(), name, default_value);
582 }
583
608 bool GetBoolean(StringParam name, bool default_value) const
609 {
610 return SDL_GetBooleanProperty(get(), name, default_value);
611 }
612
623 void Clear(StringParam name) { CheckError(SDL_ClearProperty(get(), name)); }
624
633 template<std::output_iterator<const char*> IT>
634 void Enumerate(IT outputIter) const
635 {
636 Enumerate(
637 [&outputIter](auto props, const char name) { *outputIter++ = name; });
638 }
639
655 void Enumerate(EnumeratePropertiesCB callback) const;
656
671 void Enumerate(EnumeratePropertiesCallback callback, void* userdata) const
672 {
673 CheckError(SDL_EnumerateProperties(get(), callback, userdata));
674 }
675
681 Uint64 GetCount() const;
682};
683
693{
694 using PropertiesBase::PropertiesBase;
695
699 constexpr PropertiesRef(const PropertiesRef& other)
700 : PropertiesBase(other.get())
701 {
702 }
703
707 constexpr PropertiesRef(PropertiesRef&& other)
708 : PropertiesBase(other.release())
709 {
710 }
711
715 constexpr ~PropertiesRef() = default;
716
721 {
722 release(other.release());
723 return *this;
724 }
725
741 void reset(SDL_PropertiesID newResource = {})
742 {
743 SDL_DestroyProperties(release(newResource));
744 }
745};
746
756{
758
762 constexpr explicit Properties(SDL_PropertiesID resource = {})
763 : PropertiesRef(resource)
764 {
765 }
766
767 constexpr Properties(const Properties& other) = delete;
768
772 constexpr Properties(Properties&& other) = default;
773
789 : Properties(CheckError(SDL_CreateProperties()))
790 {
791 }
792
797
802 {
803 reset(other.release());
804 return *this;
805 }
806};
807
813{
814 PropertiesRef properties;
815
819 explicit PropertiesLock(PropertiesRef properties)
820 : properties(properties)
821 {
822 }
823
824public:
826 constexpr PropertiesLock() = default;
827
828 PropertiesLock(const PropertiesLock& other) = delete;
829
832 : properties(std::move(other.properties))
833 {
834 }
835
840
843 {
844 std::swap(properties, other.properties);
845 return *this;
846 }
847
851 constexpr operator bool() const { return bool(properties); }
852
862 void Unlock() { SDL_UnlockProperties(properties.release()); }
863
864 friend class PropertiesBase;
865};
866
876{
877 return CheckError(SDL_GetGlobalProperties());
878}
879
881{
882 CheckError(SDL_LockProperties(get()));
883 return PropertiesLock{get()};
884}
885
901{
902 return Properties{SDL_CreateProperties()};
903}
904
905#pragma region impl
907
909{
910 return Enumerate(
911 [](void* userdata, SDL_PropertiesID props, const char* name) {
912 auto& f = *static_cast<EnumeratePropertiesCB*>(userdata);
913 f(props, name);
914 },
915 &callback);
916}
917
918inline Uint64 PropertiesBase::GetCount() const
919{
920 Uint64 count = 0;
921 Enumerate([&](auto, const char*) { count++; });
922 return count;
923}
924
925#pragma endregion impl
926
927} // namespace SDL
928
929#endif /* SDL3PP_PROPERTIES_H_ */
Wrap the lock state for PropertiesBase.
Definition SDL3pp_properties.h:813
~PropertiesLock()
Definition SDL3pp_properties.h:839
constexpr PropertiesLock()=default
Default ctor.
void Unlock()
Unlock a group of properties.
Definition SDL3pp_properties.h:862
PropertiesLock(PropertiesLock &&other)
Move ctor.
Definition SDL3pp_properties.h:831
PropertiesLock & operator=(PropertiesLock other)
Assignment operator.
Definition SDL3pp_properties.h:842
A SDL managed resource.
Definition SDL3pp_resource.h:17
constexpr SDL_PropertiesID release(SDL_PropertiesID newResource={})
Return contained resource and empties or replace value.
Definition SDL3pp_resource.h:60
constexpr Resource(T resource={})
Constructs the underlying resource.
Definition SDL3pp_resource.h:22
constexpr SDL_PropertiesID get() const
Return contained resource;.
Definition SDL3pp_resource.h:57
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:206
constexpr PropertyType PROPERTY_TYPE_BOOLEAN
BOOLEAN.
Definition SDL3pp_properties.h:163
SDL_PropertyType PropertyType
SDL property type.
Definition SDL3pp_properties.h:45
SDL_EnumeratePropertiesCallback EnumeratePropertiesCallback
A callback used to enumerate all the properties in a group of properties.
Definition SDL3pp_properties.h:110
Properties CreateProperties()
Create a group of properties.
Definition SDL3pp_properties.h:900
constexpr PropertyType PROPERTY_TYPE_NUMBER
NUMBER.
Definition SDL3pp_properties.h:158
constexpr PropertyType PROPERTY_TYPE_POINTER
POINTER.
Definition SDL3pp_properties.h:152
PropertiesLock Lock() &
Lock a group of properties.
Definition SDL3pp_properties.h:880
constexpr PropertyType PROPERTY_TYPE_INVALID
INVALID.
Definition SDL3pp_properties.h:149
constexpr PropertyType PROPERTY_TYPE_FLOAT
FLOAT.
Definition SDL3pp_properties.h:161
constexpr PropertyType PROPERTY_TYPE_STRING
STRING.
Definition SDL3pp_properties.h:155
std::function< void(void *)> CleanupPropertyCB
A callback used to free resources when a property is deleted.
Definition SDL3pp_properties.h:85
std::function< void(PropertiesRef props, const char *name)> EnumeratePropertiesCB
A callback used to enumerate all the properties in a group of properties.
Definition SDL3pp_properties.h:136
SDL_CleanupPropertyCallback CleanupPropertyCallback
A callback used to free resources when a property is deleted.
Definition SDL3pp_properties.h:74
PropertiesRef GetGlobalProperties()
Get the global SDL properties.
Definition SDL3pp_properties.h:875
the main namespace where all SDL3pp public functions and types live
Definition SDL3pp_assert.h:7
Definition SDL3pp_callbackWrapper.h:66
Wrap properties id.
Definition SDL3pp_properties.h:203
Uint64 GetCount() const
Returns the number of properties this has.
Definition SDL3pp_properties.h:918
const char * GetString(StringParam name, StringParam default_value) const
Get a string property from a group of properties.
Definition SDL3pp_properties.h:521
void Clear(StringParam name)
Clear a property from a group of properties.
Definition SDL3pp_properties.h:623
void SetFloat(StringParam name, float value)
Set a floating point property in a group of properties.
Definition SDL3pp_properties.h:399
void SetPointer(StringParam name, void *value)
Set a pointer property in a group of properties.
Definition SDL3pp_properties.h:341
void CopyPropertiesTo(PropertiesBase &dst) const
Copy a group of properties.
Definition SDL3pp_properties.h:221
void SetBoolean(StringParam name, bool value)
Set a boolean property in a group of properties.
Definition SDL3pp_properties.h:417
void SetPointerWithCleanup(StringParam name, void *value, CleanupPropertyCB cleanup)
Set a pointer property in a group of properties with a cleanup function that is called when the prope...
Definition SDL3pp_properties.h:273
float GetFloat(StringParam name, float default_value) const
Get a floating point property from a group of properties.
Definition SDL3pp_properties.h:579
void SetString(StringParam name, StringParam value)
Set a string property in a group of properties.
Definition SDL3pp_properties.h:363
void SetNumber(StringParam name, Sint64 value)
Set an integer property in a group of properties.
Definition SDL3pp_properties.h:381
void SetPointerWithCleanup(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:312
void Enumerate(IT outputIter) const
Enumerate the properties contained in a group of properties.
Definition SDL3pp_properties.h:634
Sint64 GetNumber(StringParam name, Sint64 default_value) const
Get a number property from a group of properties.
Definition SDL3pp_properties.h:550
bool Has(StringParam name) const
Return whether a property exists in a group of properties.
Definition SDL3pp_properties.h:434
void * GetPointer(StringParam name, void *default_value) const
Get a pointer property from a group of properties.
Definition SDL3pp_properties.h:487
void Enumerate(EnumeratePropertiesCallback callback, void *userdata) const
Enumerate the properties contained in a group of properties.
Definition SDL3pp_properties.h:671
bool GetBoolean(StringParam name, bool default_value) const
Get a boolean property from a group of properties.
Definition SDL3pp_properties.h:608
PropertyType GetType(StringParam name) const
Get the type of a property.
Definition SDL3pp_properties.h:449
Handle to a non owned properties.
Definition SDL3pp_properties.h:693
void reset(SDL_PropertiesID newResource={})
Destroy a group of properties.
Definition SDL3pp_properties.h:741
PropertiesRef & operator=(PropertiesRef other)
Assignment operator.
Definition SDL3pp_properties.h:720
constexpr PropertiesRef(const PropertiesRef &other)
Copy constructor.
Definition SDL3pp_properties.h:699
constexpr ~PropertiesRef()=default
Default constructor.
constexpr PropertiesRef(PropertiesRef &&other)
Move constructor.
Definition SDL3pp_properties.h:707
Handle to an owned properties.
Definition SDL3pp_properties.h:756
Properties & operator=(Properties other)
Assignment operator.
Definition SDL3pp_properties.h:801
constexpr Properties(Properties &&other)=default
Move constructor.
~Properties()
Frees up resource when object goes out of scope.
Definition SDL3pp_properties.h:796
Properties()
Create a group of properties.
Definition SDL3pp_properties.h:788
constexpr Properties(SDL_PropertiesID resource={})
Constructs from the underlying resource.
Definition SDL3pp_properties.h:762