| 1 | #ifndef GI_BASE_HPP |
| 2 | #define GI_BASE_HPP |
| 3 | |
| 4 | #include "gi_inc.hpp" |
| 5 | |
| 6 | #include "boxed.hpp" |
| 7 | #include "objectbase.hpp" |
| 8 | |
| 9 | // un-inline some glib parts |
| 10 | // (otherwise they have internal linkage and not usable in non-TU-local context) |
| 11 | #ifdef GI_MODULE_IN_INTERFACE |
| 12 | #ifdef g_strdup |
| 13 | #undef g_strdup |
| 14 | #endif |
| 15 | #endif |
| 16 | |
| 17 | GI_MODULE_EXPORT |
| 18 | namespace gi |
| 19 | { |
| 20 | namespace detail |
| 21 | { |
| 22 | inline std::string |
| 23 | exception_desc(const std::exception &e) |
| 24 | { |
| 25 | auto desc = e.what(); |
| 26 | return desc ? desc : typeid(e).name(); |
| 27 | } |
| 28 | |
| 29 | inline std::string |
| 30 | exception_desc(...) |
| 31 | { |
| 32 | return "[unknown]"; |
| 33 | } |
| 34 | |
| 35 | template<typename E> |
| 36 | [[noreturn]] inline void |
| 37 | try_throw(E &&e) |
| 38 | { |
| 39 | #if GI_CONFIG_EXCEPTIONS |
| 40 | throw std::forward<E>(e); |
| 41 | #else |
| 42 | g_critical("no throw exception; %s", exception_desc(e).c_str()); |
| 43 | abort(); |
| 44 | #endif |
| 45 | } |
| 46 | |
| 47 | // constructor does not appreciate NULL, so wrap that here |
| 48 | // map NULL to empty string; not quite the same, but it will do |
| 49 | inline std::string |
| 50 | make_string(const char *s) |
| 51 | { |
| 52 | return std::string(s ? s : ""); |
| 53 | } |
| 54 | |
| 55 | // helper string subtype |
| 56 | // used to overload unwrap of optional string argument |
| 57 | // (transfrom empty string to null) |
| 58 | // NOTE std::optional requires C++17 |
| 59 | class optional_string : public std::string |
| 60 | {}; |
| 61 | |
| 62 | class noncopyable |
| 63 | { |
| 64 | public: |
| 65 | noncopyable() {} |
| 66 | noncopyable(const noncopyable &) = delete; |
| 67 | noncopyable &operator=(const noncopyable &) = delete; |
| 68 | |
| 69 | noncopyable(noncopyable &&) = default; |
| 70 | noncopyable &operator=(noncopyable &&) = default; |
| 71 | }; |
| 72 | |
| 73 | class scope_guard : public noncopyable |
| 74 | { |
| 75 | private: |
| 76 | std::function<void()> cleanup_; |
| 77 | |
| 78 | public: |
| 79 | scope_guard(std::function<void()> &&cleanup) : cleanup_(std::move(cleanup)) {} |
| 80 | |
| 81 | ~scope_guard() noexcept(false) |
| 82 | { |
| 83 | #if GI_CONFIG_EXCEPTIONS |
| 84 | #if __cplusplus >= 201703L |
| 85 | auto pending = std::uncaught_exceptions(); |
| 86 | #else |
| 87 | auto pending = std::uncaught_exception(); |
| 88 | #endif |
| 89 | try { |
| 90 | #endif |
| 91 | cleanup_(); |
| 92 | #if GI_CONFIG_EXCEPTIONS |
| 93 | } catch (...) { |
| 94 | if (!pending) |
| 95 | throw; |
| 96 | } |
| 97 | #endif |
| 98 | } |
| 99 | }; |
| 100 | |
| 101 | // as in |
| 102 | // http://ericniebler.com/2013/08/07/universal-references-and-the-copy-constructo/ |
| 103 | template<typename A, typename B> |
| 104 | using disable_if_same_or_derived = typename std::enable_if< |
| 105 | !std::is_base_of<A, typename std::remove_reference<B>::type>::value>::type; |
| 106 | } // namespace detail |
| 107 | |
| 108 | namespace repository |
| 109 | { |
| 110 | // class types declare c type within class |
| 111 | // others can do so using this (e.g. enum) |
| 112 | template<typename CppType> |
| 113 | struct declare_ctype_of |
| 114 | {}; |
| 115 | |
| 116 | // and for all cases the reverse cpp type |
| 117 | template<typename CType> |
| 118 | struct declare_cpptype_of |
| 119 | {}; |
| 120 | |
| 121 | // generate code must specialize appropriately |
| 122 | template<typename T> |
| 123 | struct is_enumeration : public std::false_type |
| 124 | {}; |
| 125 | |
| 126 | template<typename T> |
| 127 | struct is_bitfield : public std::false_type |
| 128 | {}; |
| 129 | |
| 130 | } // namespace repository |
| 131 | |
| 132 | struct transfer_full_t; |
| 133 | struct transfer_none_t; |
| 134 | |
| 135 | namespace traits |
| 136 | { |
| 137 | template<typename T, typename U = void> |
| 138 | struct if_valid_type |
| 139 | { |
| 140 | typedef U type; |
| 141 | }; |
| 142 | |
| 143 | template<typename, typename = void> |
| 144 | struct is_type_complete : public std::false_type |
| 145 | {}; |
| 146 | |
| 147 | template<typename T> |
| 148 | struct is_type_complete<T, typename if_valid_type<decltype(sizeof( |
| 149 | typename std::decay<T>::type))>::type> |
| 150 | : public std::true_type |
| 151 | {}; |
| 152 | |
| 153 | template<typename T> |
| 154 | using is_decayed = std::is_same<typename std::decay<T>::type, T>; |
| 155 | |
| 156 | template<typename T> |
| 157 | using is_cboxed = |
| 158 | typename std::conditional<std::is_base_of<detail::CBoxed, T>::value, |
| 159 | std::true_type, std::false_type>::type; |
| 160 | |
| 161 | template<typename T> |
| 162 | using is_gboxed = |
| 163 | typename std::conditional<std::is_base_of<detail::GBoxed, T>::value, |
| 164 | std::true_type, std::false_type>::type; |
| 165 | |
| 166 | template<typename T> |
| 167 | using is_boxed = |
| 168 | typename std::conditional<std::is_base_of<detail::Boxed, T>::value, |
| 169 | std::true_type, std::false_type>::type; |
| 170 | |
| 171 | // avoid derived cases |
| 172 | template<typename T> |
| 173 | using is_object = |
| 174 | typename std::conditional<std::is_base_of<detail::ObjectBase, T>::value && |
| 175 | sizeof(T) == sizeof(gpointer), |
| 176 | std::true_type, std::false_type>::type; |
| 177 | |
| 178 | template<typename T> |
| 179 | using is_wrapper = |
| 180 | typename std::conditional<std::is_base_of<detail::wrapper_tag, T>::value && |
| 181 | sizeof(T) == sizeof(gpointer), |
| 182 | std::true_type, std::false_type>::type; |
| 183 | |
| 184 | // bring in to this namespace |
| 185 | using repository::is_bitfield; |
| 186 | using repository::is_enumeration; |
| 187 | |
| 188 | // aka passthrough |
| 189 | template<typename T> |
| 190 | using is_basic = |
| 191 | typename std::conditional<std::is_same<T, gpointer>::value || |
| 192 | std::is_same<T, gconstpointer>::value || |
| 193 | std::is_arithmetic<T>::value, |
| 194 | std::true_type, std::false_type>::type; |
| 195 | |
| 196 | // almost passthrough (on lower level at least) |
| 197 | template<typename T> |
| 198 | using is_plain = typename std::conditional<traits::is_basic<T>::value || |
| 199 | std::is_enum<T>::value, |
| 200 | std::true_type, std::false_type>::type; |
| 201 | |
| 202 | template<typename T, typename E = void> |
| 203 | struct is_reftype : public std::false_type |
| 204 | {}; |
| 205 | |
| 206 | template<typename T> |
| 207 | struct is_reftype<T, |
| 208 | typename if_valid_type<typename std::decay<T>::type::BoxType>::type> |
| 209 | : public std::true_type |
| 210 | {}; |
| 211 | |
| 212 | template<typename T, typename Enable = void> |
| 213 | struct has_ctype_member : public std::false_type |
| 214 | {}; |
| 215 | |
| 216 | template<typename T> |
| 217 | struct has_ctype_member<T, |
| 218 | typename if_valid_type<typename T::BaseObjectType>::type> |
| 219 | : public std::true_type |
| 220 | {}; |
| 221 | |
| 222 | // return corresponding c type (if any) |
| 223 | // (string and basic type not considered) |
| 224 | // preserve const |
| 225 | template<typename T, typename Enable = void> |
| 226 | struct ctype |
| 227 | {}; |
| 228 | |
| 229 | // class case |
| 230 | template<typename T> |
| 231 | struct ctype<T, |
| 232 | typename if_valid_type<typename std::decay<T>::type::BaseObjectType>::type> |
| 233 | { |
| 234 | typedef typename std::remove_reference<T>::type CppType; |
| 235 | // make sure; avoid subclassed cases |
| 236 | static_assert(is_wrapper<CppType>::value || is_boxed<CppType>::value, |
| 237 | "must be object or boxed wrapper"); |
| 238 | typedef typename CppType::BaseObjectType CType; |
| 239 | typedef typename std::conditional<std::is_const<CppType>::value, const CType, |
| 240 | CType>::type *type; |
| 241 | }; |
| 242 | |
| 243 | // remaining cases |
| 244 | template<typename T> |
| 245 | struct ctype<T, typename if_valid_type< |
| 246 | typename repository::declare_ctype_of<T>::type>::type> |
| 247 | { |
| 248 | typedef typename repository::declare_ctype_of<T>::type CType; |
| 249 | typedef typename std::conditional<std::is_const<T>::value, const CType, |
| 250 | CType>::type type; |
| 251 | }; |
| 252 | |
| 253 | // basic cases passthrough |
| 254 | template<typename T> |
| 255 | struct ctype<T, |
| 256 | typename std::enable_if<(std::is_fundamental<T>::value && |
| 257 | !std::is_same<T, bool>::value) || |
| 258 | std::is_same<T, gpointer>::value || |
| 259 | std::is_same<T, gconstpointer>::value>::type> |
| 260 | { |
| 261 | typedef T type; |
| 262 | }; |
| 263 | |
| 264 | // ... exception though for bool |
| 265 | template<> |
| 266 | struct ctype<bool, void> |
| 267 | { |
| 268 | typedef gboolean type; |
| 269 | }; |
| 270 | |
| 271 | // as used in callback signatures |
| 272 | // or in list (un)wrapping |
| 273 | template<> |
| 274 | struct ctype<const std::string &, void> |
| 275 | { |
| 276 | typedef const char *type; |
| 277 | }; |
| 278 | template<> |
| 279 | struct ctype<std::string, void> |
| 280 | { |
| 281 | typedef char *type; |
| 282 | }; |
| 283 | |
| 284 | template<typename T1, typename T2> |
| 285 | struct ctype<std::pair<T1, T2>> |
| 286 | { |
| 287 | typedef std::pair<typename ctype<T1>::type, typename ctype<T2>::type> type; |
| 288 | }; |
| 289 | |
| 290 | // conversely |
| 291 | // return corresponding cpp type (if known) |
| 292 | // (string and basic type not considered) |
| 293 | // preserve const |
| 294 | template<typename T, typename Transfer = transfer_full_t, |
| 295 | typename Enable = void> |
| 296 | struct cpptype |
| 297 | {}; |
| 298 | |
| 299 | // generic |
| 300 | template<typename T> |
| 301 | struct cpptype<T *, transfer_full_t, |
| 302 | typename if_valid_type<typename repository::declare_cpptype_of< |
| 303 | typename std::remove_const<T>::type>::type>::type> |
| 304 | { |
| 305 | typedef typename repository::declare_cpptype_of< |
| 306 | typename std::remove_const<T>::type>::type CppType; |
| 307 | typedef typename std::conditional<std::is_const<T>::value, const CppType, |
| 308 | CppType>::type type; |
| 309 | }; |
| 310 | |
| 311 | template<typename T> |
| 312 | struct cpptype<T, transfer_full_t, |
| 313 | typename if_valid_type<typename repository::declare_cpptype_of< |
| 314 | typename std::remove_const<T>::type>::type>::type> |
| 315 | { |
| 316 | typedef typename repository::declare_cpptype_of< |
| 317 | typename std::remove_const<T>::type>::type CppType; |
| 318 | typedef typename std::conditional<std::is_const<T>::value, const CppType, |
| 319 | CppType>::type type; |
| 320 | }; |
| 321 | |
| 322 | // basic cases passthrough |
| 323 | template<typename T> |
| 324 | struct cpptype<T, transfer_full_t, |
| 325 | typename std::enable_if<std::is_fundamental<T>::value || |
| 326 | std::is_same<T, gpointer>::value || |
| 327 | std::is_same<T, gconstpointer>::value>::type> |
| 328 | { |
| 329 | typedef T type; |
| 330 | }; |
| 331 | |
| 332 | #if 0 |
| 333 | template<> |
| 334 | struct cpptype<char *, transfer_full_t> |
| 335 | { |
| 336 | using type = std::string; |
| 337 | }; |
| 338 | #endif |
| 339 | |
| 340 | // handle none transfer case |
| 341 | template<typename T> |
| 342 | struct cpptype<T, transfer_none_t> |
| 343 | { |
| 344 | using CppType = typename cpptype<T, transfer_full_t>::type; |
| 345 | template<typename TT, typename Enable = void> |
| 346 | struct map_type |
| 347 | { |
| 348 | using type = TT; |
| 349 | }; |
| 350 | template<typename TT> |
| 351 | struct map_type<TT, typename if_valid_type<typename TT::ReferenceType>::type> |
| 352 | { |
| 353 | using type = typename TT::ReferenceType; |
| 354 | }; |
| 355 | using type = typename map_type<CppType>::type; |
| 356 | }; |
| 357 | |
| 358 | // map owning box type to corresponding reference box type |
| 359 | template<typename T> |
| 360 | struct reftype |
| 361 | { |
| 362 | typedef typename T::ReferenceType type; |
| 363 | }; |
| 364 | |
| 365 | } // namespace traits |
| 366 | |
| 367 | // specify transfer type when (un)wrapping |
| 368 | // this approach is safer than some booleans and allows overload combinations |
| 369 | struct transfer_t |
| 370 | { |
| 371 | const int value; |
| 372 | constexpr explicit transfer_t(int v = 0) : value(v) {} |
| 373 | }; |
| 374 | struct transfer_none_t : public transfer_t |
| 375 | { |
| 376 | constexpr transfer_none_t() : transfer_t(0) {} |
| 377 | }; |
| 378 | struct transfer_full_t : public transfer_t |
| 379 | { |
| 380 | constexpr transfer_full_t() : transfer_t(1) {} |
| 381 | }; |
| 382 | struct transfer_container_t : public transfer_t |
| 383 | { |
| 384 | constexpr transfer_container_t() : transfer_t(2) {} |
| 385 | }; |
| 386 | |
| 387 | GI_MODULE_INLINE const constexpr transfer_t transfer_dummy = transfer_t(); |
| 388 | GI_MODULE_INLINE const constexpr transfer_none_t transfer_none = |
| 389 | transfer_none_t(); |
| 390 | GI_MODULE_INLINE const constexpr transfer_full_t transfer_full = |
| 391 | transfer_full_t(); |
| 392 | GI_MODULE_INLINE const constexpr transfer_container_t transfer_container = |
| 393 | transfer_container_t(); |
| 394 | |
| 395 | template<typename Transfer> |
| 396 | struct element_transfer |
| 397 | {}; |
| 398 | template<> |
| 399 | struct element_transfer<transfer_none_t> |
| 400 | { |
| 401 | typedef transfer_none_t type; |
| 402 | }; |
| 403 | template<> |
| 404 | struct element_transfer<transfer_full_t> |
| 405 | { |
| 406 | typedef transfer_full_t type; |
| 407 | }; |
| 408 | template<> |
| 409 | struct element_transfer<transfer_container_t> |
| 410 | { |
| 411 | typedef transfer_none_t type; |
| 412 | }; |
| 413 | |
| 414 | // unwrapping a callback |
| 415 | // specify call scope type |
| 416 | struct scope_t |
| 417 | { |
| 418 | const int value; |
| 419 | constexpr explicit scope_t(int v = 0) : value(v) {} |
| 420 | }; |
| 421 | struct scope_call_t : public scope_t |
| 422 | { |
| 423 | constexpr scope_call_t() : scope_t(0) {} |
| 424 | }; |
| 425 | struct scope_async_t : public scope_t |
| 426 | { |
| 427 | constexpr scope_async_t() : scope_t(1) {} |
| 428 | }; |
| 429 | struct scope_notified_t : public scope_t |
| 430 | { |
| 431 | constexpr scope_notified_t() : scope_t(2) {} |
| 432 | }; |
| 433 | |
| 434 | GI_MODULE_INLINE const constexpr scope_t scope_dummy = scope_t(); |
| 435 | GI_MODULE_INLINE const constexpr scope_call_t scope_call = scope_call_t(); |
| 436 | GI_MODULE_INLINE const constexpr scope_async_t scope_async = scope_async_t(); |
| 437 | GI_MODULE_INLINE const constexpr scope_notified_t scope_notified = |
| 438 | scope_notified_t(); |
| 439 | |
| 440 | // (dummy) helper tag to aid in overload resolution |
| 441 | template<typename Interface> |
| 442 | struct interface_tag |
| 443 | { |
| 444 | typedef Interface type; |
| 445 | }; |
| 446 | |
| 447 | // CallArgs minimal helper type |
| 448 | // only provide what is needed |
| 449 | // not intended for general use, even though not in internal namespace |
| 450 | template<typename T> |
| 451 | class required |
| 452 | { |
| 453 | T data_; |
| 454 | |
| 455 | public: |
| 456 | constexpr required(T v) : data_(std::move(v)) {} |
| 457 | constexpr operator T &() &noexcept { return data_; } |
| 458 | constexpr operator T &&() &&noexcept { return std::move(data_); } |
| 459 | constexpr T &value() &noexcept { return data_; } |
| 460 | constexpr T &&value() &&noexcept { return std::move(data_); } |
| 461 | }; |
| 462 | |
| 463 | // helper tag type to aid in overload resolution of CallArgs |
| 464 | // this is used as the first argument of a (non-plain) signature variant |
| 465 | // (since it may otherwise have only 1 struct argument, like the plain variant) |
| 466 | template<typename... CA_TAG> |
| 467 | using ca = std::tuple<CA_TAG...>; |
| 468 | |
| 469 | struct ca_tag |
| 470 | {}; |
| 471 | struct ca_in_tag : public ca_tag |
| 472 | {}; |
| 473 | struct ca_bc_tag : public ca_tag |
| 474 | {}; |
| 475 | // convenience abbreviation |
| 476 | using ca_in = ca<ca_in_tag>; |
| 477 | |
| 478 | #if GI_DL |
| 479 | namespace detail |
| 480 | { |
| 481 | // dynamic load of symbol |
| 482 | inline void * |
| 483 | load_symbol(const std::vector<const char *> libs, const char *symbol) |
| 484 | { |
| 485 | void *s = nullptr; |
| 486 | for (const auto &l : libs) { |
| 487 | auto h = dlopen(l, RTLD_LAZY | RTLD_GLOBAL | RTLD_NODELETE); |
| 488 | if (h) { |
| 489 | s = dlsym(h, symbol); |
| 490 | dlclose(h); |
| 491 | if (s) |
| 492 | break; |
| 493 | } |
| 494 | } |
| 495 | return s; |
| 496 | } |
| 497 | |
| 498 | } // namespace detail |
| 499 | #endif // GI_DL |
| 500 | |
| 501 | } // namespace gi |
| 502 | |
| 503 | #endif // GI_BASE_HPP |