OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/float_util.h" | 9 #include "base/float_util.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
291 | 291 |
292 bool StringValue::Equals(const Value* other) const { | 292 bool StringValue::Equals(const Value* other) const { |
293 if (other->GetType() != GetType()) | 293 if (other->GetType() != GetType()) |
294 return false; | 294 return false; |
295 std::string lhs, rhs; | 295 std::string lhs, rhs; |
296 return GetAsString(&lhs) && other->GetAsString(&rhs) && lhs == rhs; | 296 return GetAsString(&lhs) && other->GetAsString(&rhs) && lhs == rhs; |
297 } | 297 } |
298 | 298 |
299 ///////////////////// BinaryValue //////////////////// | 299 ///////////////////// BinaryValue //////////////////// |
300 | 300 |
301 BinaryValue::BinaryValue() | 301 BinaryValue::~BinaryValue() { |
302 : Value(TYPE_BINARY), | 302 DCHECK(buffer_); |
303 buffer_(NULL), | 303 if (buffer_) |
304 size_(0) { | 304 delete[] buffer_; |
305 } | 305 } |
306 | 306 |
307 BinaryValue::BinaryValue(scoped_ptr<char> buffer, size_t size) | 307 // static |
308 : Value(TYPE_BINARY), | 308 BinaryValue* BinaryValue::Create(char* buffer, size_t size) { |
309 buffer_(buffer.release()), | 309 if (!buffer) |
310 size_(size) { | 310 return NULL; |
311 } | |
312 | 311 |
313 BinaryValue::~BinaryValue() { | 312 return new BinaryValue(buffer, size); |
314 } | 313 } |
315 | 314 |
316 // static | 315 // static |
317 BinaryValue* BinaryValue::CreateWithCopiedBuffer(const char* buffer, | 316 BinaryValue* BinaryValue::CreateWithCopiedBuffer(const char* buffer, |
318 size_t size) { | 317 size_t size) { |
| 318 if (!buffer) |
| 319 return NULL; |
| 320 |
319 char* buffer_copy = new char[size]; | 321 char* buffer_copy = new char[size]; |
320 memcpy(buffer_copy, buffer, size); | 322 memcpy(buffer_copy, buffer, size); |
321 scoped_ptr<char> scoped_buffer_copy(buffer_copy); | 323 return new BinaryValue(buffer_copy, size); |
322 return new BinaryValue(scoped_buffer_copy.Pass(), size); | |
323 } | 324 } |
324 | 325 |
325 BinaryValue* BinaryValue::DeepCopy() const { | 326 BinaryValue* BinaryValue::DeepCopy() const { |
326 return CreateWithCopiedBuffer(buffer_.get(), size_); | 327 return CreateWithCopiedBuffer(buffer_, size_); |
327 } | 328 } |
328 | 329 |
329 bool BinaryValue::Equals(const Value* other) const { | 330 bool BinaryValue::Equals(const Value* other) const { |
330 if (other->GetType() != GetType()) | 331 if (other->GetType() != GetType()) |
331 return false; | 332 return false; |
332 const BinaryValue* other_binary = static_cast<const BinaryValue*>(other); | 333 const BinaryValue* other_binary = static_cast<const BinaryValue*>(other); |
333 if (other_binary->size_ != size_) | 334 if (other_binary->size_ != size_) |
334 return false; | 335 return false; |
335 return !memcmp(GetBuffer(), other_binary->GetBuffer(), size_); | 336 return !memcmp(buffer_, other_binary->buffer_, size_); |
| 337 } |
| 338 |
| 339 BinaryValue::BinaryValue(char* buffer, size_t size) |
| 340 : Value(TYPE_BINARY), |
| 341 buffer_(buffer), |
| 342 size_(size) { |
| 343 DCHECK(buffer_); |
336 } | 344 } |
337 | 345 |
338 ///////////////////// DictionaryValue //////////////////// | 346 ///////////////////// DictionaryValue //////////////////// |
339 | 347 |
340 DictionaryValue::DictionaryValue() | 348 DictionaryValue::DictionaryValue() |
341 : Value(TYPE_DICTIONARY) { | 349 : Value(TYPE_DICTIONARY) { |
342 } | 350 } |
343 | 351 |
344 DictionaryValue::~DictionaryValue() { | 352 DictionaryValue::~DictionaryValue() { |
345 Clear(); | 353 Clear(); |
(...skipping 599 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
945 if (lhs_it != end() || rhs_it != other_list->end()) | 953 if (lhs_it != end() || rhs_it != other_list->end()) |
946 return false; | 954 return false; |
947 | 955 |
948 return true; | 956 return true; |
949 } | 957 } |
950 | 958 |
951 ValueSerializer::~ValueSerializer() { | 959 ValueSerializer::~ValueSerializer() { |
952 } | 960 } |
953 | 961 |
954 } // namespace base | 962 } // namespace base |
OLD | NEW |