OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 MOJO_SERVICES_VIEW_MANAGER_PUBLIC_CPP_VIEW_PROPERTY_H_ | 5 #ifndef MOJO_SERVICES_VIEW_MANAGER_PUBLIC_CPP_VIEW_PROPERTY_H_ |
6 #define MOJO_SERVICES_VIEW_MANAGER_PUBLIC_CPP_VIEW_PROPERTY_H_ | 6 #define MOJO_SERVICES_VIEW_MANAGER_PUBLIC_CPP_VIEW_PROPERTY_H_ |
7 | 7 |
8 #include "base/basictypes.h" | 8 #include <stdint.h> |
9 | 9 |
10 // This header should be included by code that defines ViewProperties. It | 10 // This header should be included by code that defines ViewProperties. It |
11 // should not be included by code that only gets and sets ViewProperties. | 11 // should not be included by code that only gets and sets ViewProperties. |
12 // | 12 // |
13 // To define a new ViewProperty: | 13 // To define a new ViewProperty: |
14 // | 14 // |
15 // #include "view_manager/public/cpp/view_property.h" | 15 // #include "view_manager/public/cpp/view_property.h" |
16 // | 16 // |
17 // DECLARE_EXPORTED_VIEW_PROPERTY_TYPE(FOO_EXPORT, MyType); | 17 // DECLARE_EXPORTED_VIEW_PROPERTY_TYPE(FOO_EXPORT, MyType); |
18 // namespace foo { | 18 // namespace foo { |
(...skipping 17 matching lines...) Expand all Loading... |
36 // | 36 // |
37 // // outside all namespaces: | 37 // // outside all namespaces: |
38 // DECLARE_EXPORTED_VIEW_PROPERTY_TYPE(FOO_EXPORT, MyType) | 38 // DECLARE_EXPORTED_VIEW_PROPERTY_TYPE(FOO_EXPORT, MyType) |
39 // | 39 // |
40 // If a property type is not exported, use DECLARE_VIEW_PROPERTY_TYPE(MyType) | 40 // If a property type is not exported, use DECLARE_VIEW_PROPERTY_TYPE(MyType) |
41 // which is a shorthand for DECLARE_EXPORTED_VIEW_PROPERTY_TYPE(, MyType). | 41 // which is a shorthand for DECLARE_EXPORTED_VIEW_PROPERTY_TYPE(, MyType). |
42 | 42 |
43 namespace mojo { | 43 namespace mojo { |
44 namespace { | 44 namespace { |
45 | 45 |
46 // No single new-style cast works for every conversion to/from int64, so we | 46 // No single new-style cast works for every conversion to/from int64_t, so we |
47 // need this helper class. A third specialization is needed for bool because | 47 // need this helper class. A third specialization is needed for bool because |
48 // MSVC warning C4800 (forcing value to bool) is not suppressed by an explicit | 48 // MSVC warning C4800 (forcing value to bool) is not suppressed by an explicit |
49 // cast (!). | 49 // cast (!). |
50 template <typename T> | 50 template <typename T> |
51 class ViewPropertyCaster { | 51 class ViewPropertyCaster { |
52 public: | 52 public: |
53 static int64 ToInt64(T x) { return static_cast<int64>(x); } | 53 static int64_t ToInt64(T x) { return static_cast<int64_t>(x); } |
54 static T FromInt64(int64 x) { return static_cast<T>(x); } | 54 static T FromInt64(int64_t x) { return static_cast<T>(x); } |
55 }; | 55 }; |
56 template <typename T> | 56 template <typename T> |
57 class ViewPropertyCaster<T*> { | 57 class ViewPropertyCaster<T*> { |
58 public: | 58 public: |
59 static int64 ToInt64(T* x) { return reinterpret_cast<int64>(x); } | 59 static int64_t ToInt64(T* x) { return reinterpret_cast<int64_t>(x); } |
60 static T* FromInt64(int64 x) { return reinterpret_cast<T*>(x); } | 60 static T* FromInt64(int64_t x) { return reinterpret_cast<T*>(x); } |
61 }; | 61 }; |
62 template <> | 62 template <> |
63 class ViewPropertyCaster<bool> { | 63 class ViewPropertyCaster<bool> { |
64 public: | 64 public: |
65 static int64 ToInt64(bool x) { return static_cast<int64>(x); } | 65 static int64_t ToInt64(bool x) { return static_cast<int64_t>(x); } |
66 static bool FromInt64(int64 x) { return x != 0; } | 66 static bool FromInt64(int64_t x) { return x != 0; } |
67 }; | 67 }; |
68 | 68 |
69 } // namespace | 69 } // namespace |
70 | 70 |
71 template <typename T> | 71 template <typename T> |
72 struct ViewProperty { | 72 struct ViewProperty { |
73 T default_value; | 73 T default_value; |
74 const char* name; | 74 const char* name; |
75 View::PropertyDeallocator deallocator; | 75 View::PropertyDeallocator deallocator; |
76 }; | 76 }; |
77 | 77 |
78 template <typename T> | 78 template <typename T> |
79 void View::SetLocalProperty(const ViewProperty<T>* property, T value) { | 79 void View::SetLocalProperty(const ViewProperty<T>* property, T value) { |
80 int64 old = SetLocalPropertyInternal( | 80 int64_t old = SetLocalPropertyInternal( |
81 property, property->name, | 81 property, property->name, |
82 value == property->default_value ? nullptr : property->deallocator, | 82 value == property->default_value ? nullptr : property->deallocator, |
83 ViewPropertyCaster<T>::ToInt64(value), | 83 ViewPropertyCaster<T>::ToInt64(value), |
84 ViewPropertyCaster<T>::ToInt64(property->default_value)); | 84 ViewPropertyCaster<T>::ToInt64(property->default_value)); |
85 if (property->deallocator && | 85 if (property->deallocator && |
86 old != ViewPropertyCaster<T>::ToInt64(property->default_value)) { | 86 old != ViewPropertyCaster<T>::ToInt64(property->default_value)) { |
87 (*property->deallocator)(old); | 87 (*property->deallocator)(old); |
88 } | 88 } |
89 } | 89 } |
90 | 90 |
(...skipping 13 matching lines...) Expand all Loading... |
104 // Macros to instantiate the property getter/setter template functions. | 104 // Macros to instantiate the property getter/setter template functions. |
105 #define DECLARE_EXPORTED_VIEW_PROPERTY_TYPE(EXPORT, T) \ | 105 #define DECLARE_EXPORTED_VIEW_PROPERTY_TYPE(EXPORT, T) \ |
106 template EXPORT void mojo::View::SetLocalProperty( \ | 106 template EXPORT void mojo::View::SetLocalProperty( \ |
107 const mojo::ViewProperty<T>*, T); \ | 107 const mojo::ViewProperty<T>*, T); \ |
108 template EXPORT T mojo::View::GetLocalProperty(const mojo::ViewProperty<T>*) \ | 108 template EXPORT T mojo::View::GetLocalProperty(const mojo::ViewProperty<T>*) \ |
109 const; \ | 109 const; \ |
110 template EXPORT void mojo::View::ClearLocalProperty( \ | 110 template EXPORT void mojo::View::ClearLocalProperty( \ |
111 const mojo::ViewProperty<T>*); | 111 const mojo::ViewProperty<T>*); |
112 #define DECLARE_VIEW_PROPERTY_TYPE(T) DECLARE_EXPORTED_VIEW_PROPERTY_TYPE(, T) | 112 #define DECLARE_VIEW_PROPERTY_TYPE(T) DECLARE_EXPORTED_VIEW_PROPERTY_TYPE(, T) |
113 | 113 |
114 #define DEFINE_VIEW_PROPERTY_KEY(TYPE, NAME, DEFAULT) \ | 114 #define DEFINE_VIEW_PROPERTY_KEY(TYPE, NAME, DEFAULT) \ |
115 COMPILE_ASSERT(sizeof(TYPE) <= sizeof(int64), property_type_too_large); \ | 115 COMPILE_ASSERT(sizeof(TYPE) <= sizeof(int64_t), property_type_too_large); \ |
116 namespace { \ | 116 namespace { \ |
117 const mojo::ViewProperty<TYPE> NAME##_Value = {DEFAULT, #NAME, nullptr}; \ | 117 const mojo::ViewProperty<TYPE> NAME##_Value = {DEFAULT, #NAME, nullptr}; \ |
118 } \ | 118 } \ |
119 const mojo::ViewProperty<TYPE>* const NAME = &NAME##_Value; | 119 const mojo::ViewProperty<TYPE>* const NAME = &NAME##_Value; |
120 | 120 |
121 #define DEFINE_LOCAL_VIEW_PROPERTY_KEY(TYPE, NAME, DEFAULT) \ | 121 #define DEFINE_LOCAL_VIEW_PROPERTY_KEY(TYPE, NAME, DEFAULT) \ |
122 COMPILE_ASSERT(sizeof(TYPE) <= sizeof(int64), property_type_too_large); \ | 122 COMPILE_ASSERT(sizeof(TYPE) <= sizeof(int64_t), property_type_too_large); \ |
123 namespace { \ | 123 namespace { \ |
124 const mojo::ViewProperty<TYPE> NAME##_Value = {DEFAULT, #NAME, nullptr}; \ | 124 const mojo::ViewProperty<TYPE> NAME##_Value = {DEFAULT, #NAME, nullptr}; \ |
125 const mojo::ViewProperty<TYPE>* const NAME = &NAME##_Value; \ | 125 const mojo::ViewProperty<TYPE>* const NAME = &NAME##_Value; \ |
126 } | 126 } |
127 | 127 |
128 #define DEFINE_OWNED_VIEW_PROPERTY_KEY(TYPE, NAME, DEFAULT) \ | 128 #define DEFINE_OWNED_VIEW_PROPERTY_KEY(TYPE, NAME, DEFAULT) \ |
129 namespace { \ | 129 namespace { \ |
130 void Deallocator##NAME(int64 p) { \ | 130 void Deallocator##NAME(int64_t p) { \ |
131 enum { type_must_be_complete = sizeof(TYPE) }; \ | 131 enum { type_must_be_complete = sizeof(TYPE) }; \ |
132 delete mojo::ViewPropertyCaster<TYPE*>::FromInt64(p); \ | 132 delete mojo::ViewPropertyCaster<TYPE*>::FromInt64(p); \ |
133 } \ | 133 } \ |
134 const mojo::ViewProperty<TYPE*> NAME##_Value = {DEFAULT, \ | 134 const mojo::ViewProperty<TYPE*> NAME##_Value = {DEFAULT, \ |
135 #NAME, \ | 135 #NAME, \ |
136 &Deallocator##NAME}; \ | 136 &Deallocator##NAME}; \ |
137 } \ | 137 } \ |
138 const mojo::ViewProperty<TYPE*>* const NAME = &NAME##_Value; | 138 const mojo::ViewProperty<TYPE*>* const NAME = &NAME##_Value; |
139 | 139 |
140 #endif // MOJO_SERVICES_VIEW_MANAGER_PUBLIC_CPP_VIEW_PROPERTY_H_ | 140 #endif // MOJO_SERVICES_VIEW_MANAGER_PUBLIC_CPP_VIEW_PROPERTY_H_ |
OLD | NEW |