Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(429)

Side by Side Diff: ui/aura/window_property.h

Issue 2632543003: Refactor and push window properties up to class properties. (Closed)
Patch Set: More build fixes Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #ifndef UI_AURA_WINDOW_PROPERTY_H_
6 #define UI_AURA_WINDOW_PROPERTY_H_
7
8 #include <stdint.h>
9
10 #include "ui/aura/aura_export.h"
11 #include "ui/aura/window.h"
12
13 // This header should be included by code that defines WindowProperties.
14 //
15 // To define a new WindowProperty:
16 //
17 // #include "foo/foo_export.h"
18 // #include "ui/aura/window_property.h"
19 //
20 // DECLARE_EXPORTED_WINDOW_PROPERTY_TYPE(FOO_EXPORT, MyType);
21 // namespace foo {
22 // // Use this to define an exported property that is primitive,
23 // // or a pointer you don't want automatically deleted.
24 // DEFINE_WINDOW_PROPERTY_KEY(MyType, kMyKey, MyDefault);
25 //
26 // // Use this to define an exported property whose value is a heap
27 // // allocated object, and has to be owned and freed by the window.
28 // DEFINE_OWNED_WINDOW_PROPERTY_KEY(gfx::Rect, kRestoreBoundsKey, NULL);
29 //
30 // // Use this to define a non exported property that is primitive,
31 // // or a pointer you don't want to automatically deleted, and is used
32 // // only in a specific file. This will define the property in an unnamed
33 // // namespace which cannot be accessed from another file.
34 // DEFINE_LOCAL_WINDOW_PROPERTY_KEY(MyType, kMyKey, MyDefault);
35 //
36 // } // foo namespace
37 //
38 // To define a new type used for WindowProperty.
39 //
40 // // outside all namespaces:
41 // DECLARE_EXPORTED_WINDOW_PROPERTY_TYPE(FOO_EXPORT, MyType)
42 //
43 // If a property type is not exported, use DECLARE_WINDOW_PROPERTY_TYPE(MyType)
44 // which is a shorthand for DECLARE_EXPORTED_WINDOW_PROPERTY_TYPE(, MyType).
45
46 namespace aura {
47 namespace {
48
49 // No single new-style cast works for every conversion to/from int64_t, so we
50 // need this helper class. A third specialization is needed for bool because
51 // MSVC warning C4800 (forcing value to bool) is not suppressed by an explicit
52 // cast (!).
53 template<typename T>
54 class WindowPropertyCaster {
55 public:
56 static int64_t ToInt64(T x) { return static_cast<int64_t>(x); }
57 static T FromInt64(int64_t x) { return static_cast<T>(x); }
58 };
59 template<typename T>
60 class WindowPropertyCaster<T*> {
61 public:
62 static int64_t ToInt64(T* x) { return reinterpret_cast<int64_t>(x); }
63 static T* FromInt64(int64_t x) { return reinterpret_cast<T*>(x); }
64 };
65 template<>
66 class WindowPropertyCaster<bool> {
67 public:
68 static int64_t ToInt64(bool x) { return static_cast<int64_t>(x); }
69 static bool FromInt64(int64_t x) { return x != 0; }
70 };
71
72 } // namespace
73
74 template<typename T>
75 struct WindowProperty {
76 T default_value;
77 const char* name;
78 Window::PropertyDeallocator deallocator;
79 };
80
81 namespace subtle {
82
83 class AURA_EXPORT PropertyHelper {
84 public:
85 template<typename T>
86 static void Set(Window* window, const WindowProperty<T>* property, T value) {
87 int64_t old = window->SetPropertyInternal(
88 property, 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
108 } // namespace subtle
109
110 } // namespace aura
111
112 // Macros to instantiate the property getter/setter template functions.
113 #define DECLARE_EXPORTED_WINDOW_PROPERTY_TYPE(EXPORT, T) \
114 namespace aura { \
115 template <> \
116 EXPORT void Window::SetProperty(const WindowProperty<T>* property, \
117 T value) { \
118 subtle::PropertyHelper::Set<T>(this, property, value); \
119 } \
120 template <> \
121 EXPORT T Window::GetProperty(const WindowProperty<T>* property) const { \
122 return subtle::PropertyHelper::Get<T>(this, property); \
123 } \
124 template <> \
125 EXPORT void Window::ClearProperty(const WindowProperty<T>* property) { \
126 subtle::PropertyHelper::Clear<T>(this, property); \
127 } \
128 }
129 #define DECLARE_WINDOW_PROPERTY_TYPE(T) \
130 DECLARE_EXPORTED_WINDOW_PROPERTY_TYPE(, T)
131
132 #define DEFINE_WINDOW_PROPERTY_KEY(TYPE, NAME, DEFAULT) \
133 static_assert(sizeof(TYPE) <= sizeof(int64_t), "property type too large"); \
134 namespace { \
135 const aura::WindowProperty<TYPE> NAME##_Value = {DEFAULT, #NAME, nullptr}; \
136 } \
137 const aura::WindowProperty<TYPE>* const NAME = &NAME##_Value;
138
139 #define DEFINE_LOCAL_WINDOW_PROPERTY_KEY(TYPE, NAME, DEFAULT) \
140 static_assert(sizeof(TYPE) <= sizeof(int64_t), "property type too large"); \
141 namespace { \
142 const aura::WindowProperty<TYPE> NAME##_Value = {DEFAULT, #NAME, nullptr}; \
143 const aura::WindowProperty<TYPE>* const NAME = &NAME##_Value; \
144 }
145
146 #define DEFINE_OWNED_WINDOW_PROPERTY_KEY(TYPE, NAME, DEFAULT) \
147 namespace { \
148 void Deallocator##NAME(int64_t p) { \
149 enum { type_must_be_complete = sizeof(TYPE) }; \
150 delete aura::WindowPropertyCaster<TYPE*>::FromInt64(p); \
151 } \
152 const aura::WindowProperty<TYPE*> NAME##_Value = {DEFAULT, #NAME, \
153 &Deallocator##NAME}; \
154 } \
155 const aura::WindowProperty<TYPE*>* const NAME = &NAME##_Value;
156
157 #endif // UI_AURA_WINDOW_PROPERTY_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698