Chromium Code Reviews| 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_PUBLIC_CPP_VIEW_MANAGER_VIEW_PROPERTY_H_ | |
| 6 #define MOJO_SERVICES_PUBLIC_CPP_VIEW_MANAGER_VIEW_PROPERTY_H_ | |
| 7 | |
| 8 #include "base/basictypes.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 "mojo/services/public/cpp/view_manager/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, 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 ToInt64(T x) { return static_cast<int64>(x); } | |
| 54 static T FromInt64(int64 x) { return static_cast<T>(x); } | |
| 55 }; | |
| 56 template<typename T> | |
| 57 class ViewPropertyCaster<T*> { | |
| 58 public: | |
| 59 static int64 ToInt64(T* x) { return reinterpret_cast<int64>(x); } | |
| 60 static T* FromInt64(int64 x) { return reinterpret_cast<T*>(x); } | |
| 61 }; | |
| 62 template<> | |
| 63 class ViewPropertyCaster<bool> { | |
| 64 public: | |
| 65 static int64 ToInt64(bool x) { return static_cast<int64>(x); } | |
| 66 static bool FromInt64(int64 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 old = SetLocalPropertyInternal( | |
| 81 property, | |
| 82 property->name, | |
| 83 value == property->default_value ? nullptr : property->deallocator, | |
| 84 ViewPropertyCaster<T>::ToInt64(value), | |
| 85 ViewPropertyCaster<T>::ToInt64(property->default_value)); | |
| 86 if (property->deallocator && | |
| 87 old != ViewPropertyCaster<T>::ToInt64(property->default_value)) { | |
| 88 (*property->deallocator)(old); | |
| 89 } | |
| 90 } | |
| 91 | |
| 92 template<typename T> | |
| 93 T View::GetLocalProperty(const ViewProperty<T>* property) const { | |
| 94 return ViewPropertyCaster<T>::FromInt64(GetLocalPropertyInternal( | |
| 95 property, ViewPropertyCaster<T>::ToInt64(property->default_value))); | |
| 96 } | |
| 97 | |
| 98 template<typename T> | |
| 99 void View::ClearLocalProperty(const ViewProperty<T>* property) { | |
| 100 SetProperty(property, property->default_value); | |
| 101 } | |
| 102 | |
| 103 } // namespace mojo | |
| 104 | |
| 105 // Macros to instantiate the property getter/setter template functions. | |
| 106 #define DECLARE_EXPORTED_VIEW_PROPERTY_TYPE(EXPORT, T) \ | |
|
sky
2014/11/14 23:11:27
My eyes have glazed over from this code in the pas
| |
| 107 template EXPORT void mojo::View::SetLocalProperty( \ | |
| 108 const mojo::ViewProperty<T >*, T); \ | |
| 109 template EXPORT T mojo::View::GetLocalProperty( \ | |
| 110 const mojo::ViewProperty<T >*) const; \ | |
| 111 template EXPORT void mojo::View::ClearLocalProperty( \ | |
| 112 const mojo::ViewProperty<T >*); | |
| 113 #define DECLARE_VIEW_PROPERTY_TYPE(T) \ | |
| 114 DECLARE_EXPORTED_VIEW_PROPERTY_TYPE(, T) | |
| 115 | |
| 116 #define DEFINE_VIEW_PROPERTY_KEY(TYPE, NAME, DEFAULT) \ | |
| 117 COMPILE_ASSERT(sizeof(TYPE) <= sizeof(int64), property_type_too_large); \ | |
| 118 namespace { \ | |
| 119 const mojo::ViewProperty<TYPE> NAME ## _Value = {DEFAULT, #NAME, nullptr}; \ | |
| 120 } \ | |
| 121 const mojo::ViewProperty<TYPE>* const NAME = & NAME ## _Value; | |
| 122 | |
| 123 #define DEFINE_LOCAL_VIEW_PROPERTY_KEY(TYPE, NAME, DEFAULT) \ | |
| 124 COMPILE_ASSERT(sizeof(TYPE) <= sizeof(int64), property_type_too_large); \ | |
| 125 namespace { \ | |
| 126 const mojo::ViewProperty<TYPE> NAME ## _Value = {DEFAULT, #NAME, nullptr}; \ | |
| 127 const mojo::ViewProperty<TYPE>* const NAME = & NAME ## _Value; \ | |
| 128 } | |
| 129 | |
| 130 #define DEFINE_OWNED_VIEW_PROPERTY_KEY(TYPE, NAME, DEFAULT) \ | |
| 131 namespace { \ | |
| 132 void Deallocator ## NAME (int64 p) { \ | |
| 133 enum { type_must_be_complete = sizeof(TYPE) }; \ | |
| 134 delete mojo::ViewPropertyCaster<TYPE*>::FromInt64(p); \ | |
| 135 } \ | |
| 136 const mojo::ViewProperty<TYPE*> NAME ## _Value = \ | |
| 137 {DEFAULT,#NAME,&Deallocator ## NAME}; \ | |
| 138 } \ | |
| 139 const mojo::ViewProperty<TYPE*>* const NAME = & NAME ## _Value; | |
| 140 | |
| 141 #endif // MOJO_SERVICES_PUBLIC_CPP_VIEW_MANAGER_VIEW_PROPERTY_H_ | |
| OLD | NEW |