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

Side by Side Diff: base/values.cc

Issue 10389088: BinaryValue does not support NULL buffer. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Explicit out-of-line destructor. Take 2. Created 8 years, 7 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 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
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 DCHECK(buffer_); 302 : Value(TYPE_BINARY),
303 if (buffer_) 303 buffer_(NULL),
304 delete[] buffer_; 304 size_(0) {
305 } 305 }
306 306
307 // static 307 BinaryValue::BinaryValue(scoped_ptr<char> buffer, size_t size)
308 BinaryValue* BinaryValue::Create(char* buffer, size_t size) { 308 : Value(TYPE_BINARY),
309 if (!buffer) 309 buffer_(buffer.release()),
310 return NULL; 310 size_(size) {
311 }
311 312
312 return new BinaryValue(buffer, size); 313 BinaryValue::~BinaryValue() {
313 } 314 }
314 315
315 // static 316 // static
316 BinaryValue* BinaryValue::CreateWithCopiedBuffer(const char* buffer, 317 BinaryValue* BinaryValue::CreateWithCopiedBuffer(const char* buffer,
317 size_t size) { 318 size_t size) {
318 if (!buffer)
319 return NULL;
320
321 char* buffer_copy = new char[size]; 319 char* buffer_copy = new char[size];
322 memcpy(buffer_copy, buffer, size); 320 memcpy(buffer_copy, buffer, size);
323 return new BinaryValue(buffer_copy, size); 321 scoped_ptr<char> scoped_buffer_copy(buffer_copy);
322 return new BinaryValue(scoped_buffer_copy.Pass(), size);
324 } 323 }
325 324
326 BinaryValue* BinaryValue::DeepCopy() const { 325 BinaryValue* BinaryValue::DeepCopy() const {
327 return CreateWithCopiedBuffer(buffer_, size_); 326 return CreateWithCopiedBuffer(buffer_.get(), size_);
328 } 327 }
329 328
330 bool BinaryValue::Equals(const Value* other) const { 329 bool BinaryValue::Equals(const Value* other) const {
331 if (other->GetType() != GetType()) 330 if (other->GetType() != GetType())
332 return false; 331 return false;
333 const BinaryValue* other_binary = static_cast<const BinaryValue*>(other); 332 const BinaryValue* other_binary = static_cast<const BinaryValue*>(other);
334 if (other_binary->size_ != size_) 333 if (other_binary->size_ != size_)
335 return false; 334 return false;
336 return !memcmp(buffer_, other_binary->buffer_, size_); 335 return !memcmp(GetBuffer(), other_binary->GetBuffer(), size_);
337 }
338
339 BinaryValue::BinaryValue(char* buffer, size_t size)
340 : Value(TYPE_BINARY),
341 buffer_(buffer),
342 size_(size) {
343 DCHECK(buffer_);
344 } 336 }
345 337
346 ///////////////////// DictionaryValue //////////////////// 338 ///////////////////// DictionaryValue ////////////////////
347 339
348 DictionaryValue::DictionaryValue() 340 DictionaryValue::DictionaryValue()
349 : Value(TYPE_DICTIONARY) { 341 : Value(TYPE_DICTIONARY) {
350 } 342 }
351 343
352 DictionaryValue::~DictionaryValue() { 344 DictionaryValue::~DictionaryValue() {
353 Clear(); 345 Clear();
(...skipping 599 matching lines...) Expand 10 before | Expand all | Expand 10 after
953 if (lhs_it != end() || rhs_it != other_list->end()) 945 if (lhs_it != end() || rhs_it != other_list->end())
954 return false; 946 return false;
955 947
956 return true; 948 return true;
957 } 949 }
958 950
959 ValueSerializer::~ValueSerializer() { 951 ValueSerializer::~ValueSerializer() {
960 } 952 }
961 953
962 } // namespace base 954 } // 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