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 operator void*()
const {
return m_props.GetPointerProperty(m_name.c_str()); }
895 operator const char*()
const
897 return m_props.GetStringProperty(m_name.c_str());
901 template<std::
integral T>
904 return T(m_props.GetNumberProperty(m_name.c_str()));
908 operator float()
const {
return m_props.GetFloatProperty(m_name.c_str()); }
911 operator bool()
const {
return m_props.GetBooleanProperty(m_name.c_str()); }
918 case SDL_PROPERTY_TYPE_POINTER:
return visitor((
void*)(*
this));
919 case SDL_PROPERTY_TYPE_STRING:
return visitor((
const char*)(*
this));
920 case SDL_PROPERTY_TYPE_NUMBER:
return visitor(
Sint64(*
this));
921 case SDL_PROPERTY_TYPE_FLOAT:
return visitor(
float(*
this));
922 case SDL_PROPERTY_TYPE_BOOLEAN:
return visitor(
bool(*
this));
923 default:
return visitor(std::nullopt);
941 template<PropertyValue V>
961 [
this](
auto,
const char* key) { m_keys.emplace_back(key); });
962 if (m_index < m_keys.size()) m_proxy = {props, m_keys[m_index]};
970 return m_index >= m_keys.size();
976 return m_index == other.m_index;
1018 std::vector<std::string> m_keys;
1024 if (m_index < m_keys.size()) m_proxy.m_name = m_keys[m_index];
1028#if SDL_VERSION_ATLEAST(3, 4, 0)
1069 return {
CheckError(SDL_GetGlobalProperties())};
1169 SDL_UnlockProperties(props);
1175 std::move(lock).reset();
1180 if (!m_lock)
return;
1187 return {*
this, name};
1192 return {*
this, name};
1208template<PropertyValue V>
1211 if constexpr (std::is_same_v<V, bool>) {
1213 }
else if constexpr (std::is_integral_v<V>) {
1221 void operator()(std::monostate) { props.
ClearProperty(name); }
1222 void operator()(std::nullopt_t) { props.
ClearProperty(name); }
1234 prop.visit([
this](
auto v) { (*this)(v); });
1237 Visitor{props, name}(std::forward<V>(value));
1241template<PropertyValue V>
1283 SDL_SetPointerPropertyWithCleanup(props, name, value, cleanup, userdata));
1320 SDL_SetPointerPropertyWithCleanup(
1321 props, name, value, &Wrapper::CallOnce, Wrapper::Wrap(std::move(cleanup)));
1366 CheckError(SDL_SetPointerProperty(props, name, value));
1396 CheckError(SDL_SetStringProperty(props, name, value));
1423 CheckError(SDL_SetNumberProperty(props, name, value));
1447 CheckError(SDL_SetFloatProperty(props, name, value));
1473 CheckError(SDL_SetBooleanProperty(props, name, value));
1496 return SDL_HasProperty(props, name);
1519 return SDL_GetPropertyType(props, name);
1536 return {props, name};
1576 void* default_value =
nullptr)
1578 return SDL_GetPointerProperty(props, name, default_value);
1582 void* default_value)
const
1612 return SDL_GetStringProperty(props, name, default_value);
1644 Sint64 default_value = 0)
1646 return SDL_GetNumberProperty(props, name, default_value);
1650 Sint64 default_value)
const
1677 float default_value = 0)
1679 return SDL_GetFloatProperty(props, name, default_value);
1683 float default_value)
const
1710 bool default_value =
false)
1712 return SDL_GetBooleanProperty(props, name, default_value);
1716 bool default_value)
const
1761 CheckError(SDL_EnumerateProperties(props, callback, userdata));
1783 [](
void* userdata,
PropertiesID props,
const char* name) {
1791 void* userdata)
const
1839 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:951
PropertyIterator operator--(int)
Post-decrement operator to move to the previous property.
Definition SDL3pp_properties.h:1010
constexpr PropertyIterator()=default
Default constructor.
PropertyIterator & operator--()
Pre-decrement operator to move to the previous property.
Definition SDL3pp_properties.h:1002
constexpr bool operator==(const PropertyIterator &other) const
Comparison operator.
Definition SDL3pp_properties.h:974
PropertyIterator(PropertiesRef props, size_t index=0)
Constructor.
Definition SDL3pp_properties.h:957
const PropertyProxy & operator*() const
Dereference operator to get the current property proxy.
Definition SDL3pp_properties.h:980
PropertyIterator operator++(int)
Post-increment operator to move to the next property.
Definition SDL3pp_properties.h:994
const PropertyProxy * operator->() const
Arrow operator to get a pointer to the current property proxy.
Definition SDL3pp_properties.h:983
PropertyIterator & operator++()
Pre-increment operator to move to the next property.
Definition SDL3pp_properties.h:986
constexpr bool operator==(std::nullptr_t) const
Comparison with nullptr, to check if the iterator is at the end.
Definition SDL3pp_properties.h:968
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
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:915
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:1150
Uint64 GetCount() const
Returns the number of properties this has.
Definition SDL3pp_properties.h:1816
void ClearProperty(PropertiesRef props, StringParam name)
Clear a property from a group of properties.
Definition SDL3pp_properties.h:1732
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:1809
void SetNumberProperty(StringParam name, Sint64 value)
Set an integer property in a group of properties.
Definition SDL3pp_properties.h:1426
bool GetBooleanProperty(StringParam name, bool default_value=false) const
Get a boolean property from a group of properties.
Definition SDL3pp_properties.h:1715
bool GetBooleanProperty(PropertiesRef props, StringParam name, bool default_value=false)
Get a boolean property from a group of properties.
Definition SDL3pp_properties.h:1708
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:1052
void LockProperties(PropertiesRef props)
Lock a group of properties.
Definition SDL3pp_properties.h:1143
void EnumerateProperties(PropertiesRef props, EnumeratePropertiesCallback callback, void *userdata)
Enumerate the properties contained in a group of properties.
Definition SDL3pp_properties.h:1757
void * GetPointerProperty(StringParam name, void *default_value=nullptr) const
Get a pointer property from a group of properties.
Definition SDL3pp_properties.h:1581
PropertyProxy GetProperty(PropertiesRef props, StringParam name)
Get a property from a group of properties.
Definition SDL3pp_properties.h:1534
const char * GetStringProperty(PropertiesRef props, StringParam name, StringParam default_value="")
Get a string property from a group of properties.
Definition SDL3pp_properties.h:1608
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:1276
void SetFloatProperty(PropertiesRef props, StringParam name, float value)
Set a floating point property in a group of properties.
Definition SDL3pp_properties.h:1445
PropertiesLock Lock()
Lock a group of properties.
Definition SDL3pp_properties.h:1148
void SetBooleanProperty(PropertiesRef props, StringParam name, bool value)
Set a boolean property in a group of properties.
Definition SDL3pp_properties.h:1469
PropertyProxy operator[](StringParam name) const
Access a property from a group of properties.
Definition SDL3pp_properties.h:1185
Properties()
Create a group of properties.
Definition SDL3pp_properties.h:1089
float GetFloatProperty(PropertiesRef props, StringParam name, float default_value=0)
Get a floating point property from a group of properties.
Definition SDL3pp_properties.h:1675
PropertyIterator begin() const
Gets an iterator to the first property in a group of properties.
Definition SDL3pp_properties.h:1195
std::nullptr_t end() const
Gets sentinel iterator for a group of properties.
Definition SDL3pp_properties.h:1197
const char * GetStringProperty(StringParam name, StringParam default_value="") const
Get a string property from a group of properties.
Definition SDL3pp_properties.h:1615
PropertyType GetPropertyType(StringParam name) const
Get the type of a property in a group of properties.
Definition SDL3pp_properties.h:1522
Properties CreateProperties()
Create a group of properties.
Definition SDL3pp_properties.h:1087
void SetNumberProperty(PropertiesRef props, StringParam name, Sint64 value)
Set an integer property in a group of properties.
Definition SDL3pp_properties.h:1419
void Set(StringParam name, V &&value)
Set a property in a group of properties.
Definition SDL3pp_properties.h:1242
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:1112
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:1392
bool HasProperty(PropertiesRef props, StringParam name)
Return whether a property exists in a group of properties.
Definition SDL3pp_properties.h:1494
Sint64 GetNumberProperty(PropertiesRef props, StringParam name, Sint64 default_value=0)
Get a number property from a group of properties.
Definition SDL3pp_properties.h:1642
PropertyType GetPropertyType(PropertiesRef props, StringParam name)
Get the type of a property in a group of properties.
Definition SDL3pp_properties.h:1517
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:1737
constexpr PropertyType PROPERTY_TYPE_INVALID
INVALID.
Definition SDL3pp_properties.h:67
void UnlockProperties(PropertiesRef props)
Unlock a group of properties.
Definition SDL3pp_properties.h:1167
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:1842
void Copy(PropertiesRef dst)
Copy a group of properties.
Definition SDL3pp_properties.h:1117
float GetFloatProperty(StringParam name, float default_value=0) const
Get a floating point property from a group of properties.
Definition SDL3pp_properties.h:1682
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:1649
void SetPointerProperty(PropertiesRef props, StringParam name, void *value)
Set a pointer property in a group of properties.
Definition SDL3pp_properties.h:1362
void SetProperty(PropertiesRef props, StringParam name, V &&value)
Set a property in a group of properties.
Definition SDL3pp_properties.h:1209
void Enumerate(EnumeratePropertiesCallback callback, void *userdata) const
Enumerate the properties contained in a group of properties.
Definition SDL3pp_properties.h:1790
void reset()
Unlock a group of properties.
Definition SDL3pp_properties.h:1178
void SetBooleanProperty(StringParam name, bool value)
Set a boolean property in a group of properties.
Definition SDL3pp_properties.h:1476
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:1574
void SetFloatProperty(StringParam name, float value)
Set a floating point property in a group of properties.
Definition SDL3pp_properties.h:1450
void Unlock(PropertiesLock &&lock)
Unlock a group of properties.
Definition SDL3pp_properties.h:1172
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:1324
void SetPointerProperty(StringParam name, void *value)
Set a pointer property in a group of properties.
Definition SDL3pp_properties.h:1369
PropertyProxy Get(StringParam name) const
Get a property from a group of properties.
Definition SDL3pp_properties.h:1539
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:1499
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:1399
void DestroyProperties(PropertiesID props)
Destroy a group of properties.
Definition SDL3pp_properties.h:1837
PropertiesRef GetGlobalProperties()
Get the global SDL properties.
Definition SDL3pp_properties.h:1067
::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:3778
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:937
PropertyMutableProxy & operator=(V &&value)
Change the value of this property.
Definition SDL3pp_properties.h:942
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