From a3434d83cf988f930a73aaca5ba88f6f6fa8f547 Mon Sep 17 00:00:00 2001 From: bungeman Date: Mon, 7 Sep 2015 12:45:52 -0700 Subject: [PATCH] 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 --- include/gpu/gl/GrGLExtensions.h | 2 +- include/private/SkFunction.h | 7 +- include/private/SkTLogic.h | 104 ++++++++++- include/private/SkTemplates.h | 149 +++------------ include/private/SkUniquePtr.h | 396 ++++++++++++++++++++++++++++++++++++++++ include/private/SkUtility.h | 32 ++++ tests/CPlusPlusEleven.cpp | 81 +++++++- 7 files changed, 634 insertions(+), 137 deletions(-) create mode 100644 include/private/SkUniquePtr.h create mode 100644 include/private/SkUtility.h 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 @@ public: 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 class SkFunction; @@ -29,7 +30,7 @@ public: 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 @@ private: R (*fFn)(Args...); }; - SkAutoTDelete fFunction; + skstd::unique_ptr 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 #include namespace skstd { +using nullptr_t = decltype(nullptr); + template struct integral_constant { static const/*expr*/ T value = v; using value_type = T; @@ -54,6 +59,11 @@ template struct remove_reference { using type = T; }; template struct remove_reference { using type = T; }; template using remove_reference_t = typename remove_reference::type; +template struct remove_extent { using type = T; }; +template struct remove_extent { using type = T; }; +template struct remove_extent { using type = T;}; +template using remove_extent_t = typename remove_extent::type; + template struct is_same : false_type {}; template struct is_same : true_type {}; @@ -65,6 +75,10 @@ template struct is_const : true_type {}; template struct is_volatile : false_type {}; template struct is_volatile : true_type {}; +template struct is_pointer_detector : false_type {}; +template struct is_pointer_detector : true_type {}; +template struct is_pointer : is_pointer_detector> {}; + template struct is_reference : false_type {}; template struct is_reference : true_type {}; template struct is_reference : true_type {}; @@ -72,12 +86,51 @@ template struct is_reference : true_type {}; template struct is_lvalue_reference : false_type {}; template struct is_lvalue_reference : true_type {}; -template struct is_empty_detector { - struct Derived : public remove_cv_t { char unused; }; - static const bool value = sizeof(Derived) == sizeof(char); +template struct is_rvalue_reference : false_type {}; +template struct is_rvalue_reference : true_type {}; + +template struct is_class_detector { + using yes_type = uint8_t; + using no_type = uint16_t; + template static yes_type clazz(int U::*); + template static no_type clazz(...); + static const/*expr*/ bool value = sizeof(clazz(0)) == sizeof(yes_type) /*&& !is_union::value*/; +}; +template struct is_class : bool_constant::value> {}; + +template ::value> struct is_empty_detector { + struct Derived : public T { char unused; }; + static const/*expr*/ bool value = sizeof(Derived) == sizeof(char); +}; +template struct is_empty_detector { + static const/*expr*/ bool value = false; }; template struct is_empty : bool_constant::value> {}; +template struct is_array : false_type {}; +template struct is_array : true_type {}; +template struct is_array : true_type {}; + +// template 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 struct is_function : false_type { }; +#if !defined(SK_BUILD_FOR_WIN) +template struct is_function : true_type {}; +#else +#if defined(_M_IX86) +template struct is_function : true_type {}; +template struct is_function : true_type {}; +template struct is_function : true_type {}; +template struct is_function : true_type {}; +#else +template struct is_function : true_type {}; +template struct is_function : true_type {}; +#endif +#endif +template struct is_function : true_type {}; + template struct add_const { using type = const T; }; template using add_const_t = typename add_const::type; @@ -87,11 +140,50 @@ template using add_volatile_t = typename add_volatile::type; template struct add_cv { using type = add_volatile_t>; }; template using add_cv_t = typename add_cv::type; -template struct add_rvalue_reference { - using type = conditional_t::value || is_reference::value, T, T&&>; -}; +template struct add_pointer { using type = remove_reference_t*; }; +template using add_pointer_t = typename add_pointer::type; + +template ::value> struct add_lvalue_reference_init { using type = T; }; +template struct add_lvalue_reference_init { using type = T&; }; +template struct add_lvalue_reference : add_lvalue_reference_init { }; +template using add_lvalue_reference_t = typename add_lvalue_reference::type; + +template ::value> struct add_rvalue_reference_init { using type = T; }; +template struct add_rvalue_reference_init { using type = T&&; }; +template struct add_rvalue_reference : add_rvalue_reference_init {}; template using add_rvalue_reference_t = typename add_rvalue_reference::type; +/* This is 'just' a forward declaration. */ +template add_rvalue_reference_t declval() /*noexcept*/; + +template ::value||is_function::value||is_array::value> +struct is_convertible_detector { + static const/*expr*/ bool value = is_void::value; +}; +template struct is_convertible_detector { + using yes_type = uint8_t; + using no_type = uint16_t; + + template static void param_convertable_to(To); + + template + static decltype(param_convertable_to(declval()), yes_type()) convertible(int); + + template static no_type convertible(...); + + static const/*expr*/ bool value = sizeof(convertible(0)) == sizeof(yes_type); +}; +template struct is_convertible + : bool_constant::value> { }; + +template struct decay { + using U = remove_reference_t; + using type = conditional_t::value, + remove_extent_t*, + conditional_t::value, add_pointer_t, remove_cv_t>>; +}; +template using decay_t = typename decay::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 #include @@ -28,25 +30,6 @@ */ template inline void sk_ignore_unused_variable(const T&) { } -namespace skstd { - -template inline remove_reference_t&& move(T&& t) { - return static_cast&&>(t); -} - -template inline T&& forward(remove_reference_t& t) /*noexcept*/ { - return static_cast(t); -} -template inline T&& forward(remove_reference_t&& t) /*noexcept*/ { - static_assert(!is_lvalue_reference::value, - "Forwarding an rvalue reference as an lvalue reference is not allowed."); - return static_cast(t); -} - -template add_rvalue_reference_t declval(); - -} // namespace skstd - /** * Returns a pointer to a D which comes immediately after S[count]. */ @@ -63,6 +46,10 @@ template static D* SkTAddOffset(S* ptr, size_t byteOffs return reinterpret_cast(reinterpret_cast*>(ptr) + byteOffset); } +template 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 @@ template static D* SkTAddOffset(S* ptr, size_t byteOffs reference is null when the destructor is called, we do not call the function. */ -template class SkAutoTCallVProc : SkNoncopyable { +template class SkAutoTCallVProc + : public skstd::unique_ptr> { public: - SkAutoTCallVProc(T* obj): fObj(obj) {} - ~SkAutoTCallVProc() { if (fObj) P(fObj); } + SkAutoTCallVProc(T* obj): skstd::unique_ptr>(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 @@ 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 class SkAutoTCallIProc : SkNoncopyable { +template class SkAutoTCallIProc + : public skstd::unique_ptr> { public: - SkAutoTCallIProc(T* obj): fObj(obj) {} - ~SkAutoTCallIProc() { if (fObj) P(fObj); } - - operator T*() const { return fObj; } - T* operator->() const { SkASSERT(fObj); return fObj; } + SkAutoTCallIProc(T* obj): skstd::unique_ptr>(obj) {} - 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 @@ private: The size of a SkAutoTDelete is small: sizeof(SkAutoTDelete) == sizeof(T*) */ -template class SkAutoTDelete : SkNoncopyable { -public: - SkAutoTDelete(T* obj = NULL) : fObj(obj) {} - ~SkAutoTDelete() { delete fObj; } - - 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; -}; - -// Calls ~T() in the destructor. -template class SkAutoTDestroy : SkNoncopyable { +template class SkAutoTDelete : public skstd::unique_ptr { public: - SkAutoTDestroy(T* obj = NULL) : fObj(obj) {} - ~SkAutoTDestroy() { - if (fObj) { - fObj->~T(); - } - } - - T* get() const { return fObj; } - T& operator*() const { SkASSERT(fObj); return *fObj; } - T* operator->() const { SkASSERT(fObj); return fObj; } + SkAutoTDelete(T* obj = NULL) : skstd::unique_ptr(obj) {} -private: - T* fObj; + operator T*() const { return this->get(); } + void free() { this->reset(nullptr); } + T* detach() { return this->release(); } }; -template class SkAutoTDeleteArray : SkNoncopyable { +template class SkAutoTDeleteArray : public skstd::unique_ptr { 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; - } - } + SkAutoTDeleteArray(T array[]) : skstd::unique_ptr(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 struct default_delete { + /*constexpr*/ default_delete() /*noexcept*/ = default; + + template ::value>> + default_delete(const default_delete&) /*noexcept*/ {} + + void operator()(T* obj) const { + static_assert(sizeof(T) > 0, "Deleting pointer to incomplete type!"); + delete obj; + } +}; +template struct default_delete { + /*constexpr*/ default_delete() /*noexcept*/ = default; + + void operator()(T* obj) const { + static_assert(sizeof(T) > 0, "Deleting pointer to incomplete type!"); + delete [] obj; + } +}; + +template > class unique_ptr { + // remove_reference_t::pointer if that type exists, otherwise T*. + struct pointer_type_detector { + template static typename U::pointer detector(typename U::pointer*); + template static T* detector(...); + using type = decltype(detector>(0)); + }; + +public: + using pointer = typename pointer_type_detector::type; + using element_type = T; + using deleter_type = D; + +private: + template ::value /*&& !is_final::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 struct compressed_base { + 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 { + pointer fPtr; + /*constexpr*/ compressed_data() : compressed_base(), fPtr() {} + /*constexpr*/ compressed_data(const pointer& ptr, const deleter_type& d) + : compressed_base(d), fPtr(ptr) {} + template ::value && is_convertible::value + >> /*constexpr*/ compressed_data(U1&& ptr, U2&& d) + : compressed_base(skstd::forward(d)), fPtr(skstd::forward(ptr)) {} + /*constexpr*/ pointer& getPointer() /*noexcept*/ { return fPtr; } + /*constexpr*/ pointer const& getPointer() const /*noexcept*/ { return fPtr; } + /*constexpr*/ deleter_type& getDeleter() /*noexcept*/ { + return compressed_base::get(); + } + /*constexpr*/ deleter_type const& getDeleter() const /*noexcept*/ { + return compressed_base::get(); + } + void swap(compressed_data& that) /*noexcept*/ { + compressed_base::swap(static_cast>(that)); + SkTSwap(fPtr, that.fPtr); + } + }; + compressed_data data; + +public: + /*constexpr*/ unique_ptr() /*noexcept*/ : data() { + static_assert(!is_pointer::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::value, "Deleter is nullptr function pointer!"); + } + + unique_ptr(pointer ptr, + conditional_t::value, deleter_type,const deleter_type&> d) + /*noexcept*/ : data(ptr, d) + {} + + unique_ptr(pointer ptr, remove_reference_t&& d) /*noexcept*/ + : data(move(ptr), move(d)) + { + static_assert(!is_reference::value, + "Binding an rvalue reference deleter as an lvalue reference deleter is not allowed."); + } + + + unique_ptr(unique_ptr&& that) /*noexcept*/ + : data(that.release(), forward(that.get_deleter())) + {} + + template ::pointer, pointer>::value && + !is_array::value && + conditional_t::value, is_same, is_convertible>::value>> + unique_ptr(unique_ptr&& that) /*noexcept*/ + : data(that.release(), forward(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(that.get_deleter()); + return *this; + } + + template enable_if_t< + is_convertible::pointer, pointer>::value && + !is_array::value, + unique_ptr&> operator=(unique_ptr&& that) /*noexcept*/ { + reset(that.release()); + get_deleter() = forward(that.get_deleter()); + return *this; + } + + unique_ptr& operator=(skstd::nullptr_t) /*noexcept*/ { + reset(); + return *this; + } + + add_lvalue_reference_t 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 class unique_ptr { + // remove_reference_t::pointer if that type exists, otherwise T*. + struct pointer_type_detector { + template static typename U::pointer detector(typename U::pointer*); + template static T* detector(...); + using type = decltype(detector>(0)); + }; + +public: + using pointer = typename pointer_type_detector::type; + using element_type = T; + using deleter_type = D; + +private: + template ::value /*&& !is_final::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 struct compressed_base { + 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 { + pointer fPtr; + /*constexpr*/ compressed_data() : compressed_base(), fPtr() {} + /*constexpr*/ compressed_data(const pointer& ptr, const deleter_type& d) + : compressed_base(d), fPtr(ptr) {} + template ::value && is_convertible::value + >> /*constexpr*/ compressed_data(U1&& ptr, U2&& d) + : compressed_base(skstd::forward(d)), fPtr(skstd::forward(ptr)) {} + /*constexpr*/ pointer& getPointer() /*noexcept*/ { return fPtr; } + /*constexpr*/ pointer const& getPointer() const /*noexcept*/ { return fPtr; } + /*constexpr*/ deleter_type& getDeleter() /*noexcept*/ { + return compressed_base::get(); + } + /*constexpr*/ deleter_type const& getDeleter() const /*noexcept*/ { + return compressed_base::get(); + } + void swap(compressed_data& that) /*noexcept*/ { + compressed_base::swap(static_cast>(that)); + SkTSwap(fPtr, that.fPtr); + } + }; + compressed_data data; + +public: + /*constexpr*/ unique_ptr() /*noexcept*/ : data() { + static_assert(!is_pointer::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::value, "Deleter is nullptr function pointer!"); + } + + unique_ptr(pointer ptr, + conditional_t::value, deleter_type,const deleter_type&> d) + /*noexcept*/ : data(ptr, d) + {} + + unique_ptr(pointer ptr, remove_reference_t&& d) /*noexcept*/ + : data(move(ptr), move(d)) + { + static_assert(!is_reference::value, + "Binding an rvalue reference deleter as an lvalue reference deleter is not allowed."); + } + + unique_ptr(unique_ptr&& that) /*noexcept*/ + : data(that.release(), forward(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(that.get_deleter()); + return *this; + } + + unique_ptr& operator=(skstd::nullptr_t) /*noexcept*/ { + reset(); + return *this; + } + + add_lvalue_reference_t 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 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 +inline void swap(unique_ptr& a, unique_ptr& b) /*noexcept*/ { + a.swap(b); +} + +template +inline bool operator==(const unique_ptr& a, const unique_ptr& b) { + return a.get() == b.get(); +} + +template +inline bool operator==(const unique_ptr& a, skstd::nullptr_t) /*noexcept*/ { + //return !a; + return !a.is_attached(); +} + +template +inline bool operator==(skstd::nullptr_t, const unique_ptr& b) /*noexcept*/ { + //return !b; + return !b.is_attached(); +} + +template +inline bool operator!=(const unique_ptr& a, const unique_ptr& b) { + return a.get() != b.get(); +} + +template +inline bool operator!=(const unique_ptr& a, skstd::nullptr_t) /*noexcept*/ { + //return (bool)a; + return a.is_attached(); +} + +template +inline bool operator!=(skstd::nullptr_t, const unique_ptr& 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 inline remove_reference_t&& move(T&& t) { + return static_cast&&>(t); +} + +template inline T&& forward(remove_reference_t& t) /*noexcept*/ { + return static_cast(t); +} +template inline T&& forward(remove_reference_t&& t) /*noexcept*/ { + static_assert(!is_lvalue_reference::value, + "Forwarding an rvalue reference as an lvalue reference is not allowed."); + return static_cast(t); +} + +template add_rvalue_reference_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 T&& Move(T& o) { return static_cast(o); } - class Moveable { public: Moveable() {} @@ -18,9 +18,82 @@ private: Moveable(const Moveable&); Moveable& operator=(const Moveable&); }; +template void deleter(T*) { } +template struct Deleter { + void operator()(T* t) { delete static_cast(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) == sizeof(SmallUniquePtr), TOO_BIG); + static_assert(sizeof(skstd::unique_ptr) == sizeof(SmallUniquePtr), TOO_BIG); + + using proc = void(*)(Moveable*); + static_assert(sizeof(skstd::unique_ptr) == sizeof(BigUniquePtr), WEIRD_SIZE); + static_assert(sizeof(skstd::unique_ptr) == sizeof(BigUniquePtr), WEIRD_SIZE); + + { + skstd::unique_ptr u(nullptr, deleter); + static_assert(sizeof(u) == sizeof(BigUniquePtr), WEIRD_SIZE); + + auto u2 = skstd::move(u); + static_assert(sizeof(u2) == sizeof(BigUniquePtr), WEIRD_SIZE); + } + + { + skstd::unique_ptr 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 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> u(nullptr, Deleter()); + static_assert(sizeof(u) == sizeof(SmallUniquePtr), TOO_BIG); + + auto u2 = skstd::move(u); + static_assert(sizeof(u2) == sizeof(SmallUniquePtr), TOO_BIG); + } + + { + skstd::unique_ptr> u(new Moveable(), Deleter()); + static_assert(sizeof(u) == sizeof(SmallUniquePtr), TOO_BIG); + + auto u2 = skstd::move(u); + static_assert(sizeof(u2) == sizeof(SmallUniquePtr), TOO_BIG); + } + + { + skstd::unique_ptr> u(new Moveable(), Deleter()); + static_assert(sizeof(u) == sizeof(SmallUniquePtr), TOO_BIG); + + auto u2 = skstd::move(u); + static_assert(sizeof(u2) == sizeof(SmallUniquePtr), TOO_BIG); + } } -- 2.7.4