1#ifndef SDL3PP_PROPERTIES_H_
2#define SDL3PP_PROPERTIES_H_
6#include <SDL3/SDL_properties.h>
7#include "SDL3pp_callbackWrapper.h"
8#include "SDL3pp_error.h"
9#include "SDL3pp_resource.h"
10#include "SDL3pp_strings.h"
11#include "SDL3pp_version.h"
68 SDL_PROPERTY_TYPE_INVALID;
71 SDL_PROPERTY_TYPE_POINTER;
74 SDL_PROPERTY_TYPE_STRING;
77 SDL_PROPERTY_TYPE_NUMBER;
82 SDL_PROPERTY_TYPE_BOOLEAN;
96 std::convertible_to<T, const PropertyProxy&> ||
97 std::convertible_to<T, void*> || std::convertible_to<T, StringParam> ||
98 std::convertible_to<T, Sint64> || std::convertible_to<T, float>;
140 std::function<void(
PropertiesID props,
const char* name)>;
236 std::nullptr_t
end()
const;
306 template<PropertyValue V>
526 void* default_value =
nullptr)
const;
679 using PropertiesBase::PropertiesBase;
734 std::initializer_list<
736 std::variant<StringParam, void*, Sint64, float, bool>>> entries)
739 for (
auto& [name, value] : entries) {
741 [
this, &name](
auto& arg) {
742 if constexpr (std::is_convertible_v<
decltype(arg),
744 Set(name.c_str(), arg.c_str());
746 Set(name.c_str(), arg);
820 : m_lock(std::move(other.m_lock))
840 std::swap(m_lock, other.m_lock);
845 constexpr operator bool()
const {
return bool(m_lock); }
877 bool IsValid()
const {
return m_props.HasProperty(m_name.c_str()); }
882 return m_props.GetPropertyType(m_name.c_str());
886 const char*
GetName()
const {
return m_name; }
892 template<PropertyType T>
896 template<PropertyValue T>
900 operator void*()
const {
return m_props.GetPointerProperty(m_name); }
903 operator const char*()
const {
return m_props.GetStringProperty(m_name); }
906 template<std::
integral T>
909 return T(m_props.GetNumberProperty(m_name));
913 template<std::
floating_po
int T>
916 return T(m_props.GetFloatProperty(m_name));
920 operator bool()
const {
return m_props.GetBooleanProperty(m_name); }
927 case SDL_PROPERTY_TYPE_POINTER:
return visitor((
void*)(*
this));
928 case SDL_PROPERTY_TYPE_STRING:
return visitor((
const char*)(*
this));
929 case SDL_PROPERTY_TYPE_NUMBER:
return visitor(
Sint64(*
this));
930 case SDL_PROPERTY_TYPE_FLOAT:
return visitor(
float(*
this));
931 case SDL_PROPERTY_TYPE_BOOLEAN:
return visitor(
bool(*
this));
932 default:
return visitor(std::nullopt);
950 template<PropertyValue V>
970 [
this](
auto,
const char* key) { m_keys.emplace_back(key); });
971 if (m_index < m_keys.size()) m_proxy = {props, m_keys[m_index]};
979 return m_index >= m_keys.size();
985 return m_index == other.m_index;
1027 std::vector<std::string> m_keys;
1033 if (m_index < m_keys.size()) m_proxy.m_name = m_keys[m_index];
1037#if SDL_VERSION_ATLEAST(3, 4, 0)
1078 return {
CheckError(SDL_GetGlobalProperties())};
1178 SDL_UnlockProperties(props);
1184 std::move(lock).reset();
1189 if (!m_lock)
return;
1196 return {*
this, name};
1201 return {*
this, name};
1217template<PropertyValue V>
1225 void operator()(std::nullopt_t) { props.
ClearProperty(name); }
1233 } visitor(props, name);
1234 if constexpr (std::is_same_v<V, bool>) {
1236 }
else if constexpr (std::integral<V>) {
1238 }
else if constexpr (std::floating_point<V>) {
1239 visitor(
float(value));
1240 }
else if constexpr (std::convertible_to<V, StringParam>) {
1247template<PropertyValue V>
1289 SDL_SetPointerPropertyWithCleanup(props, name, value, cleanup, userdata));
1326 SDL_SetPointerPropertyWithCleanup(
1327 props, name, value, &Wrapper::CallOnce, Wrapper::Wrap(std::move(cleanup)));
1372 CheckError(SDL_SetPointerProperty(props, name, value));
1402 CheckError(SDL_SetStringProperty(props, name, value));
1429 CheckError(SDL_SetNumberProperty(props, name, value));
1453 CheckError(SDL_SetFloatProperty(props, name, value));
1479 CheckError(SDL_SetBooleanProperty(props, name, value));
1502 return SDL_HasProperty(props, name);
1525 return SDL_GetPropertyType(props, name);
1542 return {props, name};
1582 void* default_value =
nullptr)
1584 return SDL_GetPointerProperty(props, name, default_value);
1588 void* default_value)
const
1618 return SDL_GetStringProperty(props, name, default_value);
1650 Sint64 default_value = 0)
1652 return SDL_GetNumberProperty(props, name, default_value);
1656 Sint64 default_value)
const
1683 float default_value = 0)
1685 return SDL_GetFloatProperty(props, name, default_value);
1689 float default_value)
const
1716 bool default_value =
false)
1718 return SDL_GetBooleanProperty(props, name, default_value);
1722 bool default_value)
const
1728template<PropertyType T>
1746template<PropertyValue T>
1749 if constexpr (std::is_same_v<T, bool>) {
1751 }
else if constexpr (std::integral<T>) {
1753 }
else if constexpr (std::floating_point<T>) {
1755 }
else if constexpr (std::is_same_v<T, char*>) {
1757 }
else if constexpr (std::convertible_to<T, StringParam>) {
1759 }
else if constexpr (std::convertible_to<T, void*>) {
1762 static_assert(std::convertible_to<PropertyProxy, T>,
"invalid type");
1768template<PropertyType T>
1775template<PropertyValue T>
1782template<PropertyType T>
1789template<PropertyValue T>
1835 CheckError(SDL_EnumerateProperties(props, callback, userdata));
1857 [](
void* userdata,
PropertiesID props,
const char* name) {
1865 void* userdata)
const
1913 SDL_DestroyProperties(props);
Lock a group of properties.
Definition SDL3pp_properties.h:787
~PropertiesLock()
Unlock a group of properties.
Definition SDL3pp_properties.h:833
PropertiesLock(const PropertiesLock &other)=delete
Copy constructor.
void release()
Releases the lock without unlocking.
Definition SDL3pp_properties.h:862
PropertiesRef resource() const
Get the reference to locked resource.
Definition SDL3pp_properties.h:859
PropertiesLock(PropertiesLock &&other) noexcept
Move constructor.
Definition SDL3pp_properties.h:819
PropertiesLock & operator=(PropertiesLock &&other) noexcept
Assignment operator.
Definition SDL3pp_properties.h:838
Iterator for properties in a Properties set.
Definition SDL3pp_properties.h:960
PropertyIterator operator--(int)
Post-decrement operator to move to the previous property.
Definition SDL3pp_properties.h:1019
constexpr PropertyIterator()=default
Default constructor.
PropertyIterator & operator--()
Pre-decrement operator to move to the previous property.
Definition SDL3pp_properties.h:1011
constexpr bool operator==(const PropertyIterator &other) const
Comparison operator.
Definition SDL3pp_properties.h:983
PropertyIterator(PropertiesRef props, size_t index=0)
Constructor.
Definition SDL3pp_properties.h:966
const PropertyProxy & operator*() const
Dereference operator to get the current property proxy.
Definition SDL3pp_properties.h:989
PropertyIterator operator++(int)
Post-increment operator to move to the next property.
Definition SDL3pp_properties.h:1003
const PropertyProxy * operator->() const
Arrow operator to get a pointer to the current property proxy.
Definition SDL3pp_properties.h:992
PropertyIterator & operator++()
Pre-increment operator to move to the next property.
Definition SDL3pp_properties.h:995
constexpr bool operator==(std::nullptr_t) const
Comparison with nullptr, to check if the iterator is at the end.
Definition SDL3pp_properties.h:977
Represent a property name from a given Properties.
Definition SDL3pp_properties.h:867
PropertiesRef GetProperties() const
Get the property group this proxy belongs to.
Definition SDL3pp_properties.h:889
const char * GetName() const
Get Property name.
Definition SDL3pp_properties.h:886
auto As() const
Convert to given type.
PropertyProxy(PropertiesRef props, StringParam name)
Constructor.
Definition SDL3pp_properties.h:870
bool IsValid() const
True if the there is a property with this name.
Definition SDL3pp_properties.h:877
PropertyType GetType() const
Get Property type.
Definition SDL3pp_properties.h:880
auto visit(T visitor) const
Call visitor with the correct type based on its GetType().
Definition SDL3pp_properties.h:924
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
A value assignable to a property.
Definition SDL3pp_properties.h:95
#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:81
PropertiesLock(PropertiesRef resource)
Lock a group of properties.
Definition SDL3pp_properties.h:1159
Uint64 GetCount() const
Returns the number of properties this has.
Definition SDL3pp_properties.h:1890
void ClearProperty(PropertiesRef props, StringParam name)
Clear a property from a group of properties.
Definition SDL3pp_properties.h:1806
std::function< void(void *value)> CleanupPropertyCB
A callback used to free resources when a property is deleted.
Definition SDL3pp_properties.h:189
Uint64 CountProperties(PropertiesRef props)
Returns the number of properties this has.
Definition SDL3pp_properties.h:1883
void SetNumberProperty(StringParam name, Sint64 value)
Set an integer property in a group of properties.
Definition SDL3pp_properties.h:1432
bool GetBooleanProperty(StringParam name, bool default_value=false) const
Get a boolean property from a group of properties.
Definition SDL3pp_properties.h:1721
bool GetBooleanProperty(PropertiesRef props, StringParam name, bool default_value=false)
Get a boolean property from a group of properties.
Definition SDL3pp_properties.h:1714
void(SDLCALL *)(void *userdata, void *value) CleanupPropertyCallback
A callback used to free resources when a property is deleted.
Definition SDL3pp_properties.h:164
constexpr const char * PROP_NAME_STRING
A generic property for naming things.
Definition SDL3pp_properties.h:1061
void LockProperties(PropertiesRef props)
Lock a group of properties.
Definition SDL3pp_properties.h:1152
void EnumerateProperties(PropertiesRef props, EnumeratePropertiesCallback callback, void *userdata)
Enumerate the properties contained in a group of properties.
Definition SDL3pp_properties.h:1831
void * GetPointerProperty(StringParam name, void *default_value=nullptr) const
Get a pointer property from a group of properties.
Definition SDL3pp_properties.h:1587
PropertyProxy GetProperty(PropertiesRef props, StringParam name)
Get a property from a group of properties.
Definition SDL3pp_properties.h:1540
const char * GetStringProperty(PropertiesRef props, StringParam name, StringParam default_value="")
Get a string property from a group of properties.
Definition SDL3pp_properties.h:1614
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:1282
void SetFloatProperty(PropertiesRef props, StringParam name, float value)
Set a floating point property in a group of properties.
Definition SDL3pp_properties.h:1451
PropertiesLock Lock()
Lock a group of properties.
Definition SDL3pp_properties.h:1157
void SetBooleanProperty(PropertiesRef props, StringParam name, bool value)
Set a boolean property in a group of properties.
Definition SDL3pp_properties.h:1475
PropertyProxy operator[](StringParam name) const
Access a property from a group of properties.
Definition SDL3pp_properties.h:1194
Properties()
Create a group of properties.
Definition SDL3pp_properties.h:1098
float GetFloatProperty(PropertiesRef props, StringParam name, float default_value=0)
Get a floating point property from a group of properties.
Definition SDL3pp_properties.h:1681
PropertyIterator begin() const
Gets an iterator to the first property in a group of properties.
Definition SDL3pp_properties.h:1204
void SetProperty(PropertiesRef props, StringParam name, V value)
Set a property in a group of properties.
Definition SDL3pp_properties.h:1218
std::nullptr_t end() const
Gets sentinel iterator for a group of properties.
Definition SDL3pp_properties.h:1206
const char * GetStringProperty(StringParam name, StringParam default_value="") const
Get a string property from a group of properties.
Definition SDL3pp_properties.h:1621
PropertyType GetPropertyType(StringParam name) const
Get the type of a property in a group of properties.
Definition SDL3pp_properties.h:1528
Properties CreateProperties()
Create a group of properties.
Definition SDL3pp_properties.h:1096
void SetNumberProperty(PropertiesRef props, StringParam name, Sint64 value)
Set an integer property in a group of properties.
Definition SDL3pp_properties.h:1425
void Set(StringParam name, V &&value)
Set a property in a group of properties.
Definition SDL3pp_properties.h:1248
constexpr PropertyType PROPERTY_TYPE_NUMBER
NUMBER.
Definition SDL3pp_properties.h:76
void CopyProperties(PropertiesRef src, PropertiesRef dst)
Copy a group of properties.
Definition SDL3pp_properties.h:1121
constexpr PropertyType PROPERTY_TYPE_POINTER
POINTER.
Definition SDL3pp_properties.h:70
void SetStringProperty(PropertiesRef props, StringParam name, StringParam value)
Set a string property in a group of properties.
Definition SDL3pp_properties.h:1398
bool HasProperty(PropertiesRef props, StringParam name)
Return whether a property exists in a group of properties.
Definition SDL3pp_properties.h:1500
Sint64 GetNumberProperty(PropertiesRef props, StringParam name, Sint64 default_value=0)
Get a number property from a group of properties.
Definition SDL3pp_properties.h:1648
PropertyType GetPropertyType(PropertiesRef props, StringParam name)
Get the type of a property in a group of properties.
Definition SDL3pp_properties.h:1523
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 ClearProperty(StringParam name)
Clear a property from a group of properties.
Definition SDL3pp_properties.h:1811
auto prop_cast(PropertiesRef props, StringParam name)
Cast property to given PropertyType.
Definition SDL3pp_properties.h:1729
constexpr PropertyType PROPERTY_TYPE_INVALID
INVALID.
Definition SDL3pp_properties.h:67
void UnlockProperties(PropertiesRef props)
Unlock a group of properties.
Definition SDL3pp_properties.h:1176
constexpr PropertyType PROPERTY_TYPE_FLOAT
FLOAT.
Definition SDL3pp_properties.h:79
constexpr PropertyType PROPERTY_TYPE_STRING
STRING.
Definition SDL3pp_properties.h:73
void Destroy()
Destroy a group of properties.
Definition SDL3pp_properties.h:1916
void Copy(PropertiesRef dst)
Copy a group of properties.
Definition SDL3pp_properties.h:1126
float GetFloatProperty(StringParam name, float default_value=0) const
Get a floating point property from a group of properties.
Definition SDL3pp_properties.h:1688
auto As() const
Convert to given type.
Definition SDL3pp_properties.h:1783
SDL_PropertiesID PropertiesID
Alias to raw representation for Properties.
Definition SDL3pp_properties.h:48
Sint64 GetNumberProperty(StringParam name, Sint64 default_value=0) const
Get a number property from a group of properties.
Definition SDL3pp_properties.h:1655
void SetPointerProperty(PropertiesRef props, StringParam name, void *value)
Set a pointer property in a group of properties.
Definition SDL3pp_properties.h:1368
void Enumerate(EnumeratePropertiesCallback callback, void *userdata) const
Enumerate the properties contained in a group of properties.
Definition SDL3pp_properties.h:1864
void reset()
Unlock a group of properties.
Definition SDL3pp_properties.h:1187
void SetBooleanProperty(StringParam name, bool value)
Set a boolean property in a group of properties.
Definition SDL3pp_properties.h:1482
SDL_PropertyType PropertyType
SDL property type.
Definition SDL3pp_properties.h:65
void * GetPointerProperty(PropertiesRef props, StringParam name, void *default_value=nullptr)
Get a pointer property from a group of properties.
Definition SDL3pp_properties.h:1580
void SetFloatProperty(StringParam name, float value)
Set a floating point property in a group of properties.
Definition SDL3pp_properties.h:1456
void Unlock(PropertiesLock &&lock)
Unlock a group of properties.
Definition SDL3pp_properties.h:1181
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:1330
void SetPointerProperty(StringParam name, void *value)
Set a pointer property in a group of properties.
Definition SDL3pp_properties.h:1375
PropertyProxy Get(StringParam name) const
Get a property from a group of properties.
Definition SDL3pp_properties.h:1545
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:117
bool HasProperty(StringParam name) const
Return whether a property exists in a group of properties.
Definition SDL3pp_properties.h:1505
ResourceRefT< PropertiesBase > PropertiesRef
Reference for Properties.
Definition SDL3pp_properties.h:55
void SetStringProperty(StringParam name, StringParam value)
Set a string property in a group of properties.
Definition SDL3pp_properties.h:1405
void DestroyProperties(PropertiesID props)
Destroy a group of properties.
Definition SDL3pp_properties.h:1911
PropertiesRef GetGlobalProperties()
Get the global SDL properties.
Definition SDL3pp_properties.h:1076
::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:3782
Main include header for the SDL3pp library.
Definition SDL3pp_callbackWrapper.h:20
Base class to Properties.
Definition SDL3pp_properties.h:197
constexpr ResourceBaseT()=default
Default constructor, creates null/invalid resource.
An ID that represents a properties set.
Definition SDL3pp_properties.h:678
constexpr Properties & operator=(Properties &&other) noexcept
Assignment operator.
Definition SDL3pp_properties.h:757
~Properties()
Destructor.
Definition SDL3pp_properties.h:754
constexpr Properties(PropertiesID resource) noexcept
Constructs from raw Properties.
Definition SDL3pp_properties.h:688
constexpr Properties(Properties &&other) noexcept
Move constructor.
Definition SDL3pp_properties.h:694
Properties(std::initializer_list< std::pair< StringParam, std::variant< StringParam, void *, Sint64, float, bool > > > entries)
Create a group of properties.
Definition SDL3pp_properties.h:733
Represent a mutable property name from a given Properties.
Definition SDL3pp_properties.h:946
PropertyMutableProxy & operator=(V &&value)
Change the value of this property.
Definition SDL3pp_properties.h:951
PropertyProxy(PropertiesRef props, StringParam name)
Constructor.
Definition SDL3pp_properties.h:870
A non-owning reference wrapper for a given resource.
Definition SDL3pp_resource.h:93