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 ? nullptr : 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 namespace aura { \ |
111 const aura::WindowProperty<T >*, T); \ | 115 template<> EXPORT void aura::Window::SetProperty( \ |
112 template EXPORT T aura::Window::GetProperty( \ | 116 const WindowProperty<T >* property, T value) { \ |
113 const aura::WindowProperty<T >*) const; \ | 117 subtle::PropertyHelper::Set<T>(this, property, value); \ |
114 template EXPORT void aura::Window::ClearProperty( \ | 118 } \ |
115 const aura::WindowProperty<T >*); | 119 template<> EXPORT T Window::GetProperty( \ |
| 120 const WindowProperty<T >* property) const { \ |
| 121 return subtle::PropertyHelper::Get<T>(this, property); \ |
| 122 } \ |
| 123 template<> EXPORT void Window::ClearProperty( \ |
| 124 const WindowProperty<T >* property) { \ |
| 125 subtle::PropertyHelper::Clear<T>(this, property); \ |
| 126 } \ |
| 127 } |
116 #define DECLARE_WINDOW_PROPERTY_TYPE(T) \ | 128 #define DECLARE_WINDOW_PROPERTY_TYPE(T) \ |
117 DECLARE_EXPORTED_WINDOW_PROPERTY_TYPE(, T) | 129 DECLARE_EXPORTED_WINDOW_PROPERTY_TYPE(, T) |
118 | 130 |
119 #define DEFINE_WINDOW_PROPERTY_KEY(TYPE, NAME, DEFAULT) \ | 131 #define DEFINE_WINDOW_PROPERTY_KEY(TYPE, NAME, DEFAULT) \ |
120 COMPILE_ASSERT(sizeof(TYPE) <= sizeof(int64), property_type_too_large); \ | 132 COMPILE_ASSERT(sizeof(TYPE) <= sizeof(int64), property_type_too_large); \ |
121 namespace { \ | 133 namespace { \ |
122 const aura::WindowProperty<TYPE> NAME ## _Value = {DEFAULT, #NAME, NULL}; \ | 134 const aura::WindowProperty<TYPE> NAME ## _Value = \ |
123 } \ | 135 {DEFAULT, #NAME, nullptr}; \ |
| 136 } \ |
124 const aura::WindowProperty<TYPE>* const NAME = & NAME ## _Value; | 137 const aura::WindowProperty<TYPE>* const NAME = & NAME ## _Value; |
125 | 138 |
126 #define DEFINE_LOCAL_WINDOW_PROPERTY_KEY(TYPE, NAME, DEFAULT) \ | 139 #define DEFINE_LOCAL_WINDOW_PROPERTY_KEY(TYPE, NAME, DEFAULT) \ |
127 COMPILE_ASSERT(sizeof(TYPE) <= sizeof(int64), property_type_too_large); \ | 140 COMPILE_ASSERT(sizeof(TYPE) <= sizeof(int64), property_type_too_large); \ |
128 namespace { \ | 141 namespace { \ |
129 const aura::WindowProperty<TYPE> NAME ## _Value = {DEFAULT, #NAME, NULL}; \ | 142 const aura::WindowProperty<TYPE> NAME ## _Value = \ |
130 const aura::WindowProperty<TYPE>* const NAME = & NAME ## _Value; \ | 143 {DEFAULT, #NAME, nullptr}; \ |
| 144 const aura::WindowProperty<TYPE>* const NAME = & NAME ## _Value; \ |
131 } | 145 } |
132 | 146 |
133 #define DEFINE_OWNED_WINDOW_PROPERTY_KEY(TYPE, NAME, DEFAULT) \ | 147 #define DEFINE_OWNED_WINDOW_PROPERTY_KEY(TYPE, NAME, DEFAULT) \ |
134 namespace { \ | 148 namespace { \ |
135 void Deallocator ## NAME (int64 p) { \ | 149 void Deallocator ## NAME (int64 p) { \ |
136 enum { type_must_be_complete = sizeof(TYPE) }; \ | 150 enum { type_must_be_complete = sizeof(TYPE) }; \ |
137 delete aura::WindowPropertyCaster<TYPE*>::FromInt64(p); \ | 151 delete aura::WindowPropertyCaster<TYPE*>::FromInt64(p); \ |
138 } \ | 152 } \ |
139 const aura::WindowProperty<TYPE*> NAME ## _Value = \ | 153 const aura::WindowProperty<TYPE*> NAME ## _Value = \ |
140 {DEFAULT,#NAME,&Deallocator ## NAME}; \ | 154 {DEFAULT,#NAME,&Deallocator ## NAME}; \ |
141 } \ | 155 } \ |
142 const aura::WindowProperty<TYPE*>* const NAME = & NAME ## _Value; | 156 const aura::WindowProperty<TYPE*>* const NAME = & NAME ## _Value; |
143 | 157 |
144 #endif // UI_AURA_WINDOW_PROPERTY_H_ | 158 #endif // UI_AURA_WINDOW_PROPERTY_H_ |
OLD | NEW |