diff options
author | darin@chromium.org <darin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-05-29 19:00:36 +0000 |
---|---|---|
committer | darin@chromium.org <darin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-05-29 19:00:36 +0000 |
commit | 2b4e10f4dcd668a1e0ba3b800672e1ba4e6909de (patch) | |
tree | d17edf8877cdb821d779b71fe34cdf3a31bc98e9 /mojo/examples/pepper_container_app | |
parent | 27ac8181e1fc50ef07518148b113028f39d41faa (diff) | |
download | chromium_src-2b4e10f4dcd668a1e0ba3b800672e1ba4e6909de.zip chromium_src-2b4e10f4dcd668a1e0ba3b800672e1ba4e6909de.tar.gz chromium_src-2b4e10f4dcd668a1e0ba3b800672e1ba4e6909de.tar.bz2 |
Mojo: more idiomatic C++ bindings
This change eliminates AllocationScope in favor of heap allocating the "wrapper" classes, which are now no longer wrappers in the true sense but rather deep copies of the archived structs. We still use the term "wrapper" in the code generator.
With this design, the fact that structs are encoded as pointers (that may be null) is more apparent. For example, a Foo struct may be allocated and stored in a FooPtr. The FooPtr implements move-only semantics. This is because some of the members of Foo may be move-only (e.g., handles).
Strings are now just thin wrappers around std::string that impose nullability and a more restrictive API. It is not possible to mutate the elements of a string after it has been created. String acts like a pointer to a possibly null array of characters, which is very similar to a char*.
Arrays are now just thin wrappers around std::vector that impose nullability and a more restrictive API. Unlike String, Array does support mutation of its elements after creation, but there is no support for resizing an array. (We can add support for push_back and resize if needed.)
BUG=365922
R=yzshen@chromium.org
Review URL: https://codereview.chromium.org/294833002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@273527 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'mojo/examples/pepper_container_app')
-rw-r--r-- | mojo/examples/pepper_container_app/pepper_container_app.cc | 25 | ||||
-rw-r--r-- | mojo/examples/pepper_container_app/type_converters.h | 70 |
2 files changed, 46 insertions, 49 deletions
diff --git a/mojo/examples/pepper_container_app/pepper_container_app.cc b/mojo/examples/pepper_container_app/pepper_container_app.cc index 33c03d5..8d8b4e9 100644 --- a/mojo/examples/pepper_container_app/pepper_container_app.cc +++ b/mojo/examples/pepper_container_app/pepper_container_app.cc @@ -12,7 +12,6 @@ #include "mojo/examples/pepper_container_app/plugin_module.h" #include "mojo/examples/pepper_container_app/type_converters.h" #include "mojo/public/cpp/application/application.h" -#include "mojo/public/cpp/bindings/allocation_scope.h" #include "mojo/public/cpp/gles2/gles2.h" #include "mojo/public/cpp/system/core.h" #include "mojo/public/interfaces/service_provider/service_provider.mojom.h" @@ -34,18 +33,16 @@ class PepperContainerApp: public Application, virtual ~PepperContainerApp() {} - virtual void Initialize() MOJO_OVERRIDE { - mojo::AllocationScope scope; - + virtual void Initialize() MOJO_OVERRIDE { ConnectTo("mojo:mojo_native_viewport_service", &viewport_); viewport_.set_client(this); - Rect::Builder rect; - rect.set_x(10); - rect.set_y(10); - rect.set_width(800); - rect.set_height(600); - viewport_->Create(rect.Finish()); + RectPtr rect(Rect::New()); + rect->x = 10; + rect->y = 10; + rect->width = 800; + rect->height = 600; + viewport_->Create(rect.Pass()); viewport_->Show(); } @@ -69,16 +66,16 @@ class PepperContainerApp: public Application, base::MessageLoop::current()->Quit(); } - virtual void OnBoundsChanged(const Rect& bounds) OVERRIDE { + virtual void OnBoundsChanged(RectPtr bounds) OVERRIDE { ppapi::ProxyAutoLock lock; if (plugin_instance_) - plugin_instance_->DidChangeView(bounds); + plugin_instance_->DidChangeView(bounds.To<PP_Rect>()); } - virtual void OnEvent(const Event& event, + virtual void OnEvent(EventPtr event, const mojo::Callback<void()>& callback) OVERRIDE { - if (!event.location().is_null()) { + if (!event->location.is_null()) { ppapi::ProxyAutoLock lock; // TODO(yzshen): Handle events. diff --git a/mojo/examples/pepper_container_app/type_converters.h b/mojo/examples/pepper_container_app/type_converters.h index fc4c113..cfb5c02 100644 --- a/mojo/examples/pepper_container_app/type_converters.h +++ b/mojo/examples/pepper_container_app/type_converters.h @@ -14,59 +14,59 @@ namespace mojo { template <> -class TypeConverter<Point, PP_Point> { +class TypeConverter<PointPtr, PP_Point> { public: - static Point ConvertFrom(const PP_Point& input, Buffer* buf) { - Point::Builder point(buf); - point.set_x(input.x); - point.set_y(input.y); - return point.Finish(); + static PointPtr ConvertFrom(const PP_Point& input) { + PointPtr point(Point::New()); + point->x = input.x; + point->y = input.y; + return point.Pass(); } - static PP_Point ConvertTo(const Point& input) { - return PP_MakePoint(static_cast<int32_t>(input.x()), - static_cast<int32_t>(input.y())); + static PP_Point ConvertTo(const PointPtr& input) { + if (!input) + return PP_MakePoint(0, 0); + return PP_MakePoint(static_cast<int32_t>(input->x), + static_cast<int32_t>(input->y)); } - - MOJO_ALLOW_IMPLICIT_TYPE_CONVERSION(); }; template <> -class TypeConverter<Size, PP_Size> { +class TypeConverter<SizePtr, PP_Size> { public: - static Size ConvertFrom(const PP_Size& input, Buffer* buf) { - Size::Builder size(buf); - size.set_width(input.width); - size.set_height(input.height); - return size.Finish(); + static SizePtr ConvertFrom(const PP_Size& input) { + SizePtr size(Size::New()); + size->width = input.width; + size->height = input.height; + return size.Pass(); } - static PP_Size ConvertTo(const Size& input) { - return PP_MakeSize(static_cast<int32_t>(input.width()), - static_cast<int32_t>(input.height())); + static PP_Size ConvertTo(const SizePtr& input) { + if (!input) + return PP_MakeSize(0, 0); + return PP_MakeSize(static_cast<int32_t>(input->width), + static_cast<int32_t>(input->height)); } - - MOJO_ALLOW_IMPLICIT_TYPE_CONVERSION(); }; template <> -class TypeConverter<Rect, PP_Rect> { +class TypeConverter<RectPtr, PP_Rect> { public: - static Rect ConvertFrom(const PP_Rect& input, Buffer* buf) { - Rect::Builder rect(buf); - rect.set_x(input.point.x); - rect.set_y(input.point.y); - rect.set_width(input.size.width); - rect.set_height(input.size.height); - return rect.Finish(); + static RectPtr ConvertFrom(const PP_Rect& input) { + RectPtr rect(Rect::New()); + rect->x = input.point.x; + rect->y = input.point.y; + rect->width = input.size.width; + rect->height = input.size.height; + return rect.Pass(); } - static PP_Rect ConvertTo(const Rect& input) { - return PP_MakeRectFromXYWH(input.x(), input.y(), - input.width(), input.height()); + static PP_Rect ConvertTo(const RectPtr& input) { + if (!input) + return PP_MakeRectFromXYWH(0, 0, 0, 0); + return PP_MakeRectFromXYWH(input->x, input->y, + input->width, input->height); } - - MOJO_ALLOW_IMPLICIT_TYPE_CONVERSION(); }; } // namespace mojo |