24#ifndef PROPERTY_HOLDER_H
25#define PROPERTY_HOLDER_H
29#include <unordered_map>
83 static constexpr uint64_t
MAGIC_VALUE = 0x50524F5048444C52ULL;
138 m_magic = 0xDEADBEEFDEADBEEFULL;
233 template <
typename T>
249 template <
typename T>
263 return std::any_cast<T>( it->second );
265 catch(
const std::bad_any_cast& )
281 if(
auto aValue = GetProperty<T>( aKey ) )
284 return std::forward<T>(aDefaultValue);
357 std::vector<std::string> keys;
361 keys.push_back( key );
371 std::optional<std::reference_wrapper<const std::type_info>>
GetPropertyType(
const std::string& aKey )
const
381 return std::cref( it->second.type() );
390 template <
typename T>
401 return it->second.type() ==
typeid(
T );
407 template<PropertyValueType T>
bool SetProperty(const std::string &aKey, T &&aValue)
Set a property with the given key and value.
bool SetTypedProperty(const std::string &aKey, T &&aValue)
Type-safe property setter that only accepts valid property types.
static bool SafeDelete(void *aPtr) noexcept
Safely delete a PROPERTY_HOLDER from client data.
std::optional< std::reference_wrapper< const std::type_info > > GetPropertyType(const std::string &aKey) const
Get the type information for a property.
PROPERTY_HOLDER(const PROPERTY_HOLDER &other)
Copy constructor - maintains magic value.
bool Empty() const
Check if there are no properties stored.
std::vector< std::string > GetKeys() const
Get all property keys.
static PROPERTY_HOLDER * SafeCast(void *aPtr) noexcept
Safely cast a void pointer to PROPERTY_HOLDER*.
bool RemoveProperty(const std::string &aKey)
Remove a property.
~PROPERTY_HOLDER()
Destructor - clears magic value to detect use-after-free.
static constexpr uint64_t MAGIC_VALUE
Magic value for memory validation (ASCII: "PROP" + "HLDR")
PROPERTY_HOLDER & operator=(PROPERTY_HOLDER &&other) noexcept
Move assignment operator.
static bool SafeDelete(PROPERTY_HOLDER *aHolder) noexcept
std::optional< T > GetProperty(const std::string &aKey) const
Get a property value with type checking.
std::unordered_map< std::string, std::any > m_properties
Internal storage for properties using string keys and any values.
T GetPropertyOr(const std::string &aKey, T &&aDefaultValue) const
Get a property value with a default fallback.
bool HasPropertyOfType(const std::string &aKey) const
Check if a property exists and has the expected type.
PROPERTY_HOLDER & operator=(const PROPERTY_HOLDER &other)
Copy assignment operator.
static const PROPERTY_HOLDER * SafeCast(const void *aPtr) noexcept
Safely cast a const void pointer to const PROPERTY_HOLDER*.
bool HasProperty(const std::string &aKey) const
Check if a property exists.
bool IsValid() const noexcept
Check if this instance has a valid magic value.
PROPERTY_HOLDER(PROPERTY_HOLDER &&other) noexcept
Move constructor - maintains magic value.
bool Clear()
Clear all properties.
PROPERTY_HOLDER()
Default constructor - initializes magic value.
size_t Size() const
Get the number of stored properties.
uint64_t m_magic
Magic value for memory validation.
Mixin class to add property support to any class.
const PROPERTY_HOLDER & GetPropertyHolder() const
T GetPropertyOr(const std::string &aKey, T &&aDefaultValue) const
PROPERTY_HOLDER m_propertyHolder
std::optional< T > GetProperty(const std::string &aKey) const
void SetProperty(const std::string &aKey, T &&aValue)
PROPERTY_HOLDER & GetPropertyHolder()
Get the property holder for this object.
bool HasPropertyOfType(const std::string &aKey) const
bool HasProperty(const std::string &aKey) const
bool RemoveProperty(const std::string &aKey)
A C++20 property system for arbitrary key-value storage with type safety.