Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(520)

Unified Diff: mojo/public/cpp/bindings/lib/template_util.h

Issue 2031823002: Mojo C++ bindings: more consistent Clone() and Equals(). (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « mojo/public/cpp/bindings/lib/map_internal.h ('k') | mojo/public/cpp/bindings/lib/value_traits.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
« no previous file with comments | « mojo/public/cpp/bindings/lib/map_internal.h ('k') | mojo/public/cpp/bindings/lib/value_traits.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698