Revert of skstd -> std for unique_ptr (patchset #24 id:460001 of https://codereview...
authormtklein <mtklein@google.com>
Mon, 25 Jan 2016 03:49:24 +0000 (19:49 -0800)
committerCommit bot <commit-bot@chromium.org>
Mon, 25 Jan 2016 03:49:24 +0000 (19:49 -0800)
Reason for revert:
Still need an answer for SkAdvancedTypefaceMetrics (at least for Google3 iOS build).

Original issue's description:
> skstd -> std for unique_ptr
>
> TBR=reed@google.com
> No public API changes.
>
> BUG=skia:4564
>
> Committed: https://skia.googlesource.com/skia/+/755c553c17b82bb5de3d9cc8d3b2a866ff9e9e50
>
> CQ_EXTRA_TRYBOTS=client.skia.compile:Build-Mac10.9-Clang-x86_64-Release-CMake-Trybot,Build-Ubuntu-GCC-x86_64-Debug-CrOS_Link-Trybot;client.skia:Perf-Mac10.9-Clang-MacMini6.2-CPU-AVX-x86_64-Release-Trybot,Test-iOS-Clang-iPad4-GPU-SGX554-Arm7-Release-Trybot
>
> Committed: https://skia.googlesource.com/skia/+/06189155d987db5c7e69015f6ea87c2168d6a065
>
> Committed: https://skia.googlesource.com/skia/+/70e8dfca4a7f5bce97b8021a6e378c4828b09c8c
>
> Committed: https://skia.googlesource.com/skia/+/dadfc245cc9a0279ff7b73da3344f2ca5d139907
> GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1436033003
>
> Committed: https://skia.googlesource.com/skia/+/ccf1de0d9aa75f29829f1c4c462214b991fd8c9e

TBR=bungeman@google.com,mtklein@chromium.org
# Skipping CQ checks because original CL landed less than 1 days ago.
NOPRESUBMIT=true
NOTREECHECKS=true
NOTRY=true
BUG=skia:4564

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

gyp/core.gypi
include/core/SkRefCnt.h
include/private/SkOncePtr.h
include/private/SkTemplates.h
include/private/SkUniquePtr.h [new file with mode: 0644]
src/core/SkSharedMutex.h
tests/CPlusPlusEleven.cpp [new file with mode: 0644]

index 2d1db60..ff1282f 100644 (file)
         '<(skia_include_path)/private/SkTDict.h',
         '<(skia_include_path)/private/SkTSearch.h',
         '<(skia_include_path)/private/SkTLogic.h',
+        '<(skia_include_path)/private/SkUniquePtr.h',
         '<(skia_include_path)/private/SkWeakRefCnt.h',
 
         # Path ops
index 1779278..9d1e5f1 100644 (file)
@@ -9,8 +9,8 @@
 #define SkRefCnt_DEFINED
 
 #include "../private/SkAtomics.h"
+#include "../private/SkUniquePtr.h"
 #include "SkTypes.h"
-#include <memory>
 
 /** \class SkRefCntBase
 
@@ -185,22 +185,12 @@ template <typename T> struct SkTUnref {
 /**
  *  Utility class that simply unref's its argument in the destructor.
  */
-template <typename T> class SkAutoTUnref {
+template <typename T> class SkAutoTUnref : public skstd::unique_ptr<T, SkTUnref<T>> {
 public:
-    explicit SkAutoTUnref(T* obj = nullptr) : fPtr(obj) {}
+    explicit SkAutoTUnref(T* obj = nullptr) : skstd::unique_ptr<T, SkTUnref<T>>(obj) {}
 
-    void swap(SkAutoTUnref& other) { fPtr.swap(other.fPtr); }
-
-    T*        get() const { return fPtr.get(); }
-    operator T*  () const { return fPtr.get(); }
-    T* operator->() const { return fPtr.get(); }
-
-    void reset(T* ptr = nullptr) { fPtr.reset(ptr); }
-    T* detach()  { return fPtr.release(); }
-    T* release() { return fPtr.release(); }
-
-private:
-    std::unique_ptr<T, SkTUnref<T>> fPtr;
+    T* detach() { return this->release(); }
+    operator T*() const { return this->get(); }
 };
 // Can't use the #define trick below to guard a bare SkAutoTUnref(...) because it's templated. :(
 
index 3c1ab63..261f9a7 100644 (file)
@@ -9,7 +9,7 @@
 #define SkOncePtr_DEFINED
 
 #include "../private/SkAtomics.h"
-#include <memory>
+#include "SkUniquePtr.h"
 
 template <typename T> class SkBaseOncePtr;
 
@@ -17,7 +17,7 @@ template <typename T> class SkBaseOncePtr;
 #define SK_DECLARE_STATIC_ONCE_PTR(type, name) namespace {} static SkBaseOncePtr<type> name;
 
 // Use this for a local or member pointer that's initialized exactly once when you call get().
-template <typename T, typename Delete = std::default_delete<T>>
+template <typename T, typename Delete = skstd::default_delete<T>>
 class SkOncePtr : SkNoncopyable {
 public:
     SkOncePtr() { sk_bzero(this, sizeof(*this)); }
@@ -42,7 +42,7 @@ private:
 
 // If you ask for SkOncePtr<T[]>, we'll clean up with delete[] by default.
 template <typename T>
-class SkOncePtr<T[]> : public SkOncePtr<T, std::default_delete<T[]>> {};
+class SkOncePtr<T[]> : public SkOncePtr<T, skstd::default_delete<T[]>> {};
 
 /* TODO(mtklein): in next CL
 typedef SkBaseOncePtr<void> SkOnceFlag;
index 9776a99..496cf42 100644 (file)
@@ -13,8 +13,8 @@
 #include "SkMath.h"
 #include "SkTLogic.h"
 #include "SkTypes.h"
+#include "SkUniquePtr.h"
 #include <limits.h>
-#include <memory>
 #include <new>
 
 /** \file SkTemplates.h
@@ -57,18 +57,13 @@ template <typename R, typename T, R (*P)(T*)> struct SkFunctionWrapper {
     reference is null when the destructor is called, we do not call the
     function.
 */
-template <typename T, void (*P)(T*)> class SkAutoTCallVProc {
+template <typename T, void (*P)(T*)> class SkAutoTCallVProc
+    : public skstd::unique_ptr<T, SkFunctionWrapper<void, T, P>> {
 public:
-    SkAutoTCallVProc(T* obj) : fPtr(obj) {}
+    SkAutoTCallVProc(T* obj): skstd::unique_ptr<T, SkFunctionWrapper<void, T, P>>(obj) {}
 
-    T*        get() const { return fPtr.get(); }
-    operator T*  () const { return fPtr.get(); }
-    T* operator->() const { return fPtr.get(); }
-
-    T* detach() { return fPtr.release(); }
-    void reset(T* ptr = nullptr) { fPtr.reset(ptr); }
-private:
-    std::unique_ptr<T, SkFunctionWrapper<void, T, P>> fPtr;
+    operator T*() const { return this->get(); }
+    T* detach() { return this->release(); }
 };
 
 /** \class SkAutoTCallIProc
@@ -79,18 +74,13 @@ If detach() is called, the object reference is set to null. If the object
 reference is null when the destructor is called, we do not call the
 function.
 */
-template <typename T, int (*P)(T*)> class SkAutoTCallIProc {
+template <typename T, int (*P)(T*)> class SkAutoTCallIProc
+    : public skstd::unique_ptr<T, SkFunctionWrapper<int, T, P>> {
 public:
-    SkAutoTCallIProc(T* obj) : fPtr(obj) {}
+    SkAutoTCallIProc(T* obj): skstd::unique_ptr<T, SkFunctionWrapper<int, T, P>>(obj) {}
 
-    T*        get() const { return fPtr.get(); }
-    operator T*  () const { return fPtr.get(); }
-    T* operator->() const { return fPtr.get(); }
-
-    T* detach() { return fPtr.release(); }
-    void reset(T* ptr = nullptr) { fPtr.reset(ptr); }
-private:
-    std::unique_ptr<T, SkFunctionWrapper<int, T, P>> fPtr;
+    operator T*() const { return this->get(); }
+    T* detach() { return this->release(); }
 };
 
 /** \class SkAutoTDelete
@@ -103,27 +93,18 @@ private:
 
   The size of a SkAutoTDelete is small: sizeof(SkAutoTDelete<T>) == sizeof(T*)
 */
-template <typename T> class SkAutoTDelete {
+template <typename T> class SkAutoTDelete : public skstd::unique_ptr<T> {
 public:
-    SkAutoTDelete(T* obj = NULL) : fPtr(obj) {}
+    SkAutoTDelete(T* obj = NULL) : skstd::unique_ptr<T>(obj) {}
 
-    void swap(SkAutoTDelete& other) { fPtr.swap(other.fPtr); }
-
-    T*        get() const { return fPtr.get(); }
-    operator T*  () const { return fPtr.get(); }
-    T* operator->() const { return fPtr.get(); }
-
-    void reset(T* ptr = nullptr) { fPtr.reset(ptr); }
-    void free() { fPtr.reset(nullptr); }
-    T* detach() { return fPtr.release(); }
-    T* release() { return fPtr.release(); }
-private:
-    std::unique_ptr<T> fPtr;
+    operator T*() const { return this->get(); }
+    void free() { this->reset(nullptr); }
+    T* detach() { return this->release(); }
 };
 
-template <typename T> class SkAutoTDeleteArray : public std::unique_ptr<T[]> {
+template <typename T> class SkAutoTDeleteArray : public skstd::unique_ptr<T[]> {
 public:
-    SkAutoTDeleteArray(T array[]) : std::unique_ptr<T[]>(array) {}
+    SkAutoTDeleteArray(T array[]) : skstd::unique_ptr<T[]>(array) {}
 
     void free() { this->reset(nullptr); }
     T* detach() { return this->release(); }
diff --git a/include/private/SkUniquePtr.h b/include/private/SkUniquePtr.h
new file mode 100644 (file)
index 0000000..d87ce29
--- /dev/null
@@ -0,0 +1,401 @@
+/*
+ * 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 SkUniquePtr_DEFINED
+#define SkUniquePtr_DEFINED
+
+#include "SkTLogic.h"
+#include <cstddef>
+#include <utility>
+
+namespace skstd {
+
+template <typename T> struct default_delete {
+    /*constexpr*/ default_delete() /*noexcept*/ = default;
+
+    template <typename U, typename = enable_if_t<is_convertible<U*, T*>::value>>
+    default_delete(const default_delete<U>&) /*noexcept*/ {}
+
+    void operator()(T* obj) const {
+        static_assert(sizeof(T) > 0, "Deleting pointer to incomplete type!");
+        delete obj;
+    }
+};
+template <typename T> struct default_delete<T[]> {
+    /*constexpr*/ default_delete() /*noexcept*/ = default;
+
+    void operator()(T* obj) const {
+        static_assert(sizeof(T) > 0, "Deleting pointer to incomplete type!");
+        delete [] obj;
+    }
+};
+
+template <typename T, typename D = default_delete<T>> class unique_ptr {
+    // remove_reference_t<D>::pointer if that type exists, otherwise T*.
+    struct pointer_type_detector {
+        template <typename U> static typename U::pointer detector(typename U::pointer*);
+        template <typename U> static T* detector(...);
+        using type = decltype(detector<remove_reference_t<D>>(0));
+    };
+
+public:
+    using pointer = typename pointer_type_detector::type;
+    using element_type = T;
+    using deleter_type = D;
+
+private:
+    template <typename B, bool = std::is_empty<B>::value /*&& !is_final<B>::value*/>
+    struct compressed_base : private B {
+        /*constexpr*/ compressed_base() : B() {}
+        /*constexpr*/ compressed_base(const B& b) : B(b) {}
+        /*constexpr*/ compressed_base(B&& b) : B(std::move(b)) {}
+        /*constexpr*/ B& get() /*noexcept*/ { return *this; }
+        /*constexpr*/ B const& get() const /*noexcept*/ { return *this; }
+        void swap(compressed_base&) /*noexcept*/ { }
+    };
+
+    template <typename B> struct compressed_base<B, false> {
+        B fb;
+        /*constexpr*/ compressed_base() : B() {}
+        /*constexpr*/ compressed_base(const B& b) : fb(b) {}
+        /*constexpr*/ compressed_base(B&& b) : fb(std::move(b)) {}
+        /*constexpr*/ B& get() /*noexcept*/ { return fb; }
+        /*constexpr*/ B const& get() const /*noexcept*/ { return fb; }
+        void swap(compressed_base& that) /*noexcept*/ { SkTSwap(fb, that.fB); }
+    };
+
+    struct compressed_data : private compressed_base<deleter_type> {
+        pointer fPtr;
+        /*constexpr*/ compressed_data() : compressed_base<deleter_type>(), fPtr() {}
+        /*constexpr*/ compressed_data(const pointer& ptr, const deleter_type& d)
+            : compressed_base<deleter_type>(d), fPtr(ptr) {}
+        template <typename U1, typename U2, typename = enable_if_t<
+            is_convertible<U1, pointer>::value && is_convertible<U2, deleter_type>::value
+        >> /*constexpr*/ compressed_data(U1&& ptr, U2&& d)
+            : compressed_base<deleter_type>(std::forward<U2>(d)), fPtr(std::forward<U1>(ptr)) {}
+        /*constexpr*/ pointer& getPointer() /*noexcept*/ { return fPtr; }
+        /*constexpr*/ pointer const& getPointer() const /*noexcept*/ { return fPtr; }
+        /*constexpr*/ deleter_type& getDeleter() /*noexcept*/ {
+            return compressed_base<deleter_type>::get();
+        }
+        /*constexpr*/ deleter_type const& getDeleter() const /*noexcept*/ {
+            return compressed_base<deleter_type>::get();
+        }
+        void swap(compressed_data& that) /*noexcept*/ {
+            compressed_base<deleter_type>::swap(static_cast<compressed_base<deleter_type>>(that));
+            SkTSwap(fPtr, that.fPtr);
+        }
+    };
+    compressed_data data;
+
+public:
+    /*constexpr*/ unique_ptr() /*noexcept*/ : data() {
+        static_assert(!std::is_pointer<deleter_type>::value, "Deleter nullptr function pointer!");
+    }
+
+    /*constexpr*/ unique_ptr(std::nullptr_t) /*noexcept*/ : unique_ptr() { }
+
+    explicit unique_ptr(pointer ptr) /*noexcept*/ : data(ptr, deleter_type()) {
+        static_assert(!std::is_pointer<deleter_type>::value, "Deleter nullptr function pointer!");
+    }
+
+    unique_ptr(pointer ptr,
+               conditional_t<std::is_reference<deleter_type>::value,
+                             deleter_type, const deleter_type&> d)
+    /*noexcept*/ : data(ptr, d)
+    {}
+
+    unique_ptr(pointer ptr, remove_reference_t<deleter_type>&& d) /*noexcept*/
+        : data(std::move(ptr), std::move(d))
+    {
+        static_assert(!std::is_reference<deleter_type>::value,
+            "Binding an rvalue reference deleter as an lvalue reference deleter is not allowed.");
+    }
+
+
+    unique_ptr(unique_ptr&& that) /*noexcept*/
+        : data(that.release(), std::forward<deleter_type>(that.get_deleter()))
+    {}
+
+    template <typename U, typename ThatD, typename = enable_if_t<
+        is_convertible<typename unique_ptr<U, ThatD>::pointer, pointer>::value &&
+        !std::is_array<U>::value &&
+        conditional_t<std::is_reference<D>::value,
+                      std::is_same<ThatD, D>,
+                      is_convertible<ThatD, D>>::value>>
+    unique_ptr(unique_ptr<U, ThatD>&& that) /*noexcept*/
+        : data(that.release(), std::forward<ThatD>(that.get_deleter()))
+    {}
+
+    ~unique_ptr() /*noexcept*/ {
+        pointer& ptr = data.getPointer();
+        if (ptr != nullptr) {
+            get_deleter()(ptr);
+        }
+        ptr = pointer();
+    }
+
+    unique_ptr& operator=(unique_ptr&& that) /*noexcept*/ {
+        reset(that.release());
+        get_deleter() = std::forward<deleter_type>(that.get_deleter());
+        return *this;
+    }
+
+    template <typename U, typename ThatD> enable_if_t<
+        is_convertible<typename unique_ptr<U, ThatD>::pointer, pointer>::value &&
+        !std::is_array<U>::value,
+    unique_ptr&> operator=(unique_ptr<U, ThatD>&& that) /*noexcept*/ {
+        reset(that.release());
+        get_deleter() = std::forward<ThatD>(that.get_deleter());
+        return *this;
+    }
+
+    unique_ptr& operator=(std::nullptr_t) /*noexcept*/ {
+        reset();
+        return *this;
+    }
+
+    add_lvalue_reference_t<element_type> operator*() const {
+        SkASSERT(get() != pointer());
+        return *get();
+    }
+
+    pointer operator->() const /*noexcept*/ {
+        SkASSERT(get() != pointer());
+        return get();
+    }
+
+    pointer get() const /*noexcept*/ {
+        return data.getPointer();
+    }
+
+    deleter_type& get_deleter() /*noexcept*/ {
+        return data.getDeleter();
+    }
+
+    const deleter_type& get_deleter() const /*noexcept*/ {
+        return data.getDeleter();
+    }
+
+    //explicit operator bool() const noexcept {
+    bool is_attached() const /*noexcept*/ {
+        return get() == pointer() ? false : true;
+    }
+
+    pointer release() /*noexcept*/ {
+        pointer ptr = get();
+        data.getPointer() = pointer();
+        return ptr;
+    }
+
+    void reset(pointer ptr = pointer()) /*noexcept*/ {
+        SkTSwap(data.getPointer(), ptr);
+        if (ptr != pointer()) {
+            get_deleter()(ptr);
+        }
+    }
+
+    void swap(unique_ptr& that) /*noexcept*/ {
+        SkTSwap(data, that.data);
+    }
+
+    unique_ptr(const unique_ptr&) = delete;
+    unique_ptr& operator=(const unique_ptr&) = delete;
+};
+
+template <typename T, typename D> class unique_ptr<T[], D> {
+    // remove_reference_t<D>::pointer if that type exists, otherwise T*.
+    struct pointer_type_detector {
+        template <typename U> static typename U::pointer detector(typename U::pointer*);
+        template <typename U> static T* detector(...);
+        using type = decltype(detector<remove_reference_t<D>>(0));
+    };
+
+public:
+    using pointer = typename pointer_type_detector::type;
+    using element_type = T;
+    using deleter_type = D;
+
+private:
+    template <typename B, bool = std::is_empty<B>::value /*&& !is_final<B>::value*/>
+    struct compressed_base : private B {
+        /*constexpr*/ compressed_base() : B() {}
+        /*constexpr*/ compressed_base(const B& b) : B(b) {}
+        /*constexpr*/ compressed_base(B&& b) : B(std::move(b)) {}
+        /*constexpr*/ B& get() /*noexcept*/ { return *this; }
+        /*constexpr*/ B const& get() const /*noexcept*/ { return *this; }
+        void swap(compressed_base&) /*noexcept*/ { }
+    };
+
+    template <typename B> struct compressed_base<B, false> {
+        B fb;
+        /*constexpr*/ compressed_base() : B() {}
+        /*constexpr*/ compressed_base(const B& b) : fb(b) {}
+        /*constexpr*/ compressed_base(B&& b) : fb(std::move(b)) {}
+        /*constexpr*/ B& get() /*noexcept*/ { return fb; }
+        /*constexpr*/ B const& get() const /*noexcept*/ { return fb; }
+        void swap(compressed_base& that) /*noexcept*/ { SkTSwap(fb, that.fB); }
+    };
+
+    struct compressed_data : private compressed_base<deleter_type> {
+        pointer fPtr;
+        /*constexpr*/ compressed_data() : compressed_base<deleter_type>(), fPtr() {}
+        /*constexpr*/ compressed_data(const pointer& ptr, const deleter_type& d)
+            : compressed_base<deleter_type>(d), fPtr(ptr) {}
+        template <typename U1, typename U2, typename = enable_if_t<
+            is_convertible<U1, pointer>::value && is_convertible<U2, deleter_type>::value
+        >> /*constexpr*/ compressed_data(U1&& ptr, U2&& d)
+            : compressed_base<deleter_type>(std::forward<U2>(d)), fPtr(std::forward<U1>(ptr)) {}
+        /*constexpr*/ pointer& getPointer() /*noexcept*/ { return fPtr; }
+        /*constexpr*/ pointer const& getPointer() const /*noexcept*/ { return fPtr; }
+        /*constexpr*/ deleter_type& getDeleter() /*noexcept*/ {
+            return compressed_base<deleter_type>::get();
+        }
+        /*constexpr*/ deleter_type const& getDeleter() const /*noexcept*/ {
+            return compressed_base<deleter_type>::get();
+        }
+        void swap(compressed_data& that) /*noexcept*/ {
+            compressed_base<deleter_type>::swap(static_cast<compressed_base<deleter_type>>(that));
+            SkTSwap(fPtr, that.fPtr);
+        }
+    };
+    compressed_data data;
+
+public:
+    /*constexpr*/ unique_ptr() /*noexcept*/ : data() {
+        static_assert(!std::is_pointer<deleter_type>::value, "Deleter nullptr function pointer!");
+    }
+
+    /*constexpr*/ unique_ptr(std::nullptr_t) /*noexcept*/ : unique_ptr() { }
+
+    explicit unique_ptr(pointer ptr) /*noexcept*/ : data(ptr, deleter_type()) {
+        static_assert(!std::is_pointer<deleter_type>::value, "Deleter nullptr function pointer!");
+    }
+
+    unique_ptr(pointer ptr,
+               conditional_t<std::is_reference<deleter_type>::value,
+                             deleter_type, const deleter_type&> d)
+    /*noexcept*/ : data(ptr, d)
+    {}
+
+    unique_ptr(pointer ptr, remove_reference_t<deleter_type>&& d) /*noexcept*/
+        : data(std::move(ptr), std::move(d))
+    {
+        static_assert(!std::is_reference<deleter_type>::value,
+            "Binding an rvalue reference deleter as an lvalue reference deleter is not allowed.");
+    }
+
+    unique_ptr(unique_ptr&& that) /*noexcept*/
+        : data(that.release(), std::forward<deleter_type>(that.get_deleter()))
+    {}
+
+    ~unique_ptr() {
+        pointer& ptr = data.getPointer();
+        if (ptr != nullptr) {
+          get_deleter()(ptr);
+        }
+        ptr = pointer();
+    }
+
+    unique_ptr& operator=(unique_ptr&& that) /*noexcept*/ {
+        reset(that.release());
+        get_deleter() = std::forward<deleter_type>(that.get_deleter());
+        return *this;
+    }
+
+    unique_ptr& operator=(std::nullptr_t) /*noexcept*/ {
+        reset();
+        return *this;
+    }
+
+    add_lvalue_reference_t<element_type> operator[](size_t i) const {
+        SkASSERT(get() != pointer());
+        return get()[i];
+    }
+
+    pointer get() const /*noexcept*/ {
+        return data.getPointer();
+    }
+
+    deleter_type& get_deleter() /*noexcept*/ {
+        return data.getDeleter();
+    }
+
+    const deleter_type& get_deleter() const /*noexcept*/ {
+        return data.getDeleter();
+    }
+
+    //explicit operator bool() const noexcept {
+    bool is_attached() const /*noexcept*/ {
+        return get() == pointer() ? false : true;
+    }
+
+    pointer release() /*noexcept*/ {
+        pointer ptr = get();
+        data.getPointer() = pointer();
+        return ptr;
+    }
+
+    void reset(pointer ptr = pointer()) /*noexcept*/ {
+        SkTSwap(data.getPointer(), ptr);
+        if (ptr != pointer()) {
+            get_deleter()(ptr);
+        }
+    }
+
+    template <typename U> void reset(U*) = delete;
+
+    void swap(unique_ptr& that) /*noexcept*/ {
+        data.swap(that.data);
+    }
+
+    unique_ptr(const unique_ptr&) = delete;
+    unique_ptr& operator=(const unique_ptr&) = delete;
+};
+
+template <typename T, typename D>
+inline void swap(unique_ptr<T, D>& a, unique_ptr<T, D>& b) /*noexcept*/ {
+    a.swap(b);
+}
+
+template <typename T, typename D, typename U, typename ThatD>
+inline bool operator==(const unique_ptr<T, D>& a, const unique_ptr<U, ThatD>& b) {
+    return a.get() == b.get();
+}
+
+template <typename T, typename D>
+inline bool operator==(const unique_ptr<T, D>& a, std::nullptr_t) /*noexcept*/ {
+    //return !a;
+    return !a.is_attached();
+}
+
+template <typename T, typename D>
+inline bool operator==(std::nullptr_t, const unique_ptr<T, D>& b) /*noexcept*/ {
+    //return !b;
+    return !b.is_attached();
+}
+
+template <typename T, typename D, typename U, typename ThatD>
+inline bool operator!=(const unique_ptr<T, D>& a, const unique_ptr<U, ThatD>& b) {
+    return a.get() != b.get();
+}
+
+template <typename T, typename D>
+inline bool operator!=(const unique_ptr<T, D>& a, std::nullptr_t) /*noexcept*/ {
+    //return (bool)a;
+    return a.is_attached();
+}
+
+template <typename T, typename D>
+inline bool operator!=(std::nullptr_t, const unique_ptr<T, D>& b) /*noexcept*/ {
+    //return (bool)b;
+    return b.is_attached();
+}
+
+}  // namespace skstd
+
+#endif
index 21c9f46..840c2d3 100644 (file)
@@ -14,7 +14,7 @@
 
 #ifdef SK_DEBUG
     #include "SkMutex.h"
-    #include <memory>
+    #include "SkUniquePtr.h"
 #endif  // SK_DEBUG
 
 // There are two shared lock implementations one debug the other is high performance. They implement
@@ -50,9 +50,9 @@ public:
 private:
 #ifdef SK_DEBUG
     class ThreadIDSet;
-    std::unique_ptr<ThreadIDSet> fCurrentShared;
-    std::unique_ptr<ThreadIDSet> fWaitingExclusive;
-    std::unique_ptr<ThreadIDSet> fWaitingShared;
+    skstd::unique_ptr<ThreadIDSet> fCurrentShared;
+    skstd::unique_ptr<ThreadIDSet> fWaitingExclusive;
+    skstd::unique_ptr<ThreadIDSet> fWaitingShared;
     int fSharedQueueSelect{0};
     mutable SkMutex fMu;
     SkSemaphore fSharedQueue[2];
diff --git a/tests/CPlusPlusEleven.cpp b/tests/CPlusPlusEleven.cpp
new file mode 100644 (file)
index 0000000..0cd2e93
--- /dev/null
@@ -0,0 +1,99 @@
+/*
+ * 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 "Test.h"
+#include "SkTemplates.h"
+#include <utility>
+
+namespace {
+class Moveable {
+public:
+    Moveable() {}
+    Moveable(Moveable&&) {}
+    Moveable& operator=(Moveable&&) { return *this; }
+private:
+    Moveable(const Moveable&);
+    Moveable& operator=(const Moveable&);
+};
+template <typename T> void deleter(T*) { }
+template <typename T> struct Deleter {
+    void operator()(T* t) { delete static_cast<const Moveable*>(t); }
+};
+} // namespace
+
+DEF_TEST(CPlusPlusEleven_RvalueAndMove, r) {
+    Moveable src1; Moveable dst1(std::move(src1));
+    Moveable src2, dst2; dst2 = std::move(src2);
+}
+
+#define TOO_BIG "The unique_ptr was bigger than expected."
+#define WEIRD_SIZE "The unique_ptr was a different size than expected."
+
+DEF_TEST(CPlusPlusEleven_UniquePtr, r) {
+    struct SmallUniquePtr {
+        Moveable* p;
+    };
+    struct BigUniquePtr {
+        void(*d)(Moveable*);
+        Moveable* p;
+    };
+
+    static_assert(sizeof(skstd::unique_ptr<Moveable>) == sizeof(SmallUniquePtr), TOO_BIG);
+    static_assert(sizeof(skstd::unique_ptr<Moveable[]>) == sizeof(SmallUniquePtr), TOO_BIG);
+
+    using proc = void(*)(Moveable*);
+    static_assert(sizeof(skstd::unique_ptr<Moveable, proc>) == sizeof(BigUniquePtr), WEIRD_SIZE);
+    static_assert(sizeof(skstd::unique_ptr<Moveable[], proc>) == sizeof(BigUniquePtr), WEIRD_SIZE);
+
+    {
+        skstd::unique_ptr<Moveable, void(*)(Moveable*)> u(nullptr, deleter<Moveable>);
+        static_assert(sizeof(u) == sizeof(BigUniquePtr), WEIRD_SIZE);
+
+        auto u2 = std::move(u);
+        static_assert(sizeof(u2) == sizeof(BigUniquePtr), WEIRD_SIZE);
+    }
+
+    {
+        skstd::unique_ptr<Moveable, void(*)(Moveable*)> u(nullptr, [](Moveable* m){ deleter(m); });
+        static_assert(sizeof(u) == sizeof(BigUniquePtr), WEIRD_SIZE);
+
+        auto u2 = std::move(u);
+        static_assert(sizeof(u2) == sizeof(BigUniquePtr), WEIRD_SIZE);
+    }
+
+    {
+        auto d = [](Moveable* m){ deleter(m); };
+        skstd::unique_ptr<Moveable, decltype(d)> u(nullptr, d);
+        static_assert(sizeof(u) == sizeof(SmallUniquePtr), TOO_BIG);
+
+        auto u2 = std::move(u);
+        static_assert(sizeof(u2) == sizeof(SmallUniquePtr), TOO_BIG);
+    }
+
+    {
+        skstd::unique_ptr<Moveable, Deleter<Moveable>> u(nullptr, Deleter<Moveable>());
+        static_assert(sizeof(u) == sizeof(SmallUniquePtr), TOO_BIG);
+
+        auto u2 = std::move(u);
+        static_assert(sizeof(u2) == sizeof(SmallUniquePtr), TOO_BIG);
+    }
+
+    {
+        skstd::unique_ptr<Moveable, Deleter<Moveable>> u(new Moveable(), Deleter<Moveable>());
+        static_assert(sizeof(u) == sizeof(SmallUniquePtr), TOO_BIG);
+
+        auto u2 = std::move(u);
+        static_assert(sizeof(u2) == sizeof(SmallUniquePtr), TOO_BIG);
+    }
+
+    {
+        skstd::unique_ptr<const void, Deleter<const void>> u(new Moveable(), Deleter<const void>());
+        static_assert(sizeof(u) == sizeof(SmallUniquePtr), TOO_BIG);
+
+        auto u2 = std::move(u);
+        static_assert(sizeof(u2) == sizeof(SmallUniquePtr), TOO_BIG);
+    }
+}