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

Side by Side Diff: base/values.cc

Issue 6324004: Made return types of various Value::DeepCopy() implementations more specific (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add test for covariant return types Created 9 years, 11 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) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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 "base/logging.h" 7 #include "base/logging.h"
8 #include "base/string_util.h" 8 #include "base/string_util.h"
9 #include "base/utf_string_conversions.h" 9 #include "base/utf_string_conversions.h"
10 10
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
63 63
64 Value::~Value() { 64 Value::~Value() {
65 } 65 }
66 66
67 // static 67 // static
68 Value* Value::CreateNullValue() { 68 Value* Value::CreateNullValue() {
69 return new Value(TYPE_NULL); 69 return new Value(TYPE_NULL);
70 } 70 }
71 71
72 // static 72 // static
73 Value* Value::CreateBooleanValue(bool in_value) { 73 FundamentalValue* Value::CreateBooleanValue(bool in_value) {
74 return new FundamentalValue(in_value); 74 return new FundamentalValue(in_value);
75 } 75 }
76 76
77 // static 77 // static
78 Value* Value::CreateIntegerValue(int in_value) { 78 FundamentalValue* Value::CreateIntegerValue(int in_value) {
79 return new FundamentalValue(in_value); 79 return new FundamentalValue(in_value);
80 } 80 }
81 81
82 // static 82 // static
83 Value* Value::CreateRealValue(double in_value) { 83 FundamentalValue* Value::CreateRealValue(double in_value) {
84 return new FundamentalValue(in_value); 84 return new FundamentalValue(in_value);
85 } 85 }
86 86
87 // static 87 // static
88 Value* Value::CreateStringValue(const std::string& in_value) { 88 StringValue* Value::CreateStringValue(const std::string& in_value) {
89 return new StringValue(in_value); 89 return new StringValue(in_value);
90 } 90 }
91 91
92 // static 92 // static
93 Value* Value::CreateStringValue(const string16& in_value) { 93 StringValue* Value::CreateStringValue(const string16& in_value) {
94 return new StringValue(in_value); 94 return new StringValue(in_value);
95 } 95 }
96 96
97 // static 97 // static
98 BinaryValue* Value::CreateBinaryValue(char* buffer, size_t size) { 98 BinaryValue* Value::CreateBinaryValue(char* buffer, size_t size) {
99 return BinaryValue::Create(buffer, size); 99 return BinaryValue::Create(buffer, size);
100 } 100 }
101 101
102 bool Value::GetAsBoolean(bool* out_value) const { 102 bool Value::GetAsBoolean(bool* out_value) const {
103 return false; 103 return false;
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
175 *out_value = integer_value_; 175 *out_value = integer_value_;
176 return (IsType(TYPE_INTEGER)); 176 return (IsType(TYPE_INTEGER));
177 } 177 }
178 178
179 bool FundamentalValue::GetAsReal(double* out_value) const { 179 bool FundamentalValue::GetAsReal(double* out_value) const {
180 if (out_value && IsType(TYPE_REAL)) 180 if (out_value && IsType(TYPE_REAL))
181 *out_value = real_value_; 181 *out_value = real_value_;
182 return (IsType(TYPE_REAL)); 182 return (IsType(TYPE_REAL));
183 } 183 }
184 184
185 Value* FundamentalValue::DeepCopy() const { 185 FundamentalValue* FundamentalValue::DeepCopy() const {
186 switch (GetType()) { 186 switch (GetType()) {
187 case TYPE_BOOLEAN: 187 case TYPE_BOOLEAN:
188 return CreateBooleanValue(boolean_value_); 188 return CreateBooleanValue(boolean_value_);
189 189
190 case TYPE_INTEGER: 190 case TYPE_INTEGER:
191 return CreateIntegerValue(integer_value_); 191 return CreateIntegerValue(integer_value_);
192 192
193 case TYPE_REAL: 193 case TYPE_REAL:
194 return CreateRealValue(real_value_); 194 return CreateRealValue(real_value_);
195 195
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
243 *out_value = value_; 243 *out_value = value_;
244 return true; 244 return true;
245 } 245 }
246 246
247 bool StringValue::GetAsString(string16* out_value) const { 247 bool StringValue::GetAsString(string16* out_value) const {
248 if (out_value) 248 if (out_value)
249 *out_value = UTF8ToUTF16(value_); 249 *out_value = UTF8ToUTF16(value_);
250 return true; 250 return true;
251 } 251 }
252 252
253 Value* StringValue::DeepCopy() const { 253 StringValue* StringValue::DeepCopy() const {
254 return CreateStringValue(value_); 254 return CreateStringValue(value_);
255 } 255 }
256 256
257 bool StringValue::Equals(const Value* other) const { 257 bool StringValue::Equals(const Value* other) const {
258 if (other->GetType() != GetType()) 258 if (other->GetType() != GetType())
259 return false; 259 return false;
260 std::string lhs, rhs; 260 std::string lhs, rhs;
261 return GetAsString(&lhs) && other->GetAsString(&rhs) && lhs == rhs; 261 return GetAsString(&lhs) && other->GetAsString(&rhs) && lhs == rhs;
262 } 262 }
263 263
(...skipping 17 matching lines...) Expand all
281 BinaryValue* BinaryValue::CreateWithCopiedBuffer(const char* buffer, 281 BinaryValue* BinaryValue::CreateWithCopiedBuffer(const char* buffer,
282 size_t size) { 282 size_t size) {
283 if (!buffer) 283 if (!buffer)
284 return NULL; 284 return NULL;
285 285
286 char* buffer_copy = new char[size]; 286 char* buffer_copy = new char[size];
287 memcpy(buffer_copy, buffer, size); 287 memcpy(buffer_copy, buffer, size);
288 return new BinaryValue(buffer_copy, size); 288 return new BinaryValue(buffer_copy, size);
289 } 289 }
290 290
291 Value* BinaryValue::DeepCopy() const { 291 BinaryValue* BinaryValue::DeepCopy() const {
292 return CreateWithCopiedBuffer(buffer_, size_); 292 return CreateWithCopiedBuffer(buffer_, size_);
293 } 293 }
294 294
295 bool BinaryValue::Equals(const Value* other) const { 295 bool BinaryValue::Equals(const Value* other) const {
296 if (other->GetType() != GetType()) 296 if (other->GetType() != GetType())
297 return false; 297 return false;
298 const BinaryValue* other_binary = static_cast<const BinaryValue*>(other); 298 const BinaryValue* other_binary = static_cast<const BinaryValue*>(other);
299 if (other_binary->size_ != size_) 299 if (other_binary->size_ != size_)
300 return false; 300 return false;
301 return !memcmp(buffer_, other_binary->buffer_, size_); 301 return !memcmp(buffer_, other_binary->buffer_, size_);
(...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after
639 static_cast<const DictionaryValue*>(merge_value)); 639 static_cast<const DictionaryValue*>(merge_value));
640 continue; 640 continue;
641 } 641 }
642 } 642 }
643 // All other cases: Make a copy and hook it up. 643 // All other cases: Make a copy and hook it up.
644 SetWithoutPathExpansion(*key, merge_value->DeepCopy()); 644 SetWithoutPathExpansion(*key, merge_value->DeepCopy());
645 } 645 }
646 } 646 }
647 } 647 }
648 648
649 Value* DictionaryValue::DeepCopy() const { 649 DictionaryValue* DictionaryValue::DeepCopy() const {
650 DictionaryValue* result = new DictionaryValue; 650 DictionaryValue* result = new DictionaryValue;
651 651
652 for (ValueMap::const_iterator current_entry(dictionary_.begin()); 652 for (ValueMap::const_iterator current_entry(dictionary_.begin());
653 current_entry != dictionary_.end(); ++current_entry) { 653 current_entry != dictionary_.end(); ++current_entry) {
654 result->SetWithoutPathExpansion(current_entry->first, 654 result->SetWithoutPathExpansion(current_entry->first,
655 current_entry->second->DeepCopy()); 655 current_entry->second->DeepCopy());
656 } 656 }
657 657
658 return result; 658 return result;
659 } 659 }
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after
855 list_.insert(list_.begin() + index, in_value); 855 list_.insert(list_.begin() + index, in_value);
856 return true; 856 return true;
857 } 857 }
858 858
859 bool ListValue::GetAsList(ListValue** out_value) { 859 bool ListValue::GetAsList(ListValue** out_value) {
860 if (out_value) 860 if (out_value)
861 *out_value = this; 861 *out_value = this;
862 return true; 862 return true;
863 } 863 }
864 864
865 Value* ListValue::DeepCopy() const { 865 ListValue* ListValue::DeepCopy() const {
866 ListValue* result = new ListValue; 866 ListValue* result = new ListValue;
867 867
868 for (ValueVector::const_iterator i(list_.begin()); i != list_.end(); ++i) 868 for (ValueVector::const_iterator i(list_.begin()); i != list_.end(); ++i)
869 result->Append((*i)->DeepCopy()); 869 result->Append((*i)->DeepCopy());
870 870
871 return result; 871 return result;
872 } 872 }
873 873
874 bool ListValue::Equals(const Value* other) const { 874 bool ListValue::Equals(const Value* other) const {
875 if (other->GetType() != GetType()) 875 if (other->GetType() != GetType())
876 return false; 876 return false;
877 877
878 const ListValue* other_list = 878 const ListValue* other_list =
879 static_cast<const ListValue*>(other); 879 static_cast<const ListValue*>(other);
880 const_iterator lhs_it, rhs_it; 880 const_iterator lhs_it, rhs_it;
881 for (lhs_it = begin(), rhs_it = other_list->begin(); 881 for (lhs_it = begin(), rhs_it = other_list->begin();
882 lhs_it != end() && rhs_it != other_list->end(); 882 lhs_it != end() && rhs_it != other_list->end();
883 ++lhs_it, ++rhs_it) { 883 ++lhs_it, ++rhs_it) {
884 if (!(*lhs_it)->Equals(*rhs_it)) 884 if (!(*lhs_it)->Equals(*rhs_it))
885 return false; 885 return false;
886 } 886 }
887 if (lhs_it != end() || rhs_it != other_list->end()) 887 if (lhs_it != end() || rhs_it != other_list->end())
888 return false; 888 return false;
889 889
890 return true; 890 return true;
891 } 891 }
892 892
893 ValueSerializer::~ValueSerializer() { 893 ValueSerializer::~ValueSerializer() {
894 } 894 }
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