wayland: unique_ptr'd wayland_proxy
[staging/windowmanager.git] / src / wayland.hpp
index 40b18b0..2321fff 100644 (file)
@@ -6,8 +6,8 @@
 #include "util.h"
 
 #include <functional>
-#include <unordered_map>
 #include <memory>
+#include <unordered_map>
 #include <vector>
 
 //                      _                 _
 //                |___/                   |_____|_|                  |___/
 template <typename ProxyT>
 struct wayland_proxy {
-   ProxyT *proxy;
-
-   wayland_proxy(void *p) : proxy(static_cast<ProxyT *>(p)) {}
-
-   virtual ~wayland_proxy() {
-      // If this is the nullptr, then it means it already was destroyed by a
-      // custom wayland dtor
-      if (this->proxy) {
-         logdebug("%s %p @ %p", __func__, this, this->proxy);
-         wl_proxy_destroy(reinterpret_cast<struct wl_proxy *>(this->proxy));
-      }
-   }
+   std::unique_ptr<ProxyT, std::function<void(ProxyT *)>> proxy;
+   wayland_proxy(void *p)
+      : wayland_proxy(p, [](ProxyT *p) {
+           wl_proxy_destroy(reinterpret_cast<struct wl_proxy *>(p));
+        }) {}
+   wayland_proxy(void *p, std::function<void(ProxyT *)> p_del)
+      : proxy(std::unique_ptr<ProxyT, std::function<void(ProxyT *)>>(
+           static_cast<ProxyT *>(p), p_del)) {}
 };
 
 //                                                                  _
@@ -146,15 +142,14 @@ struct surface : public wayland_proxy<struct ivi_controller_surface>,
    float opacity;
 
    surface(uint32_t i, struct controller *c);
-   ~surface() override;
 
    // Requests
    void set_visibility(uint32_t visibility);
    void set_opacity(wl_fixed_t opacity);
    void set_source_rectangle(int32_t x, int32_t y, int32_t width,
-                                    int32_t height);
+                             int32_t height);
    void set_destination_rectangle(int32_t x, int32_t y, int32_t width,
-                                         int32_t height);
+                                  int32_t height);
    void set_configuration(int32_t width, int32_t height);
    void set_orientation(int32_t orientation);
    void screenshot(const char *filename);
@@ -179,7 +174,6 @@ struct layer : public wayland_proxy<struct ivi_controller_layer>,
 
    layer(uint32_t i, struct controller *c);
    layer(uint32_t i, int32_t w, int32_t h, struct controller *c);
-   ~layer() override;
 
    // Requests
    void set_visibility(uint32_t visibility);
@@ -239,13 +233,16 @@ struct controller : public wayland_proxy<struct ivi_controller> {
    void add_proxy_to_id_mapping(struct wl_output *p, uint32_t id);
    void remove_proxy_to_id_mapping(struct wl_output *p);
 
-   void add_task(char const *name, std::function<void(struct controller *)> &&f);
+   void add_task(char const *name,
+                 std::function<void(struct controller *)> &&f);
    void execute_pending();
 
    controller(struct wl_registry *r, uint32_t name, uint32_t version);
 
    // Requests
-   void commit_changes() const { ivi_controller_commit_changes(this->proxy); }
+   void commit_changes() const {
+      ivi_controller_commit_changes(this->proxy.get());
+   }
    void layer_create(uint32_t id, int32_t w, int32_t h);
    void surface_create(uint32_t id);