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

Unified Diff: mojo/public/cpp/bindings/type_converter.h

Issue 507173003: Change TypeConverter<X,Y>::ConvertFrom and ConvertTo into a single symmetric (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: compile for real Created 6 years, 3 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
Index: mojo/public/cpp/bindings/type_converter.h
diff --git a/mojo/public/cpp/bindings/type_converter.h b/mojo/public/cpp/bindings/type_converter.h
index 0ac5f6d47603042826bcfb2e352b5323cc071e90..ff94cdadd3a463606a0fac650f4fbe5fde9b6a1f 100644
--- a/mojo/public/cpp/bindings/type_converter.h
+++ b/mojo/public/cpp/bindings/type_converter.h
@@ -8,18 +8,19 @@
namespace mojo {
// Specialize the following class:
-// template <typename T, typename U> class TypeConverter;
+// template <typename T, typename U> struct TypeConverter;
// to perform type conversion for Mojom-defined structs and arrays. Here, T is
-// the Mojom-defined struct or array, and U is some other non-Mojom
-// struct or array type.
+// the target type; U is the input type.
//
-// Specializations should implement the following interface:
+// Specializations should implement the following interfaces:
// namespace mojo {
// template <>
-// class TypeConverter<T, U> {
-// public:
-// static T ConvertFrom(const U& input);
-// static U ConvertTo(const T& input);
+// struct TypeConverter<X, Y> {
+// static X Convert(const Y& input);
+// };
+// template <>
+// struct TypeConverter<Y, X> {
+// static Y Convert(const X& input);
// };
// }
//
@@ -39,15 +40,17 @@ namespace mojo {
//
// namespace mojo {
// template <>
-// class TypeConverter<geometry::PointPtr, gfx::Point> {
-// public:
-// static geometry::PointPtr ConvertFrom(const gfx::Point& input) {
+// struct TypeConverter<geometry::PointPtr, gfx::Point> {
+// static geometry::PointPtr Convert(const gfx::Point& input) {
// geometry::PointPtr result;
// result->x = input.x();
// result->y = input.y();
// return result.Pass();
// }
-// static gfx::Point ConvertTo(const geometry::PointPtr& input) {
+// };
+// template <>
+// struct TypeConverter<gfx::Point, geometry::PointPtr> {
+// static gfx::Point Convert(const geometry::PointPtr& input) {
// return input ? gfx::Point(input->x, input->y) : gfx::Point();
// }
// };
@@ -61,20 +64,27 @@ namespace mojo {
//
// // With an explicit cast using the static From() method.
// geometry::PointPtr output = geometry::Point::From(pt);
+//
+// // Inferring the input type using the ConvertTo helper function.
+// gfx::Point pt2 = ConvertTo<gfx::Point>(input);
// }
//
-template <typename T, typename U> class TypeConverter;
+template <typename T, typename U>
+struct TypeConverter;
// The following specialization is useful when you are converting between
// Array<POD> and std::vector<POD>.
-template <typename T> class TypeConverter<T, T> {
- public:
- static T ConvertFrom(const T& obj) {
- return obj;
- }
- static T ConvertTo(const T& obj) {
- return obj;
- }
+template <typename T>
+struct TypeConverter<T, T> {
+ static T Convert(const T& obj) { return obj; }
+};
+
+// The following helper function is useful for shorthand. The compiler can infer
+// the input type, so you can write:
+// OutputType out = ConvertTo<OutputType>(input);
+template <typename T, typename U>
+inline T ConvertTo(const U& obj) {
+ return TypeConverter<T, U>::Convert(obj);
};
} // namespace mojo

Powered by Google App Engine
This is Rietveld 408576698