summaryrefslogtreecommitdiff
path: root/src/xcb_resource.hh
blob: 86e5a03d3ad4ab761586b6d389a610ce0dbb272e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
 * Copyright 2026 Joel Klinghed
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

#ifndef XCB_RESOURCE_HH
#define XCB_RESOURCE_HH

#include "xcb_connection.hh"

#include <memory>
#include <xcb/xproto.h>

namespace xcb {

namespace internal {

struct WndDeleter {
  void operator()(xcb_connection_t* conn, xcb_window_t wnd) const {
    xcb_destroy_window(conn, wnd);
  }
};

struct GCDeleter {
  void operator()(xcb_connection_t* conn, xcb_gcontext_t gc) const {
    xcb_free_gc(conn, gc);
  }
};

struct FontDeleter {
  void operator()(xcb_connection_t* conn, xcb_font_t font) const {
    xcb_close_font(conn, font);
  }
};

}  // namespace internal

template <typename T, typename Deleter>
class xcb_resource {
 public:
  explicit xcb_resource(shared_conn conn)
      : conn_(conn), id_(xcb_generate_id(conn_.get())) {}
  constexpr xcb_resource() : id_(XCB_NONE) {}
  xcb_resource(xcb_resource const& res) = delete;
  xcb_resource(xcb_resource&& res) : conn_(res.conn_), id_(res.release()) {}
  ~xcb_resource() { reset(); }

  xcb_resource& operator=(xcb_resource const& res) = delete;
  xcb_resource& operator=(xcb_resource&& res) {
    reset();
    conn_ = res.conn_;
    id_ = res.release();
    return *this;
  }

  T id() const { return id_; }

  void reset() {
    if (id_ == XCB_NONE)
      return;
    deleter_(conn_.get(), id_);
    id_ = XCB_NONE;
  }

  T release() {
    auto ret = id_;
    id_ = XCB_NONE;
    conn_.reset();
    return ret;
  }

 private:
  shared_conn conn_;
  T id_;
  Deleter const deleter_{};
};

typedef std::unique_ptr<xcb_resource<xcb_window_t, internal::WndDeleter>>
    unique_wnd;
typedef std::shared_ptr<xcb_resource<xcb_window_t, internal::WndDeleter>>
    shared_wnd;

unique_wnd make_unique_wnd(shared_conn conn);
shared_wnd make_shared_wnd(shared_conn conn);

typedef std::unique_ptr<xcb_resource<xcb_gcontext_t, internal::GCDeleter>>
    unique_gc;
typedef std::shared_ptr<xcb_resource<xcb_gcontext_t, internal::GCDeleter>>
    shared_gc;

unique_gc make_unique_gc(shared_conn conn);
shared_gc make_shared_gc(shared_conn conn);

typedef std::unique_ptr<xcb_resource<xcb_font_t, internal::FontDeleter>>
    unique_font;
typedef std::shared_ptr<xcb_resource<xcb_font_t, internal::FontDeleter>>
    shared_font;

unique_font make_unique_font(shared_conn conn);
shared_font make_shared_font(shared_conn conn);

}  // namespace xcb

#endif  // XCB_RESOURCE_HH