Index: ui/ozone/platform/wayland/wayland_object.h |
diff --git a/ui/ozone/platform/wayland/wayland_object.h b/ui/ozone/platform/wayland/wayland_object.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..e4b8b4e43d3ae653b07bc197a9e6a61680374a00 |
--- /dev/null |
+++ b/ui/ozone/platform/wayland/wayland_object.h |
@@ -0,0 +1,72 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef UI_OZONE_PLATFORM_WAYLAND_WAYLAND_OBJECT_H_ |
+#define UI_OZONE_PLATFORM_WAYLAND_WAYLAND_OBJECT_H_ |
+ |
+#include <wayland-client.h> |
+#include <xdg-shell-unstable-v5-client-protocol.h> |
+ |
+#include "base/memory/scoped_ptr.h" |
+ |
+namespace wl { |
+ |
+template <typename T> |
+struct ObjectTraits; |
+ |
+#define DEFINE_TRAITS_WITH_DELETER(name, deleter_fn) \ |
Michael Forney
2016/01/21 22:11:48
If this is not acceptable, I can either expand the
spang
2016/01/22 23:42:32
I'd prefer the macros expanded because token pasti
Michael Forney
2016/01/23 01:08:05
Done.
|
+ template <> \ |
+ struct ObjectTraits<name> { \ |
+ static constexpr const wl_interface* interface = &name##_interface; \ |
+ static constexpr void (*deleter)(name*) = &deleter_fn; \ |
+ } |
+ |
+#define DEFINE_TRAITS(name) DEFINE_TRAITS_WITH_DELETER(name, name##_destroy) |
+ |
+DEFINE_TRAITS(wl_buffer); |
+DEFINE_TRAITS(wl_compositor); |
+DEFINE_TRAITS(wl_registry); |
+DEFINE_TRAITS(wl_seat); |
+DEFINE_TRAITS(wl_shell); |
+DEFINE_TRAITS(wl_shm); |
+DEFINE_TRAITS(wl_shm_pool); |
+DEFINE_TRAITS(wl_surface); |
+DEFINE_TRAITS(xdg_shell); |
+DEFINE_TRAITS(xdg_surface); |
+ |
+DEFINE_TRAITS_WITH_DELETER(wl_display, wl_display_disconnect); |
+DEFINE_TRAITS_WITH_DELETER(wl_keyboard, wl_keyboard_release); |
+DEFINE_TRAITS_WITH_DELETER(wl_pointer, wl_pointer_release); |
+ |
+#undef DEFINE_TRAITS |
+#undef DEFINE_TRAITS_WITH_DELETER |
+ |
+struct Deleter { |
+ template <typename T> |
+ void operator()(T* obj) { |
+ ObjectTraits<T>::deleter(obj); |
+ } |
+}; |
+ |
+template <typename T> |
+class Object : public scoped_ptr<T, Deleter> { |
+ public: |
+ Object() {} |
+ explicit Object(T* obj) : scoped_ptr<T, Deleter>(obj) {} |
+ |
+ uint32_t id() { |
+ return wl_proxy_get_id( |
+ reinterpret_cast<wl_proxy*>(scoped_ptr<T, Deleter>::get())); |
+ } |
+}; |
+ |
+template <typename T> |
+wl::Object<T> Bind(wl_registry* registry, uint32_t name, uint32_t version) { |
+ return wl::Object<T>(static_cast<T*>( |
+ wl_registry_bind(registry, name, ObjectTraits<T>::interface, version))); |
+} |
+ |
+} // namespace wl |
+ |
+#endif // UI_OZONE_PLATFORM_WAYLAND_WAYLAND_OBJECT_H_ |