1#ifndef SDL3PP_PROPERTIES_H_
2#define SDL3PP_PROPERTIES_H_
7#include <SDL3/SDL_properties.h>
8#include "SDL3pp_callbackWrapper.h"
9#include "SDL3pp_error.h"
10#include "SDL3pp_resource.h"
11#include "SDL3pp_strings.h"
12#include "SDL3pp_version.h"
69 SDL_PROPERTY_TYPE_INVALID;
72 SDL_PROPERTY_TYPE_POINTER;
75 SDL_PROPERTY_TYPE_STRING;
78 SDL_PROPERTY_TYPE_NUMBER;
83 SDL_PROPERTY_TYPE_BOOLEAN;
97 std::convertible_to<T, const PropertyProxy&> ||
98 std::convertible_to<T, void*> || std::convertible_to<T, StringParam> ||
99 std::convertible_to<T, Sint64> || std::convertible_to<T, float>;
141 std::function<void(
PropertiesID props,
const char* name)>;
237 std::nullptr_t
end()
const;
307 template<PropertyValue V>
527 void* default_value =
nullptr)
const;
680 using PropertiesBase::PropertiesBase;
735 std::initializer_list<
737 std::variant<StringParam, void*, Sint64, float, bool>>> entries)
740 for (
auto& [name, value] : entries) {
742 [
this, &name](
auto& arg) {
743 if constexpr (std::is_convertible_v<
decltype(arg),
745 Set(name.c_str(), arg.c_str());
747 Set(name.c_str(), arg);
821 : m_lock(std::move(other.m_lock))
841 std::swap(m_lock, other.m_lock);
846 constexpr operator bool()
const {
return bool(m_lock); }
878 bool IsValid()
const {
return m_props.HasProperty(m_name.c_str()); }
883 return m_props.GetPropertyType(m_name.c_str());
887 const char*
GetName()
const {
return m_name; }
893 template<PropertyType T>
897 template<PropertyValue T>
901 operator void*()
const {
return m_props.GetPointerProperty(m_name); }
904 operator const char*()
const {
return m_props.GetStringProperty(m_name); }
907 template<std::
integral T>
910 return T(m_props.GetNumberProperty(m_name));
914 template<std::
floating_po
int T>
917 return T(m_props.GetFloatProperty(m_name));
921 operator bool()
const {
return m_props.GetBooleanProperty(m_name); }
928 case SDL_PROPERTY_TYPE_POINTER:
return visitor((
void*)(*
this));
929 case SDL_PROPERTY_TYPE_STRING:
return visitor((
const char*)(*
this));
930 case SDL_PROPERTY_TYPE_NUMBER:
return visitor(
Sint64(*
this));
931 case SDL_PROPERTY_TYPE_FLOAT:
return visitor(
float(*
this));
932 case SDL_PROPERTY_TYPE_BOOLEAN:
return visitor(
bool(*
this));
933 default:
return visitor(std::nullopt);
951 template<PropertyValue V>
971 [
this](
auto,
const char* key) { m_keys.emplace_back(key); });
972 if (m_index < m_keys.size()) m_proxy = {props, m_keys[m_index]};
980 return m_index >= m_keys.size();
986 return m_index == other.m_index;
1028 std::vector<std::string> m_keys;
1034 if (m_index < m_keys.size()) m_proxy.m_name = m_keys[m_index];
1038#if SDL_VERSION_ATLEAST(3, 4, 0)
1079 return {
CheckError(SDL_GetGlobalProperties())};
1179 SDL_UnlockProperties(props);
1185 std::move(lock).reset();
1190 if (!m_lock)
return;
1197 return {*
this, name};
1202 return {*
this, name};
1218template<PropertyValue V>
1226 void operator()(std::nullopt_t) { props.
ClearProperty(name); }
1234 } visitor(props, name);
1235 if constexpr (std::is_same_v<V, bool>) {
1237 }
else if constexpr (std::integral<V>) {
1239 }
else if constexpr (std::floating_point<V>) {
1240 visitor(
float(value));
1241 }
else if constexpr (std::convertible_to<V, StringParam>) {
1248template<PropertyValue V>
1290 SDL_SetPointerPropertyWithCleanup(props, name, value, cleanup, userdata));
1327 SDL_SetPointerPropertyWithCleanup(
1328 props, name, value, &Wrapper::CallOnce, Wrapper::Wrap(std::move(cleanup)));
1373 CheckError(SDL_SetPointerProperty(props, name, value));
1403 CheckError(SDL_SetStringProperty(props, name, value));
1430 CheckError(SDL_SetNumberProperty(props, name, value));
1454 CheckError(SDL_SetFloatProperty(props, name, value));
1480 CheckError(SDL_SetBooleanProperty(props, name, value));
1503 return SDL_HasProperty(props, name);
1526 return SDL_GetPropertyType(props, name);
1543 return {props, name};
1583 void* default_value =
nullptr)
1585 return SDL_GetPointerProperty(props, name, default_value);
1589 void* default_value)
const
1619 return SDL_GetStringProperty(props, name, default_value);
1651 Sint64 default_value = 0)
1653 return SDL_GetNumberProperty(props, name, default_value);
1657 Sint64 default_value)
const
1684 float default_value = 0)
1686 return SDL_GetFloatProperty(props, name, default_value);
1690 float default_value)
const
1717 bool default_value =
false)
1719 return SDL_GetBooleanProperty(props, name, default_value);
1723 bool default_value)
const
1729template<PropertyType T>
1747template<PropertyValue T>
1750 if constexpr (std::is_same_v<T, bool>) {
1752 }
else if constexpr (std::integral<T>) {
1754 }
else if constexpr (std::floating_point<T>) {
1756 }
else if constexpr (std::is_same_v<T, char*>) {
1758 }
else if constexpr (std::convertible_to<T, StringParam>) {
1760 }
else if constexpr (std::convertible_to<T, void*>) {
1763 static_assert(std::convertible_to<PropertyProxy, T>,
"invalid type");
1769template<PropertyType T>
1776template<PropertyValue T>
1783template<PropertyType T>
1790template<PropertyValue T>
1836 CheckError(SDL_EnumerateProperties(props, callback, userdata));
1858 [](
void* userdata,
PropertiesID props,
const char* name) {
1866 void* userdata)
const
1914 SDL_DestroyProperties(props);
Lock a group of properties.
Definition SDL3pp_properties.h:788
~PropertiesLock()
Unlock a group of properties.
Definition SDL3pp_properties.h:834
PropertiesLock(const PropertiesLock &other)=delete
Copy constructor.
void release()
Releases the lock without unlocking.
Definition SDL3pp_properties.h:863
PropertiesRef resource() const
Get the reference to locked resource.
Definition SDL3pp_properties.h:860
PropertiesLock(PropertiesLock &&other) noexcept
Move constructor.
Definition SDL3pp_properties.h:820
PropertiesLock & operator=(PropertiesLock &&other) noexcept
Assignment operator.
Definition SDL3pp_properties.h:839
Iterator for properties in a Properties set.
Definition SDL3pp_properties.h:961
PropertyIterator operator--(int)
Post-decrement operator to move to the previous property.
Definition SDL3pp_properties.h:1020
constexpr PropertyIterator()=default
Default constructor.
PropertyIterator & operator--()
Pre-decrement operator to move to the previous property.
Definition SDL3pp_properties.h:1012
constexpr bool operator==(const PropertyIterator &other) const
Comparison operator.
Definition SDL3pp_properties.h:984
PropertyIterator(PropertiesRef props, size_t index=0)
Constructor.
Definition SDL3pp_properties.h:967
const PropertyProxy & operator*() const
Dereference operator to get the current property proxy.
Definition SDL3pp_properties.h:990
PropertyIterator operator++(int)
Post-increment operator to move to the next property.
Definition SDL3pp_properties.h:1004
const PropertyProxy * operator->() const
Arrow operator to get a pointer to the current property proxy.
Definition SDL3pp_properties.h:993
PropertyIterator & operator++()
Pre-increment operator to move to the next property.
Definition SDL3pp_properties.h:996
constexpr bool operator==(std::nullptr_t) const
Comparison with nullptr, to check if the iterator is at the end.
Definition SDL3pp_properties.h:978
Represent a property name from a given Properties.
Definition SDL3pp_properties.h:868
PropertiesRef GetProperties() const
Get the property group this proxy belongs to.
Definition SDL3pp_properties.h:890
const char * GetName() const
Get Property name.
Definition SDL3pp_properties.h:887
auto As() const
Convert to given type.
PropertyProxy(PropertiesRef props, StringParam name)
Constructor.
Definition SDL3pp_properties.h:871
bool IsValid() const
True if the there is a property with this name.
Definition SDL3pp_properties.h:878
PropertyType GetType() const
Get Property type.
Definition SDL3pp_properties.h:881
auto visit(T visitor) const
Call visitor with the correct type based on its GetType().
Definition SDL3pp_properties.h:925
constexpr RawPointer release() noexcept
Definition SDL3pp_resource.h:57
friend constexpr void swap(ResourceBaseT &lhs, ResourceBaseT &rhs) noexcept
Definition SDL3pp_resource.h:65
constexpr RawPointer get() const noexcept
Definition SDL3pp_resource.h:54
constexpr ResourceBaseT()=default
Default constructor, creates null/invalid resource.
Helpers to use C++ strings parameters.
Definition SDL3pp_strings.h:58
constexpr StringParam(const char *str="")
Constructs from a C string.
Definition SDL3pp_strings.h:69
A value assignable to a property.
Definition SDL3pp_properties.h:96
#define SDL_assert_paranoid(condition)
An assertion test that is performed only when built with paranoid settings.
Definition SDL3pp_assert.h:383
constexpr void CheckError(bool result)
Check and throw if returned value from SDL is an error.
Definition SDL3pp_error.h:199
constexpr PropertyType PROPERTY_TYPE_BOOLEAN
BOOLEAN.
Definition SDL3pp_properties.h:82
PropertiesLock(PropertiesRef resource)
Lock a group of properties.
Definition SDL3pp_properties.h:1160
Uint64 GetCount() const
Returns the number of properties this has.
Definition SDL3pp_properties.h:1891
void ClearProperty(PropertiesRef props, StringParam name)
Clear a property from a group of properties.
Definition SDL3pp_properties.h:1807
std::function< void(void *value)> CleanupPropertyCB
A callback used to free resources when a property is deleted.
Definition SDL3pp_properties.h:190
Uint64 CountProperties(PropertiesRef props)
Returns the number of properties this has.
Definition SDL3pp_properties.h:1884
void SetNumberProperty(StringParam name, Sint64 value)
Set an integer property in a group of properties.
Definition SDL3pp_properties.h:1433
bool GetBooleanProperty(StringParam name, bool default_value=false) const
Get a boolean property from a group of properties.
Definition SDL3pp_properties.h:1722
bool GetBooleanProperty(PropertiesRef props, StringParam name, bool default_value=false)
Get a boolean property from a group of properties.
Definition SDL3pp_properties.h:1715
void(SDLCALL *)(void *userdata, void *value) CleanupPropertyCallback
A callback used to free resources when a property is deleted.
Definition SDL3pp_properties.h:165
constexpr const char * PROP_NAME_STRING
A generic property for naming things.
Definition SDL3pp_properties.h:1062
void LockProperties(PropertiesRef props)
Lock a group of properties.
Definition SDL3pp_properties.h:1153
void EnumerateProperties(PropertiesRef props, EnumeratePropertiesCallback callback, void *userdata)
Enumerate the properties contained in a group of properties.
Definition SDL3pp_properties.h:1832
void * GetPointerProperty(StringParam name, void *default_value=nullptr) const
Get a pointer property from a group of properties.
Definition SDL3pp_properties.h:1588
PropertyProxy GetProperty(PropertiesRef props, StringParam name)
Get a property from a group of properties.
Definition SDL3pp_properties.h:1541
const char * GetStringProperty(PropertiesRef props, StringParam name, StringParam default_value="")
Get a string property from a group of properties.
Definition SDL3pp_properties.h:1615
void SetPointerPropertyWithCleanup(PropertiesRef 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:1283
void SetFloatProperty(PropertiesRef props, StringParam name, float value)
Set a floating point property in a group of properties.
Definition SDL3pp_properties.h:1452
PropertiesLock Lock()
Lock a group of properties.
Definition SDL3pp_properties.h:1158
void SetBooleanProperty(PropertiesRef props, StringParam name, bool value)
Set a boolean property in a group of properties.
Definition SDL3pp_properties.h:1476
PropertyProxy operator[](StringParam name) const
Access a property from a group of properties.
Definition SDL3pp_properties.h:1195
Properties()
Create a group of properties.
Definition SDL3pp_properties.h:1099
float GetFloatProperty(PropertiesRef props, StringParam name, float default_value=0)
Get a floating point property from a group of properties.
Definition SDL3pp_properties.h:1682
PropertyIterator begin() const
Gets an iterator to the first property in a group of properties.
Definition SDL3pp_properties.h:1205
void SetProperty(PropertiesRef props, StringParam name, V value)
Set a property in a group of properties.
Definition SDL3pp_properties.h:1219
std::nullptr_t end() const
Gets sentinel iterator for a group of properties.
Definition SDL3pp_properties.h:1207
const char * GetStringProperty(StringParam name, StringParam default_value="") const
Get a string property from a group of properties.
Definition SDL3pp_properties.h:1622
PropertyType GetPropertyType(StringParam name) const
Get the type of a property in a group of properties.
Definition SDL3pp_properties.h:1529
Properties CreateProperties()
Create a group of properties.
Definition SDL3pp_properties.h:1097
void SetNumberProperty(PropertiesRef props, StringParam name, Sint64 value)
Set an integer property in a group of properties.
Definition SDL3pp_properties.h:1426
void Set(StringParam name, V &&value)
Set a property in a group of properties.
Definition SDL3pp_properties.h:1249
constexpr PropertyType PROPERTY_TYPE_NUMBER
NUMBER.
Definition SDL3pp_properties.h:77
void CopyProperties(PropertiesRef src, PropertiesRef dst)
Copy a group of properties.
Definition SDL3pp_properties.h:1122
constexpr PropertyType PROPERTY_TYPE_POINTER
POINTER.
Definition SDL3pp_properties.h:71
void SetStringProperty(PropertiesRef props, StringParam name, StringParam value)
Set a string property in a group of properties.
Definition SDL3pp_properties.h:1399
bool HasProperty(PropertiesRef props, StringParam name)
Return whether a property exists in a group of properties.
Definition SDL3pp_properties.h:1501
Sint64 GetNumberProperty(PropertiesRef props, StringParam name, Sint64 default_value=0)
Get a number property from a group of properties.
Definition SDL3pp_properties.h:1649
PropertyType GetPropertyType(PropertiesRef props, StringParam name)
Get the type of a property in a group of properties.
Definition SDL3pp_properties.h:1524
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 ClearProperty(StringParam name)
Clear a property from a group of properties.
Definition SDL3pp_properties.h:1812
auto prop_cast(PropertiesRef props, StringParam name)
Cast property to given PropertyType.
Definition SDL3pp_properties.h:1730
constexpr PropertyType PROPERTY_TYPE_INVALID
INVALID.
Definition SDL3pp_properties.h:68
void UnlockProperties(PropertiesRef props)
Unlock a group of properties.
Definition SDL3pp_properties.h:1177
constexpr PropertyType PROPERTY_TYPE_FLOAT
FLOAT.
Definition SDL3pp_properties.h:80
constexpr PropertyType PROPERTY_TYPE_STRING
STRING.
Definition SDL3pp_properties.h:74
void Destroy()
Destroy a group of properties.
Definition SDL3pp_properties.h:1917
void Copy(PropertiesRef dst)
Copy a group of properties.
Definition SDL3pp_properties.h:1127
float GetFloatProperty(StringParam name, float default_value=0) const
Get a floating point property from a group of properties.
Definition SDL3pp_properties.h:1689
auto As() const
Convert to given type.
Definition SDL3pp_properties.h:1784
SDL_PropertiesID PropertiesID
Alias to raw representation for Properties.
Definition SDL3pp_properties.h:49
Sint64 GetNumberProperty(StringParam name, Sint64 default_value=0) const
Get a number property from a group of properties.
Definition SDL3pp_properties.h:1656
void SetPointerProperty(PropertiesRef props, StringParam name, void *value)
Set a pointer property in a group of properties.
Definition SDL3pp_properties.h:1369
void Enumerate(EnumeratePropertiesCallback callback, void *userdata) const
Enumerate the properties contained in a group of properties.
Definition SDL3pp_properties.h:1865
void reset()
Unlock a group of properties.
Definition SDL3pp_properties.h:1188
void SetBooleanProperty(StringParam name, bool value)
Set a boolean property in a group of properties.
Definition SDL3pp_properties.h:1483
SDL_PropertyType PropertyType
SDL property type.
Definition SDL3pp_properties.h:66
void * GetPointerProperty(PropertiesRef props, StringParam name, void *default_value=nullptr)
Get a pointer property from a group of properties.
Definition SDL3pp_properties.h:1581
void SetFloatProperty(StringParam name, float value)
Set a floating point property in a group of properties.
Definition SDL3pp_properties.h:1457
void Unlock(PropertiesLock &&lock)
Unlock a group of properties.
Definition SDL3pp_properties.h:1182
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:1331
void SetPointerProperty(StringParam name, void *value)
Set a pointer property in a group of properties.
Definition SDL3pp_properties.h:1376
PropertyProxy Get(StringParam name) const
Get a property from a group of properties.
Definition SDL3pp_properties.h:1546
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:118
bool HasProperty(StringParam name) const
Return whether a property exists in a group of properties.
Definition SDL3pp_properties.h:1506
ResourceRefT< PropertiesBase > PropertiesRef
Reference for Properties.
Definition SDL3pp_properties.h:56
void SetStringProperty(StringParam name, StringParam value)
Set a string property in a group of properties.
Definition SDL3pp_properties.h:1406
void DestroyProperties(PropertiesID props)
Destroy a group of properties.
Definition SDL3pp_properties.h:1912
PropertiesRef GetGlobalProperties()
Get the global SDL properties.
Definition SDL3pp_properties.h:1077
::Sint64 Sint64
A signed 64-bit integer type.
Definition SDL3pp_stdinc.h:311
::Uint64 Uint64
An unsigned 64-bit integer type.
Definition SDL3pp_stdinc.h:326
Constants for Properties keys.
Definition SDL3pp_audio.h:3815
Main include header for the SDL3pp library.
Definition SDL3pp_callbackWrapper.h:20
Base class to Properties.
Definition SDL3pp_properties.h:198
constexpr ResourceBaseT()=default
Default constructor, creates null/invalid resource.
An ID that represents a properties set.
Definition SDL3pp_properties.h:679
constexpr Properties & operator=(Properties &&other) noexcept
Assignment operator.
Definition SDL3pp_properties.h:758
~Properties()
Destructor.
Definition SDL3pp_properties.h:755
constexpr Properties(PropertiesID resource) noexcept
Constructs from raw Properties.
Definition SDL3pp_properties.h:689
constexpr Properties(Properties &&other) noexcept
Move constructor.
Definition SDL3pp_properties.h:695
Properties(std::initializer_list< std::pair< StringParam, std::variant< StringParam, void *, Sint64, float, bool > > > entries)
Create a group of properties.
Definition SDL3pp_properties.h:734
Represent a mutable property name from a given Properties.
Definition SDL3pp_properties.h:947
PropertyMutableProxy & operator=(V &&value)
Change the value of this property.
Definition SDL3pp_properties.h:952
PropertyProxy(PropertiesRef props, StringParam name)
Constructor.
Definition SDL3pp_properties.h:871
A non-owning reference wrapper for a given resource.
Definition SDL3pp_resource.h:93