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

Side by Side Diff: base/values.cc

Issue 7892052: Adds Find method to the ListValue class. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Simplified the code a bit. Created 9 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/values.h ('k') | base/values_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
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 "base/values.h" 5 #include "base/values.h"
6 6
7 #include <algorithm>
8
7 #include "base/float_util.h" 9 #include "base/float_util.h"
8 #include "base/logging.h" 10 #include "base/logging.h"
9 #include "base/string_util.h" 11 #include "base/string_util.h"
10 #include "base/utf_string_conversions.h" 12 #include "base/utf_string_conversions.h"
11 13
12 namespace { 14 namespace {
13 15
14 // Make a deep copy of |node|, but don't include empty lists or dictionaries 16 // Make a deep copy of |node|, but don't include empty lists or dictionaries
15 // in the copy. It's possible for this function to return NULL and it 17 // in the copy. It's possible for this function to return NULL and it
16 // expects |node| to always be non-NULL. 18 // expects |node| to always be non-NULL.
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
51 delete copy; 53 delete copy;
52 return NULL; 54 return NULL;
53 } 55 }
54 56
55 default: 57 default:
56 // For everything else, just make a copy. 58 // For everything else, just make a copy.
57 return node->DeepCopy(); 59 return node->DeepCopy();
58 } 60 }
59 } 61 }
60 62
63 // A small functor for comparing Values for std::find_if and similar.
64 class ValueEquals {
65 public:
66 // Pass the value against which all consecutive calls of the () operator will
67 // compare their argument to. This Value object must not be destroyed while
68 // the ValueEquals is in use.
69 ValueEquals(const Value* first) : first_(first) { }
70
71 bool operator ()(const Value* second) const {
72 return first_->Equals(second);
73 }
74
75 private:
76 const Value* first_;
77 };
78
61 } // namespace 79 } // namespace
62 80
63 namespace base { 81 namespace base {
64 82
65 ///////////////////// Value //////////////////// 83 ///////////////////// Value ////////////////////
66 84
67 Value::~Value() { 85 Value::~Value() {
68 } 86 }
69 87
70 // static 88 // static
(...skipping 787 matching lines...) Expand 10 before | Expand all | Expand 10 after
858 876
859 bool ListValue::Insert(size_t index, Value* in_value) { 877 bool ListValue::Insert(size_t index, Value* in_value) {
860 DCHECK(in_value); 878 DCHECK(in_value);
861 if (index > list_.size()) 879 if (index > list_.size())
862 return false; 880 return false;
863 881
864 list_.insert(list_.begin() + index, in_value); 882 list_.insert(list_.begin() + index, in_value);
865 return true; 883 return true;
866 } 884 }
867 885
886 ListValue::const_iterator ListValue::Find(const Value& value) const {
887 return std::find_if(list_.begin(), list_.end(), ValueEquals(&value));
888 }
889
868 bool ListValue::GetAsList(ListValue** out_value) { 890 bool ListValue::GetAsList(ListValue** out_value) {
869 if (out_value) 891 if (out_value)
870 *out_value = this; 892 *out_value = this;
871 return true; 893 return true;
872 } 894 }
873 895
874 bool ListValue::GetAsList(const ListValue** out_value) const { 896 bool ListValue::GetAsList(const ListValue** out_value) const {
875 if (out_value) 897 if (out_value)
876 *out_value = this; 898 *out_value = this;
877 return true; 899 return true;
(...skipping 24 matching lines...) Expand all
902 if (lhs_it != end() || rhs_it != other_list->end()) 924 if (lhs_it != end() || rhs_it != other_list->end())
903 return false; 925 return false;
904 926
905 return true; 927 return true;
906 } 928 }
907 929
908 ValueSerializer::~ValueSerializer() { 930 ValueSerializer::~ValueSerializer() {
909 } 931 }
910 932
911 } // namespace base 933 } // namespace base
OLDNEW
« no previous file with comments | « base/values.h ('k') | base/values_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698