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

Unified Diff: ui/views/view_model.h

Issue 598013003: Added views::ViewModelT<T>, a type-safe template version of ViewModel. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@appsgridview-static-casts
Patch Set: DISALLOW_COPY_AND_ASSIGN. Created 6 years, 3 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 side-by-side diff with in-line comments
Download patch
Index: ui/views/view_model.h
diff --git a/ui/views/view_model.h b/ui/views/view_model.h
index e8c84a9568edfdb311eeca8bafea671700b96eaa..fd4a78deec700749c614fe135265c77bd579c04d 100644
--- a/ui/views/view_model.h
+++ b/ui/views/view_model.h
@@ -16,19 +16,14 @@ namespace views {
class View;
-// ViewModel is used to track an 'interesting' set of a views. Often times
-// during animations views are removed after a delay, which makes for tricky
-// coordinate conversion as you have to account for the possibility of the
-// indices from the model not lining up with those you expect. This class lets
-// you define the 'interesting' views and operate on those views.
-class VIEWS_EXPORT ViewModel {
+// Internal implementation of the templated ViewModel class. Provides
+// non-templated "unsafe" methods for ViewModel to wrap around. Any methods that
+// allow insertion of or access to a View* should be protected, and have a
+// public method in the ViewModel subclass that provides type-safe access to the
+// correct View subclass.
+class VIEWS_EXPORT ViewModelBase {
public:
- ViewModel();
- ~ViewModel();
-
- // Adds |view| to this model. This does not add |view| to a view hierarchy,
- // only to this model.
- void Add(View* view, int index);
+ ~ViewModelBase();
// Removes the view at the specified index. This does not actually remove the
// view from the view hierarchy.
@@ -50,8 +45,10 @@ class VIEWS_EXPORT ViewModel {
// Removes and deletes all the views.
void Clear();
- // Returns the view at the specified index.
- View* view_at(int index) const {
+ // Returns the view at the specified index. Note: Most users should use
+ // view_at() in the subclass, to get a view of the correct type. (Do not call
+ // ViewAtBase then static_cast to the desired type.)
+ View* ViewAtBase(int index) const {
sky 2014/10/01 19:20:37 Can we name this view_at and the subclass implemen
Matt Giuca 2014/10/02 01:23:51 I think I prefer the friends approach (since "norm
check_index(index);
return entries_[index].view;
}
@@ -70,6 +67,13 @@ class VIEWS_EXPORT ViewModel {
// model.
int GetIndexOfView(const View* view) const;
+ protected:
+ ViewModelBase();
+
+ // Adds |view| to this model. This does not add |view| to a view hierarchy,
+ // only to this model.
+ void AddUnsafe(View* view, int index);
+
private:
struct Entry {
Entry() : view(NULL) {}
@@ -90,7 +94,28 @@ class VIEWS_EXPORT ViewModel {
Entries entries_;
- DISALLOW_COPY_AND_ASSIGN(ViewModel);
+ DISALLOW_COPY_AND_ASSIGN(ViewModelBase);
+};
+
+// ViewModel is used to track an 'interesting' set of a views. Often times
+// during animations views are removed after a delay, which makes for tricky
+// coordinate conversion as you have to account for the possibility of the
+// indices from the model not lining up with those you expect. This class lets
+// you define the 'interesting' views and operate on those views.
+template <class T>
+class ViewModel : public ViewModelBase {
sky 2014/10/01 19:20:38 I like naming this ViewModelT and having classes t
Matt Giuca 2014/10/02 01:23:51 Done.
+ public:
+ ViewModel<T>() {}
+
+ // Adds |view| to this model. This does not add |view| to a view hierarchy,
+ // only to this model.
+ void Add(T* view, int index) { AddUnsafe(view, index); }
+
+ // Returns the view at the specified index.
+ T* view_at(int index) const { return static_cast<T*>(ViewAtBase(index)); }
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(ViewModel<T>);
};
} // namespace views

Powered by Google App Engine
This is Rietveld 408576698