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