summaryrefslogtreecommitdiffstats
path: root/components/mus/public/cpp/view_property.h
blob: ccebb7c29d9897e66db80392ddeb7f653192a914 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_MUS_PUBLIC_CPP_VIEW_PROPERTY_H_
#define COMPONENTS_MUS_PUBLIC_CPP_VIEW_PROPERTY_H_

#include <stdint.h>

// This header should be included by code that defines ViewProperties. It
// should not be included by code that only gets and sets ViewProperties.
//
// To define a new ViewProperty:
//
//  #include "components/mus/public/cpp/view_property.h"
//
//  DECLARE_EXPORTED_VIEW_PROPERTY_TYPE(FOO_EXPORT, MyType);
//  namespace foo {
//    // Use this to define an exported property that is primitive,
//    // or a pointer you don't want automatically deleted.
//    DEFINE_VIEW_PROPERTY_KEY(MyType, kMyKey, MyDefault);
//
//    // Use this to define an exported property whose value is a heap
//    // allocated object, and has to be owned and freed by the view.
//    DEFINE_OWNED_VIEW_PROPERTY_KEY(gfx::Rect, kRestoreBoundsKey, nullptr);
//
//    // Use this to define a non exported property that is primitive,
//    // or a pointer you don't want to automatically deleted, and is used
//    // only in a specific file. This will define the property in an unnamed
//    // namespace which cannot be accessed from another file.
//    DEFINE_LOCAL_VIEW_PROPERTY_KEY(MyType, kMyKey, MyDefault);
//
//  }  // foo namespace
//
// To define a new type used for ViewProperty.
//
//  // outside all namespaces:
//  DECLARE_EXPORTED_VIEW_PROPERTY_TYPE(FOO_EXPORT, MyType)
//
// If a property type is not exported, use DECLARE_VIEW_PROPERTY_TYPE(MyType)
// which is a shorthand for DECLARE_EXPORTED_VIEW_PROPERTY_TYPE(, MyType).

namespace mus {
namespace {

// No single new-style cast works for every conversion to/from int64_t, so we
// need this helper class. A third specialization is needed for bool because
// MSVC warning C4800 (forcing value to bool) is not suppressed by an explicit
// cast (!).
template <typename T>
class ViewPropertyCaster {
 public:
  static int64_t ToInt64(T x) { return static_cast<int64_t>(x); }
  static T FromInt64(int64_t x) { return static_cast<T>(x); }
};
template <typename T>
class ViewPropertyCaster<T*> {
 public:
  static int64_t ToInt64(T* x) { return reinterpret_cast<int64_t>(x); }
  static T* FromInt64(int64_t x) { return reinterpret_cast<T*>(x); }
};
template <>
class ViewPropertyCaster<bool> {
 public:
  static int64_t ToInt64(bool x) { return static_cast<int64_t>(x); }
  static bool FromInt64(int64_t x) { return x != 0; }
};

}  // namespace

template <typename T>
struct ViewProperty {
  T default_value;
  const char* name;
  View::PropertyDeallocator deallocator;
};

template <typename T>
void View::SetLocalProperty(const ViewProperty<T>* property, T value) {
  int64_t old = SetLocalPropertyInternal(
      property, property->name,
      value == property->default_value ? nullptr : property->deallocator,
      ViewPropertyCaster<T>::ToInt64(value),
      ViewPropertyCaster<T>::ToInt64(property->default_value));
  if (property->deallocator &&
      old != ViewPropertyCaster<T>::ToInt64(property->default_value)) {
    (*property->deallocator)(old);
  }
}

template <typename T>
T View::GetLocalProperty(const ViewProperty<T>* property) const {
  return ViewPropertyCaster<T>::FromInt64(GetLocalPropertyInternal(
      property, ViewPropertyCaster<T>::ToInt64(property->default_value)));
}

template <typename T>
void View::ClearLocalProperty(const ViewProperty<T>* property) {
  SetLocalProperty(property, property->default_value);
}

}  // namespace mus

// Macros to instantiate the property getter/setter template functions.
#define DECLARE_EXPORTED_VIEW_PROPERTY_TYPE(EXPORT, T)                       \
  template EXPORT void mus::View::SetLocalProperty(                          \
      const mus::ViewProperty<T>*, T);                                       \
  template EXPORT T mus::View::GetLocalProperty(const mus::ViewProperty<T>*) \
      const;                                                                 \
  template EXPORT void mus::View::ClearLocalProperty(                        \
      const mus::ViewProperty<T>*);
#define DECLARE_VIEW_PROPERTY_TYPE(T) DECLARE_EXPORTED_VIEW_PROPERTY_TYPE(, T)

#define DEFINE_VIEW_PROPERTY_KEY(TYPE, NAME, DEFAULT)                       \
  COMPILE_ASSERT(sizeof(TYPE) <= sizeof(int64_t), property_type_too_large); \
  namespace {                                                               \
  const mus::ViewProperty<TYPE> NAME##_Value = {DEFAULT, #NAME, nullptr};   \
  }                                                                         \
  const mus::ViewProperty<TYPE>* const NAME = &NAME##_Value;

#define DEFINE_LOCAL_VIEW_PROPERTY_KEY(TYPE, NAME, DEFAULT)                 \
  COMPILE_ASSERT(sizeof(TYPE) <= sizeof(int64_t), property_type_too_large); \
  namespace {                                                               \
  const mus::ViewProperty<TYPE> NAME##_Value = {DEFAULT, #NAME, nullptr};   \
  const mus::ViewProperty<TYPE>* const NAME = &NAME##_Value;                \
  }

#define DEFINE_OWNED_VIEW_PROPERTY_KEY(TYPE, NAME, DEFAULT)           \
  namespace {                                                         \
  void Deallocator##NAME(int64_t p) {                                 \
    enum { type_must_be_complete = sizeof(TYPE) };                    \
    delete mus::ViewPropertyCaster<TYPE*>::FromInt64(p);              \
  }                                                                   \
  const mus::ViewProperty<TYPE*> NAME##_Value = {DEFAULT, #NAME,      \
                                                 &Deallocator##NAME}; \
  }                                                                   \
  const mus::ViewProperty<TYPE*>* const NAME = &NAME##_Value;

#endif  // COMPONENTS_MUS_PUBLIC_CPP_VIEW_PROPERTY_H_