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

Side by Side Diff: base/values.cc

Issue 11745016: BinaryValue support for NULL buffer. (Closed) Base URL: https://git.chromium.org/chromium/src.git@master
Patch Set: Created 7 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
« 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) 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 #include <ostream> 8 #include <ostream>
9 9
10 #include "base/float_util.h" 10 #include "base/float_util.h"
(...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after
299 299
300 bool StringValue::Equals(const Value* other) const { 300 bool StringValue::Equals(const Value* other) const {
301 if (other->GetType() != GetType()) 301 if (other->GetType() != GetType())
302 return false; 302 return false;
303 std::string lhs, rhs; 303 std::string lhs, rhs;
304 return GetAsString(&lhs) && other->GetAsString(&rhs) && lhs == rhs; 304 return GetAsString(&lhs) && other->GetAsString(&rhs) && lhs == rhs;
305 } 305 }
306 306
307 ///////////////////// BinaryValue //////////////////// 307 ///////////////////// BinaryValue ////////////////////
308 308
309 BinaryValue::~BinaryValue() { 309 BinaryValue::BinaryValue()
310 DCHECK(buffer_); 310 : Value(TYPE_BINARY),
311 if (buffer_) 311 buffer_(NULL),
312 delete[] buffer_; 312 size_(0) {
313 } 313 }
314 314
315 // static 315 BinaryValue::BinaryValue(scoped_array<char> buffer, size_t size)
316 BinaryValue* BinaryValue::Create(char* buffer, size_t size) { 316 : Value(TYPE_BINARY),
317 if (!buffer) 317 buffer_(buffer.release()),
brettw 2013/01/03 22:01:23 Can you use .Pass() here instead?
rpaquay 2013/01/03 23:20:02 Done.
318 return NULL; 318 size_(size) {
319 }
319 320
320 return new BinaryValue(buffer, size); 321 BinaryValue::~BinaryValue() {
321 } 322 }
322 323
323 // static 324 // static
324 BinaryValue* BinaryValue::CreateWithCopiedBuffer(const char* buffer, 325 BinaryValue* BinaryValue::CreateWithCopiedBuffer(const char* buffer,
325 size_t size) { 326 size_t size) {
326 if (!buffer)
327 return NULL;
328
329 char* buffer_copy = new char[size]; 327 char* buffer_copy = new char[size];
330 memcpy(buffer_copy, buffer, size); 328 memcpy(buffer_copy, buffer, size);
331 return new BinaryValue(buffer_copy, size); 329 scoped_array<char> scoped_buffer_copy(buffer_copy);
330 return new BinaryValue(scoped_buffer_copy.Pass(), size);
332 } 331 }
333 332
334 BinaryValue* BinaryValue::DeepCopy() const { 333 BinaryValue* BinaryValue::DeepCopy() const {
335 return CreateWithCopiedBuffer(buffer_, size_); 334 return CreateWithCopiedBuffer(buffer_.get(), size_);
336 } 335 }
337 336
338 bool BinaryValue::Equals(const Value* other) const { 337 bool BinaryValue::Equals(const Value* other) const {
339 if (other->GetType() != GetType()) 338 if (other->GetType() != GetType())
340 return false; 339 return false;
341 const BinaryValue* other_binary = static_cast<const BinaryValue*>(other); 340 const BinaryValue* other_binary = static_cast<const BinaryValue*>(other);
342 if (other_binary->size_ != size_) 341 if (other_binary->size_ != size_)
343 return false; 342 return false;
344 return !memcmp(buffer_, other_binary->buffer_, size_); 343 return !memcmp(GetBuffer(), other_binary->GetBuffer(), size_);
345 }
346
347 BinaryValue::BinaryValue(char* buffer, size_t size)
348 : Value(TYPE_BINARY),
349 buffer_(buffer),
350 size_(size) {
351 DCHECK(buffer_);
352 } 344 }
353 345
354 ///////////////////// DictionaryValue //////////////////// 346 ///////////////////// DictionaryValue ////////////////////
355 347
356 DictionaryValue::DictionaryValue() 348 DictionaryValue::DictionaryValue()
357 : Value(TYPE_DICTIONARY) { 349 : Value(TYPE_DICTIONARY) {
358 } 350 }
359 351
360 DictionaryValue::~DictionaryValue() { 352 DictionaryValue::~DictionaryValue() {
361 Clear(); 353 Clear();
(...skipping 773 matching lines...) Expand 10 before | Expand all | Expand 10 after
1135 1127
1136 std::ostream& operator<<(std::ostream& out, const Value& value) { 1128 std::ostream& operator<<(std::ostream& out, const Value& value) {
1137 std::string json; 1129 std::string json;
1138 JSONWriter::WriteWithOptions(&value, 1130 JSONWriter::WriteWithOptions(&value,
1139 JSONWriter::OPTIONS_PRETTY_PRINT, 1131 JSONWriter::OPTIONS_PRETTY_PRINT,
1140 &json); 1132 &json);
1141 return out << json; 1133 return out << json;
1142 } 1134 }
1143 1135
1144 } // namespace base 1136 } // 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