SkMakeUnique.h defines skstd::make_unique<T>(Args...)

BUG=skia:
GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2293963002

Review-Url: https://codereview.chromium.org/2293963002
This commit is contained in:
halcanary 2016-09-02 11:29:46 -07:00 committed by Commit bot
parent 233eb0adc7
commit 022c2bd37a
6 changed files with 38 additions and 12 deletions

23
src/core/SkMakeUnique.h Normal file
View File

@ -0,0 +1,23 @@
/*
* Copyright 2016 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef SkMakeUnique_DEFINED
#define SkMakeUnique_DEFINED
#include <memory>
namespace skstd {
// std::make_unique is in C++14
template<typename T, typename... Args>
std::unique_ptr<T> make_unique(Args&&... args) {
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
}
#endif // SkMakeUnique_DEFINED

View File

@ -8,6 +8,7 @@
#include "SkData.h" #include "SkData.h"
#include "SkDeflate.h" #include "SkDeflate.h"
#include "SkMakeUnique.h"
#include "zlib.h" #include "zlib.h"
@ -62,7 +63,7 @@ struct SkDeflateWStream::Impl {
SkDeflateWStream::SkDeflateWStream(SkWStream* out, SkDeflateWStream::SkDeflateWStream(SkWStream* out,
int compressionLevel, int compressionLevel,
bool gzip) bool gzip)
: fImpl(new SkDeflateWStream::Impl) { : fImpl(skstd::make_unique<SkDeflateWStream::Impl>()) {
fImpl->fOut = out; fImpl->fOut = out;
fImpl->fInBufferIndex = 0; fImpl->fInBufferIndex = 0;
if (!fImpl->fOut) { if (!fImpl->fOut) {

View File

@ -13,6 +13,7 @@
#include "SkColorFilter.h" #include "SkColorFilter.h"
#include "SkDraw.h" #include "SkDraw.h"
#include "SkGlyphCache.h" #include "SkGlyphCache.h"
#include "SkMakeUnique.h"
#include "SkPath.h" #include "SkPath.h"
#include "SkPathEffect.h" #include "SkPathEffect.h"
#include "SkPathOps.h" #include "SkPathOps.h"
@ -1344,11 +1345,11 @@ std::unique_ptr<SkStreamAsset> SkPDFDevice::content() const {
entry.fContent.writeToStream(&buffer); entry.fContent.writeToStream(&buffer);
} }
gsState.drainStack(); gsState.drainStack();
if (buffer.bytesWritten() > 0) {
return std::unique_ptr<SkStreamAsset>( return std::unique_ptr<SkStreamAsset>(buffer.detachAsStream());
buffer.bytesWritten() > 0 } else {
? buffer.detachAsStream() return skstd::make_unique<SkMemoryStream>();
: new SkMemoryStream); }
} }
/* Draws an inverse filled path by using Path Ops to compute the positive /* Draws an inverse filled path by using Path Ops to compute the positive

View File

@ -5,6 +5,7 @@
* found in the LICENSE file. * found in the LICENSE file.
*/ */
#include "SkMakeUnique.h"
#include "SkPDFCanon.h" #include "SkPDFCanon.h"
#include "SkPDFCanvas.h" #include "SkPDFCanvas.h"
#include "SkPDFDevice.h" #include "SkPDFDevice.h"
@ -167,7 +168,6 @@ static sk_sp<SkPDFDict> generate_page_tree(SkTArray<sk_sp<SkPDFDict>>* pages) {
return std::move(curNodes[0]); return std::move(curNodes[0]);
} }
template <typename T> static T* clone(const T* o) { return o ? new T(*o) : nullptr; }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
SkPDFDocument::SkPDFDocument(SkWStream* stream, SkPDFDocument::SkPDFDocument(SkWStream* stream,
@ -466,7 +466,7 @@ sk_sp<SkDocument> SkPDFMakeDocument(SkWStream* stream,
sk_sp<SkDocument> SkDocument::MakePDF(const char path[], SkScalar dpi) { sk_sp<SkDocument> SkDocument::MakePDF(const char path[], SkScalar dpi) {
auto delete_wstream = [](SkWStream* stream, bool) { delete stream; }; auto delete_wstream = [](SkWStream* stream, bool) { delete stream; };
std::unique_ptr<SkFILEWStream> stream(new SkFILEWStream(path)); auto stream = skstd::make_unique<SkFILEWStream>(path);
return stream->isValid() return stream->isValid()
? SkPDFMakeDocument(stream.release(), delete_wstream, dpi, ? SkPDFMakeDocument(stream.release(), delete_wstream, dpi,
SkDocument::PDFMetadata(), nullptr, SkDocument::PDFMetadata(), nullptr,

View File

@ -7,6 +7,7 @@
#include "SkData.h" #include "SkData.h"
#include "SkDeflate.h" #include "SkDeflate.h"
#include "SkMakeUnique.h"
#include "SkPDFTypes.h" #include "SkPDFTypes.h"
#include "SkPDFUtils.h" #include "SkPDFUtils.h"
#include "SkStream.h" #include "SkStream.h"
@ -506,8 +507,7 @@ void SkPDFSharedStream::addResources(
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
SkPDFStream:: SkPDFStream(sk_sp<SkData> data) { SkPDFStream:: SkPDFStream(sk_sp<SkData> data) {
this->setData(std::unique_ptr<SkStreamAsset>( this->setData(skstd::make_unique<SkMemoryStream>(std::move(data)));
new SkMemoryStream(std::move(data))));
} }
SkPDFStream::SkPDFStream(std::unique_ptr<SkStreamAsset> stream) { SkPDFStream::SkPDFStream(std::unique_ptr<SkStreamAsset> stream) {

View File

@ -12,6 +12,7 @@
#include "SkDocument.h" #include "SkDocument.h"
#include "SkDeflate.h" #include "SkDeflate.h"
#include "SkImageEncoder.h" #include "SkImageEncoder.h"
#include "SkMakeUnique.h"
#include "SkMatrix.h" #include "SkMatrix.h"
#include "SkPDFCanon.h" #include "SkPDFCanon.h"
#include "SkPDFDevice.h" #include "SkPDFDevice.h"
@ -75,8 +76,8 @@ static void assert_emit_eq(skiatest::Reporter* reporter,
static void TestPDFStream(skiatest::Reporter* reporter) { static void TestPDFStream(skiatest::Reporter* reporter) {
char streamBytes[] = "Test\nFoo\tBar"; char streamBytes[] = "Test\nFoo\tBar";
std::unique_ptr<SkStreamAsset> streamData(new SkMemoryStream( auto streamData = skstd::make_unique<SkMemoryStream>(
streamBytes, strlen(streamBytes), true)); streamBytes, strlen(streamBytes), true);
auto stream = sk_make_sp<SkPDFStream>(std::move(streamData)); auto stream = sk_make_sp<SkPDFStream>(std::move(streamData));
assert_emit_eq(reporter, assert_emit_eq(reporter,
*stream, *stream,