Linux: Depend on liberation-fonts package for RPMs.
[chromium-blink-merge.git] / components / mus / public / cpp / view_property.h
blobccebb7c29d9897e66db80392ddeb7f653192a914
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.
5 #ifndef COMPONENTS_MUS_PUBLIC_CPP_VIEW_PROPERTY_H_
6 #define COMPONENTS_MUS_PUBLIC_CPP_VIEW_PROPERTY_H_
8 #include <stdint.h>
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.
13 // To define a new ViewProperty:
15 // #include "components/mus/public/cpp/view_property.h"
17 // DECLARE_EXPORTED_VIEW_PROPERTY_TYPE(FOO_EXPORT, MyType);
18 // namespace foo {
19 // // Use this to define an exported property that is primitive,
20 // // or a pointer you don't want automatically deleted.
21 // DEFINE_VIEW_PROPERTY_KEY(MyType, kMyKey, MyDefault);
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);
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);
33 // } // foo namespace
35 // To define a new type used for ViewProperty.
37 // // outside all namespaces:
38 // DECLARE_EXPORTED_VIEW_PROPERTY_TYPE(FOO_EXPORT, 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).
43 namespace mus {
44 namespace {
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); }
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); }
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; }
69 } // namespace
71 template <typename T>
72 struct ViewProperty {
73 T default_value;
74 const char* name;
75 View::PropertyDeallocator deallocator;
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);
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)));
97 template <typename T>
98 void View::ClearLocalProperty(const ViewProperty<T>* property) {
99 SetLocalProperty(property, property->default_value);
102 } // namespace mus
104 // Macros to instantiate the property getter/setter template functions.
105 #define DECLARE_EXPORTED_VIEW_PROPERTY_TYPE(EXPORT, T) \
106 template EXPORT void mus::View::SetLocalProperty( \
107 const mus::ViewProperty<T>*, T); \
108 template EXPORT T mus::View::GetLocalProperty(const mus::ViewProperty<T>*) \
109 const; \
110 template EXPORT void mus::View::ClearLocalProperty( \
111 const mus::ViewProperty<T>*);
112 #define DECLARE_VIEW_PROPERTY_TYPE(T) DECLARE_EXPORTED_VIEW_PROPERTY_TYPE(, T)
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 mus::ViewProperty<TYPE> NAME##_Value = {DEFAULT, #NAME, nullptr}; \
119 const mus::ViewProperty<TYPE>* const NAME = &NAME##_Value;
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 mus::ViewProperty<TYPE> NAME##_Value = {DEFAULT, #NAME, nullptr}; \
125 const mus::ViewProperty<TYPE>* const NAME = &NAME##_Value; \
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 mus::ViewPropertyCaster<TYPE*>::FromInt64(p); \
134 const mus::ViewProperty<TYPE*> NAME##_Value = {DEFAULT, #NAME, \
135 &Deallocator##NAME}; \
137 const mus::ViewProperty<TYPE*>* const NAME = &NAME##_Value;
139 #endif // COMPONENTS_MUS_PUBLIC_CPP_VIEW_PROPERTY_H_