| 1 | #ifndef GI_VALUE_HPP |
| 2 | #define GI_VALUE_HPP |
| 3 | |
| 4 | #include "exception.hpp" |
| 5 | #include "wrap.hpp" |
| 6 | |
| 7 | GI_MODULE_EXPORT |
| 8 | namespace gi |
| 9 | { |
| 10 | namespace repository |
| 11 | { |
| 12 | // specialize to declare gtype info |
| 13 | // if not within class get_type() |
| 14 | // gvalue info can also be included this way |
| 15 | // to inject support into Value wrapper |
| 16 | template<typename T> |
| 17 | struct declare_gtype_of : public std::false_type |
| 18 | {}; |
| 19 | |
| 20 | } // namespace repository |
| 21 | |
| 22 | namespace traits |
| 23 | { |
| 24 | namespace detail |
| 25 | { |
| 26 | template<typename T, class Enable = void> |
| 27 | struct gtype : public std::false_type |
| 28 | { |
| 29 | static GType get_type() |
| 30 | { |
| 31 | // dummy test to trigger (almost) always |
| 32 | static_assert(std::is_void<T>::value, "type is not a registered GType"); |
| 33 | return 0; |
| 34 | } |
| 35 | }; |
| 36 | |
| 37 | // gboxed/gobject (or otherwise class) cases |
| 38 | template<typename T> |
| 39 | struct gtype<T, typename if_valid_type<decltype(T::get_type_())>::type> |
| 40 | : public std::true_type |
| 41 | { |
| 42 | typedef typename std::remove_reference<T>::type CppType; |
| 43 | static GType get_type() { return CppType::get_type_(); } |
| 44 | }; |
| 45 | |
| 46 | // otherwise externally declared |
| 47 | template<typename T> |
| 48 | struct gtype<T, typename if_valid_type<decltype(repository::declare_gtype_of< |
| 49 | T>::get_type())>::type> : public std::true_type |
| 50 | { |
| 51 | static constexpr GType ( |
| 52 | *get_type)() = repository::declare_gtype_of<T>::get_type; |
| 53 | }; |
| 54 | |
| 55 | // gvalue helper info |
| 56 | template<typename T, class Enable = void> |
| 57 | struct gvalue : public std::false_type |
| 58 | {}; |
| 59 | |
| 60 | // as declared (both of set_value and get_value or neither) |
| 61 | template<typename T> |
| 62 | struct gvalue<T, |
| 63 | typename if_valid_type<decltype(repository::declare_gtype_of<T>::get_value( |
| 64 | nullptr))>::type> : public std::true_type |
| 65 | { |
| 66 | static T get(const GValue *val) |
| 67 | { |
| 68 | return repository::declare_gtype_of<T>::get_value(val); |
| 69 | } |
| 70 | static void set(GValue *val, T t) |
| 71 | { |
| 72 | repository::declare_gtype_of<T>::set_value(val, t); |
| 73 | } |
| 74 | }; |
| 75 | |
| 76 | template<typename T> |
| 77 | using is_enum_or_bitfield = |
| 78 | typename std::conditional<std::is_enum<T>::value && gtype<T>::value, |
| 79 | std::true_type, std::false_type>::type; |
| 80 | |
| 81 | // handle enum/flags cases, rather than many declares |
| 82 | template<typename T> |
| 83 | struct gvalue<T, typename std::enable_if<is_enum_or_bitfield<T>::value>::type> |
| 84 | : public std::true_type |
| 85 | { |
| 86 | static T get(const GValue *val) |
| 87 | { |
| 88 | GType t = gtype<T>::get_type(); |
| 89 | if (G_TYPE_IS_FLAGS(t)) |
| 90 | return static_cast<T>(g_value_get_flags(val)); |
| 91 | // assume enum, let glib complain otherwise |
| 92 | return static_cast<T>(g_value_get_enum(val)); |
| 93 | } |
| 94 | |
| 95 | static void set(GValue *val, T v) |
| 96 | { |
| 97 | GType t = gtype<T>::get_type(); |
| 98 | if (G_TYPE_IS_FLAGS(t)) { |
| 99 | g_value_set_flags(val, (guint)v); |
| 100 | } else { |
| 101 | // assume enum, let glib complain otherwise |
| 102 | g_value_set_enum(val, (gint)v); |
| 103 | } |
| 104 | } |
| 105 | }; |
| 106 | |
| 107 | } // namespace detail |
| 108 | |
| 109 | template<typename T> |
| 110 | using gtype = detail::gtype< |
| 111 | typename std::decay<typename std::remove_reference<T>::type>::type>; |
| 112 | |
| 113 | template<typename T> |
| 114 | using gvalue = detail::gvalue< |
| 115 | typename std::decay<typename std::remove_reference<T>::type>::type>; |
| 116 | |
| 117 | template<typename T> |
| 118 | using is_enum_or_bitfield = detail::is_enum_or_bitfield< |
| 119 | typename std::decay<typename std::remove_reference<T>::type>::type>; |
| 120 | |
| 121 | #if 0 |
| 122 | template<typename T, typename Enable = void> |
| 123 | struct is_flag : public std::false_type {}; |
| 124 | |
| 125 | // TODO extend fundamental type stuff ?? |
| 126 | template<typename T> |
| 127 | struct is_flag<T, |
| 128 | typename std::enable_if<std::is_enum<T>::value && |
| 129 | repository::declare_gtype_of<T>::get_fundamental_type() == G_TYPE_FLAGS>::type> : |
| 130 | public std::true_type {}; |
| 131 | #endif |
| 132 | |
| 133 | } // namespace traits |
| 134 | |
| 135 | // C++ types are used below (e.g. int) instead of e.g. gint |
| 136 | // since gint64 might map to same as glong (or not) |
| 137 | // so some of the int types are "best approximation" from C++ type to GType |
| 138 | // instead; use the following types to guide to the right overload |
| 139 | typedef char vchar; |
| 140 | typedef long vlong; |
| 141 | typedef int vint; |
| 142 | typedef long long vint64; |
| 143 | typedef bool vboolean; |
| 144 | |
| 145 | typedef unsigned char vuchar; |
| 146 | typedef unsigned long vulong; |
| 147 | typedef unsigned int vuint; |
| 148 | typedef unsigned long long vuint64; |
| 149 | |
| 150 | typedef float vfloat; |
| 151 | typedef double vdouble; |
| 152 | |
| 153 | // NOTE: (plain) char might be signed or unsigned depending on platform |
| 154 | // but gchar == char always anyway |
| 155 | static_assert(std::is_same<gchar, char>::value, "now what"); |
| 156 | |
| 157 | namespace repository |
| 158 | { |
| 159 | template<> |
| 160 | struct declare_gtype_of<void> |
| 161 | { |
| 162 | static GType get_type() { return G_TYPE_NONE; } |
| 163 | }; |
| 164 | |
| 165 | #define GI_DECLARE_GTYPE(cpptype, g_type) \ |
| 166 | template<> \ |
| 167 | struct declare_gtype_of<cpptype> \ |
| 168 | { \ |
| 169 | static constexpr GType get_type() { return g_type; } \ |
| 170 | }; |
| 171 | |
| 172 | #define GI_DECLARE_GTYPE_VALUE(cpptype, g_type, value_suffix) \ |
| 173 | template<> \ |
| 174 | struct declare_gtype_of<cpptype> \ |
| 175 | { \ |
| 176 | static constexpr GType get_type() { return g_type; } \ |
| 177 | static void set_value(GValue *val, cpptype v) \ |
| 178 | { \ |
| 179 | g_value_set_##value_suffix(val, v); \ |
| 180 | } \ |
| 181 | static cpptype get_value(const GValue *val) \ |
| 182 | { \ |
| 183 | return g_value_get_##value_suffix(val); \ |
| 184 | } \ |
| 185 | }; |
| 186 | |
| 187 | // declare non-cv qualified type |
| 188 | GI_DECLARE_GTYPE_VALUE(gpointer, G_TYPE_POINTER, pointer) |
| 189 | GI_DECLARE_GTYPE_VALUE(bool, G_TYPE_BOOLEAN, boolean) |
| 190 | GI_DECLARE_GTYPE_VALUE(char, G_TYPE_CHAR, schar) |
| 191 | GI_DECLARE_GTYPE_VALUE(unsigned char, G_TYPE_UCHAR, uchar) |
| 192 | GI_DECLARE_GTYPE_VALUE(int, G_TYPE_INT, int) |
| 193 | GI_DECLARE_GTYPE_VALUE(unsigned int, G_TYPE_UINT, uint) |
| 194 | GI_DECLARE_GTYPE_VALUE(long, G_TYPE_LONG, long) |
| 195 | GI_DECLARE_GTYPE_VALUE(unsigned long, G_TYPE_ULONG, ulong) |
| 196 | GI_DECLARE_GTYPE_VALUE(long long, G_TYPE_INT64, int64) |
| 197 | GI_DECLARE_GTYPE_VALUE(unsigned long long, G_TYPE_UINT64, uint64) |
| 198 | GI_DECLARE_GTYPE_VALUE(float, G_TYPE_FLOAT, float) |
| 199 | GI_DECLARE_GTYPE_VALUE(double, G_TYPE_DOUBLE, double) |
| 200 | // some custom set/get for these |
| 201 | // remember; the pointer is non-const |
| 202 | GI_DECLARE_GTYPE(const char *, G_TYPE_STRING) |
| 203 | GI_DECLARE_GTYPE(char *, G_TYPE_STRING) |
| 204 | GI_DECLARE_GTYPE(std::string, G_TYPE_STRING) |
| 205 | GI_DECLARE_GTYPE(gi::cstring, G_TYPE_STRING) |
| 206 | GI_DECLARE_GTYPE(gi::cstring_v, G_TYPE_STRING) |
| 207 | |
| 208 | #undef GI_DECLARE_GTYPE_VALUE |
| 209 | #undef GI_DECLARE_GTYPE |
| 210 | |
| 211 | } // namespace repository |
| 212 | |
| 213 | namespace detail |
| 214 | { |
| 215 | // GValue helpers |
| 216 | |
| 217 | // set_value |
| 218 | template<typename T, |
| 219 | typename std::enable_if<traits::gvalue<T>::value>::type * = nullptr> |
| 220 | inline void |
| 221 | set_value(GValue *val, T v) |
| 222 | { |
| 223 | traits::gvalue<T>::set(val, v); |
| 224 | } |
| 225 | |
| 226 | inline void |
| 227 | set_value(GValue *val, const std::string &s) |
| 228 | { |
| 229 | g_value_set_string(val, s.c_str()); |
| 230 | } |
| 231 | |
| 232 | inline void |
| 233 | set_value(GValue *val, gi::cstring_v s) |
| 234 | { |
| 235 | g_value_set_string(val, s.c_str()); |
| 236 | } |
| 237 | |
| 238 | inline void |
| 239 | set_value(GValue *val, const char *s) |
| 240 | { |
| 241 | g_value_set_string(val, s); |
| 242 | } |
| 243 | |
| 244 | template<typename T, |
| 245 | typename std::enable_if<traits::is_object<T>::value>::type * = nullptr> |
| 246 | inline void |
| 247 | set_value(GValue *val, T v) |
| 248 | { |
| 249 | // set might not handle NULL case |
| 250 | g_value_take_object(val, v.gobj_copy_()); |
| 251 | } |
| 252 | |
| 253 | template<typename T, |
| 254 | typename std::enable_if<traits::is_gboxed<T>::value>::type * = nullptr> |
| 255 | inline void |
| 256 | set_value(GValue *val, T v) |
| 257 | { |
| 258 | // set might not handle NULL case |
| 259 | g_value_take_boxed(val, v.gobj_copy_()); |
| 260 | } |
| 261 | |
| 262 | // container case |
| 263 | template<typename T, typename T::_detail::DataType * = nullptr> |
| 264 | inline void |
| 265 | set_value(GValue *val, T v) |
| 266 | { |
| 267 | v._set_value(val); |
| 268 | } |
| 269 | |
| 270 | // get_value |
| 271 | template<typename T, |
| 272 | typename std::enable_if<traits::is_object<T>::value>::type * = nullptr> |
| 273 | inline T |
| 274 | get_value(const GValue *val) |
| 275 | { |
| 276 | // ensure sanity |
| 277 | static_assert(std::is_class<T>::value && !std::is_const<T>::value, |
| 278 | "non cv-qualified class type required"); |
| 279 | auto cv = |
| 280 | static_cast<typename traits::ctype<T>::type>(g_value_dup_object(val)); |
| 281 | if (cv && !g_type_is_a(G_OBJECT_TYPE(cv), traits::gtype<T>::get_type())) |
| 282 | detail::try_throw(transform_error(G_OBJECT_TYPE(cv))); |
| 283 | return gi::wrap(cv, transfer_full); |
| 284 | } |
| 285 | |
| 286 | template<typename T, |
| 287 | typename std::enable_if<traits::is_gboxed<T>::value && |
| 288 | !traits::is_reftype<T>::value>::type * = nullptr> |
| 289 | inline T |
| 290 | get_value(const GValue *val) |
| 291 | { |
| 292 | // no way to know whether boxed type is correct |
| 293 | // ensure sanity |
| 294 | static_assert(std::is_class<T>::value && !std::is_const<T>::value, |
| 295 | "non cv-qualified class type required"); |
| 296 | auto cv = |
| 297 | static_cast<typename traits::ctype<T>::type>(g_value_dup_boxed(val)); |
| 298 | return gi::wrap(cv, transfer_full); |
| 299 | } |
| 300 | |
| 301 | template<typename T, |
| 302 | typename std::enable_if<traits::is_gboxed<T>::value && |
| 303 | traits::is_reftype<T>::value>::type * = nullptr> |
| 304 | inline T |
| 305 | get_value(const GValue *val) |
| 306 | { |
| 307 | // no way to know whether boxed type is correct |
| 308 | // ensure sanity |
| 309 | static_assert(std::is_class<T>::value && !std::is_const<T>::value, |
| 310 | "non cv-qualified class type required"); |
| 311 | auto cv = |
| 312 | static_cast<typename traits::ctype<T>::type>(g_value_get_boxed(val)); |
| 313 | return gi::wrap(cv, transfer_none); |
| 314 | } |
| 315 | |
| 316 | template<typename T, |
| 317 | typename std::enable_if<traits::gvalue<T>::value>::type * = nullptr> |
| 318 | inline T |
| 319 | get_value(const GValue *val) |
| 320 | { |
| 321 | return traits::gvalue<T>::get(val); |
| 322 | } |
| 323 | |
| 324 | // sigh ... |
| 325 | template<typename T, |
| 326 | typename std::enable_if<std::is_same<T, std::string>::value || |
| 327 | std::is_base_of<detail::String, T>::value>::type * = |
| 328 | nullptr> |
| 329 | inline T |
| 330 | get_value(const GValue *val) |
| 331 | { |
| 332 | return gi::wrap(g_value_get_string(val), transfer_none); |
| 333 | } |
| 334 | |
| 335 | // container case |
| 336 | template<typename T, typename T::_detail::DataType * = nullptr> |
| 337 | inline T |
| 338 | get_value(const GValue *val) |
| 339 | { |
| 340 | static_assert(traits::is_decayed<T>::value, ""); |
| 341 | return T::template _get_value<T>(val); |
| 342 | } |
| 343 | |
| 344 | // convenience helper ... |
| 345 | template<typename T, |
| 346 | typename std::enable_if<std::is_same<T, void>::value>::type * = nullptr> |
| 347 | inline T |
| 348 | get_value(const GValue * /*val*/) |
| 349 | {} |
| 350 | |
| 351 | // simple (RAII) Value wrapper for (internal) use |
| 352 | struct Value : public GValue, noncopyable |
| 353 | { |
| 354 | void clear() { memset((void *)this, 0, sizeof(*this)); } |
| 355 | |
| 356 | Value() { clear(); } |
| 357 | |
| 358 | template<typename T, typename Enable = disable_if_same_or_derived<T, Value>> |
| 359 | explicit Value(T &&v) |
| 360 | { |
| 361 | clear(); |
| 362 | g_value_init(this, traits::gtype<T>::get_type()); |
| 363 | set_value(this, std::forward<T>(v)); |
| 364 | } |
| 365 | |
| 366 | template<typename T> |
| 367 | void init() |
| 368 | { |
| 369 | // handle no-op void (return value) corner case |
| 370 | const GType tp = traits::gtype<T>::get_type(); |
| 371 | if (tp != G_TYPE_NONE) |
| 372 | g_value_init(this, tp); |
| 373 | } |
| 374 | |
| 375 | // let's not copy, but ok to move around |
| 376 | Value(Value &&other) |
| 377 | { |
| 378 | memcpy((void *)this, &other, sizeof(*this)); |
| 379 | other.clear(); |
| 380 | } |
| 381 | |
| 382 | Value &operator=(Value &&other) |
| 383 | { |
| 384 | if (this != &other) { |
| 385 | memcpy((void *)this, &other, sizeof(*this)); |
| 386 | other.clear(); |
| 387 | } |
| 388 | return *this; |
| 389 | } |
| 390 | |
| 391 | ~Value() |
| 392 | { |
| 393 | if (G_VALUE_TYPE(this)) |
| 394 | g_value_unset(this); |
| 395 | } |
| 396 | }; |
| 397 | |
| 398 | // we really rely upon this as part of the ABI |
| 399 | // justifies operations above and some explicit casts above |
| 400 | // (to avoid -Wclass-memaccess) |
| 401 | static_assert(sizeof(Value) == sizeof(GValue), "unsupported compiler"); |
| 402 | |
| 403 | template<typename R> |
| 404 | inline R |
| 405 | transform_value(const GValue *val) |
| 406 | { |
| 407 | detail::Value dest; |
| 408 | dest.init<R>(); |
| 409 | if (!g_value_transform(val, &dest)) |
| 410 | detail::try_throw(detail::transform_error(G_VALUE_TYPE(&dest))); |
| 411 | return detail::get_value<R>(&dest); |
| 412 | } |
| 413 | |
| 414 | // hand-crafted Value wrapper with an interface as it would be generated |
| 415 | // and used by generated code, along with additional convenience |
| 416 | class ValueBase : public gi::detail::GBoxedWrapperBase<ValueBase, GValue> |
| 417 | { |
| 418 | using self_type = ValueBase; |
| 419 | |
| 420 | public: |
| 421 | static GType get_type_() G_GNUC_CONST { return G_TYPE_VALUE; } |
| 422 | |
| 423 | void copy(self_type dest) const { g_value_copy(gobj_(), dest.gobj_()); } |
| 424 | |
| 425 | void reset() { g_value_reset(gobj_()); } |
| 426 | |
| 427 | void unset() { g_value_unset(gobj_()); } |
| 428 | |
| 429 | bool transform(self_type dest) const |
| 430 | { |
| 431 | return g_value_transform(gobj_(), dest.gobj_()); |
| 432 | } |
| 433 | |
| 434 | static bool type_compatible(GType src_type, GType dest_type) |
| 435 | { |
| 436 | return g_value_type_compatible(src_type, dest_type); |
| 437 | } |
| 438 | |
| 439 | static bool type_transformable(GType src_type, GType dest_type) |
| 440 | { |
| 441 | return g_value_type_transformable(src_type, dest_type); |
| 442 | } |
| 443 | |
| 444 | template<typename T> |
| 445 | self_type &set_value(T &&v) |
| 446 | { |
| 447 | detail::set_value(gobj_(), std::forward<T>(v)); |
| 448 | return *this; |
| 449 | } |
| 450 | |
| 451 | template<typename T> |
| 452 | T get_value() const |
| 453 | { |
| 454 | return detail::get_value<T>(gobj_()); |
| 455 | } |
| 456 | |
| 457 | template<typename R> |
| 458 | R transform_value() |
| 459 | { |
| 460 | return detail::transform_value<R>(gobj_()); |
| 461 | } |
| 462 | }; |
| 463 | |
| 464 | } // namespace detail |
| 465 | |
| 466 | namespace repository |
| 467 | { |
| 468 | namespace GObject |
| 469 | { |
| 470 | // build on above base with additional convenience (in owning case) |
| 471 | class Value_Ref; |
| 472 | class Value : public gi::detail::GBoxedWrapper<Value, GValue, detail::ValueBase, |
| 473 | Value_Ref> |
| 474 | { |
| 475 | typedef gi::detail::GBoxedWrapper<Value, GValue, detail::ValueBase, Value_Ref> |
| 476 | super_type; |
| 477 | typedef Value self_type; |
| 478 | |
| 479 | public: |
| 480 | using detail::ValueBase::copy; |
| 481 | using super_type::copy; |
| 482 | |
| 483 | // hybrid GBoxed/CBoxed |
| 484 | void allocate_() |
| 485 | { |
| 486 | if (this->data_) |
| 487 | return; |
| 488 | // make sure we match GValue boxed allocation with boxed free |
| 489 | // (though last kown implementation uses g_new0/g_free) |
| 490 | detail::Value tmp; |
| 491 | this->data_ = (::GValue *)g_boxed_copy(G_TYPE_VALUE, &tmp); |
| 492 | } |
| 493 | |
| 494 | // convenience |
| 495 | Value() { allocate_(); } |
| 496 | |
| 497 | // allow non-explicit use for convenient calling |
| 498 | // but avoid copy/move construct use |
| 499 | template<typename T, |
| 500 | typename std::enable_if<!std::is_base_of<Value, |
| 501 | typename std::remove_reference<T>::type>::value>::type * = nullptr> |
| 502 | Value(T &&t) |
| 503 | { |
| 504 | allocate_(); |
| 505 | init<T>(std::forward<T>(t)); |
| 506 | } |
| 507 | |
| 508 | Value &init(GType tp) |
| 509 | { |
| 510 | // no-op void corner case |
| 511 | if (tp != G_TYPE_NONE) |
| 512 | g_value_init(gobj_(), tp); |
| 513 | return *this; |
| 514 | } |
| 515 | |
| 516 | template<typename T> |
| 517 | Value &init(T &&v) |
| 518 | { |
| 519 | g_value_init(gobj_(), traits::gtype<T>::get_type()); |
| 520 | set_value(std::forward<T>(v)); |
| 521 | return *this; |
| 522 | } |
| 523 | }; |
| 524 | |
| 525 | class Value_Ref |
| 526 | : public gi::detail::GBoxedRefWrapper<Value, ::GValue, detail::ValueBase> |
| 527 | { |
| 528 | typedef gi::detail::GBoxedRefWrapper<Value, ::GValue, detail::ValueBase> |
| 529 | super_type; |
| 530 | using super_type::super_type; |
| 531 | }; |
| 532 | |
| 533 | } // namespace GObject |
| 534 | |
| 535 | template<> |
| 536 | struct declare_cpptype_of<GValue> |
| 537 | { |
| 538 | typedef GObject::Value type; |
| 539 | }; |
| 540 | |
| 541 | } // namespace repository |
| 542 | |
| 543 | } // namespace gi |
| 544 | |
| 545 | #endif // GI_VALUE_HPP |