26 #ifndef WAYLAND_UTIL_HPP
27 #define WAYLAND_UTIL_HPP
36 #include <wayland-client-core.h>
38 #define wl_array_for_each_cpp(pos, array) \
39 for ((pos) = static_cast<decltype(pos)>((array)->data); \
40 static_cast<const char*>(pos) < (static_cast<const char*>((array)->data) + (array)->size); \
59 int check_return_value(
int return_value, std::string
const &function_name);
66 template<
typename native_t>
70 native_t *
object =
nullptr;
90 *
this = std::move(other);
93 native_t *c_ptr()
const
96 throw std::runtime_error(
"Tried to access empty object");
100 bool has_object()
const
105 operator bool()
const
110 operator native_t*()
const
120 object = right.object;
126 std::swap(
object, right.object);
132 return object == right.object;
137 return !(*
this == right);
146 template<
typename native_t>
150 std::shared_ptr<native_t> object;
158 std::shared_ptr<native_t> ref_ptr()
const
175 *
this = std::move(other);
178 native_t *c_ptr()
const
181 throw std::runtime_error(
"Tried to access empty object");
185 bool has_object()
const
190 operator bool()
const
195 operator native_t*()
const
205 object = right.object;
211 std::swap(
object, right.object);
217 return object == right.object;
222 return !(*
this == right);
233 virtual const std::type_info &type_info()
const = 0;
234 virtual base *clone()
const = 0;
237 template <
typename T>
238 class derived :
public base
248 const std::type_info &type_info()
const override
253 base *clone()
const override
255 return new derived<T>(val);
266 : val(a.val ? a.val->clone() : nullptr) { }
268 template <
typename T>
270 : val(new derived<T>(t)) { }
277 any &operator=(
const any &a)
280 val = a.val ? a.val->clone() :
nullptr;
284 template <
typename T>
285 any &operator=(
const T &t)
287 if(val &&
typeid(T) == val->type_info())
288 static_cast<derived<T>*
>(val)->val = t;
292 val =
new derived<T>(t);
297 template <
typename T>
300 if(val &&
typeid(T) == val->type_info())
301 return static_cast<derived<T>*
>(val)->val;
302 throw std::bad_cast();
306 template<
unsigned int size,
int id = 0>
310 static const uint32_t mask = (1 << size) - 1;
313 explicit bitfield(
const uint32_t value = 0)
318 explicit operator uint32_t()
const
323 operator bool()
const
328 bitfield(
const bitfield<size, id> &b)
333 bool operator==(
const bitfield<size, id> &b)
338 bool operator!=(
const bitfield<size, id> &b)
340 return !operator==(b);
343 bitfield<size, id> &operator=(
const bitfield<size, id> &b)
345 v = static_cast<uint32_t>(b);
349 bitfield<size, id> operator|(
const bitfield<size, id> &b)
const
351 return bitfield<size, id>(v | static_cast<uint32_t>(b));
354 bitfield<size, id> operator&(
const bitfield<size, id> &b)
const
356 return bitfield<size, id>(v & static_cast<uint32_t>(b));
359 bitfield<size, id> operator^(
const bitfield<size, id> &b)
const
361 return bitfield<size, id>((v ^ static_cast<uint32_t>(b)) & mask);
364 bitfield<size, id> operator~()
const
366 return bitfield<size, id>(~v & mask);
369 bitfield<size, id> &operator|=(
const bitfield<size, id> &b)
371 operator=(*
this | b);
375 bitfield<size, id> &operator&=(
const bitfield<size, id> &b)
377 operator=(*
this & b);
381 bitfield<size, id> &operator^=(
const bitfield<size, id> &b)
383 operator=(*
this ^ b);
391 bool is_array{
false};
396 wl_argument argument;
398 argument_t(
const argument_t &arg);
399 argument_t &operator=(
const argument_t &arg);
403 argument_t(uint32_t i);
404 argument_t(int32_t i);
407 argument_t(
double f);
410 argument_t(
const std::string &s);
413 argument_t(wl_object *o);
416 argument_t(array_t a);
419 argument_t(std::nullptr_t);
422 static argument_t fd(
int fileno);
431 array_t(wl_array *arr);
432 void get(wl_array *arr)
const;
434 friend class proxy_t;
435 friend class detail::argument_t;
439 array_t(
const array_t &arr);
440 array_t(array_t &&arr);
442 template <
typename T> array_t(
const std::vector<T> &v)
445 wl_array_add(&a, v.size()*
sizeof(T));
448 wl_array_for_each_cpp(p, &a)
453 array_t &operator=(
const array_t &arr);
454 array_t &operator=(array_t &&arr);
456 template <
typename T> array_t &operator=(
const std::vector<T> &v)
458 wl_array_release(&a);
460 wl_array_add(&a, v.size()*
sizeof(T));
463 wl_array_for_each_cpp(p, &a)
468 template <
typename T>
operator std::vector<T>()
const
472 wl_array_for_each_cpp(p, &a)