Add skstd::unique_ptr and use it.

TBR=bsalomon@google.com
The one gpu include change is just to fix swap in implementation.

Review URL: https://codereview.chromium.org/1330503006
diff --git a/include/gpu/gl/GrGLExtensions.h b/include/gpu/gl/GrGLExtensions.h
index 6a8557b..96c5ed1 100644
--- a/include/gpu/gl/GrGLExtensions.h
+++ b/include/gpu/gl/GrGLExtensions.h
@@ -28,7 +28,7 @@
     GrGLExtensions& operator=(const GrGLExtensions&);
 
     void swap(GrGLExtensions* that) {
-        fStrings.swap(&that->fStrings);
+        fStrings.swap(that->fStrings);
         SkTSwap(fInitialized, that->fInitialized);
     }
 
diff --git a/include/private/SkFunction.h b/include/private/SkFunction.h
index 1b99030..6be9539 100644
--- a/include/private/SkFunction.h
+++ b/include/private/SkFunction.h
@@ -10,7 +10,8 @@
 
 // TODO: document, more pervasive move support in constructors, small-Fn optimization
 
-#include "SkTemplates.h"
+#include "SkUtility.h"
+#include "SkUniquePtr.h"
 #include "SkTypes.h"
 
 template <typename> class SkFunction;
@@ -29,7 +30,7 @@
     SkFunction(const SkFunction& other) { *this = other; }
     SkFunction& operator=(const SkFunction& other) {
         if (this != &other) {
-            fFunction.reset(other.fFunction ? other.fFunction->clone() : nullptr);
+            fFunction.reset(other.fFunction.get() ? other.fFunction->clone() : nullptr);
         }
         return *this;
     }
@@ -69,7 +70,7 @@
         R (*fFn)(Args...);
     };
 
-    SkAutoTDelete<Interface> fFunction;
+    skstd::unique_ptr<Interface> fFunction;
 };
 
 #endif//SkFunction_DEFINED
diff --git a/include/private/SkTLogic.h b/include/private/SkTLogic.h
index 0a71d70..e7fc3dd 100644
--- a/include/private/SkTLogic.h
+++ b/include/private/SkTLogic.h
@@ -13,10 +13,15 @@
 #ifndef SkTLogic_DEFINED
 #define SkTLogic_DEFINED
 
+#include "SkTypes.h"
+
+#include <stddef.h>
 #include <stdint.h>
 
 namespace skstd {
 
+using nullptr_t = decltype(nullptr);
+
 template <typename T, T v> struct integral_constant {
     static const/*expr*/ T value = v;
     using value_type = T;
@@ -54,6 +59,11 @@
 template <typename T> struct remove_reference<T&&> { using type = T; };
 template <typename T> using remove_reference_t = typename remove_reference<T>::type;
 
+template <typename T> struct remove_extent { using type = T; };
+template <typename T> struct remove_extent<T[]> { using type = T; };
+template <typename T, size_t N> struct remove_extent<T[N]> { using type = T;};
+template <typename T> using remove_extent_t = typename remove_extent<T>::type;
+
 template <typename T, typename U> struct is_same : false_type {};
 template <typename T> struct is_same<T, T> : true_type {};
 
@@ -65,6 +75,10 @@
 template <typename T> struct is_volatile : false_type {};
 template <typename T> struct is_volatile<volatile T> : true_type {};
 
+template <typename T> struct is_pointer_detector : false_type {};
+template <typename T> struct is_pointer_detector<T*> : true_type {};
+template <typename T> struct is_pointer : is_pointer_detector<remove_cv_t<T>> {};
+
 template <typename T> struct is_reference : false_type {};
 template <typename T> struct is_reference<T&> : true_type {};
 template <typename T> struct is_reference<T&&> : true_type {};
@@ -72,12 +86,51 @@
 template <typename T> struct is_lvalue_reference : false_type {};
 template <typename T> struct is_lvalue_reference<T&> : true_type {};
 
-template <typename T> struct is_empty_detector {
-    struct Derived : public remove_cv_t<T> { char unused; };
-    static const bool value = sizeof(Derived) == sizeof(char);
+template <typename T> struct is_rvalue_reference : false_type {};
+template <typename T> struct is_rvalue_reference<T&&> : true_type {};
+
+template <typename T> struct is_class_detector {
+    using yes_type = uint8_t;
+    using no_type = uint16_t;
+    template <typename U> static yes_type clazz(int U::*);
+    template <typename U> static no_type clazz(...);
+    static const/*expr*/ bool value = sizeof(clazz<T>(0)) == sizeof(yes_type) /*&& !is_union<T>::value*/;
+};
+template <typename T> struct is_class : bool_constant<is_class_detector<T>::value> {};
+
+template <typename T, bool = is_class<T>::value> struct is_empty_detector {
+    struct Derived : public T { char unused; };
+    static const/*expr*/ bool value = sizeof(Derived) == sizeof(char);
+};
+template <typename T> struct is_empty_detector<T, false> {
+    static const/*expr*/ bool value = false;
 };
 template <typename T> struct is_empty : bool_constant<is_empty_detector<T>::value> {};
 
+template <typename T> struct is_array : false_type {};
+template <typename T> struct is_array<T[]> : true_type {};
+template <typename T, size_t N> struct is_array<T[N]> : true_type {};
+
+// template<typename R, typename... Args> struct is_function<
+//     R [calling-convention] (Args...[, ...]) [const] [volatile] [&|&&]> : true_type {};
+// The cv and ref-qualified versions are strange types we're currently avoiding, so not supported.
+// On all platforms, variadic functions only exist in the c calling convention.
+template <typename> struct is_function : false_type { };
+#if !defined(SK_BUILD_FOR_WIN)
+template <typename R, typename... Args> struct is_function<R(Args...)> : true_type {};
+#else
+#if defined(_M_IX86)
+template <typename R, typename... Args> struct is_function<R __cdecl (Args...)> : true_type {};
+template <typename R, typename... Args> struct is_function<R __stdcall (Args...)> : true_type {};
+template <typename R, typename... Args> struct is_function<R __fastcall (Args...)> : true_type {};
+template <typename R, typename... Args> struct is_function<R __vectorcall (Args...)> : true_type {};
+#else
+template <typename R, typename... Args> struct is_function<R __cdecl (Args...)> : true_type {};
+template <typename R, typename... Args> struct is_function<R __vectorcall (Args...)> : true_type {};
+#endif
+#endif
+template <typename R, typename... Args> struct is_function<R(Args..., ...)> : true_type {};
+
 template <typename T> struct add_const { using type = const T; };
 template <typename T> using add_const_t = typename add_const<T>::type;
 
@@ -87,11 +140,50 @@
 template <typename T> struct add_cv { using type = add_volatile_t<add_const_t<T>>; };
 template <typename T> using add_cv_t = typename add_cv<T>::type;
 
-template <typename T> struct add_rvalue_reference {
-    using type = conditional_t<is_void<T>::value || is_reference<T>::value, T, T&&>;
-};
+template <typename T> struct add_pointer { using type = remove_reference_t<T>*; };
+template <typename T> using add_pointer_t = typename add_pointer<T>::type;
+
+template <typename T, bool=is_void<T>::value> struct add_lvalue_reference_init { using type = T; };
+template <typename T> struct add_lvalue_reference_init<T, false> { using type = T&; };
+template <typename T> struct add_lvalue_reference : add_lvalue_reference_init<T> { };
+template <typename T> using add_lvalue_reference_t = typename add_lvalue_reference<T>::type;
+
+template <typename T, bool=is_void<T>::value> struct add_rvalue_reference_init { using type = T; };
+template <typename T> struct add_rvalue_reference_init<T, false> { using type = T&&; };
+template <typename T> struct add_rvalue_reference : add_rvalue_reference_init<T> {};
 template <typename T> using add_rvalue_reference_t = typename add_rvalue_reference<T>::type;
 
+/* This is 'just' a forward declaration. */
+template <typename T> add_rvalue_reference_t<T> declval() /*noexcept*/;
+
+template <typename S, typename D, bool=is_void<S>::value||is_function<D>::value||is_array<D>::value>
+struct is_convertible_detector {
+    static const/*expr*/ bool value = is_void<D>::value;
+};
+template <typename S, typename D> struct is_convertible_detector<S, D, false> {
+    using yes_type = uint8_t;
+    using no_type = uint16_t;
+
+    template <typename To> static void param_convertable_to(To);
+
+    template <typename From, typename To>
+    static decltype(param_convertable_to<To>(declval<From>()), yes_type()) convertible(int);
+
+    template <typename, typename> static no_type convertible(...);
+
+    static const/*expr*/ bool value = sizeof(convertible<S, D>(0)) == sizeof(yes_type);
+};
+template<typename S, typename D> struct is_convertible
+    : bool_constant<is_convertible_detector<S, D>::value> { };
+
+template <typename T> struct decay {
+    using U = remove_reference_t<T>;
+    using type = conditional_t<is_array<U>::value,
+        remove_extent_t<U>*,
+        conditional_t<is_function<U>::value, add_pointer_t<U>, remove_cv_t<U>>>;
+};
+template <typename T> using decay_t = typename decay<T>::type;
+
 }  // namespace skstd
 
 // The sknonstd namespace contains things we would like to be proposed and feel std-ish.
diff --git a/include/private/SkTemplates.h b/include/private/SkTemplates.h
index 023bba3..533cb26 100644
--- a/include/private/SkTemplates.h
+++ b/include/private/SkTemplates.h
@@ -10,9 +10,11 @@
 #ifndef SkTemplates_DEFINED
 #define SkTemplates_DEFINED
 
-#include "../private/SkTLogic.h"
 #include "SkMath.h"
+#include "SkTLogic.h"
 #include "SkTypes.h"
+#include "SkUniquePtr.h"
+#include "SkUtility.h"
 #include <limits.h>
 #include <new>
 
@@ -28,25 +30,6 @@
  */
 template<typename T> inline void sk_ignore_unused_variable(const T&) { }
 
-namespace skstd {
-
-template <typename T> inline remove_reference_t<T>&& move(T&& t) {
-  return static_cast<remove_reference_t<T>&&>(t);
-}
-
-template <typename T> inline T&& forward(remove_reference_t<T>& t) /*noexcept*/ {
-    return static_cast<T&&>(t);
-}
-template <typename T> inline T&& forward(remove_reference_t<T>&& t) /*noexcept*/ {
-    static_assert(!is_lvalue_reference<T>::value,
-                  "Forwarding an rvalue reference as an lvalue reference is not allowed.");
-    return static_cast<T&&>(t);
-}
-
-template <typename T> add_rvalue_reference_t<T> declval();
-
-}  // namespace skstd
-
 /**
  *  Returns a pointer to a D which comes immediately after S[count].
  */
@@ -63,6 +46,10 @@
     return reinterpret_cast<D*>(reinterpret_cast<sknonstd::same_cv_t<char, D>*>(ptr) + byteOffset);
 }
 
+template <typename R, typename T, R (*P)(T*)> struct SkFunctionWrapper {
+    R operator()(T* t) { return P(t); }
+};
+
 /** \class SkAutoTCallVProc
 
     Call a function when this goes out of scope. The template uses two
@@ -71,25 +58,13 @@
     reference is null when the destructor is called, we do not call the
     function.
 */
-template <typename T, void (*P)(T*)> class SkAutoTCallVProc : SkNoncopyable {
+template <typename T, void (*P)(T*)> class SkAutoTCallVProc
+    : public skstd::unique_ptr<T, SkFunctionWrapper<void, T, P>> {
 public:
-    SkAutoTCallVProc(T* obj): fObj(obj) {}
-    ~SkAutoTCallVProc() { if (fObj) P(fObj); }
+    SkAutoTCallVProc(T* obj): skstd::unique_ptr<T, SkFunctionWrapper<void, T, P>>(obj) {}
 
-    operator T*() const { return fObj; }
-    T* operator->() const { SkASSERT(fObj); return fObj; }
-
-    T* detach() { T* obj = fObj; fObj = NULL; return obj; }
-    void reset(T* obj = NULL) {
-        if (fObj != obj) {
-            if (fObj) {
-                P(fObj);
-            }
-            fObj = obj;
-        }
-    }
-private:
-    T* fObj;
+    operator T*() const { return this->get(); }
+    T* detach() { return this->release(); }
 };
 
 /** \class SkAutoTCallIProc
@@ -100,17 +75,13 @@
 reference is null when the destructor is called, we do not call the
 function.
 */
-template <typename T, int (*P)(T*)> class SkAutoTCallIProc : SkNoncopyable {
+template <typename T, int (*P)(T*)> class SkAutoTCallIProc
+    : public skstd::unique_ptr<T, SkFunctionWrapper<int, T, P>> {
 public:
-    SkAutoTCallIProc(T* obj): fObj(obj) {}
-    ~SkAutoTCallIProc() { if (fObj) P(fObj); }
+    SkAutoTCallIProc(T* obj): skstd::unique_ptr<T, SkFunctionWrapper<int, T, P>>(obj) {}
 
-    operator T*() const { return fObj; }
-    T* operator->() const { SkASSERT(fObj); return fObj; }
-
-    T* detach() { T* obj = fObj; fObj = NULL; return obj; }
-private:
-    T* fObj;
+    operator T*() const { return this->get(); }
+    T* detach() { return this->release(); }
 };
 
 /** \class SkAutoTDelete
@@ -123,89 +94,21 @@
 
   The size of a SkAutoTDelete is small: sizeof(SkAutoTDelete<T>) == sizeof(T*)
 */
-template <typename T> class SkAutoTDelete : SkNoncopyable {
+template <typename T> class SkAutoTDelete : public skstd::unique_ptr<T> {
 public:
-    SkAutoTDelete(T* obj = NULL) : fObj(obj) {}
-    ~SkAutoTDelete() { delete fObj; }
+    SkAutoTDelete(T* obj = NULL) : skstd::unique_ptr<T>(obj) {}
 
-    T* get() const { return fObj; }
-    operator T*() const { return fObj; }
-    T& operator*() const { SkASSERT(fObj); return *fObj; }
-    T* operator->() const { SkASSERT(fObj); return fObj; }
-
-    void reset(T* obj) {
-        if (fObj != obj) {
-            delete fObj;
-            fObj = obj;
-        }
-    }
-
-    /**
-     *  Delete the owned object, setting the internal pointer to NULL.
-     */
-    void free() {
-        delete fObj;
-        fObj = NULL;
-    }
-
-    /**
-     *  Transfer ownership of the object to the caller, setting the internal
-     *  pointer to NULL. Note that this differs from get(), which also returns
-     *  the pointer, but it does not transfer ownership.
-     */
-    T* detach() {
-        T* obj = fObj;
-        fObj = NULL;
-        return obj;
-    }
-
-    void swap(SkAutoTDelete* that) {
-        SkTSwap(fObj, that->fObj);
-    }
-
-private:
-    T*  fObj;
+    operator T*() const { return this->get(); }
+    void free() { this->reset(nullptr); }
+    T* detach() { return this->release(); }
 };
 
-// Calls ~T() in the destructor.
-template <typename T> class SkAutoTDestroy : SkNoncopyable {
+template <typename T> class SkAutoTDeleteArray : public skstd::unique_ptr<T[]> {
 public:
-    SkAutoTDestroy(T* obj = NULL) : fObj(obj) {}
-    ~SkAutoTDestroy() {
-        if (fObj) {
-            fObj->~T();
-        }
-    }
+    SkAutoTDeleteArray(T array[]) : skstd::unique_ptr<T[]>(array) {}
 
-    T* get() const { return fObj; }
-    T& operator*() const { SkASSERT(fObj); return *fObj; }
-    T* operator->() const { SkASSERT(fObj); return fObj; }
-
-private:
-    T*  fObj;
-};
-
-template <typename T> class SkAutoTDeleteArray : SkNoncopyable {
-public:
-    SkAutoTDeleteArray(T array[]) : fArray(array) {}
-    ~SkAutoTDeleteArray() { delete[] fArray; }
-
-    T*      get() const { return fArray; }
-    void free() {
-        delete[] fArray;
-        fArray = NULL;
-    }
-    T*      detach() { T* array = fArray; fArray = NULL; return array; }
-
-    void reset(T array[]) {
-        if (fArray != array) {
-            delete[] fArray;
-            fArray = array;
-        }
-    }
-
-private:
-    T*  fArray;
+    void free() { this->reset(nullptr); }
+    T* detach() { return this->release(); }
 };
 
 /** Allocate an array of T elements, and free the array in the destructor
diff --git a/include/private/SkUniquePtr.h b/include/private/SkUniquePtr.h
new file mode 100644
index 0000000..5d6e722
--- /dev/null
+++ b/include/private/SkUniquePtr.h
@@ -0,0 +1,396 @@
+/*
+ * 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 "SkUtility.h"
+
+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 = 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(const B&& b) : B(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(const B&& b) : fb(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>(skstd::forward<U2>(d)), fPtr(skstd::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(!is_pointer<deleter_type>::value, "Deleter is nullptr function pointer!");
+    }
+
+    /*constexpr*/ unique_ptr(skstd::nullptr_t) /*noexcept*/ : unique_ptr() { }
+
+    explicit unique_ptr(pointer ptr) /*noexcept*/ : data(ptr, deleter_type()) {
+        static_assert(!is_pointer<deleter_type>::value, "Deleter is nullptr function pointer!");
+    }
+
+    unique_ptr(pointer ptr,
+               conditional_t<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(move(ptr), move(d))
+    {
+        static_assert(!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(), 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 &&
+        !is_array<U>::value &&
+        conditional_t<is_reference<D>::value, is_same<ThatD, D>, is_convertible<ThatD, D>>::value>>
+    unique_ptr(unique_ptr<U, ThatD>&& that) /*noexcept*/
+        : data(that.release(), 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() = 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 &&
+        !is_array<U>::value,
+    unique_ptr&> operator=(unique_ptr<U, ThatD>&& that) /*noexcept*/ {
+        reset(that.release());
+        get_deleter() = forward<ThatD>(that.get_deleter());
+        return *this;
+    }
+
+    unique_ptr& operator=(skstd::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 = 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(const B&& b) : B(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(const B&& b) : fb(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>(skstd::forward<U2>(d)), fPtr(skstd::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(!is_pointer<deleter_type>::value, "Deleter is nullptr function pointer!");
+    }
+
+    /*constexpr*/ unique_ptr(skstd::nullptr_t) /*noexcept*/ : unique_ptr() { }
+
+    explicit unique_ptr(pointer ptr) /*noexcept*/ : data(ptr, deleter_type()) {
+        static_assert(!is_pointer<deleter_type>::value, "Deleter is nullptr function pointer!");
+    }
+
+    unique_ptr(pointer ptr,
+               conditional_t<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(move(ptr), move(d))
+    {
+        static_assert(!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(), 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() = forward<deleter_type>(that.get_deleter());
+        return *this;
+    }
+
+    unique_ptr& operator=(skstd::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, skstd::nullptr_t) /*noexcept*/ {
+    //return !a;
+    return !a.is_attached();
+}
+
+template <typename T, typename D>
+inline bool operator==(skstd::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, skstd::nullptr_t) /*noexcept*/ {
+    //return (bool)a;
+    return a.is_attached();
+}
+
+template <typename T, typename D>
+inline bool operator!=(skstd::nullptr_t, const unique_ptr<T, D>& b) /*noexcept*/ {
+    //return (bool)b;
+    return b.is_attached();
+}
+
+}  // namespace skstd
+
+#endif
diff --git a/include/private/SkUtility.h b/include/private/SkUtility.h
new file mode 100644
index 0000000..a96e8fe
--- /dev/null
+++ b/include/private/SkUtility.h
@@ -0,0 +1,32 @@
+/*
+ * 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 SkUtility_DEFINED
+#define SkUtility_DEFINED
+
+#include "SkTLogic.h"
+
+namespace skstd {
+
+template <typename T> inline remove_reference_t<T>&& move(T&& t) {
+  return static_cast<remove_reference_t<T>&&>(t);
+}
+
+template <typename T> inline T&& forward(remove_reference_t<T>& t) /*noexcept*/ {
+    return static_cast<T&&>(t);
+}
+template <typename T> inline T&& forward(remove_reference_t<T>&& t) /*noexcept*/ {
+    static_assert(!is_lvalue_reference<T>::value,
+                  "Forwarding an rvalue reference as an lvalue reference is not allowed.");
+    return static_cast<T&&>(t);
+}
+
+template <typename T> add_rvalue_reference_t<T> declval();
+
+}  // namespace skstd
+
+#endif
diff --git a/tests/CPlusPlusEleven.cpp b/tests/CPlusPlusEleven.cpp
index 4182112..4f74b80 100644
--- a/tests/CPlusPlusEleven.cpp
+++ b/tests/CPlusPlusEleven.cpp
@@ -5,10 +5,10 @@
  * found in the LICENSE file.
  */
 #include "Test.h"
+#include "SkTemplates.h"
+#include "SkFunction.h"
 
 namespace {
-template <class T> T&& Move(T& o) { return static_cast<T&&>(o); }
-
 class Moveable {
 public:
     Moveable() {}
@@ -18,9 +18,82 @@
     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(Move(src1));
-    Moveable src2, dst2; dst2 = Move(src2);
+    Moveable src1; Moveable dst1(skstd::move(src1));
+    Moveable src2, dst2; dst2 = skstd::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 = skstd::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 = skstd::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 = skstd::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 = skstd::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 = skstd::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 = skstd::move(u);
+        static_assert(sizeof(u2) == sizeof(SmallUniquePtr), TOO_BIG);
+    }
 }