Android: Get rid of extra dup()s on launching child processes
[chromium-blink-merge.git] / ui / views / view_model.h
blob22c8d7ef144b053f61db7b1594bb5703b023b6ec
1 // Copyright (c) 2012 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 UI_VIEWS_VIEW_MODEL_H_
6 #define UI_VIEWS_VIEW_MODEL_H_
8 #include <vector>
10 #include "base/basictypes.h"
11 #include "base/logging.h"
12 #include "ui/gfx/geometry/rect.h"
13 #include "ui/views/views_export.h"
15 namespace views {
17 class View;
18 class ViewModelUtils;
20 // Internal implementation of the templated ViewModelT class. Provides
21 // non-templated "unsafe" methods for ViewModelT to wrap around. Any methods
22 // that allow insertion of or access to a View* should be protected, and have a
23 // public method in the ViewModelT subclass that provides type-safe access to
24 // the correct View subclass.
25 class VIEWS_EXPORT ViewModelBase {
26 public:
27 ~ViewModelBase();
29 // Removes the view at the specified index. This does not actually remove the
30 // view from the view hierarchy.
31 void Remove(int index);
33 // Moves the view at |index| to |target_index|. |target_index| is in terms
34 // of the model *after* the view at |index| is removed.
35 void Move(int index, int target_index);
37 // Variant of Move() that leaves the bounds as is. That is, after invoking
38 // this the bounds of the view at |target_index| (and all other indices) are
39 // exactly the same as the bounds of the view at |target_index| before
40 // invoking this.
41 void MoveViewOnly(int index, int target_index);
43 // Returns the number of views.
44 int view_size() const { return static_cast<int>(entries_.size()); }
46 // Removes and deletes all the views.
47 void Clear();
49 void set_ideal_bounds(int index, const gfx::Rect& bounds) {
50 check_index(index);
51 entries_[index].ideal_bounds = bounds;
54 const gfx::Rect& ideal_bounds(int index) const {
55 check_index(index);
56 return entries_[index].ideal_bounds;
59 // Returns the index of the specified view, or -1 if the view isn't in the
60 // model.
61 int GetIndexOfView(const View* view) const;
63 protected:
64 ViewModelBase();
66 // Returns the view at the specified index. Note: Most users should use
67 // view_at() in the subclass, to get a view of the correct type. (Do not call
68 // ViewAtBase then static_cast to the desired type.)
69 View* ViewAtBase(int index) const {
70 check_index(index);
71 return entries_[index].view;
74 // Adds |view| to this model. This does not add |view| to a view hierarchy,
75 // only to this model.
76 void AddUnsafe(View* view, int index);
78 private:
79 // For access to ViewAtBase().
80 friend class ViewModelUtils;
82 struct Entry {
83 Entry() : view(NULL) {}
85 View* view;
86 gfx::Rect ideal_bounds;
88 typedef std::vector<Entry> Entries;
90 #if !defined(NDEBUG)
91 void check_index(int index) const {
92 DCHECK_LT(index, static_cast<int>(entries_.size()));
93 DCHECK_GE(index, 0);
95 #else
96 void check_index(int index) const {}
97 #endif
99 Entries entries_;
101 DISALLOW_COPY_AND_ASSIGN(ViewModelBase);
104 // ViewModelT is used to track an 'interesting' set of a views. Often times
105 // during animations views are removed after a delay, which makes for tricky
106 // coordinate conversion as you have to account for the possibility of the
107 // indices from the model not lining up with those you expect. This class lets
108 // you define the 'interesting' views and operate on those views.
109 template <class T>
110 class ViewModelT : public ViewModelBase {
111 public:
112 ViewModelT<T>() {}
114 // Adds |view| to this model. This does not add |view| to a view hierarchy,
115 // only to this model.
116 void Add(T* view, int index) { AddUnsafe(view, index); }
118 // Returns the view at the specified index.
119 T* view_at(int index) const { return static_cast<T*>(ViewAtBase(index)); }
121 private:
122 DISALLOW_COPY_AND_ASSIGN(ViewModelT<T>);
125 // ViewModel is a collection of views with no specfic type. If all views have
126 // the same type, the use of ViewModelT is preferred so that the views can be
127 // retrieved without potentially unsafe downcasts.
128 typedef ViewModelT<View> ViewModel;
130 } // namespace views
132 #endif // UI_VIEWS_VIEW_MODEL_H_