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
101using CleanupPropertyCB = std::function<void(void*)>;
102
104
126using EnumeratePropertiesCallback = SDL_EnumeratePropertiesCallback;
127
128// Forward decl
129struct PropertiesRef;
130
151 std::function<void(PropertiesRef props, const char* name)>;
152
154
155// Forward decl
156struct Properties;
157
167
177
178// Forward decl
179struct PropertiesLock;
180
182 SDL_PROPERTY_TYPE_INVALID;
183
185 SDL_PROPERTY_TYPE_POINTER;
186
188 SDL_PROPERTY_TYPE_STRING;
189
191 SDL_PROPERTY_TYPE_NUMBER;
192
193constexpr PropertyType PROPERTY_TYPE_FLOAT = SDL_PROPERTY_TYPE_FLOAT;
194
196 SDL_PROPERTY_TYPE_BOOLEAN;
197
208struct PropertiesRef : Resource<SDL_PropertiesID>
209{
210 using Resource::Resource;
211
228 {
229 CheckError(SDL_CopyProperties(get(), dst.get()));
230 }
231
254
280 void* value,
281 CleanupPropertyCB cleanup)
282 {
284 SetPointerWithCleanup(std::move(name),
285 value,
286 &Wrapper::CallOnce,
287 Wrapper::Wrap(std::move(cleanup)));
288 }
289
319 void* value,
321 void* userdata)
322 {
324 SDL_SetPointerPropertyWithCleanup(get(), name, value, cleanup, userdata));
325 }
326
347 void SetPointer(StringParam name, void* value)
348 {
349 CheckError(SDL_SetPointerProperty(get(), name, value));
350 }
351
370 {
371 CheckError(SDL_SetStringProperty(get(), name, value));
372 }
373
387 void SetNumber(StringParam name, Sint64 value)
388 {
389 CheckError(SDL_SetNumberProperty(get(), name, value));
390 }
391
405 void SetFloat(StringParam name, float value)
406 {
407 CheckError(SDL_SetFloatProperty(get(), name, value));
408 }
409
423 void SetBoolean(StringParam name, bool value)
424 {
425 CheckError(SDL_SetBooleanProperty(get(), name, value));
426 }
427
440 bool Has(StringParam name) const { return SDL_HasProperty(get(), name); }
441
456 {
457 return SDL_GetPropertyType(get(), name);
458 }
459
493 void* GetPointer(StringParam name, void* default_value) const
494 {
495 return SDL_GetPointerProperty(get(), name, default_value);
496 }
497
527 const char* GetString(StringParam name, StringParam default_value) const
528 {
529 return SDL_GetStringProperty(get(), name, default_value);
530 }
531
556 Sint64 GetNumber(StringParam name, Sint64 default_value) const
557 {
558 return SDL_GetNumberProperty(get(), name, default_value);
559 }
560
585 float GetFloat(StringParam name, float default_value) const
586 {
587 return SDL_GetFloatProperty(get(), name, default_value);
588 }
589
614 bool GetBoolean(StringParam name, bool default_value) const
615 {
616 return SDL_GetBooleanProperty(get(), name, default_value);
617 }
618
629 void Clear(StringParam name) { CheckError(SDL_ClearProperty(get(), name)); }
630
639 template<std::output_iterator<const char*> IT>
640 void Enumerate(IT outputIter) const
641 {
642 Enumerate(
643 [&outputIter](auto props, const char name) { *outputIter++ = name; });
644 }
645
661 void Enumerate(EnumeratePropertiesCB callback) const;
662
677 void Enumerate(EnumeratePropertiesCallback callback, void* userdata) const
678 {
679 CheckError(SDL_EnumerateProperties(get(), callback, userdata));
680 }
681
687 Uint64 GetCount() const;
688
705 static void reset(SDL_PropertiesID resource)
706 {
707 SDL_DestroyProperties(resource);
708 }
709};
710
718struct Properties : ResourceUnique<PropertiesRef>
719{
721
737 {
738 return Properties(CheckError(SDL_CreateProperties()));
739 }
740
755 void Destroy() { reset(); }
760
761};
762
763
765{
766 return PropertiesShared(std::move(*this));
767}
768
778struct PropertiesUnsafe : ResourceUnsafe<PropertiesRef>
779{
781
785 constexpr explicit PropertiesUnsafe(Properties&& other)
786 : PropertiesUnsafe(other.release())
787 {
788 }
789};
790
795struct PropertiesLock : LockBase<PropertiesRef>
796{
800 constexpr PropertiesLock() = default;
801
806 : LockBase(other.release())
807 {
808 }
809
832 : LockBase<PropertiesRef>(std::move(props))
833 {
834 CheckError(SDL_LockProperties(get()));
835 }
836
843
853 void Unlock() { SDL_UnlockProperties(release()); }
854
858 void reset() { Unlock(); }
859};
860
870{
871 return CheckError(SDL_GetGlobalProperties());
872}
873
875{
876 CheckError(SDL_LockProperties(get()));
877 return PropertiesLock{get()};
878}
879
880#pragma region impl
882
884{
885 return Enumerate(
886 [](void* userdata, SDL_PropertiesID props, const char* name) {
887 auto& f = *static_cast<EnumeratePropertiesCB*>(userdata);
888 f(props, name);
889 },
890 &callback);
891}
892
893inline Uint64 PropertiesRef::GetCount() const
894{
895 Uint64 count = 0;
896 Enumerate([&](auto, const char*) { count++; });
897 return count;
898}
899
900#pragma endregion impl
901
902} // namespace SDL
903
904#endif /* SDL3PP_PROPERTIES_H_ */
Base class for locks.
Definition SDL3pp_resource.h:408
RESOURCE release()
Returns reference and reset this.
Definition SDL3pp_resource.h:178
reference & get()
Get reference.
Definition SDL3pp_resource.h:120
Implement shared ownership for a resource.
Definition SDL3pp_resource.h:283
Implement unique ownership for a resource.
Definition SDL3pp_resource.h:226
constexpr ResourceUnique(std::nullptr_t=nullptr)
Default constructor.
Definition SDL3pp_resource.h:231
void reset()
Resets the value, destroying the resource if not nullptr.
Definition SDL3pp_resource.h:265
A dumb pointer to resource.
Definition SDL3pp_resource.h:197
constexpr ResourceUnsafe()=default
Default constructor.
Implement weak ownership for a resource.
Definition SDL3pp_resource.h:328
A SDL managed resource.
Definition SDL3pp_resource.h:29
constexpr Resource(T resource={})
Constructs from the underlying resource.
Definition SDL3pp_resource.h:37
constexpr SDL_PropertiesID get() const
Return contained resource;.
Definition SDL3pp_resource.h:76
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:195
SDL_PropertyType PropertyType
SDL property type.
Definition SDL3pp_properties.h:45
ResourceShared< Properties > PropertiesShared
Handle to a shared properties.
Definition SDL3pp_properties.h:166
PropertiesLock Lock() &
Lock a group of properties.
Definition SDL3pp_properties.h:874
SDL_EnumeratePropertiesCallback EnumeratePropertiesCallback
A callback used to enumerate all the properties in a group of properties.
Definition SDL3pp_properties.h:126
constexpr PropertyType PROPERTY_TYPE_NUMBER
NUMBER.
Definition SDL3pp_properties.h:190
constexpr PropertyType PROPERTY_TYPE_POINTER
POINTER.
Definition SDL3pp_properties.h:184
constexpr PropertyType PROPERTY_TYPE_INVALID
INVALID.
Definition SDL3pp_properties.h:181
constexpr PropertyType PROPERTY_TYPE_FLOAT
FLOAT.
Definition SDL3pp_properties.h:193
constexpr PropertyType PROPERTY_TYPE_STRING
STRING.
Definition SDL3pp_properties.h:187
std::function< void(void *)> CleanupPropertyCB
A callback used to free resources when a property is deleted.
Definition SDL3pp_properties.h:101
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:151
SDL_CleanupPropertyCallback CleanupPropertyCallback
A callback used to free resources when a property is deleted.
Definition SDL3pp_properties.h:74
PropertiesShared share()
Move this properties into a PropertiesShared.
Definition SDL3pp_properties.h:764
PropertiesRef GetGlobalProperties()
Get the global SDL properties.
Definition SDL3pp_properties.h:869
the main namespace where all SDL3pp public functions and types live
Definition SDL3pp_assert.h:7
Definition SDL3pp_callbackWrapper.h:66
Wrap the lock state for Properties.
Definition SDL3pp_properties.h:796
~PropertiesLock()
Destructor.
Definition SDL3pp_properties.h:842
PropertiesLock(PropertiesRef props)
Lock a group of properties.
Definition SDL3pp_properties.h:831
constexpr PropertiesLock(PropertiesLock &&other)
Move constructor.
Definition SDL3pp_properties.h:805
constexpr PropertiesLock()=default
Creates an empty lock.
void Unlock()
Unlock a group of properties.
Definition SDL3pp_properties.h:853
void reset()
Same as Unlock(), just for uniformity.
Definition SDL3pp_properties.h:858
SDL properties ID.
Definition SDL3pp_properties.h:209
float GetFloat(StringParam name, float default_value) const
Get a floating point property from a group of properties.
Definition SDL3pp_properties.h:585
Sint64 GetNumber(StringParam name, Sint64 default_value) const
Get a number property from a group of properties.
Definition SDL3pp_properties.h:556
void Enumerate(EnumeratePropertiesCallback callback, void *userdata) const
Enumerate the properties contained in a group of properties.
Definition SDL3pp_properties.h:677
void SetNumber(StringParam name, Sint64 value)
Set an integer property in a group of properties.
Definition SDL3pp_properties.h:387
PropertyType GetType(StringParam name) const
Get the type of a property.
Definition SDL3pp_properties.h:455
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:279
void Clear(StringParam name)
Clear a property from a group of properties.
Definition SDL3pp_properties.h:629
void SetBoolean(StringParam name, bool value)
Set a boolean property in a group of properties.
Definition SDL3pp_properties.h:423
void CopyPropertiesTo(PropertiesRef dst) const
Copy a group of properties.
Definition SDL3pp_properties.h:227
void Enumerate(IT outputIter) const
Enumerate the properties contained in a group of properties.
Definition SDL3pp_properties.h:640
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:318
void SetString(StringParam name, StringParam value)
Set a string property in a group of properties.
Definition SDL3pp_properties.h:369
Uint64 GetCount() const
Returns the number of properties this has.
Definition SDL3pp_properties.h:893
const char * GetString(StringParam name, StringParam default_value) const
Get a string property from a group of properties.
Definition SDL3pp_properties.h:527
static void reset(SDL_PropertiesID resource)
Destroy a group of properties.
Definition SDL3pp_properties.h:705
bool GetBoolean(StringParam name, bool default_value) const
Get a boolean property from a group of properties.
Definition SDL3pp_properties.h:614
void SetFloat(StringParam name, float value)
Set a floating point property in a group of properties.
Definition SDL3pp_properties.h:405
void * GetPointer(StringParam name, void *default_value) const
Get a pointer property from a group of properties.
Definition SDL3pp_properties.h:493
void SetPointer(StringParam name, void *value)
Set a pointer property in a group of properties.
Definition SDL3pp_properties.h:347
bool Has(StringParam name) const
Return whether a property exists in a group of properties.
Definition SDL3pp_properties.h:440
Unsafe Handle to properties.
Definition SDL3pp_properties.h:779
constexpr PropertiesUnsafe(Properties &&other)
Constructs PropertiesUnsafe from Properties.
Definition SDL3pp_properties.h:785
Handle to an owned properties.
Definition SDL3pp_properties.h:719
static Properties Create()
Create a group of properties.
Definition SDL3pp_properties.h:736
void Destroy()
Destroy a group of properties.
Definition SDL3pp_properties.h:755