| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef MOJO_SERVICES_VIEW_MANAGER_PUBLIC_CPP_VIEW_PROPERTY_H_ | |
| 6 #define MOJO_SERVICES_VIEW_MANAGER_PUBLIC_CPP_VIEW_PROPERTY_H_ | |
| 7 | |
| 8 #include <stdint.h> | |
| 9 | |
| 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. | |
| 12 // | |
| 13 // To define a new ViewProperty: | |
| 14 // | |
| 15 // #include "view_manager/public/cpp/view_property.h" | |
| 16 // | |
| 17 // DECLARE_EXPORTED_VIEW_PROPERTY_TYPE(FOO_EXPORT, MyType); | |
| 18 // namespace foo { | |
| 19 // // Use this to define an exported property that is premitive, | |
| 20 // // or a pointer you don't want automatically deleted. | |
| 21 // DEFINE_VIEW_PROPERTY_KEY(MyType, kMyKey, MyDefault); | |
| 22 // | |
| 23 // // Use this to define an exported property whose value is a heap | |
| 24 // // allocated object, and has to be owned and freed by the view. | |
| 25 // DEFINE_OWNED_VIEW_PROPERTY_KEY(gfx::Rect, kRestoreBoundsKey, nullptr); | |
| 26 // | |
| 27 // // Use this to define a non exported property that is primitive, | |
| 28 // // or a pointer you don't want to automatically deleted, and is used | |
| 29 // // only in a specific file. This will define the property in an unnamed | |
| 30 // // namespace which cannot be accessed from another file. | |
| 31 // DEFINE_LOCAL_VIEW_PROPERTY_KEY(MyType, kMyKey, MyDefault); | |
| 32 // | |
| 33 // } // foo namespace | |
| 34 // | |
| 35 // To define a new type used for ViewProperty. | |
| 36 // | |
| 37 // // outside all namespaces: | |
| 38 // DECLARE_EXPORTED_VIEW_PROPERTY_TYPE(FOO_EXPORT, MyType) | |
| 39 // | |
| 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). | |
| 42 | |
| 43 namespace mojo { | |
| 44 namespace { | |
| 45 | |
| 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 | |
| 48 // MSVC warning C4800 (forcing value to bool) is not suppressed by an explicit | |
| 49 // cast (!). | |
| 50 template <typename T> | |
| 51 class ViewPropertyCaster { | |
| 52 public: | |
| 53 static int64_t ToInt64(T x) { return static_cast<int64_t>(x); } | |
| 54 static T FromInt64(int64_t x) { return static_cast<T>(x); } | |
| 55 }; | |
| 56 template <typename T> | |
| 57 class ViewPropertyCaster<T*> { | |
| 58 public: | |
| 59 static int64_t ToInt64(T* x) { return reinterpret_cast<int64_t>(x); } | |
| 60 static T* FromInt64(int64_t x) { return reinterpret_cast<T*>(x); } | |
| 61 }; | |
| 62 template <> | |
| 63 class ViewPropertyCaster<bool> { | |
| 64 public: | |
| 65 static int64_t ToInt64(bool x) { return static_cast<int64_t>(x); } | |
| 66 static bool FromInt64(int64_t x) { return x != 0; } | |
| 67 }; | |
| 68 | |
| 69 } // namespace | |
| 70 | |
| 71 template <typename T> | |
| 72 struct ViewProperty { | |
| 73 T default_value; | |
| 74 const char* name; | |
| 75 View::PropertyDeallocator deallocator; | |
| 76 }; | |
| 77 | |
| 78 template <typename T> | |
| 79 void View::SetLocalProperty(const ViewProperty<T>* property, T value) { | |
| 80 int64_t old = SetLocalPropertyInternal( | |
| 81 property, property->name, | |
| 82 value == property->default_value ? nullptr : property->deallocator, | |
| 83 ViewPropertyCaster<T>::ToInt64(value), | |
| 84 ViewPropertyCaster<T>::ToInt64(property->default_value)); | |
| 85 if (property->deallocator && | |
| 86 old != ViewPropertyCaster<T>::ToInt64(property->default_value)) { | |
| 87 (*property->deallocator)(old); | |
| 88 } | |
| 89 } | |
| 90 | |
| 91 template <typename T> | |
| 92 T View::GetLocalProperty(const ViewProperty<T>* property) const { | |
| 93 return ViewPropertyCaster<T>::FromInt64(GetLocalPropertyInternal( | |
| 94 property, ViewPropertyCaster<T>::ToInt64(property->default_value))); | |
| 95 } | |
| 96 | |
| 97 template <typename T> | |
| 98 void View::ClearLocalProperty(const ViewProperty<T>* property) { | |
| 99 SetLocalProperty(property, property->default_value); | |
| 100 } | |
| 101 | |
| 102 } // namespace mojo | |
| 103 | |
| 104 // Macros to instantiate the property getter/setter template functions. | |
| 105 #define DECLARE_EXPORTED_VIEW_PROPERTY_TYPE(EXPORT, T) \ | |
| 106 template EXPORT void mojo::View::SetLocalProperty( \ | |
| 107 const mojo::ViewProperty<T>*, T); \ | |
| 108 template EXPORT T mojo::View::GetLocalProperty(const mojo::ViewProperty<T>*) \ | |
| 109 const; \ | |
| 110 template EXPORT void mojo::View::ClearLocalProperty( \ | |
| 111 const mojo::ViewProperty<T>*); | |
| 112 #define DECLARE_VIEW_PROPERTY_TYPE(T) DECLARE_EXPORTED_VIEW_PROPERTY_TYPE(, T) | |
| 113 | |
| 114 #define DEFINE_VIEW_PROPERTY_KEY(TYPE, NAME, DEFAULT) \ | |
| 115 COMPILE_ASSERT(sizeof(TYPE) <= sizeof(int64_t), property_type_too_large); \ | |
| 116 namespace { \ | |
| 117 const mojo::ViewProperty<TYPE> NAME##_Value = {DEFAULT, #NAME, nullptr}; \ | |
| 118 } \ | |
| 119 const mojo::ViewProperty<TYPE>* const NAME = &NAME##_Value; | |
| 120 | |
| 121 #define DEFINE_LOCAL_VIEW_PROPERTY_KEY(TYPE, NAME, DEFAULT) \ | |
| 122 COMPILE_ASSERT(sizeof(TYPE) <= sizeof(int64_t), property_type_too_large); \ | |
| 123 namespace { \ | |
| 124 const mojo::ViewProperty<TYPE> NAME##_Value = {DEFAULT, #NAME, nullptr}; \ | |
| 125 const mojo::ViewProperty<TYPE>* const NAME = &NAME##_Value; \ | |
| 126 } | |
| 127 | |
| 128 #define DEFINE_OWNED_VIEW_PROPERTY_KEY(TYPE, NAME, DEFAULT) \ | |
| 129 namespace { \ | |
| 130 void Deallocator##NAME(int64_t p) { \ | |
| 131 enum { type_must_be_complete = sizeof(TYPE) }; \ | |
| 132 delete mojo::ViewPropertyCaster<TYPE*>::FromInt64(p); \ | |
| 133 } \ | |
| 134 const mojo::ViewProperty<TYPE*> NAME##_Value = {DEFAULT, \ | |
| 135 #NAME, \ | |
| 136 &Deallocator##NAME}; \ | |
| 137 } \ | |
| 138 const mojo::ViewProperty<TYPE*>* const NAME = &NAME##_Value; | |
| 139 | |
| 140 #endif // MOJO_SERVICES_VIEW_MANAGER_PUBLIC_CPP_VIEW_PROPERTY_H_ | |
| OLD | NEW |