Chromium Code Reviews| 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 83597747c55d2887b8ca1531a328ab448fd02730..f66d4f75242a82c154e44a6bdc156e2509f6b992 100644 |
| --- a/mojo/public/cpp/bindings/type_converter.h |
| +++ b/mojo/public/cpp/bindings/type_converter.h |
| @@ -18,12 +18,8 @@ namespace mojo { |
| // template <> |
| // class TypeConverter<T, U> { |
| // public: |
| -// static T ConvertFrom(const U& input, Buffer* buf); |
| +// static T ConvertFrom(const U& input); |
| // static U ConvertTo(const T& input); |
| -// |
| -// // Maybe (mutually exclusive): |
| -// MOJO_ALLOW_IMPLICIT_TYPE_CONVERSION(); |
| -// MOJO_INHERIT_IMPLICIT_TYPE_CONVERSION(X, Y); |
| // }; |
| // } |
| // |
| @@ -43,83 +39,44 @@ namespace mojo { |
| // |
| // namespace mojo { |
| // template <> |
| -// class TypeConverter<geometry::Point, gfx::Point> { |
| +// class TypeConverter<geometry::PointPtr, gfx::Point> { |
| // public: |
| -// static geometry::Point ConvertFrom(const gfx::Point& input, |
| -// Buffer* buf) { |
| -// geometry::Point::Builder builder(buf); |
| -// builder.set_x(input.x()); |
| -// builder.set_y(input.y()); |
| -// return builder.Finish(); |
| +// static geometry::PointPtr ConvertFrom(const gfx::Point& input) { |
| +// geometry::PointPtr result; |
| +// result->x = input.x(); |
| +// result->y = input.y(); |
| +// return result.Pass(); |
| // } |
| -// static gfx::Point ConvertTo(const geometry::Point& input) { |
| -// return gfx::Point(input.x(), input.y()); |
| +// static gfx::Point ConvertTo(const geometry::PointPtr& input) { |
| +// return input ? gfx::Point(input->x, input->y) : gfx::Point(); |
| // } |
| // }; |
| // } |
| // |
| // With the above TypeConverter defined, it is possible to write code like this: |
| // |
| -// void AcceptPoint(const geometry::Point& input) { |
| +// void AcceptPoint(const geometry::PointPtr& input) { |
| // // With an explicit cast using the .To<> method. |
| // gfx::Point pt = input.To<gfx::Point>(); |
| // |
| -// mojo::AllocationScope scope; |
| // // With an explicit cast using the static From() method. |
| // geometry::Point output = geometry::Point::From(pt); |
|
yzshen1
2014/05/28 19:45:51
nit: |output| type is geometry::PointPtr.
|
| // } |
| // |
| -// More "direct" conversions can be enabled by adding the following macro to the |
| -// TypeConverter specialization: |
| -// MOJO_ALLOW_IMPLICIT_TYPE_CONVERSION(); |
| -// |
| -// To be exact, this macro enables: |
| -// - converting constructor: |
| -// T(const U& u, mojo::Buffer* buf = mojo::Buffer::current()); |
| -// - assignment operator: |
| -// T& operator=(const U& u); |
| -// - conversion operator: |
| -// operator U() const; |
| -// |
| -// If the macro is added to TypeConverter<geometry::Point, gfx::Point>, for |
| -// example, it is possible to write code like this: |
| -// |
| -// void SomeFunction(const gfx::Point& pt); |
| -// |
| -// void AcceptPoint(const geometry::Point& input) { |
| -// // Using the conversion operator. |
| -// SomeFunction(input); |
| -// |
| -// mojo::AllocationScope scope; |
| -// // Using the converting constructor. |
| -// geometry::Point output_1(pt); |
| -// |
| -// geometry::Point output_2; |
| -// // Using the assignment operator. |
| -// output_2 = pt; |
| -// } |
| -// |
| -// There is another macro to inherit implicit conversion settings from another |
| -// TypeConverter: |
| -// MOJO_INHERIT_IMPLICIT_TYPE_CONVERSION(X, Y); |
| -// |
| -// It allows implicit conversions if and only if TypeConverter<X, Y> allows |
| -// implicit conversions. This is useful when defining TypeConverter for |
| -// container types. |
| -// |
| -// Although these macros are convenient, they make conversions less obvious. |
| -// Users may do conversions excessively without paying attention to the cost. So |
| -// please use them wisely. |
| template <typename T, typename U> class TypeConverter; |
| -} // namespace mojo |
| - |
| -#define MOJO_ALLOW_IMPLICIT_TYPE_CONVERSION() \ |
| - static void AssertAllowImplicitTypeConversion() {} |
| - |
| -#define MOJO_INHERIT_IMPLICIT_TYPE_CONVERSION(T, U) \ |
| - static void AssertAllowImplicitTypeConversion() { \ |
| - TypeConverter<T, U>::AssertAllowImplicitTypeConversion(); \ |
| +// 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; |
| } |
| +}; |
| + |
| +} // namespace mojo |
| #endif // MOJO_PUBLIC_CPP_BINDINGS_TYPE_CONVERTER_H_ |