Index: mojo/public/cpp/bindings/lib/template_util.h |
diff --git a/mojo/public/cpp/bindings/lib/template_util.h b/mojo/public/cpp/bindings/lib/template_util.h |
index cca06de0a10cd17ee79089d0c687097db2020905..34011cf4b90b10068b6ceb6f723c5c9ed0ba77bd 100644 |
--- a/mojo/public/cpp/bindings/lib/template_util.h |
+++ b/mojo/public/cpp/bindings/lib/template_util.h |
@@ -5,6 +5,8 @@ |
#ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_TEMPLATE_UTIL_H_ |
#define MOJO_PUBLIC_CPP_BINDINGS_LIB_TEMPLATE_UTIL_H_ |
+#include <type_traits> |
+ |
namespace mojo { |
namespace internal { |
@@ -118,6 +120,54 @@ struct Conditional<false, T, F> { |
typedef F type; |
}; |
+template <typename T> |
+struct HasCloneMethod { |
+ template <typename U> |
+ static char Test(decltype(&U::Clone)); |
+ template <typename U> |
+ static int Test(...); |
+ static const bool value = sizeof(Test<T>(0)) == sizeof(char); |
+ |
+ private: |
+ EnsureTypeIsComplete<T> check_t_; |
+}; |
+ |
+template <typename T, |
+ typename std::enable_if<HasCloneMethod<T>::value>::type* = nullptr> |
+T Clone(const T& input) { |
+ return input.Clone(); |
+}; |
+ |
+template <typename T, |
+ typename std::enable_if<!HasCloneMethod<T>::value>::type* = nullptr> |
+T Clone(const T& input) { |
+ return input; |
+} |
+ |
+template <typename T> |
+struct HasEqualsMethod { |
+ template <typename U> |
+ static char Test(decltype(&U::Equals)); |
+ template <typename U> |
+ static int Test(...); |
+ static const bool value = sizeof(Test<T>(0)) == sizeof(char); |
+ |
+ private: |
+ EnsureTypeIsComplete<T> check_t_; |
+}; |
+ |
+template <typename T, |
+ typename std::enable_if<HasEqualsMethod<T>::value>::type* = nullptr> |
+bool Equals(const T& a, const T& b) { |
+ return a.Equals(b); |
+}; |
+ |
+template <typename T, |
+ typename std::enable_if<!HasEqualsMethod<T>::value>::type* = nullptr> |
+bool Equals(const T& a, const T& b) { |
+ return a == b; |
+} |
+ |
} // namespace internal |
} // namespace mojo |