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

Side by Side Diff: ui/views/view_model.cc

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, 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
sky 2014/10/01 19:20:37 codereview is showing this file as deleted. Make s
Matt Giuca 2014/10/02 01:23:51 What the hell? OK, this is https://code.google.co
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/views/view_model.h" 5 #include "ui/views/view_model.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "ui/views/view.h" 8 #include "ui/views/view.h"
9 9
10 namespace views { 10 namespace views {
11 11
12 ViewModel::ViewModel() { 12 ViewModelBase::~ViewModelBase() {
13 }
14
15 ViewModel::~ViewModel() {
16 // view are owned by their parent, no need to delete them. 13 // view are owned by their parent, no need to delete them.
17 } 14 }
18 15
19 void ViewModel::Add(View* view, int index) { 16 void ViewModelBase::Remove(int index) {
20 DCHECK_LE(index, static_cast<int>(entries_.size()));
21 DCHECK_GE(index, 0);
22 Entry entry;
23 entry.view = view;
24 entries_.insert(entries_.begin() + index, entry);
25 }
26
27 void ViewModel::Remove(int index) {
28 if (index == -1) 17 if (index == -1)
29 return; 18 return;
30 19
31 check_index(index); 20 check_index(index);
32 entries_.erase(entries_.begin() + index); 21 entries_.erase(entries_.begin() + index);
33 } 22 }
34 23
35 void ViewModel::Move(int index, int target_index) { 24 void ViewModelBase::Move(int index, int target_index) {
36 DCHECK_LT(index, static_cast<int>(entries_.size())); 25 DCHECK_LT(index, static_cast<int>(entries_.size()));
37 DCHECK_GE(index, 0); 26 DCHECK_GE(index, 0);
38 DCHECK_LT(target_index, static_cast<int>(entries_.size())); 27 DCHECK_LT(target_index, static_cast<int>(entries_.size()));
39 DCHECK_GE(target_index, 0); 28 DCHECK_GE(target_index, 0);
40 29
41 if (index == target_index) 30 if (index == target_index)
42 return; 31 return;
43 Entry entry(entries_[index]); 32 Entry entry(entries_[index]);
44 entries_.erase(entries_.begin() + index); 33 entries_.erase(entries_.begin() + index);
45 entries_.insert(entries_.begin() + target_index, entry); 34 entries_.insert(entries_.begin() + target_index, entry);
46 } 35 }
47 36
48 void ViewModel::MoveViewOnly(int index, int target_index) { 37 void ViewModelBase::MoveViewOnly(int index, int target_index) {
49 if (index == target_index) 38 if (index == target_index)
50 return; 39 return;
51 if (target_index < index) { 40 if (target_index < index) {
52 View* view = entries_[index].view; 41 View* view = entries_[index].view;
53 for (int i = index; i > target_index; --i) 42 for (int i = index; i > target_index; --i)
54 entries_[i].view = entries_[i - 1].view; 43 entries_[i].view = entries_[i - 1].view;
55 entries_[target_index].view = view; 44 entries_[target_index].view = view;
56 } else { 45 } else {
57 View* view = entries_[index].view; 46 View* view = entries_[index].view;
58 for (int i = index; i < target_index; ++i) 47 for (int i = index; i < target_index; ++i)
59 entries_[i].view = entries_[i + 1].view; 48 entries_[i].view = entries_[i + 1].view;
60 entries_[target_index].view = view; 49 entries_[target_index].view = view;
61 } 50 }
62 } 51 }
63 52
64 void ViewModel::Clear() { 53 void ViewModelBase::Clear() {
65 Entries entries; 54 Entries entries;
66 entries.swap(entries_); 55 entries.swap(entries_);
67 for (size_t i = 0; i < entries.size(); ++i) 56 for (size_t i = 0; i < entries.size(); ++i)
68 delete entries[i].view; 57 delete entries[i].view;
69 } 58 }
70 59
71 int ViewModel::GetIndexOfView(const View* view) const { 60 int ViewModelBase::GetIndexOfView(const View* view) const {
72 for (size_t i = 0; i < entries_.size(); ++i) { 61 for (size_t i = 0; i < entries_.size(); ++i) {
73 if (entries_[i].view == view) 62 if (entries_[i].view == view)
74 return static_cast<int>(i); 63 return static_cast<int>(i);
75 } 64 }
76 return -1; 65 return -1;
77 } 66 }
78 67
68 ViewModelBase::ViewModelBase() {
69 }
70
71 void ViewModelBase::AddUnsafe(View* view, int index) {
72 DCHECK_LE(index, static_cast<int>(entries_.size()));
73 DCHECK_GE(index, 0);
74 Entry entry;
75 entry.view = view;
76 entries_.insert(entries_.begin() + index, entry);
77 }
78
79 } // namespace views 79 } // namespace views
OLDNEW
« ui/views/view_model.h ('K') | « ui/views/view_model.h ('k') | ui/views/view_model_utils.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698