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

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

Issue 294833002: Mojo: more idiomatic C++ bindings (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 6 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
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..0ac5f6d47603042826bcfb2e352b5323cc071e90 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);
-// }
-//
-// 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;
+// geometry::PointPtr output = geometry::Point::From(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_
« no previous file with comments | « mojo/public/cpp/bindings/tests/type_conversion_unittest.cc ('k') | mojo/public/cpp/environment/buffer_tls.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698