#ifndef UI_BASE_CLASS_PROPERTY_H_
#define UI_BASE_CLASS_PROPERTY_H_
#include <stdint.h>
#include <concepts>
#include <map>
#include <memory>
#include <set>
#include <type_traits>
#include <utility>
#include "base/bit_cast.h"
#include "base/component_export.h"
#include "base/time/time.h"
#include "base/types/is_complete.h"
#include "ui/base/ui_base_types.h"
namespace ui {
using PropertyDeallocator = void(*)(int64_t value);
template<typename T>
struct ClassProperty {
T default_value;
const char* name;
bool cascading = false;
PropertyDeallocator deallocator;
};
namespace subtle {
class PropertyHelper;
}
class COMPONENT_EXPORT(UI_BASE) PropertyHandler {
public:
PropertyHandler();
PropertyHandler(PropertyHandler&&);
PropertyHandler& operator=(PropertyHandler&&);
virtual ~PropertyHandler();
template <typename T>
T SetProperty(const ClassProperty<T>* property, T value);
template <typename T, typename U>
requires(std::constructible_from<T, U &&> &&
!std::same_as<T, std::remove_cvref_t<U>>)
T SetProperty(const ClassProperty<T>* property, U&& value);
template <typename T, typename U>
requires(!std::same_as<T*, std::remove_cvref_t<U>> &&
std::constructible_from<T, U &&> && std::assignable_from<T&, U &&>)
T* SetProperty(const ClassProperty<T*>* property, U&& value);
template <typename T, typename U>
requires(std::convertible_to<U*, T*>)
T* SetProperty(const ClassProperty<T*>* property, std::unique_ptr<U> value);
template<typename T>
T GetProperty(const ClassProperty<T>* property) const;
template <typename T>
void ClearProperty(const ClassProperty<T>* property);
void AcquireAllPropertiesFrom(PropertyHandler&& other);
std::set<const void*> GetAllPropertyKeys() const;
protected:
friend class subtle::PropertyHelper;
virtual void AfterPropertyChange(const void* key, int64_t old_value) {}
virtual PropertyHandler* GetParentHandler() const;
void ClearProperties();
int64_t SetPropertyInternal(const void* key,
const char* name,
PropertyDeallocator deallocator,
int64_t value,
int64_t default_value);
int64_t GetPropertyInternal(const void* key,
int64_t default_value,
bool search_parent) const;
private:
struct Value {
const char* name;
int64_t value;
PropertyDeallocator deallocator;
};
using PropMap = std::map<const void*, Value>;
PropMap prop_map_;
};
template<typename T>
class ClassPropertyCaster {
public:
static int64_t ToInt64(T x)
requires(sizeof(T) <= sizeof(int64_t))
{
return static_cast<int64_t>(x);
}
static T FromInt64(int64_t x) { return static_cast<T>(x); }
};
template<typename T>
class ClassPropertyCaster<T*> {
public:
static int64_t ToInt64(T* x) {
static_assert(sizeof(T*) <= sizeof(int64_t));
return reinterpret_cast<int64_t>(x);
}
static T* FromInt64(int64_t x) { return reinterpret_cast<T*>(x); }
};
template <>
class ClassPropertyCaster<base::TimeDelta> {
public:
static int64_t ToInt64(base::TimeDelta x) { return x.InMicroseconds(); }
static base::TimeDelta FromInt64(int64_t x) { return base::Microseconds(x); }
};
template <>
class ClassPropertyCaster<float> {
public:
static int64_t ToInt64(float x) {
static_assert(sizeof(float) == sizeof(int32_t));
return base::bit_cast<int32_t>(x);
}
static float FromInt64(int64_t x) {
return base::bit_cast<float>(static_cast<int32_t>(x));
}
};
namespace subtle {
class COMPONENT_EXPORT(UI_BASE) PropertyHelper {
public:
template <typename T>
static T Set(::ui::PropertyHandler* handler,
const ::ui::ClassProperty<T>* property,
T value) {
const int64_t old = handler->SetPropertyInternal(
property, property->name, property->deallocator,
ClassPropertyCaster<T>::ToInt64(value),
ClassPropertyCaster<T>::ToInt64(property->default_value));
if (property->deallocator &&
old != ClassPropertyCaster<T>::ToInt64(property->default_value)) {
(*property->deallocator)(old);
}
return value;
}
template <typename T>
static T Get(const ::ui::PropertyHandler* handler,
const ::ui::ClassProperty<T>* property,
bool allow_cascade) {
return ClassPropertyCaster<T>::FromInt64(handler->GetPropertyInternal(
property, ClassPropertyCaster<T>::ToInt64(property->default_value),
property->cascading && allow_cascade));
}
template <typename T>
static void Clear(::ui::PropertyHandler* handler,
const ::ui::ClassProperty<T>* property) {
Set(handler, property, property->default_value);
}
};
}
template <typename T, typename U>
requires(std::constructible_from<T, U &&> &&
!std::same_as<T, std::remove_cvref_t<U>>)
T PropertyHandler::SetProperty(const ClassProperty<T>* property, U&& value) {
return SetProperty(property, T(std::forward<U>(value)));
}
template <typename T, typename U>
requires(!std::same_as<T*, std::remove_cvref_t<U>> &&
std::constructible_from<T, U &&> && std::assignable_from<T&, U &&>)
T* PropertyHandler::SetProperty(const ClassProperty<T*>* property, U&& value) {
if (T* const old = subtle::PropertyHelper::Get<T*>(this, property, false)) {
T temp = std::exchange(*old, std::forward<U>(value));
AfterPropertyChange(property, ClassPropertyCaster<T*>::ToInt64(&temp));
return old;
}
return SetProperty(property, std::make_unique<T>(std::forward<U>(value)));
}
template <typename T, typename U>
requires(std::convertible_to<U*, T*>)
T* PropertyHandler::SetProperty(const ClassProperty<T*>* property,
std::unique_ptr<U> value) {
DCHECK(property->deallocator);
return subtle::PropertyHelper::Set<T*>(this, property, value.release());
}
}
#define DECLARE_EXPORTED_UI_CLASS_PROPERTY_TYPE(EXPORT, T) \
namespace ui { \
template <> \
EXPORT T PropertyHandler::SetProperty(const ClassProperty<T>* property, \
T value); \
template <> \
EXPORT T \
PropertyHandler::GetProperty(const ClassProperty<T>* property) const; \
template <> \
EXPORT void PropertyHandler::ClearProperty( \
const ClassProperty<T>* property); \
}
#define DEFINE_EXPORTED_UI_CLASS_PROPERTY_TYPE(EXPORT, T) \
namespace ui { \
template <> \
EXPORT T PropertyHandler::SetProperty(const ClassProperty<T>* property, \
T value) { \
\
\
\
\
return subtle::PropertyHelper::Set<T>(this, property, value); \
} \
template <> \
EXPORT T \
PropertyHandler::GetProperty(const ClassProperty<T>* property) const { \
return subtle::PropertyHelper::Get<T>(this, property, true); \
} \
template <> \
EXPORT void PropertyHandler::ClearProperty( \
const ClassProperty<T>* property) { \
subtle::PropertyHelper::Clear<T>(this, property); \
} \
}
#define DEFINE_UI_CLASS_PROPERTY_TYPE(T) \
DEFINE_EXPORTED_UI_CLASS_PROPERTY_TYPE(, T)
#define DEFINE_UI_CLASS_PROPERTY_KEY_IMPL(TYPE, NAME, DEFAULT, CASCADES) \
static_assert(sizeof(TYPE) <= sizeof(int64_t), "property type too large"); \
const ::ui::ClassProperty<TYPE> NAME##_Value = {DEFAULT, #NAME, CASCADES, \
nullptr}; \
const ::ui::ClassProperty<TYPE>* const NAME = &NAME##_Value;
#define DEFINE_UI_CLASS_PROPERTY_KEY(TYPE, NAME, DEFAULT) \
DEFINE_UI_CLASS_PROPERTY_KEY_IMPL(TYPE, NAME, DEFAULT, false)
#define DEFINE_CASCADING_UI_CLASS_PROPERTY_KEY(TYPE, NAME, DEFAULT) \
DEFINE_UI_CLASS_PROPERTY_KEY_IMPL(TYPE, NAME, DEFAULT, true)
#define DEFINE_OWNED_UI_CLASS_PROPERTY_KEY_IMPL(TYPE, NAME, CASCADES) \
static_assert(base::IsComplete<TYPE>); \
static_assert(sizeof(TYPE*) <= sizeof(int64_t)); \
namespace { \
void Deallocator##NAME(int64_t p) { \
delete ::ui::ClassPropertyCaster<TYPE*>::FromInt64(p); \
} \
constexpr ::ui::ClassProperty<TYPE*> NAME##_Value = { \
nullptr, #NAME, CASCADES, &Deallocator##NAME}; \
} \
constexpr const ::ui::ClassProperty<TYPE*>* NAME = &NAME##_Value;
#define DEFINE_OWNED_UI_CLASS_PROPERTY_KEY(TYPE, NAME) \
DEFINE_OWNED_UI_CLASS_PROPERTY_KEY_IMPL(TYPE, NAME, false)
#define DEFINE_CASCADING_OWNED_UI_CLASS_PROPERTY_KEY(TYPE, NAME) \
DEFINE_OWNED_UI_CLASS_PROPERTY_KEY_IMPL(TYPE, NAME, true)
#endif