| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef UI_AURA_WINDOW_PROPERTY_H_ | 5 #ifndef UI_AURA_WINDOW_PROPERTY_H_ |
| 6 #define UI_AURA_WINDOW_PROPERTY_H_ | 6 #define UI_AURA_WINDOW_PROPERTY_H_ |
| 7 | 7 |
| 8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
| 9 #include "ui/aura/aura_export.h" | 9 #include "ui/aura/aura_export.h" |
| 10 #include "ui/aura/window.h" | 10 #include "ui/aura/window.h" |
| 11 | 11 |
| 12 // This header should be included by code that defines WindowProperties. It | 12 // This header should be included by code that defines WindowProperties. |
| 13 // should not be included by code that only gets and sets WindowProperties. | |
| 14 // | 13 // |
| 15 // To define a new WindowProperty: | 14 // To define a new WindowProperty: |
| 16 // | 15 // |
| 17 // #include "foo/foo_export.h" | 16 // #include "foo/foo_export.h" |
| 18 // #include "ui/aura/window_property.h" | 17 // #include "ui/aura/window_property.h" |
| 19 // | 18 // |
| 20 // DECLARE_EXPORTED_WINDOW_PROPERTY_TYPE(FOO_EXPORT, MyType); | 19 // DECLARE_EXPORTED_WINDOW_PROPERTY_TYPE(FOO_EXPORT, MyType); |
| 21 // namespace foo { | 20 // namespace foo { |
| 22 // // Use this to define an exported property that is premitive, | 21 // // Use this to define an exported property that is premitive, |
| 23 // // or a pointer you don't want automatically deleted. | 22 // // or a pointer you don't want automatically deleted. |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 | 70 |
| 72 } // namespace | 71 } // namespace |
| 73 | 72 |
| 74 template<typename T> | 73 template<typename T> |
| 75 struct WindowProperty { | 74 struct WindowProperty { |
| 76 T default_value; | 75 T default_value; |
| 77 const char* name; | 76 const char* name; |
| 78 Window::PropertyDeallocator deallocator; | 77 Window::PropertyDeallocator deallocator; |
| 79 }; | 78 }; |
| 80 | 79 |
| 81 template<typename T> | 80 namespace subtle { |
| 82 void Window::SetProperty(const WindowProperty<T>* property, T value) { | 81 |
| 83 int64 old = SetPropertyInternal( | 82 class AURA_EXPORT PropertyHelper { |
| 84 property, | 83 public: |
| 85 property->name, | 84 template<typename T> |
| 86 value == property->default_value ? NULL : property->deallocator, | 85 static void Set(Window* window, const WindowProperty<T>* property, T value) { |
| 87 WindowPropertyCaster<T>::ToInt64(value), | 86 int64 old = window->SetPropertyInternal( |
| 88 WindowPropertyCaster<T>::ToInt64(property->default_value)); | 87 property, |
| 89 if (property->deallocator && | 88 property->name, |
| 90 old != WindowPropertyCaster<T>::ToInt64(property->default_value)) { | 89 value == property->default_value ? NULL : property->deallocator, |
| 91 (*property->deallocator)(old); | 90 WindowPropertyCaster<T>::ToInt64(value), |
| 91 WindowPropertyCaster<T>::ToInt64(property->default_value)); |
| 92 if (property->deallocator && |
| 93 old != WindowPropertyCaster<T>::ToInt64(property->default_value)) { |
| 94 (*property->deallocator)(old); |
| 95 } |
| 92 } | 96 } |
| 93 } | 97 template<typename T> |
| 98 static T Get(const Window* window, const WindowProperty<T>* property) { |
| 99 return WindowPropertyCaster<T>::FromInt64(window->GetPropertyInternal( |
| 100 property, WindowPropertyCaster<T>::ToInt64(property->default_value))); |
| 101 } |
| 102 template<typename T> |
| 103 static void Clear(Window* window, const WindowProperty<T>* property) { |
| 104 window->SetProperty(property, property->default_value); \ |
| 105 } |
| 106 }; |
| 94 | 107 |
| 95 template<typename T> | 108 } // namespace subtle |
| 96 T Window::GetProperty(const WindowProperty<T>* property) const { | |
| 97 return WindowPropertyCaster<T>::FromInt64(GetPropertyInternal( | |
| 98 property, WindowPropertyCaster<T>::ToInt64(property->default_value))); | |
| 99 } | |
| 100 | |
| 101 template<typename T> | |
| 102 void Window::ClearProperty(const WindowProperty<T>* property) { | |
| 103 SetProperty(property, property->default_value); | |
| 104 } | |
| 105 | 109 |
| 106 } // namespace aura | 110 } // namespace aura |
| 107 | 111 |
| 108 // Macros to instantiate the property getter/setter template functions. | 112 // Macros to instantiate the property getter/setter template functions. |
| 109 #define DECLARE_EXPORTED_WINDOW_PROPERTY_TYPE(EXPORT, T) \ | 113 #define DECLARE_EXPORTED_WINDOW_PROPERTY_TYPE(EXPORT, T) \ |
| 110 template EXPORT void aura::Window::SetProperty( \ | 114 template<> EXPORT void aura::Window::SetProperty( \ |
| 111 const aura::WindowProperty<T >*, T); \ | 115 const aura::WindowProperty<T >* property, T value) { \ |
| 112 template EXPORT T aura::Window::GetProperty( \ | 116 subtle::PropertyHelper::Set<T>(this, property, value); \ |
| 113 const aura::WindowProperty<T >*) const; \ | 117 } \ |
| 114 template EXPORT void aura::Window::ClearProperty( \ | 118 template<> EXPORT T aura::Window::GetProperty( \ |
| 115 const aura::WindowProperty<T >*); | 119 const aura::WindowProperty<T >* property) const { \ |
| 120 return subtle::PropertyHelper::Get<T>(this, property); \ |
| 121 } \ |
| 122 template<> EXPORT void aura::Window::ClearProperty( \ |
| 123 const aura::WindowProperty<T >* property) { \ |
| 124 subtle::PropertyHelper::Clear<T>(this, property); \ |
| 125 } |
| 116 #define DECLARE_WINDOW_PROPERTY_TYPE(T) \ | 126 #define DECLARE_WINDOW_PROPERTY_TYPE(T) \ |
| 117 DECLARE_EXPORTED_WINDOW_PROPERTY_TYPE(, T) | 127 DECLARE_EXPORTED_WINDOW_PROPERTY_TYPE(, T) |
| 118 | 128 |
| 119 #define DEFINE_WINDOW_PROPERTY_KEY(TYPE, NAME, DEFAULT) \ | 129 #define DEFINE_WINDOW_PROPERTY_KEY(TYPE, NAME, DEFAULT) \ |
| 120 COMPILE_ASSERT(sizeof(TYPE) <= sizeof(int64), property_type_too_large); \ | 130 COMPILE_ASSERT(sizeof(TYPE) <= sizeof(int64), property_type_too_large); \ |
| 121 namespace { \ | 131 namespace { \ |
| 122 const aura::WindowProperty<TYPE> NAME ## _Value = {DEFAULT, #NAME, NULL}; \ | 132 const aura::WindowProperty<TYPE> NAME ## _Value = {DEFAULT, #NAME, NULL}; \ |
| 123 } \ | 133 } \ |
| 124 const aura::WindowProperty<TYPE>* const NAME = & NAME ## _Value; | 134 const aura::WindowProperty<TYPE>* const NAME = & NAME ## _Value; |
| 125 | 135 |
| 126 #define DEFINE_LOCAL_WINDOW_PROPERTY_KEY(TYPE, NAME, DEFAULT) \ | 136 #define DEFINE_LOCAL_WINDOW_PROPERTY_KEY(TYPE, NAME, DEFAULT) \ |
| 127 COMPILE_ASSERT(sizeof(TYPE) <= sizeof(int64), property_type_too_large); \ | 137 COMPILE_ASSERT(sizeof(TYPE) <= sizeof(int64), property_type_too_large); \ |
| 128 namespace { \ | 138 namespace { \ |
| 129 const aura::WindowProperty<TYPE> NAME ## _Value = {DEFAULT, #NAME, NULL}; \ | 139 const aura::WindowProperty<TYPE> NAME ## _Value = {DEFAULT, #NAME, NULL}; \ |
| 130 const aura::WindowProperty<TYPE>* const NAME = & NAME ## _Value; \ | 140 const aura::WindowProperty<TYPE>* const NAME = & NAME ## _Value; \ |
| 131 } | 141 } |
| 132 | 142 |
| 133 #define DEFINE_OWNED_WINDOW_PROPERTY_KEY(TYPE, NAME, DEFAULT) \ | 143 #define DEFINE_OWNED_WINDOW_PROPERTY_KEY(TYPE, NAME, DEFAULT) \ |
| 134 namespace { \ | 144 namespace { \ |
| 135 void Deallocator ## NAME (int64 p) { \ | 145 void Deallocator ## NAME (int64 p) { \ |
| 136 enum { type_must_be_complete = sizeof(TYPE) }; \ | 146 enum { type_must_be_complete = sizeof(TYPE) }; \ |
| 137 delete aura::WindowPropertyCaster<TYPE*>::FromInt64(p); \ | 147 delete aura::WindowPropertyCaster<TYPE*>::FromInt64(p); \ |
| 138 } \ | 148 } \ |
| 139 const aura::WindowProperty<TYPE*> NAME ## _Value = \ | 149 const aura::WindowProperty<TYPE*> NAME ## _Value = \ |
| 140 {DEFAULT,#NAME,&Deallocator ## NAME}; \ | 150 {DEFAULT,#NAME,&Deallocator ## NAME}; \ |
| 141 } \ | 151 } \ |
| 142 const aura::WindowProperty<TYPE*>* const NAME = & NAME ## _Value; | 152 const aura::WindowProperty<TYPE*>* const NAME = & NAME ## _Value; |
| 143 | 153 |
| 144 #endif // UI_AURA_WINDOW_PROPERTY_H_ | 154 #endif // UI_AURA_WINDOW_PROPERTY_H_ |
| OLD | NEW |