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

Side by Side Diff: base/values.cc

Issue 2065793002: Return a unique_ptr from BinaryValue::CreateWithCopiedBuffer. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix Android and CrOS Created 4 years, 6 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
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 <string.h> 7 #include <string.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 #include <cmath> 10 #include <cmath>
(...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after
297 size_(0) { 297 size_(0) {
298 } 298 }
299 299
300 BinaryValue::BinaryValue(std::unique_ptr<char[]> buffer, size_t size) 300 BinaryValue::BinaryValue(std::unique_ptr<char[]> buffer, size_t size)
301 : Value(TYPE_BINARY), buffer_(std::move(buffer)), size_(size) {} 301 : Value(TYPE_BINARY), buffer_(std::move(buffer)), size_(size) {}
302 302
303 BinaryValue::~BinaryValue() { 303 BinaryValue::~BinaryValue() {
304 } 304 }
305 305
306 // static 306 // static
307 BinaryValue* BinaryValue::CreateWithCopiedBuffer(const char* buffer, 307 std::unique_ptr<BinaryValue> BinaryValue::CreateWithCopiedBuffer(
308 size_t size) { 308 const char* buffer,
309 char* buffer_copy = new char[size]; 309 size_t size) {
310 memcpy(buffer_copy, buffer, size); 310 std::unique_ptr<char[]> buffer_copy(new char[size]);
311 std::unique_ptr<char[]> scoped_buffer_copy(buffer_copy); 311 memcpy(buffer_copy.get(), buffer, size);
312 return new BinaryValue(std::move(scoped_buffer_copy), size); 312 return base::MakeUnique<BinaryValue>(std::move(buffer_copy), size);
313 } 313 }
314 314
315 bool BinaryValue::GetAsBinary(const BinaryValue** out_value) const { 315 bool BinaryValue::GetAsBinary(const BinaryValue** out_value) const {
316 if (out_value) 316 if (out_value)
317 *out_value = this; 317 *out_value = this;
318 return true; 318 return true;
319 } 319 }
320 320
321 BinaryValue* BinaryValue::DeepCopy() const { 321 BinaryValue* BinaryValue::DeepCopy() const {
322 return CreateWithCopiedBuffer(buffer_.get(), size_); 322 return CreateWithCopiedBuffer(buffer_.get(), size_).release();
323 } 323 }
324 324
325 bool BinaryValue::Equals(const Value* other) const { 325 bool BinaryValue::Equals(const Value* other) const {
326 if (other->GetType() != GetType()) 326 if (other->GetType() != GetType())
327 return false; 327 return false;
328 const BinaryValue* other_binary = static_cast<const BinaryValue*>(other); 328 const BinaryValue* other_binary = static_cast<const BinaryValue*>(other);
329 if (other_binary->size_ != size_) 329 if (other_binary->size_ != size_)
330 return false; 330 return false;
331 return !memcmp(GetBuffer(), other_binary->GetBuffer(), size_); 331 return !memcmp(GetBuffer(), other_binary->GetBuffer(), size_);
332 } 332 }
(...skipping 827 matching lines...) Expand 10 before | Expand all | Expand 10 after
1160 ValueDeserializer::~ValueDeserializer() { 1160 ValueDeserializer::~ValueDeserializer() {
1161 } 1161 }
1162 1162
1163 std::ostream& operator<<(std::ostream& out, const Value& value) { 1163 std::ostream& operator<<(std::ostream& out, const Value& value) {
1164 std::string json; 1164 std::string json;
1165 JSONWriter::WriteWithOptions(value, JSONWriter::OPTIONS_PRETTY_PRINT, &json); 1165 JSONWriter::WriteWithOptions(value, JSONWriter::OPTIONS_PRETTY_PRINT, &json);
1166 return out << json; 1166 return out << json;
1167 } 1167 }
1168 1168
1169 } // namespace base 1169 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698