// Copyright 2017 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef UI_BASE_CLASS_PROPERTY_H_ #define UI_BASE_CLASS_PROPERTY_H_ #include #include #include #include #include "base/time/time.h" #include "ui/base/property_data.h" #include "ui/base/ui_base_export.h" #include "ui/base/ui_base_types.h" // This header should be included by code that defines ClassProperties. // // To define a new ClassProperty: // // #include "foo/foo_export.h" // #include "ui/base/class_property.h" // // DEFINE_EXPORTED_UI_CLASS_PROPERTY_TYPE(FOO_EXPORT, MyType) // namespace foo { // // Use this to define an exported property that is primitive, // // or a pointer you don't want automatically deleted. // DEFINE_UI_CLASS_PROPERTY_KEY(MyType, kMyKey, MyDefault) // // // Use this to define an exported property whose value is a heap // // allocated object, and has to be owned and freed by the class. // DEFINE_OWNED_UI_CLASS_PROPERTY_KEY(gfx::Rect, kRestoreBoundsKey, nullptr) // // } // foo namespace // // To define a new type used for ClassProperty. // // // outside all namespaces: // DEFINE_EXPORTED_UI_CLASS_PROPERTY_TYPE(FOO_EXPORT, MyType) // // If a property type is not exported, use // DEFINE_UI_CLASS_PROPERTY_TYPE(MyType) which is a shorthand for // DEFINE_EXPORTED_UI_CLASS_PROPERTY_TYPE(, MyType). // // If the properties are used outside the file where they are defined // their accessor methods should also be declared in a suitable header // using DECLARE_EXPORTED_UI_CLASS_PROPERTY_TYPE(FOO_EXPORT, MyType) namespace ui { // Type of a function to delete a property that this window owns. using PropertyDeallocator = void(*)(int64_t value); template struct ClassProperty { T default_value; const char* name; PropertyDeallocator deallocator; }; namespace subtle { class PropertyHelper; } class UI_BASE_EXPORT PropertyHandler { public: PropertyHandler(); ~PropertyHandler(); // Sets the |value| of the given class |property|. Setting to the default // value (e.g., NULL) removes the property. The caller is responsible for the // lifetime of any object set as a property on the class. template void SetProperty(const ClassProperty* property, T value); // Returns the value of the given class |property|. Returns the // property-specific default value if the property was not previously set. template T GetProperty(const ClassProperty* property) const; // Sets the |property| to its default value. Useful for avoiding a cast when // setting to NULL. template void ClearProperty(const ClassProperty* property); // Returns the value of all properties with a non-default value. std::set GetAllPropertyKeys() const; protected: friend class subtle::PropertyHelper; // Called from SetPropertyInternal() prior to changing the value. If // |is_value_changing| is false, the new value is the same as the old value // (in other words, the value isn't really changing, but observers will // still be notified). virtual std::unique_ptr BeforePropertyChange( const void* key, bool is_value_changing); virtual void AfterPropertyChange(const void* key, int64_t old_value, std::unique_ptr data) {} void ClearProperties(); // Called by the public {Set,Get,Clear}Property functions. 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) const; private: // Value struct to keep the name and deallocator for this property. // Key cannot be used for this purpose because it can be char* or // ClassProperty<>. struct Value { const char* name; int64_t value; PropertyDeallocator deallocator; }; std::map prop_map_; }; namespace { // No single new-style cast works for every conversion to/from int64_t, so we // need this helper class. A third specialization is needed for bool because // MSVC warning C4800 (forcing value to bool) is not suppressed by an explicit // cast (!). template class ClassPropertyCaster { public: static int64_t ToInt64(T x) { return static_cast(x); } static T FromInt64(int64_t x) { return static_cast(x); } }; template class ClassPropertyCaster { public: static int64_t ToInt64(T* x) { return reinterpret_cast(x); } static T* FromInt64(int64_t x) { return reinterpret_cast(x); } }; template<> class ClassPropertyCaster { public: static int64_t ToInt64(bool x) { return static_cast(x); } static bool FromInt64(int64_t x) { return x != 0; } }; template <> class ClassPropertyCaster { public: static int64_t ToInt64(base::TimeDelta x) { return x.InMicroseconds(); } static base::TimeDelta FromInt64(int64_t x) { return base::TimeDelta::FromMicroseconds(x); } }; } // namespace namespace subtle { class UI_BASE_EXPORT PropertyHelper { public: template static void Set(::ui::PropertyHandler* handler, const ::ui::ClassProperty* property, T value) { int64_t old = handler->SetPropertyInternal( property, property->name, value == property->default_value ? nullptr : property->deallocator, ClassPropertyCaster::ToInt64(value), ClassPropertyCaster::ToInt64(property->default_value)); if (property->deallocator && old != ClassPropertyCaster::ToInt64(property->default_value)) { (*property->deallocator)(old); } } template static T Get(const ::ui::PropertyHandler* handler, const ::ui::ClassProperty* property) { return ClassPropertyCaster::FromInt64(handler->GetPropertyInternal( property, ClassPropertyCaster::ToInt64(property->default_value))); } template static void Clear(::ui::PropertyHandler* handler, const ::ui::ClassProperty* property) { handler->SetProperty(property, property->default_value); } }; } // namespace subtle } // namespace ui // Macros to declare the property getter/setter template functions. #define DECLARE_EXPORTED_UI_CLASS_PROPERTY_TYPE(EXPORT, T) \ namespace ui { \ template <> \ EXPORT void PropertyHandler::SetProperty(const ClassProperty* property, \ T value); \ template <> \ EXPORT T \ PropertyHandler::GetProperty(const ClassProperty* property) const; \ template <> \ EXPORT void PropertyHandler::ClearProperty( \ const ClassProperty* property); \ } // namespace ui // Macros to instantiate the property getter/setter template functions. #define DEFINE_EXPORTED_UI_CLASS_PROPERTY_TYPE(EXPORT, T) \ namespace ui { \ template <> \ EXPORT void PropertyHandler::SetProperty(const ClassProperty* property, \ T value) { \ subtle::PropertyHelper::Set(this, property, value); \ } \ template <> \ EXPORT T \ PropertyHandler::GetProperty(const ClassProperty* property) const { \ return subtle::PropertyHelper::Get(this, property); \ } \ template <> \ EXPORT void PropertyHandler::ClearProperty( \ const ClassProperty* property) { \ subtle::PropertyHelper::Clear(this, property); \ } \ } // namespace ui #define DEFINE_UI_CLASS_PROPERTY_TYPE(T) \ DEFINE_EXPORTED_UI_CLASS_PROPERTY_TYPE(, T) #define DEFINE_UI_CLASS_PROPERTY_KEY(TYPE, NAME, DEFAULT) \ static_assert(sizeof(TYPE) <= sizeof(int64_t), "property type too large"); \ const ::ui::ClassProperty NAME##_Value = {DEFAULT, #NAME, nullptr}; \ const ::ui::ClassProperty* const NAME = &NAME##_Value; #define DEFINE_OWNED_UI_CLASS_PROPERTY_KEY(TYPE, NAME, DEFAULT) \ namespace { \ void Deallocator##NAME(int64_t p) { \ enum { type_must_be_complete = sizeof(TYPE) }; \ delete ::ui::ClassPropertyCaster::FromInt64(p); \ } \ const ::ui::ClassProperty NAME##_Value = {DEFAULT, #NAME, \ &Deallocator##NAME}; \ } /* namespace */ \ const ::ui::ClassProperty* const NAME = &NAME##_Value; #endif // UI_BASE_CLASS_PROPERTY_H_