26 #ifndef WAYLAND_UTIL_HPP
27 #define WAYLAND_UTIL_HPP
37 #include <wayland-client-core.h>
39 #define wl_array_for_each_cpp(pos, array) \
40 for ((pos) = static_cast<decltype(pos)>((array)->data); \
41 reinterpret_cast<const char*>(pos) < (reinterpret_cast<const char*>((array)->data) + (array)->size); \
60 int check_return_value(
int return_value, std::string
const &function_name);
67 template<
typename native_t>
71 native_t *
object =
nullptr;
91 *
this = std::move(other);
94 native_t *c_ptr()
const
97 throw std::runtime_error(
"Tried to access empty object");
101 bool has_object()
const
106 operator bool()
const
111 operator native_t*()
const
121 object = right.object;
127 std::swap(
object, right.object);
133 return object == right.object;
138 return !(*
this == right);
147 template<
typename native_t>
151 std::shared_ptr<native_t> object;
159 std::shared_ptr<native_t> ref_ptr()
const
176 *
this = std::move(other);
179 native_t *c_ptr()
const
182 throw std::runtime_error(
"Tried to access empty object");
186 bool has_object()
const
191 operator bool()
const
196 operator native_t*()
const
206 object = right.object;
212 std::swap(
object, right.object);
218 return object == right.object;
223 return !(*
this == right);
234 virtual const std::type_info &type_info()
const = 0;
235 virtual base *clone()
const = 0;
238 template <
typename T>
239 class derived :
public base
249 const std::type_info &type_info()
const override
254 base *clone()
const override
256 return new derived<T>(val);
267 : val(a.val ? a.val->clone() : nullptr) { }
269 template <
typename T>
271 : val(new derived<T>(t)) { }
278 any &operator=(
const any &a)
281 val = a.val ? a.val->clone() :
nullptr;
285 template <
typename T>
286 any &operator=(
const T &t)
288 if(val &&
typeid(T) == val->type_info())
289 static_cast<derived<T>*
>(val)->val = t;
293 val =
new derived<T>(t);
298 template <
typename T>
301 if(val &&
typeid(T) == val->type_info())
302 return static_cast<derived<T>*
>(val)->val;
303 throw std::bad_cast();
307 template<
unsigned int size,
int id = 0>
311 static const uint32_t mask = (1 << size) - 1;
314 explicit bitfield(
const uint32_t value = 0)
319 explicit operator uint32_t()
const
324 operator bool()
const
329 bitfield(
const bitfield<size, id> &b)
334 bool operator==(
const bitfield<size, id> &b)
339 bool operator!=(
const bitfield<size, id> &b)
341 return !operator==(b);
344 bitfield<size, id> &operator=(
const bitfield<size, id> &b)
346 v =
static_cast<uint32_t
>(b);
350 bitfield<size, id> operator|(
const bitfield<size, id> &b)
const
352 return bitfield<size, id>(v |
static_cast<uint32_t
>(b));
355 bitfield<size, id> operator&(
const bitfield<size, id> &b)
const
357 return bitfield<size, id>(v &
static_cast<uint32_t
>(b));
360 bitfield<size, id> operator^(
const bitfield<size, id> &b)
const
362 return bitfield<size, id>((v ^
static_cast<uint32_t
>(b)) & mask);
365 bitfield<size, id> operator~()
const
367 return bitfield<size, id>(~v & mask);
370 bitfield<size, id> &operator|=(
const bitfield<size, id> &b)
372 operator=(*
this | b);
376 bitfield<size, id> &operator&=(
const bitfield<size, id> &b)
378 operator=(*
this & b);
382 bitfield<size, id> &operator^=(
const bitfield<size, id> &b)
384 operator=(*
this ^ b);
392 bool is_array{
false};
397 wl_argument argument;
399 argument_t(
const argument_t &arg);
400 argument_t &operator=(
const argument_t &arg);
404 argument_t(uint32_t i);
405 argument_t(int32_t i);
408 argument_t(
double f);
411 argument_t(
const std::string &s);
414 argument_t(wl_object *o);
417 argument_t(array_t a);
420 argument_t(std::nullptr_t);
423 static argument_t fd(
int fileno);
432 array_t(wl_array *arr);
433 void get(wl_array *arr)
const;
435 friend class proxy_t;
436 friend class detail::argument_t;
440 array_t(
const array_t &arr);
441 array_t(array_t &&arr);
443 template <
typename T> array_t(
const std::vector<T> &v)
446 wl_array_add(&a, v.size()*
sizeof(T));
449 wl_array_for_each_cpp(p, &a)
454 array_t &operator=(
const array_t &arr);
455 array_t &operator=(array_t &&arr);
457 template <
typename T> array_t &operator=(
const std::vector<T> &v)
459 wl_array_release(&a);
461 wl_array_add(&a, v.size()*
sizeof(T));
464 wl_array_for_each_cpp(p, &a)
469 template <
typename T>
operator std::vector<T>()
const
473 wl_array_for_each_cpp(p, &a)