Replace SkFunction with std::function
authormtklein <mtklein@chromium.org>
Thu, 12 Nov 2015 18:41:05 +0000 (10:41 -0800)
committerCommit bot <commit-bot@chromium.org>
Thu, 12 Nov 2015 18:41:05 +0000 (10:41 -0800)
TBR=reed@google.com
No public API changes.

Review URL: https://codereview.chromium.org/1441753002

dm/DMSrcSink.cpp
gyp/core.gypi
include/private/SkFunction.h [deleted file]
include/views/SkOSWindow_Win.h
src/gpu/GrTextureParamsAdjuster.h
tests/CPlusPlusEleven.cpp
tests/FunctionTest.cpp [deleted file]
tools/BUILD.public.expected

index 3581247..c9178c0 100644 (file)
@@ -14,7 +14,6 @@
 #include "SkData.h"
 #include "SkDocument.h"
 #include "SkError.h"
-#include "SkFunction.h"
 #include "SkImageGenerator.h"
 #include "SkMultiPictureDraw.h"
 #include "SkNullCanvas.h"
@@ -30,6 +29,7 @@
 #include "SkTLogic.h"
 #include "SkXMLWriter.h"
 #include "SkSwizzler.h"
+#include <functional>
 
 DEFINE_bool(multiPage, false, "For document-type backends, render the source"
             " into multiple pages");
@@ -1058,16 +1058,16 @@ Error RasterSink::draw(const Src& src, SkBitmap* dst, SkWStream*, SkString*) con
 // Several examples below.
 
 static Error draw_to_canvas(Sink* sink, SkBitmap* bitmap, SkWStream* stream, SkString* log,
-                            SkISize size, SkFunction<Error(SkCanvas*)> draw) {
+                            SkISize size, std::function<Error(SkCanvas*)> draw) {
     class ProxySrc : public Src {
     public:
-        ProxySrc(SkISize size, SkFunction<Error(SkCanvas*)> draw) : fSize(size), fDraw(draw) {}
+        ProxySrc(SkISize size, std::function<Error(SkCanvas*)> draw) : fSize(size), fDraw(draw) {}
         Error   draw(SkCanvas* canvas) const override { return fDraw(canvas); }
         Name                    name() const override { sk_throw(); return ""; } // Won't be called.
         SkISize                 size() const override { return fSize; }
     private:
-        SkISize                      fSize;
-        SkFunction<Error(SkCanvas*)> fDraw;
+        SkISize                         fSize;
+        std::function<Error(SkCanvas*)> fDraw;
     };
     return sink->draw(ProxySrc(size, draw), bitmap, stream, log);
 }
index f54998e..6446fe1 100644 (file)
         '<(skia_include_path)/private/SkChecksum.h',
         '<(skia_include_path)/private/SkFloatBits.h',
         '<(skia_include_path)/private/SkFloatingPoint.h',
-        '<(skia_include_path)/private/SkFunction.h',
         '<(skia_include_path)/private/SkGpuFenceSync.h',
         '<(skia_include_path)/private/SkMiniRecorder.h',
         '<(skia_include_path)/private/SkMutex.h',
diff --git a/include/private/SkFunction.h b/include/private/SkFunction.h
deleted file mode 100644 (file)
index 6be9539..0000000
+++ /dev/null
@@ -1,76 +0,0 @@
-/*
- * Copyright 2015 Google Inc.
- *
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- */
-
-#ifndef SkFunction_DEFINED
-#define SkFunction_DEFINED
-
-// TODO: document, more pervasive move support in constructors, small-Fn optimization
-
-#include "SkUtility.h"
-#include "SkUniquePtr.h"
-#include "SkTypes.h"
-
-template <typename> class SkFunction;
-
-template <typename R, typename... Args>
-class SkFunction<R(Args...)> {
-public:
-    SkFunction() {}
-
-    template <typename Fn>
-    SkFunction(const Fn& fn)
-        : fFunction(new LambdaImpl<Fn>(fn)) {}
-
-    SkFunction(R (*fn)(Args...)) : fFunction(new FnPtrImpl(fn)) {}
-
-    SkFunction(const SkFunction& other) { *this = other; }
-    SkFunction& operator=(const SkFunction& other) {
-        if (this != &other) {
-            fFunction.reset(other.fFunction.get() ? other.fFunction->clone() : nullptr);
-        }
-        return *this;
-    }
-
-    R operator()(Args... args) const {
-        SkASSERT(fFunction.get());
-        return fFunction->call(skstd::forward<Args>(args)...);
-    }
-
-private:
-    struct Interface {
-        virtual ~Interface() {}
-        virtual R call(Args...) const = 0;
-        virtual Interface* clone() const = 0;
-    };
-
-    template <typename Fn>
-    class LambdaImpl final : public Interface {
-    public:
-        LambdaImpl(const Fn& fn) : fFn(fn) {}
-
-        R call(Args... args) const override { return fFn(skstd::forward<Args>(args)...); }
-        Interface* clone() const override { return new LambdaImpl<Fn>(fFn); }
-
-    private:
-        Fn fFn;
-    };
-
-    class FnPtrImpl final : public Interface {
-    public:
-        FnPtrImpl(R (*fn)(Args...)) : fFn(fn) {}
-
-        R call(Args... args) const override { return fFn(skstd::forward<Args>(args)...); }
-        Interface* clone() const override { return new FnPtrImpl(fFn); }
-
-    private:
-        R (*fFn)(Args...);
-    };
-
-    skstd::unique_ptr<Interface> fFunction;
-};
-
-#endif//SkFunction_DEFINED
index b65d4c2..c1a68c6 100644 (file)
@@ -10,9 +10,9 @@
 #ifndef SkOSWindow_Win_DEFINED
 #define SkOSWindow_Win_DEFINED
 
-#include "../private/SkFunction.h"
 #include "../private/SkTHash.h"
 #include "SkWindow.h"
+#include <functional>
 
 #if SK_ANGLE
 #include "EGL/egl.h"
@@ -71,9 +71,9 @@ public:
         return *win;
     }
 
-    // Iterates SkFunction over all the SkOSWindows and their corresponding HWNDs.
-    // The void* argument to the SkFunction is a HWND.
-    static void ForAllWindows(const SkFunction<void(void*, SkOSWindow**)>& f) {
+    // Iterates f over all the SkOSWindows and their corresponding HWNDs.
+    // The void* argument to f is a HWND.
+    static void ForAllWindows(const std::function<void(void*, SkOSWindow**)>& f) {
         gHwndToOSWindowMap.foreach(f);
     }
 
index 0a3d529..cad3920 100644 (file)
@@ -11,7 +11,6 @@
 #include "GrTextureParams.h"
 #include "GrResourceKey.h"
 #include "GrTexture.h"
-#include "SkFunction.h"
 #include "SkTLazy.h"
 
 class GrContext;
@@ -106,7 +105,7 @@ public:
      * @param textureMatrix                    Matrix to apply to local coordinates to compute
      *                                         texel coordinates. The post-transformed coordinates
      *                                         should be in texels (relative to this->width() and
-     *                                         this->height()) and not be normalized. 
+     *                                         this->height()) and not be normalized.
      * @param constraintRect                   Subrect of content area to be rendered. The
      *                                         constraint rect is relative to the content area.
      * @param filterConstriant                 Indicates whether filtering is limited to
@@ -145,7 +144,7 @@ private:
     typedef GrTextureProducer INHERITED;
 };
 
-/** 
+/**
  * Base class for sources that start out as something other than a texture (encoded image,
  * picture, ...).
  */
index 4f74b80..5c2123d 100644 (file)
@@ -6,7 +6,6 @@
  */
 #include "Test.h"
 #include "SkTemplates.h"
-#include "SkFunction.h"
 
 namespace {
 class Moveable {
diff --git a/tests/FunctionTest.cpp b/tests/FunctionTest.cpp
deleted file mode 100644 (file)
index cf6b789..0000000
+++ /dev/null
@@ -1,70 +0,0 @@
-/*
- * Copyright 2015 Google Inc.
- *
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- */
-
-#include "SkFunction.h"
-#include "Test.h"
-
-static void test_add_five(skiatest::Reporter* r, SkFunction<int(int)>& f) {
-    REPORTER_ASSERT(r, f(3) == 8);
-    REPORTER_ASSERT(r, f(4) == 9);
-}
-
-static void test_add_five(skiatest::Reporter* r, SkFunction<int(int)>&& f) { test_add_five(r, f); }
-
-static int add_five(int x) { return x + 5; }
-
-struct AddFive {
-    int operator()(int x) const { return x + 5; };
-};
-
-class MoveOnlyThree : SkNoncopyable {
-public:
-    MoveOnlyThree() {}
-    MoveOnlyThree(MoveOnlyThree&&) {}
-    MoveOnlyThree& operator=(MoveOnlyThree&&) { return *this; }
-
-    int val() { return 3; }
-};
-
-DEF_TEST(Function, r) {
-    // We should be able to turn a function pointer, an explicit functor, or a
-    // lambda into an SkFunction all equally well.
-    test_add_five(r, &add_five);
-    test_add_five(r, AddFive());
-    test_add_five(r, [](int x) { return x + 5; });
-
-    // AddFive and the lambda above are both small enough to test small-object optimization.
-    // Now test a lambda that's much too large for the small-object optimization.
-    int a = 1, b = 1, c = 1, d = 1, e = 1;
-    test_add_five(r, [&](int x) { return x + a + b + c + d + e; });
-
-    // Makes sure we forward arguments when calling SkFunction.
-    SkFunction<int(int, MoveOnlyThree&&, int)> f([](int x, MoveOnlyThree&& three, int y) {
-            return x * three.val() + y;
-    });
-    REPORTER_ASSERT(r, f(2, MoveOnlyThree(), 4) == 10);
-
-    // SkFunctions can go in containers.
-    SkTArray<SkFunction<int(int)>> add_fivers;
-    add_fivers.push_back(&add_five);
-    add_fivers.push_back(AddFive());
-    add_fivers.push_back([](int x) { return x + 5; });
-    add_fivers.push_back([&](int x) { return x + a + b + c + d + e; });
-    for (auto& f : add_fivers) {
-        test_add_five(r, f);
-    }
-
-    // SkFunctions are assignable.
-    SkFunction<int(int)> empty;
-    empty = [](int x) { return x + 5; };
-    test_add_five(r, empty);
-
-    // This all is silly acrobatics, but it should at least work correctly.
-    SkFunction<int(int)> emptyA, emptyB(emptyA);
-    emptyA = emptyB;
-    emptyA = emptyA;
-}
index 63d25e3..f9231d9 100644 (file)
@@ -363,7 +363,6 @@ DM_SRCS = ['dm/DM.cpp',
  'tests/FontNamesTest.cpp',
  'tests/FontObjTest.cpp',
  'tests/FrontBufferedStreamTest.cpp',
- 'tests/FunctionTest.cpp',
  'tests/GLInterfaceValidationTest.cpp',
  'tests/GLProgramsTest.cpp',
  'tests/GeometryTest.cpp',
@@ -979,7 +978,6 @@ SRCS = ['include/private/SkAtomics.h',
  'include/private/SkChecksum.h',
  'include/private/SkFloatBits.h',
  'include/private/SkFloatingPoint.h',
- 'include/private/SkFunction.h',
  'include/private/SkGpuFenceSync.h',
  'include/private/SkMiniRecorder.h',
  'include/private/SkMutex.h',