Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(63)

Side by Side Diff: ui/views/attributes.h

Issue 2230913003: Experimental alignment layout manager using a property on the views Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Renamed AlignAttribute and associated types to FillAttribute Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/views/anchor_attribute.cc ('k') | ui/views/attributes.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2016 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 UI_VIEWS_ATTRIBUTES_H_
6 #define UI_VIEWS_ATTRIBUTES_H_
7
8 #include <algorithm>
9 #include <cmath>
10 #include <map>
11 #include <memory>
12 #include <string>
13 #include <utility>
14
15 #include "base/macros.h"
16 #include "base/memory/ptr_util.h"
17 #include "ui/views/views_export.h"
18
19 namespace views {
20
21 class View;
22
23 using AttributeId = void*;
24
25 template <typename T>
26 class AttributeIdHelper {
27 public:
28 static AttributeId GetAttributeId() {
29 return T::TYPE_NOT_AVAILABLE();
30 }
31 };
32
33 template <typename T>
34 AttributeId GetAttributeId() {
35 return AttributeIdHelper<T>::GetAttributeId();
36 }
37
38 #define _REGISTER_ATTRIBUTE_ID(T) \
39 template <> \
40 class AttributeIdHelper<T> { \
41 public: \
42 static AttributeId GetAttributeId() { \
43 static const AttributeId id = AttributeIdFactory::GetAttributeId(#T); \
44 return id; \
45 } \
46 }
47
48 #define REGISTER_ATTRIBUTE_ID(T) _REGISTER_ATTRIBUTE_ID(T)
49
50 class VIEWS_EXPORT AttributeIdFactory {
51 public:
52 static AttributeId GetAttributeId(const char* typeName);
53 private:
54 class AttributeIdHolder {
55 public:
56 AttributeIdHolder() { attribute_id_ = static_cast<AttributeId>(this); }
57 AttributeId GetAttributeId() { return attribute_id_; }
58 private:
59 AttributeId attribute_id_;
60 };
61 AttributeIdFactory() {}
62 using AttributeIdMap =
63 std::map<std::string, std::unique_ptr<AttributeIdHolder>>;
64 static AttributeIdMap map_;
65 DISALLOW_COPY_AND_ASSIGN(AttributeIdFactory);
66 };
67
68 class VIEWS_EXPORT Attribute {
69 public:
70 enum class NotifyType {
71 AttributeAdded,
72 AttributeRemoved,
73 };
74 Attribute() : view_(nullptr) {};
75 virtual ~Attribute() {};
76 void AddedToView(View *view) { view_ = view; }
77 template <class T> T* AsAttribute() {
78 if (Identity() == GetAttributeId<T>())
79 return static_cast<T*>(this);
80 return nullptr;
81 }
82 virtual AttributeId Identity() const;
83 View *view() { return view_; }
84 virtual void Notification(NotifyType type, Attribute *attribute) {}
85 void RemovedFromView() { view_ = nullptr; }
86 virtual void ViewNotification(View* view) {}
87 private:
88 View *view_;
89 DISALLOW_COPY_AND_ASSIGN(Attribute);
90 };
91
92 REGISTER_ATTRIBUTE_ID(Attribute);
93
94 template <class Content, class A = Attribute>
95 class CustomAttribute : public A {
96 static_assert(std::is_base_of<Attribute, A>::value,
97 "'A' must derive from Attribute");
98 public:
99 CustomAttribute() {};
100 CustomAttribute(const Content& content) : content_(content) {}
101 ~CustomAttribute() override {};
102 inline Content& GetContent() { return content_; }
103 AttributeId Identity() const override {
104 return GetAttributeId<CustomAttribute<Content, A>>();
105 }
106 private:
107 Content content_;
108 CustomAttribute(const CustomAttribute&) = delete;
109 void operator=(const CustomAttribute&) = delete;
110 };
111
112 class VIEWS_EXPORT AttributeContainer {
113 public:
114 AttributeContainer(View *view);
115 ~AttributeContainer();
116 template <class T> bool Add(std::unique_ptr<T> attribute) {
117 AttributeId attributeId = GetAttributeId<T>();
118 T* attr = attribute.get();
119 if (map_.insert(AttributeMap::value_type(attributeId,
120 std::move(attribute))).second) {
121 Notification(Attribute::NotifyType::AttributeAdded, attr);
122 if (view_) {
123 attribute->AddedToView(view_);
124 attribute->ViewNotification(view_);
125 }
126 return true;
127 }
128 return false;
129 }
130 bool Remove(const AttributeId AttributeId);
131 template <class T> bool Find(T*& attribute) {
132 static_assert(std::is_base_of<Attribute, T>::value,
133 "'T' must derive from Attribute");
134 AttributeMap::iterator it = map_.find(GetAttributeId<T>());
135 bool found = it != map_.end();
136 if (found) {
137 Attribute* a = it->second.get();
138 if (a)
139 attribute = a->AsAttribute<T>();
140 else
141 found = false;
142 }
143 return found;
144 }
145 void ViewNotification(View *view);
146 private:
147 AttributeContainer();
148 void Notification(Attribute::NotifyType type, Attribute *attribute);
149 using AttributeMap = std::map<AttributeId, std::unique_ptr<Attribute>>;
150 AttributeMap map_;
151 View *view_;
152 DISALLOW_COPY_AND_ASSIGN(AttributeContainer);
153 };
154
155 } // namespace views
156
157 #endif // UI_VIEWS_ATTRIBUTES_H_
OLDNEW
« no previous file with comments | « ui/views/anchor_attribute.cc ('k') | ui/views/attributes.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698