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
3 // found in the LICENSE file.
5 #ifndef UI_AURA_WINDOW_PROPERTY_H_
6 #define UI_AURA_WINDOW_PROPERTY_H_
8 #include "base/basictypes.h"
9 #include "ui/aura/aura_export.h"
10 #include "ui/aura/window.h"
12 // This header should be included by code that defines WindowProperties.
14 // To define a new WindowProperty:
16 // #include "foo/foo_export.h"
17 // #include "ui/aura/window_property.h"
19 // DECLARE_EXPORTED_WINDOW_PROPERTY_TYPE(FOO_EXPORT, MyType);
21 // // Use this to define an exported property that is premitive,
22 // // or a pointer you don't want automatically deleted.
23 // DEFINE_WINDOW_PROPERTY_KEY(MyType, kMyKey, MyDefault);
25 // // Use this to define an exported property whose value is a heap
26 // // allocated object, and has to be owned and freed by the window.
27 // DEFINE_OWNED_WINDOW_PROPERTY_KEY(gfx::Rect, kRestoreBoundsKey, NULL);
29 // // Use this to define a non exported property that is primitive,
30 // // or a pointer you don't want to automatically deleted, and is used
31 // // only in a specific file. This will define the property in an unnamed
32 // // namespace which cannot be accessed from another file.
33 // DEFINE_LOCAL_WINDOW_PROPERTY_KEY(MyType, kMyKey, MyDefault);
37 // To define a new type used for WindowProperty.
39 // // outside all namespaces:
40 // DECLARE_EXPORTED_WINDOW_PROPERTY_TYPE(FOO_EXPORT, MyType)
42 // If a property type is not exported, use DECLARE_WINDOW_PROPERTY_TYPE(MyType)
43 // which is a shorthand for DECLARE_EXPORTED_WINDOW_PROPERTY_TYPE(, MyType).
48 // No single new-style cast works for every conversion to/from int64, so we
49 // need this helper class. A third specialization is needed for bool because
50 // MSVC warning C4800 (forcing value to bool) is not suppressed by an explicit
53 class WindowPropertyCaster
{
55 static int64
ToInt64(T x
) { return static_cast<int64
>(x
); }
56 static T
FromInt64(int64 x
) { return static_cast<T
>(x
); }
59 class WindowPropertyCaster
<T
*> {
61 static int64
ToInt64(T
* x
) { return reinterpret_cast<int64
>(x
); }
62 static T
* FromInt64(int64 x
) { return reinterpret_cast<T
*>(x
); }
65 class WindowPropertyCaster
<bool> {
67 static int64
ToInt64(bool x
) { return static_cast<int64
>(x
); }
68 static bool FromInt64(int64 x
) { return x
!= 0; }
74 struct WindowProperty
{
77 Window::PropertyDeallocator deallocator
;
82 class AURA_EXPORT PropertyHelper
{
85 static void Set(Window
* window
, const WindowProperty
<T
>* property
, T value
) {
86 int64 old
= window
->SetPropertyInternal(
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
);
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
)));
103 static void Clear(Window
* window
, const WindowProperty
<T
>* property
) {
104 window
->SetProperty(property
, property
->default_value
); \
108 } // namespace subtle
112 // Macros to instantiate the property getter/setter template functions.
113 #define DECLARE_EXPORTED_WINDOW_PROPERTY_TYPE(EXPORT, T) \
115 template<> EXPORT void aura::Window::SetProperty( \
116 const WindowProperty<T >* property, T value) { \
117 subtle::PropertyHelper::Set<T>(this, property, value); \
119 template<> EXPORT T Window::GetProperty( \
120 const WindowProperty<T >* property) const { \
121 return subtle::PropertyHelper::Get<T>(this, property); \
123 template<> EXPORT void Window::ClearProperty( \
124 const WindowProperty<T >* property) { \
125 subtle::PropertyHelper::Clear<T>(this, property); \
128 #define DECLARE_WINDOW_PROPERTY_TYPE(T) \
129 DECLARE_EXPORTED_WINDOW_PROPERTY_TYPE(, T)
131 #define DEFINE_WINDOW_PROPERTY_KEY(TYPE, NAME, DEFAULT) \
132 static_assert(sizeof(TYPE) <= sizeof(int64), "property type too large"); \
134 const aura::WindowProperty<TYPE> NAME ## _Value = \
135 {DEFAULT, #NAME, nullptr}; \
137 const aura::WindowProperty<TYPE>* const NAME = & NAME ## _Value;
139 #define DEFINE_LOCAL_WINDOW_PROPERTY_KEY(TYPE, NAME, DEFAULT) \
140 static_assert(sizeof(TYPE) <= sizeof(int64), "property type too large"); \
142 const aura::WindowProperty<TYPE> NAME ## _Value = \
143 {DEFAULT, #NAME, nullptr}; \
144 const aura::WindowProperty<TYPE>* const NAME = & NAME ## _Value; \
147 #define DEFINE_OWNED_WINDOW_PROPERTY_KEY(TYPE, NAME, DEFAULT) \
149 void Deallocator ## NAME (int64 p) { \
150 enum { type_must_be_complete = sizeof(TYPE) }; \
151 delete aura::WindowPropertyCaster<TYPE*>::FromInt64(p); \
153 const aura::WindowProperty<TYPE*> NAME ## _Value = \
154 {DEFAULT,#NAME,&Deallocator ## NAME}; \
156 const aura::WindowProperty<TYPE*>* const NAME = & NAME ## _Value;
158 #endif // UI_AURA_WINDOW_PROPERTY_H_