| 1 | #ifndef GI_CONTAINER_HPP |
| 2 | #define GI_CONTAINER_HPP |
| 3 | |
| 4 | #include "base.hpp" |
| 5 | #include "wrap.hpp" |
| 6 | |
| 7 | GI_MODULE_EXPORT |
| 8 | namespace gi |
| 9 | { |
| 10 | namespace detail |
| 11 | { |
| 12 | // helpers |
| 13 | |
| 14 | // element unwrap cast helper; |
| 15 | // element reference type depends on container reference type |
| 16 | // lvalue |
| 17 | template<typename T> |
| 18 | T |
| 19 | cast_ref(T &&el, std::true_type) |
| 20 | { |
| 21 | return el; |
| 22 | } |
| 23 | // rvalue |
| 24 | template<typename T> |
| 25 | typename std::remove_reference<T>::type && |
| 26 | cast_ref(T &&el, std::false_type) |
| 27 | { |
| 28 | return std::move(el); |
| 29 | } |
| 30 | |
| 31 | // std::iterator may be deprecated, but it is still handy |
| 32 | // so replicate it here to avoid deprecation warnings |
| 33 | template<typename Category, typename T, typename Distance = std::ptrdiff_t, |
| 34 | typename Pointer = T *, typename Reference = T &> |
| 35 | struct iterator |
| 36 | { |
| 37 | using iterator_category = Category; |
| 38 | using value_type = T; |
| 39 | using difference_type = Distance; |
| 40 | using pointer = Pointer; |
| 41 | using reference = Reference; |
| 42 | }; |
| 43 | namespace _traits |
| 44 | { |
| 45 | template<typename T> |
| 46 | struct hash |
| 47 | {}; |
| 48 | |
| 49 | template<typename T> |
| 50 | struct equal |
| 51 | {}; |
| 52 | |
| 53 | template<typename CType> |
| 54 | void |
| 55 | _destroy(CType v) |
| 56 | { |
| 57 | // destroy by wrapping a temporary with full transfer |
| 58 | // and then simply dropping that temporary |
| 59 | wrap(v, transfer_full); |
| 60 | } |
| 61 | |
| 62 | // C type case |
| 63 | template<typename CType, typename Enable = void> |
| 64 | struct destroy |
| 65 | { |
| 66 | static constexpr auto func = _destroy<CType>; |
| 67 | }; |
| 68 | |
| 69 | // Cpp type case |
| 70 | template<typename CppType> |
| 71 | struct destroy<CppType, typename traits::if_valid_type<typename traits::ctype< |
| 72 | typename std::decay<CppType>::type>::type>::type> |
| 73 | { |
| 74 | using CType = |
| 75 | typename traits::ctype<typename std::decay<CppType>::type>::type; |
| 76 | static constexpr auto func = _destroy<CType>; |
| 77 | }; |
| 78 | |
| 79 | // let's simply stick to the following for now |
| 80 | // since others are not so likely and not always clear semantics |
| 81 | template<> |
| 82 | struct hash<std::string> |
| 83 | { |
| 84 | static constexpr GHashFunc func = g_str_hash; |
| 85 | }; |
| 86 | |
| 87 | template<> |
| 88 | struct hash<gi::cstring> |
| 89 | { |
| 90 | static constexpr GHashFunc func = g_str_hash; |
| 91 | }; |
| 92 | |
| 93 | template<> |
| 94 | struct hash<gi::cstring_v> |
| 95 | { |
| 96 | static constexpr GHashFunc func = g_str_hash; |
| 97 | }; |
| 98 | |
| 99 | template<> |
| 100 | struct equal<std::string> |
| 101 | { |
| 102 | static constexpr GEqualFunc func = g_str_equal; |
| 103 | }; |
| 104 | |
| 105 | template<> |
| 106 | struct equal<gi::cstring> |
| 107 | { |
| 108 | static constexpr GEqualFunc func = g_str_equal; |
| 109 | }; |
| 110 | |
| 111 | template<> |
| 112 | struct equal<gi::cstring_v> |
| 113 | { |
| 114 | static constexpr GEqualFunc func = g_str_equal; |
| 115 | }; |
| 116 | |
| 117 | } // namespace _traits |
| 118 | |
| 119 | ///////////////// |
| 120 | // |
| 121 | //////////////// |
| 122 | |
| 123 | // helper check/trait |
| 124 | namespace trait |
| 125 | { |
| 126 | std::false_type is_initializer_list_dispatch(...); |
| 127 | template<typename T> |
| 128 | std::true_type is_initializer_list_dispatch(std::initializer_list<T> *); |
| 129 | template<typename T> |
| 130 | struct is_initializer_list |
| 131 | : decltype(is_initializer_list_dispatch(static_cast<T *>(nullptr))) |
| 132 | {}; |
| 133 | |
| 134 | } // namespace trait |
| 135 | |
| 136 | // helper; combine some traits |
| 137 | template<typename T, typename Transfer> |
| 138 | using elcpptype = traits::cpptype<T, typename element_transfer<Transfer>::type>; |
| 139 | |
| 140 | // hard convert/cast of a pointer to a C type to its wrapper type |
| 141 | // (which is expected of same size) |
| 142 | template<typename Transfer, typename CType, |
| 143 | typename CppType = typename elcpptype<CType, Transfer>::type> |
| 144 | CppType * |
| 145 | wrap_cast(CType *c) |
| 146 | { |
| 147 | static_assert(sizeof(*c) == sizeof(std::declval<CppType>()), ""); |
| 148 | return reinterpret_cast<CppType *>(c); |
| 149 | } |
| 150 | |
| 151 | // snippet to disable a method for transfer none case |
| 152 | #define GI_DISABLE_METHOD_NONE \ |
| 153 | template<typename Enable = void, \ |
| 154 | typename Check = typename std::enable_if< \ |
| 155 | std::is_void<Enable>::value && \ |
| 156 | !std::is_same<Transfer, transfer_none_t>::value>::type> |
| 157 | |
| 158 | template<typename ListType, typename ElType, typename Transfer, |
| 159 | typename Enable = void> |
| 160 | class CollectionBase |
| 161 | {}; |
| 162 | |
| 163 | template<typename ListType, typename ElCType, typename Transfer> |
| 164 | struct list_ops; |
| 165 | |
| 166 | struct GPtrArrayFuncs |
| 167 | { |
| 168 | constexpr static bool refcnt = true; |
| 169 | using value_type = gpointer; |
| 170 | static GType get_type_() { return G_TYPE_PTR_ARRAY; } |
| 171 | static ::GPtrArray *ref(::GPtrArray *data) |
| 172 | { |
| 173 | return data ? g_ptr_array_ref(data) : data; |
| 174 | } |
| 175 | static ::GPtrArray *sink(::GPtrArray *data) |
| 176 | { |
| 177 | return data ? g_ptr_array_ref(data) : data; |
| 178 | } |
| 179 | static void free(::GPtrArray *&data) |
| 180 | { |
| 181 | if (data) { |
| 182 | g_ptr_array_unref(data); |
| 183 | data = nullptr; |
| 184 | } |
| 185 | } |
| 186 | static ::GPtrArray *float_(::GPtrArray *data) { return data; } |
| 187 | }; |
| 188 | |
| 189 | template<typename ElCType, typename Transfer> |
| 190 | struct list_ops<::GPtrArray, ElCType, Transfer> : public GPtrArrayFuncs |
| 191 | {}; |
| 192 | |
| 193 | template<typename ElCType, typename Transfer> |
| 194 | class CollectionBase<::GPtrArray, ElCType, Transfer> |
| 195 | : public Wrapper<GPtrArrayFuncs, ::GPtrArray> |
| 196 | { |
| 197 | protected: |
| 198 | using list_ops = GPtrArrayFuncs; |
| 199 | |
| 200 | using state_type = int; // dummy |
| 201 | state_type _create() |
| 202 | { |
| 203 | if (!this->data_) { |
| 204 | auto &l = this->data_; |
| 205 | l = g_ptr_array_new(); |
| 206 | if (std::is_same<Transfer, transfer_full_t>::value) |
| 207 | g_ptr_array_set_free_func( |
| 208 | l, (GDestroyNotify)_traits::destroy<ElCType>::func); |
| 209 | } |
| 210 | return 0; |
| 211 | } |
| 212 | |
| 213 | void _finish() {} |
| 214 | |
| 215 | state_type _push(state_type s, list_ops::value_type item) |
| 216 | { |
| 217 | g_ptr_array_add(this->data_, item); |
| 218 | return s; |
| 219 | } |
| 220 | |
| 221 | struct iterator_type |
| 222 | { |
| 223 | CollectionBase *self; |
| 224 | int index; |
| 225 | |
| 226 | bool next(list_ops::value_type &val) |
| 227 | { |
| 228 | if (self->data_ && (index < (int)self->data_->len)) { |
| 229 | val = g_ptr_array_index(self->data_, index++); |
| 230 | return true; |
| 231 | } |
| 232 | return false; |
| 233 | } |
| 234 | }; |
| 235 | |
| 236 | iterator_type _iterator() { return iterator_type{this, 0}; } |
| 237 | |
| 238 | void _steal() |
| 239 | { |
| 240 | #if GLIB_CHECK_VERSION(2, 64, 0) |
| 241 | g_free(g_ptr_array_steal(this->data_, nullptr)); |
| 242 | #else |
| 243 | // nasty, essentially above function |
| 244 | // but works for code of the past, which no longer changes |
| 245 | auto parray = (GPtrArray *)this->data_; |
| 246 | g_free(parray->pdata); |
| 247 | parray->pdata = 0; |
| 248 | parray->len = 0; |
| 249 | #endif |
| 250 | g_ptr_array_unref(this->data_); |
| 251 | this->data_ = nullptr; |
| 252 | } |
| 253 | |
| 254 | public: |
| 255 | using value_type = typename elcpptype<ElCType, Transfer>::type; |
| 256 | |
| 257 | protected: |
| 258 | value_type *_cast() const |
| 259 | { |
| 260 | return wrap_cast<Transfer>((ElCType *)(this->data_->pdata)); |
| 261 | } |
| 262 | |
| 263 | GI_DISABLE_METHOD_NONE |
| 264 | void _ensure_array() { _create(); } |
| 265 | |
| 266 | public: |
| 267 | // assume same layout of wrapper and wrappee |
| 268 | static_assert(sizeof(value_type) == sizeof(gpointer), ""); |
| 269 | static_assert(sizeof(value_type[2]) == sizeof(gpointer[2]), ""); |
| 270 | |
| 271 | using iterator = value_type *; |
| 272 | using const_iterator = const value_type *; |
| 273 | |
| 274 | const_iterator cbegin() const { return this->data_ ? _cast() : nullptr; } |
| 275 | const_iterator cend() const |
| 276 | { |
| 277 | return this->data_ ? begin() + this->data_->len : nullptr; |
| 278 | } |
| 279 | |
| 280 | const_iterator begin() const { return cbegin(); } |
| 281 | const_iterator end() const { return cend(); } |
| 282 | |
| 283 | GI_DISABLE_METHOD_NONE |
| 284 | iterator begin() { return this->data_ ? _cast() : nullptr; } |
| 285 | GI_DISABLE_METHOD_NONE iterator end() |
| 286 | { |
| 287 | return this->data_ ? begin() + this->data_->len : nullptr; |
| 288 | } |
| 289 | |
| 290 | size_t size() const noexcept { return this->data_ ? this->data_->len : 0; } |
| 291 | bool empty() const noexcept { return size() == 0; } |
| 292 | |
| 293 | GI_DISABLE_METHOD_NONE |
| 294 | void push_front(const value_type &v) { insert(begin(), v); } |
| 295 | GI_DISABLE_METHOD_NONE |
| 296 | void push_front(value_type &&v) { insert(begin(), std::move(v)); } |
| 297 | |
| 298 | GI_DISABLE_METHOD_NONE |
| 299 | void pop_front() { erase(begin()); } |
| 300 | |
| 301 | GI_DISABLE_METHOD_NONE |
| 302 | void push_back(const value_type &v) { insert(end(), v); } |
| 303 | GI_DISABLE_METHOD_NONE |
| 304 | void push_back(value_type &&v) { insert(end(), std::move(v)); } |
| 305 | |
| 306 | GI_DISABLE_METHOD_NONE |
| 307 | void pop_back() |
| 308 | { |
| 309 | if (!empty()) |
| 310 | erase(end() - 1); |
| 311 | } |
| 312 | |
| 313 | GI_DISABLE_METHOD_NONE |
| 314 | iterator erase(const_iterator pos) { return erase(pos, pos + 1); } |
| 315 | |
| 316 | GI_DISABLE_METHOD_NONE |
| 317 | iterator erase(const_iterator first, const_iterator last) |
| 318 | { |
| 319 | if (this->data_ && first < last) { |
| 320 | auto index = first - begin(); |
| 321 | auto cnt = last - first; |
| 322 | // erase from back to front to minimize (or even avoid) data moves |
| 323 | while (cnt) { |
| 324 | --cnt; |
| 325 | auto v = g_ptr_array_steal_index(this->data_, index + cnt); |
| 326 | _traits::destroy<ElCType>::func(ElCType(v)); |
| 327 | } |
| 328 | return begin() + index; |
| 329 | } |
| 330 | return begin(); |
| 331 | } |
| 332 | |
| 333 | GI_DISABLE_METHOD_NONE |
| 334 | iterator insert(const_iterator pos, const value_type &v) |
| 335 | { |
| 336 | auto cv = unwrap(v, Transfer()); |
| 337 | return _insert(pos, cv); |
| 338 | } |
| 339 | GI_DISABLE_METHOD_NONE |
| 340 | iterator insert(const_iterator pos, value_type &&v) |
| 341 | { |
| 342 | auto cv = unwrap(std::move(v), Transfer()); |
| 343 | return _insert(pos, cv); |
| 344 | } |
| 345 | |
| 346 | // erase to avoid memory free |
| 347 | GI_DISABLE_METHOD_NONE |
| 348 | void clear() { erase(begin(), end()); } |
| 349 | |
| 350 | protected: |
| 351 | iterator _insert(const_iterator pos, gpointer v) |
| 352 | { |
| 353 | // establish index before potential create |
| 354 | auto index = pos - begin(); |
| 355 | _ensure_array(); |
| 356 | g_ptr_array_insert(this->data_, index, v); |
| 357 | return begin() + index; |
| 358 | } |
| 359 | }; |
| 360 | |
| 361 | struct GHashTableFuncs |
| 362 | { |
| 363 | constexpr static bool refcnt = true; |
| 364 | using value_type = std::pair<gpointer, gpointer>; |
| 365 | static GType get_type_() { return G_TYPE_HASH_TABLE; } |
| 366 | static ::GHashTable *ref(::GHashTable *data) |
| 367 | { |
| 368 | return data ? g_hash_table_ref(data) : data; |
| 369 | } |
| 370 | static ::GHashTable *sink(::GHashTable *data) |
| 371 | { |
| 372 | return data ? g_hash_table_ref(data) : data; |
| 373 | } |
| 374 | static void free(::GHashTable *&data) |
| 375 | { |
| 376 | if (data) { |
| 377 | g_hash_table_unref(data); |
| 378 | data = nullptr; |
| 379 | } |
| 380 | } |
| 381 | static ::GHashTable *float_(::GHashTable *data) { return data; } |
| 382 | }; |
| 383 | |
| 384 | template<typename ElCType, typename Transfer> |
| 385 | struct list_ops<::GHashTable, ElCType, Transfer> : public GHashTableFuncs |
| 386 | {}; |
| 387 | |
| 388 | template<typename ElCType, typename Transfer> |
| 389 | class CollectionBase<::GHashTable, ElCType, Transfer> |
| 390 | : public Wrapper<GHashTableFuncs, ::GHashTable> |
| 391 | { |
| 392 | protected: |
| 393 | using list_ops = GHashTableFuncs; |
| 394 | using ElCKeyType = typename std::tuple_element<0, ElCType>::type; |
| 395 | using ElCMappedType = typename std::tuple_element<1, ElCType>::type; |
| 396 | using ElCppKeyType = typename elcpptype<ElCKeyType, Transfer>::type; |
| 397 | using ElCppMappedType = typename elcpptype<ElCMappedType, Transfer>::type; |
| 398 | |
| 399 | static_assert(sizeof(ElCKeyType) == sizeof(gpointer), ""); |
| 400 | static_assert(sizeof(ElCMappedType) == sizeof(gpointer), ""); |
| 401 | |
| 402 | using key_type = const ElCppKeyType; |
| 403 | using mapped_type = ElCppMappedType; |
| 404 | using value_type = std::pair<ElCppKeyType, mapped_type>; |
| 405 | using const_value_type = std::pair<const ElCppKeyType, const mapped_type>; |
| 406 | |
| 407 | using state_type = int; // dummy |
| 408 | state_type _create() |
| 409 | { |
| 410 | if (!this->data_) { |
| 411 | if (std::is_same<Transfer, transfer_full_t>::value) { |
| 412 | this->data_ = g_hash_table_new_full(_traits::hash<ElCppKeyType>::func, |
| 413 | _traits::equal<ElCppKeyType>::func, |
| 414 | GDestroyNotify(_traits::destroy<ElCppKeyType>::func), |
| 415 | GDestroyNotify(_traits::destroy<ElCppMappedType>::func)); |
| 416 | } else { |
| 417 | this->data_ = g_hash_table_new(_traits::hash<ElCppKeyType>::func, |
| 418 | _traits::equal<ElCppKeyType>::func); |
| 419 | } |
| 420 | } |
| 421 | return 0; |
| 422 | } |
| 423 | |
| 424 | state_type _push(state_type s, list_ops::value_type item) |
| 425 | { |
| 426 | g_hash_table_replace(this->data_, item.first, item.second); |
| 427 | return s; |
| 428 | } |
| 429 | |
| 430 | void _finish() {} |
| 431 | |
| 432 | struct iterator_type |
| 433 | { |
| 434 | CollectionBase &self; |
| 435 | GHashTableIter iter; |
| 436 | int index{}; |
| 437 | |
| 438 | iterator_type(CollectionBase &_self) : self(_self) |
| 439 | { |
| 440 | if (self.data_) |
| 441 | g_hash_table_iter_init(&iter, self.data_); |
| 442 | } |
| 443 | |
| 444 | bool next(list_ops::value_type &val) |
| 445 | { |
| 446 | if (!self.data_) |
| 447 | return false; |
| 448 | gpointer key, value; |
| 449 | auto more = g_hash_table_iter_next(&iter, &key, &value); |
| 450 | val = {key, value}; |
| 451 | return more; |
| 452 | } |
| 453 | }; |
| 454 | |
| 455 | iterator_type _iterator() { return iterator_type{*this}; } |
| 456 | |
| 457 | void _steal() |
| 458 | { |
| 459 | g_hash_table_steal_all(this->data_); |
| 460 | g_hash_table_unref(this->data_); |
| 461 | this->data_ = nullptr; |
| 462 | } |
| 463 | |
| 464 | public: |
| 465 | class const_iterator |
| 466 | : public detail::iterator<std::input_iterator_tag, const_value_type> |
| 467 | { |
| 468 | using self_type = const_iterator; |
| 469 | |
| 470 | enum State { NONE, INIT, ITERATING, DONE }; |
| 471 | |
| 472 | ::GHashTableIter iter; |
| 473 | std::pair<gpointer, gpointer> kv; |
| 474 | State state; |
| 475 | |
| 476 | const_value_type *_cast() |
| 477 | { |
| 478 | // sanity check in view of rough cast |
| 479 | static_assert(sizeof(kv) == sizeof(const_value_type), ""); |
| 480 | return reinterpret_cast<const_value_type *>(&kv); |
| 481 | } |
| 482 | |
| 483 | ::GHashTable *_table() const |
| 484 | { |
| 485 | return state == NONE || state == DONE |
| 486 | ? nullptr |
| 487 | : g_hash_table_iter_get_hash_table( |
| 488 | const_cast<::GHashTableIter *>(&iter)); |
| 489 | } |
| 490 | |
| 491 | public: |
| 492 | const_iterator( |
| 493 | ::GHashTable *table, gpointer key = nullptr, gpointer value = nullptr) |
| 494 | : state(NONE) |
| 495 | { |
| 496 | if (table) { |
| 497 | g_hash_table_iter_init(&iter, table); |
| 498 | state = INIT; |
| 499 | // if iterator is created as a result of a find, |
| 500 | // then we arrange for lazy sync of hash table iterator |
| 501 | if (G_LIKELY(!key && !value)) { |
| 502 | state = ITERATING; |
| 503 | ++(*this); |
| 504 | } else { |
| 505 | kv = {key, value}; |
| 506 | } |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | const_value_type &operator*() { return *_cast(); } |
| 511 | const_value_type *operator->() { return _cast(); } |
| 512 | |
| 513 | bool operator==(const self_type &other) |
| 514 | { |
| 515 | auto t = _table(); |
| 516 | return (t == other._table()) && (!t || (kv.first == other.kv.first)); |
| 517 | } |
| 518 | bool operator!=(const self_type &other) { return !(*this == other); } |
| 519 | |
| 520 | self_type &operator++() |
| 521 | { |
| 522 | bool has_more = true; |
| 523 | // if needed, sync iterator with key |
| 524 | if (G_UNLIKELY(state != ITERATING)) { |
| 525 | gpointer key{}, value{}; |
| 526 | while (has_more && key != kv.first) { |
| 527 | has_more = g_hash_table_iter_next(&iter, &key, &value); |
| 528 | } |
| 529 | } |
| 530 | if (G_LIKELY(has_more)) |
| 531 | has_more = g_hash_table_iter_next(&iter, &kv.first, &kv.second); |
| 532 | state = has_more ? ITERATING : DONE; |
| 533 | return *this; |
| 534 | } |
| 535 | }; |
| 536 | |
| 537 | // consistency |
| 538 | using iterator = const_iterator; |
| 539 | |
| 540 | const_iterator cbegin() const { return {this->data_}; } |
| 541 | const_iterator cend() const { return {nullptr}; } |
| 542 | |
| 543 | const_iterator begin() const { return cbegin(); }; |
| 544 | const_iterator end() const { return cend(); }; |
| 545 | |
| 546 | size_t size() const |
| 547 | { |
| 548 | return this->data_ ? g_hash_table_size(this->data_) : 0; |
| 549 | } |
| 550 | bool empty() const { return size() == 0; } |
| 551 | |
| 552 | GI_DISABLE_METHOD_NONE |
| 553 | void clear() |
| 554 | { |
| 555 | if (this->data_) |
| 556 | g_hash_table_remove_all(this->data_); |
| 557 | }; |
| 558 | |
| 559 | size_t count(key_type &key) const |
| 560 | { |
| 561 | if (!this->data_) |
| 562 | return 0; |
| 563 | auto v = unwrap(key, transfer_none); |
| 564 | return g_hash_table_contains(this->data_, v) ? 1 : 0; |
| 565 | } |
| 566 | |
| 567 | const_iterator find(key_type &key) const |
| 568 | { |
| 569 | if (this->data_) { |
| 570 | auto v = unwrap(key, transfer_none); |
| 571 | gpointer lv; |
| 572 | if (g_hash_table_lookup_extended(this->data_, v, nullptr, &lv)) |
| 573 | return {this->data_, gpointer(v), gpointer(lv)}; |
| 574 | } |
| 575 | return end(); |
| 576 | } |
| 577 | |
| 578 | typename elcpptype<ElCMappedType, transfer_none_t>::type lookup( |
| 579 | key_type &key) const |
| 580 | { |
| 581 | auto it = find(key); |
| 582 | if (it == end()) |
| 583 | return {}; |
| 584 | // decay copy should be possible |
| 585 | return it->second; |
| 586 | } |
| 587 | |
| 588 | GI_DISABLE_METHOD_NONE |
| 589 | bool replace(ElCppKeyType &&key, ElCppMappedType &&value) |
| 590 | { |
| 591 | _create(); |
| 592 | auto kv = unwrap(std::move(key), Transfer()); |
| 593 | auto vv = unwrap(std::move(value), Transfer()); |
| 594 | return g_hash_table_replace(this->data_, kv, vv); |
| 595 | } |
| 596 | |
| 597 | GI_DISABLE_METHOD_NONE |
| 598 | size_t erase(key_type &key) |
| 599 | { |
| 600 | if (this->data_) { |
| 601 | auto v = unwrap(key, transfer_none); |
| 602 | return g_hash_table_remove(this->data_, gpointer(v)) ? 1 : 0; |
| 603 | } |
| 604 | return 0; |
| 605 | } |
| 606 | }; |
| 607 | |
| 608 | template<typename ListType> |
| 609 | struct ListFuncs |
| 610 | { |
| 611 | constexpr static bool refcnt = false; |
| 612 | using value_type = gpointer; |
| 613 | // this value type is used in e.g. signal argument |
| 614 | static GType get_type_() { return G_TYPE_POINTER; } |
| 615 | static ListType *ref(ListType *data) { return data; } |
| 616 | }; |
| 617 | |
| 618 | template<typename ElCType, typename Transfer> |
| 619 | struct list_ops<::GList, ElCType, Transfer> : public ListFuncs<::GList> |
| 620 | {}; |
| 621 | |
| 622 | template<typename ElCType, typename Transfer> |
| 623 | struct list_ops<::GSList, ElCType, Transfer> : public ListFuncs<::GSList> |
| 624 | {}; |
| 625 | |
| 626 | template<typename ListType> |
| 627 | struct LinkedListOps; |
| 628 | |
| 629 | template<> |
| 630 | struct LinkedListOps<::GSList> |
| 631 | { |
| 632 | static void free(::GSList *l) { g_slist_free(l); } |
| 633 | static void free_full(::GSList *l, GDestroyNotify func) |
| 634 | { |
| 635 | g_slist_free_full(l, func); |
| 636 | } |
| 637 | static ::GSList *append(::GSList *l, gpointer data) |
| 638 | { |
| 639 | return g_slist_append(l, data); |
| 640 | } |
| 641 | static ::GSList *prepend(::GSList *l, gpointer data) |
| 642 | { |
| 643 | return g_slist_prepend(l, data); |
| 644 | } |
| 645 | static ::GSList *last(::GSList *l) { return g_slist_last(l); } |
| 646 | static ::GSList *insert_before(::GSList *l, ::GSList *link, gpointer data) |
| 647 | { |
| 648 | return g_slist_insert_before(l, link, data); |
| 649 | } |
| 650 | static ::GSList *delete_link(::GSList *l, ::GSList *link) |
| 651 | { |
| 652 | return g_slist_delete_link(l, link); |
| 653 | } |
| 654 | static ::GSList *reverse(::GSList *l) { return g_slist_reverse(l); } |
| 655 | }; |
| 656 | |
| 657 | template<> |
| 658 | struct LinkedListOps<::GList> |
| 659 | { |
| 660 | static void free(::GList *l) { g_list_free(l); } |
| 661 | static void free_full(::GList *l, GDestroyNotify func) |
| 662 | { |
| 663 | return g_list_free_full(l, func); |
| 664 | } |
| 665 | static ::GList *append(::GList *l, gpointer data) |
| 666 | { |
| 667 | return g_list_append(l, data); |
| 668 | } |
| 669 | static ::GList *prepend(::GList *l, gpointer data) |
| 670 | { |
| 671 | return g_list_prepend(l, data); |
| 672 | } |
| 673 | static ::GList *last(::GList *l) { return g_list_last(l); } |
| 674 | static ::GList *insert_before(::GList *l, ::GList *link, gpointer data) |
| 675 | { |
| 676 | return g_list_insert_before(l, link, data); |
| 677 | } |
| 678 | static ::GList *delete_link(::GList *l, ::GList *link) |
| 679 | { |
| 680 | return g_list_delete_link(l, link); |
| 681 | } |
| 682 | static ::GList *reverse(::GList *l) { return g_list_reverse(l); } |
| 683 | }; |
| 684 | |
| 685 | template<typename ListType, typename ElType, typename Transfer> |
| 686 | struct LinkedListBase; |
| 687 | |
| 688 | template<typename ListType, typename ElCType> |
| 689 | struct LinkedListBase<ListType, ElCType, transfer_none_t> |
| 690 | : public SharedWrapper<ListType> |
| 691 | { |
| 692 | protected: |
| 693 | static void _deleter(ListType *&d, ...) { d = nullptr; } |
| 694 | static ListType *_copy(ListType *d) { return d; } |
| 695 | }; |
| 696 | |
| 697 | template<typename ListType, typename ElCType> |
| 698 | struct LinkedListBase<ListType, ElCType, transfer_container_t> |
| 699 | : public SharedWrapper<ListType> |
| 700 | { |
| 701 | protected: |
| 702 | static void _deleter(ListType *&l, ...) |
| 703 | { |
| 704 | LinkedListOps<ListType>::free(l); |
| 705 | l = nullptr; |
| 706 | } |
| 707 | }; |
| 708 | |
| 709 | template<typename ListType, typename ElCType> |
| 710 | struct LinkedListBase<ListType, ElCType, transfer_full_t> |
| 711 | : public SharedWrapper<ListType> |
| 712 | { |
| 713 | protected: |
| 714 | static void _deleter(ListType *&l, ...) |
| 715 | { |
| 716 | LinkedListOps<ListType>::free_full( |
| 717 | l, (GDestroyNotify)_traits::destroy<ElCType>::func); |
| 718 | l = nullptr; |
| 719 | } |
| 720 | }; |
| 721 | |
| 722 | // enable copy constructor on none transfer cases |
| 723 | // compiler otherwise always selects the deleted one, even if other options |
| 724 | template<typename Transfer, typename Base> |
| 725 | using SelectBaseWrapper = |
| 726 | typename std::conditional<std::is_same<Transfer, transfer_none_t>::value, |
| 727 | CopyWrapper<Base>, MoveWrapper<Base>>::type; |
| 728 | |
| 729 | template<typename ListType, typename ElCType, typename Transfer> |
| 730 | class CollectionBase<ListType, ElCType, Transfer, |
| 731 | typename std::enable_if<std::is_same<ListType, ::GSList>::value || |
| 732 | std::is_same<ListType, ::GList>::value>::type> |
| 733 | : public SelectBaseWrapper<Transfer, |
| 734 | LinkedListBase<ListType, ElCType, Transfer>> |
| 735 | { |
| 736 | protected: |
| 737 | using list_ops = ListFuncs<ListType>; |
| 738 | using state_type = ListType *; |
| 739 | |
| 740 | state_type _create() { return nullptr; } |
| 741 | |
| 742 | state_type _push(state_type, typename list_ops::value_type item) |
| 743 | { |
| 744 | this->data_ = LinkedListOps<ListType>::prepend(this->data_, item); |
| 745 | return nullptr; |
| 746 | } |
| 747 | |
| 748 | void _finish() |
| 749 | { |
| 750 | this->data_ = LinkedListOps<ListType>::reverse(this->data_); |
| 751 | } |
| 752 | |
| 753 | struct iterator_type |
| 754 | { |
| 755 | ListType *it; |
| 756 | bool next(typename list_ops::value_type &val) |
| 757 | { |
| 758 | if (it) { |
| 759 | val = it->data; |
| 760 | it = it->next; |
| 761 | return true; |
| 762 | } |
| 763 | return false; |
| 764 | } |
| 765 | }; |
| 766 | |
| 767 | iterator_type _iterator() { return {this->data_}; } |
| 768 | |
| 769 | void _steal() |
| 770 | { |
| 771 | LinkedListOps<ListType>::free(this->data_); |
| 772 | this->data_ = nullptr; |
| 773 | } |
| 774 | |
| 775 | static void _destroy(gpointer value) |
| 776 | { |
| 777 | _traits::destroy<ElCType>::func(ElCType(value)); |
| 778 | } |
| 779 | |
| 780 | public: |
| 781 | using value_type = typename elcpptype<ElCType, Transfer>::type; |
| 782 | static constexpr bool is_glist = std::is_same<ListType, ::GList>::value; |
| 783 | |
| 784 | template<typename value_t> |
| 785 | class iterator_t : public detail::iterator<std::forward_iterator_tag, value_t> |
| 786 | { |
| 787 | using self_type = iterator_t; |
| 788 | friend class CollectionBase; |
| 789 | ListType *it = nullptr; |
| 790 | |
| 791 | public: |
| 792 | iterator_t(ListType *l = nullptr) : it(l){}; |
| 793 | |
| 794 | // construct const from non-const |
| 795 | template<typename other_value_t, |
| 796 | typename Enable = typename std::enable_if< |
| 797 | std::is_convertible<other_value_t *, value_t *>::value>::type> |
| 798 | iterator_t(const iterator_t<other_value_t> &oi) : iterator_t(oi.it) |
| 799 | {} |
| 800 | |
| 801 | value_t &operator*() { return *operator->(); } |
| 802 | |
| 803 | value_t *operator->() |
| 804 | { |
| 805 | return wrap_cast<Transfer>((ElCType *)(&it->data)); |
| 806 | } |
| 807 | |
| 808 | bool operator==(const self_type &other) { return it == other.it; } |
| 809 | |
| 810 | bool operator!=(const self_type &other) { return !(*this == other); } |
| 811 | |
| 812 | self_type &operator++() |
| 813 | { |
| 814 | if (it) |
| 815 | it = it->next; |
| 816 | return *this; |
| 817 | } |
| 818 | |
| 819 | self_type operator++(int) |
| 820 | { |
| 821 | auto ret = *this; |
| 822 | ++(*this); |
| 823 | return ret; |
| 824 | } |
| 825 | }; |
| 826 | |
| 827 | using const_iterator = iterator_t<const value_type>; |
| 828 | using iterator = iterator_t<value_type>; |
| 829 | |
| 830 | const_iterator begin() const { return cbegin(); } |
| 831 | const_iterator end() const { return cend(); } |
| 832 | |
| 833 | const_iterator cbegin() const { return {this->data_}; } |
| 834 | const_iterator cend() const { return {}; } |
| 835 | |
| 836 | GI_DISABLE_METHOD_NONE |
| 837 | iterator begin() { return {this->data_}; } |
| 838 | GI_DISABLE_METHOD_NONE |
| 839 | iterator end() { return {}; } |
| 840 | |
| 841 | bool empty() const noexcept { return !this->data_; } |
| 842 | |
| 843 | GI_DISABLE_METHOD_NONE |
| 844 | void push_front(const value_type &v) |
| 845 | { |
| 846 | auto cv = unwrap(v, Transfer()); |
| 847 | this->data_ = LinkedListOps<ListType>::prepend(this->data_, cv); |
| 848 | } |
| 849 | GI_DISABLE_METHOD_NONE |
| 850 | void push_front(value_type &&v) |
| 851 | { |
| 852 | auto cv = unwrap(std::move(v), Transfer()); |
| 853 | this->data_ = LinkedListOps<ListType>::prepend(this->data_, cv); |
| 854 | } |
| 855 | |
| 856 | GI_DISABLE_METHOD_NONE |
| 857 | void pop_front() |
| 858 | { |
| 859 | if (this->data_) |
| 860 | _destroy(this->data_->data); |
| 861 | this->data_ = |
| 862 | LinkedListOps<ListType>::delete_link(this->data_, this->data_); |
| 863 | } |
| 864 | |
| 865 | GI_DISABLE_METHOD_NONE |
| 866 | void push_back(const value_type &v) |
| 867 | { |
| 868 | auto cv = unwrap(v, Transfer()); |
| 869 | this->data_ = LinkedListOps<ListType>::append(this->data_, cv); |
| 870 | } |
| 871 | GI_DISABLE_METHOD_NONE |
| 872 | void push_back(value_type &&v) |
| 873 | { |
| 874 | auto cv = unwrap(std::move(v), Transfer()); |
| 875 | this->data_ = LinkedListOps<ListType>::append(this->data_, cv); |
| 876 | } |
| 877 | |
| 878 | GI_DISABLE_METHOD_NONE |
| 879 | void pop_back() |
| 880 | { |
| 881 | auto last = LinkedListOps<ListType>::last(this->data_); |
| 882 | if (last) |
| 883 | _destroy(last->data); |
| 884 | this->data_ = LinkedListOps<ListType>::delete_link(this->data_, last); |
| 885 | } |
| 886 | |
| 887 | GI_DISABLE_METHOD_NONE |
| 888 | iterator erase(const_iterator pos) |
| 889 | { |
| 890 | ListType *next = nullptr; |
| 891 | if (pos.it) { |
| 892 | next = pos.it->next; |
| 893 | _destroy(pos.it->data); |
| 894 | } |
| 895 | this->data_ = LinkedListOps<ListType>::delete_link(this->data_, pos.it); |
| 896 | return {next}; |
| 897 | } |
| 898 | |
| 899 | template<typename Enable = void, |
| 900 | typename Check = typename std::enable_if< |
| 901 | std::is_void<Enable>::value && Transfer().value && is_glist>::type> |
| 902 | iterator insert(const_iterator pos, const value_type &v) |
| 903 | { |
| 904 | auto cv = unwrap(v, Transfer()); |
| 905 | return _insert(pos, cv); |
| 906 | } |
| 907 | template<typename Enable = void, |
| 908 | typename Check = typename std::enable_if< |
| 909 | std::is_void<Enable>::value && Transfer().value && is_glist>::type> |
| 910 | iterator insert(const_iterator pos, value_type &&v) |
| 911 | { |
| 912 | auto cv = unwrap(std::move(v), Transfer()); |
| 913 | return _insert(pos, cv); |
| 914 | } |
| 915 | |
| 916 | GI_DISABLE_METHOD_NONE |
| 917 | void reverse() |
| 918 | { |
| 919 | this->data_ = LinkedListOps<ListType>::reverse(this->data_); |
| 920 | } |
| 921 | |
| 922 | GI_DISABLE_METHOD_NONE |
| 923 | void clear() |
| 924 | { |
| 925 | this->_deleter(this->data_); |
| 926 | this->data_ = nullptr; |
| 927 | } |
| 928 | |
| 929 | protected: |
| 930 | iterator _insert(const_iterator pos, gpointer v) |
| 931 | { |
| 932 | this->data_ = |
| 933 | LinkedListOps<ListType>::insert_before(this->data_, pos.it, v); |
| 934 | return {pos.it ? pos.it->prev : LinkedListOps<ListType>::last(this->data_)}; |
| 935 | } |
| 936 | }; |
| 937 | |
| 938 | enum SpanType { |
| 939 | DYNAMIC = -1, |
| 940 | ZT = 0, |
| 941 | }; |
| 942 | |
| 943 | template<int SIZE> |
| 944 | struct Span |
| 945 | {}; |
| 946 | |
| 947 | template<typename ElCType, typename Transfer, int SIZE> |
| 948 | struct SpanBase : public SharedWrapper<ElCType> |
| 949 | { |
| 950 | protected: |
| 951 | // extra members needed in this case |
| 952 | // only > 0 if data locally allocated (as opposed to from elsewhere) |
| 953 | size_t capacity_ = 0; |
| 954 | size_t size_ = 0; |
| 955 | |
| 956 | static void _deleter(decltype(SpanBase::data_) &d, SpanBase *self) |
| 957 | { |
| 958 | assert(self); |
| 959 | if (std::is_same<Transfer, transfer_full_t>::value && d) { |
| 960 | auto end = d + self->size_; |
| 961 | for (auto p = d; p != end; ++p) { |
| 962 | _traits::destroy<ElCType>::func(*p); |
| 963 | } |
| 964 | } |
| 965 | if (!std::is_same<Transfer, transfer_none_t>::value) |
| 966 | g_free(d); |
| 967 | d = nullptr; |
| 968 | } |
| 969 | |
| 970 | template<typename Enable = void> |
| 971 | static decltype(SpanBase::data_) _copy(decltype(SpanBase::data_) d) |
| 972 | { |
| 973 | // should only end up used in none case |
| 974 | static_assert(std::is_same<Transfer, transfer_none_t>::value, ""); |
| 975 | return d; |
| 976 | } |
| 977 | }; |
| 978 | |
| 979 | template<typename ElCType, typename Transfer, int SIZE> |
| 980 | struct list_ops<Span<SIZE>, ElCType, Transfer> : public ListFuncs<ElCType> |
| 981 | {}; |
| 982 | |
| 983 | template<typename ElCType, typename Transfer, int SIZE> |
| 984 | class CollectionBase<Span<SIZE>, ElCType, Transfer> |
| 985 | : public SelectBaseWrapper<Transfer, SpanBase<ElCType, Transfer, SIZE>> |
| 986 | { |
| 987 | protected: |
| 988 | static auto constexpr SPAN_SIZE = SIZE; |
| 989 | struct list_ops : public ListFuncs<ElCType> |
| 990 | { |
| 991 | using value_type = ElCType; |
| 992 | }; |
| 993 | using state_type = ElCType *; |
| 994 | |
| 995 | // used by wrap |
| 996 | CollectionBase(ElCType *p = nullptr, int s = 0) |
| 997 | { |
| 998 | // NOTE no copy needed; |
| 999 | // either transfer is full/container (and ownership is assumed) |
| 1000 | // either transfer is none (and will never free/release) |
| 1001 | this->data_ = p; |
| 1002 | this->capacity_ = 0; |
| 1003 | // normalize -1 from C side to avoid potential surprises elsewhere |
| 1004 | if (s < 0 || SIZE == ZT) { |
| 1005 | s = 0; |
| 1006 | if (p) { |
| 1007 | while (*p) { |
| 1008 | ++s; |
| 1009 | ++p; |
| 1010 | } |
| 1011 | } |
| 1012 | } |
| 1013 | this->size_ = s; |
| 1014 | } |
| 1015 | |
| 1016 | ElCType *_create() |
| 1017 | { |
| 1018 | if (!this->data_) { |
| 1019 | this->capacity_ = SIZE > 0 ? SIZE : 2; |
| 1020 | this->data_ = (ElCType *)g_malloc0(this->capacity_ * sizeof(ElCType)); |
| 1021 | this->size_ = 0; |
| 1022 | } |
| 1023 | return this->data_; |
| 1024 | } |
| 1025 | |
| 1026 | void _create_from(ElCType *d, size_t lsize) |
| 1027 | { |
| 1028 | using T = ElCType; |
| 1029 | // try to re-use incoming data |
| 1030 | // otherwise fall back to plain copy |
| 1031 | // (if transfer or zero-termination fiddling is needed) |
| 1032 | bool allocate = (Transfer().value != transfer_none.value) || (SIZE == ZT); |
| 1033 | auto size = SIZE == ZT ? lsize + 1 : lsize; |
| 1034 | auto *data = d; |
| 1035 | |
| 1036 | if (allocate) { |
| 1037 | data = (T *)g_malloc(size * sizeof(T)); |
| 1038 | this->capacity_ = size; |
| 1039 | memcpy(data, d, lsize * sizeof(T)); |
| 1040 | if (SIZE == ZT) |
| 1041 | data[lsize] = T{}; |
| 1042 | } |
| 1043 | |
| 1044 | // track data |
| 1045 | assert(!this->data_); |
| 1046 | g_free(this->data_); |
| 1047 | this->data_ = data; |
| 1048 | this->size_ = lsize; |
| 1049 | } |
| 1050 | |
| 1051 | void _reserve(size_t capacity) |
| 1052 | { |
| 1053 | if (this->capacity_ < capacity + (SIZE == ZT)) { |
| 1054 | this->capacity_ = |
| 1055 | std::max(std::max(2 * this->capacity_, size_t(2)), capacity); |
| 1056 | this->data_ = |
| 1057 | (ElCType *)g_realloc_n(this->data_, this->capacity_, sizeof(ElCType)); |
| 1058 | } |
| 1059 | } |
| 1060 | |
| 1061 | state_type _push(state_type s, ElCType item) |
| 1062 | { |
| 1063 | // should only get here if previously allocated |
| 1064 | assert(this->capacity_ > 0); |
| 1065 | auto offset = s - this->data_; |
| 1066 | _reserve(offset + 1); |
| 1067 | s = this->data_ + offset; |
| 1068 | *s = item; |
| 1069 | ++this->size_; |
| 1070 | ++s; |
| 1071 | // ensure ZT if applicable |
| 1072 | if (SIZE == ZT) { |
| 1073 | assert(size_t(s - this->data_) < this->capacity_); |
| 1074 | *s = ElCType{}; |
| 1075 | } |
| 1076 | return s; |
| 1077 | } |
| 1078 | |
| 1079 | void _finish() |
| 1080 | { |
| 1081 | if ((SIZE > 0) && (this->size_ != (size_t)SIZE)) |
| 1082 | g_error("fixed list size mismatch"); |
| 1083 | } |
| 1084 | |
| 1085 | struct iterator_type |
| 1086 | { |
| 1087 | CollectionBase &self; |
| 1088 | ElCType *p; |
| 1089 | |
| 1090 | iterator_type(CollectionBase &s) : self(s), p(self.data_) {} |
| 1091 | |
| 1092 | explicit operator bool() |
| 1093 | { |
| 1094 | return p && (ZT ? !!*p : (p != self.data_ + self.size_)); |
| 1095 | }; |
| 1096 | |
| 1097 | bool next(ElCType &val) |
| 1098 | { |
| 1099 | if (*this) { |
| 1100 | val = *(p++); |
| 1101 | return true; |
| 1102 | } |
| 1103 | return false; |
| 1104 | } |
| 1105 | }; |
| 1106 | |
| 1107 | iterator_type _iterator() { return {*this}; } |
| 1108 | |
| 1109 | void _steal() |
| 1110 | { |
| 1111 | // if Holder is trying to destroy, check if we really allocated |
| 1112 | // (may have optimized array case) |
| 1113 | if (!std::is_same<Transfer, transfer_none_t>::value || this->capacity_) |
| 1114 | g_free(this->data_); |
| 1115 | this->data_ = nullptr; |
| 1116 | this->size_ = this->capacity_ = 0; |
| 1117 | }; |
| 1118 | |
| 1119 | public: |
| 1120 | // this one is used by code generation (the unwrap size part) |
| 1121 | // so it should only be used in specific situations |
| 1122 | // let's add checks to make it so |
| 1123 | size_t _size() const |
| 1124 | { |
| 1125 | static_assert(SIZE != ZT, ""); |
| 1126 | return this->size_; |
| 1127 | } |
| 1128 | |
| 1129 | using value_type = typename elcpptype<ElCType, Transfer>::type; |
| 1130 | |
| 1131 | // assume same layout of wrapper and wrappee |
| 1132 | static_assert(sizeof(value_type) == sizeof(ElCType), ""); |
| 1133 | static_assert(sizeof(value_type[2]) == sizeof(ElCType[2]), ""); |
| 1134 | |
| 1135 | using iterator = value_type *; |
| 1136 | using const_iterator = const value_type *; |
| 1137 | |
| 1138 | const_iterator cbegin() const |
| 1139 | { |
| 1140 | return this->data_ ? wrap_cast<Transfer>(this->data_) : nullptr; |
| 1141 | } |
| 1142 | const_iterator cend() const |
| 1143 | { |
| 1144 | return this->data_ ? begin() + this->size_ : nullptr; |
| 1145 | } |
| 1146 | |
| 1147 | const_iterator begin() const { return cbegin(); } |
| 1148 | const_iterator end() const { return cend(); } |
| 1149 | |
| 1150 | GI_DISABLE_METHOD_NONE |
| 1151 | iterator begin() |
| 1152 | { |
| 1153 | return this->data_ ? wrap_cast<Transfer>(this->data_) : nullptr; |
| 1154 | } |
| 1155 | GI_DISABLE_METHOD_NONE |
| 1156 | iterator end() { return this->data_ ? begin() + this->size_ : nullptr; } |
| 1157 | |
| 1158 | size_t size() const { return this->size_; } |
| 1159 | bool empty() const { return size() == 0; } |
| 1160 | |
| 1161 | GI_DISABLE_METHOD_NONE |
| 1162 | void push_front(const value_type &v) { insert(begin(), v); } |
| 1163 | GI_DISABLE_METHOD_NONE |
| 1164 | void push_front(value_type &&v) { insert(begin(), std::move(v)); } |
| 1165 | |
| 1166 | GI_DISABLE_METHOD_NONE |
| 1167 | void pop_front() { erase(begin()); } |
| 1168 | |
| 1169 | GI_DISABLE_METHOD_NONE |
| 1170 | void push_back(const value_type &v) { insert(end(), v); } |
| 1171 | GI_DISABLE_METHOD_NONE |
| 1172 | void push_back(value_type &&v) { insert(end(), std::move(v)); } |
| 1173 | |
| 1174 | GI_DISABLE_METHOD_NONE |
| 1175 | void pop_back() |
| 1176 | { |
| 1177 | if (!empty()) |
| 1178 | erase(end() - 1); |
| 1179 | } |
| 1180 | |
| 1181 | GI_DISABLE_METHOD_NONE |
| 1182 | iterator erase(const_iterator pos) { return erase(pos, pos + 1); } |
| 1183 | |
| 1184 | GI_DISABLE_METHOD_NONE |
| 1185 | iterator erase(const_iterator first, const_iterator last) |
| 1186 | { |
| 1187 | if (this->data_ && this->size_ > 0 && first < last) { |
| 1188 | size_t index = first - begin(); |
| 1189 | size_t cnt = last - first; |
| 1190 | size_t lindex = std::min(index + cnt, this->size_); |
| 1191 | for (auto i = index; i < lindex; ++i) |
| 1192 | _traits::destroy<ElCType>::func(this->data_[index]); |
| 1193 | if (lindex < this->size_) |
| 1194 | memmove(this->data_ + index, this->data_ + lindex, |
| 1195 | sizeof(*this->data_) * (this->size_ - lindex)); |
| 1196 | this->size_ -= cnt; |
| 1197 | if (SIZE == ZT) |
| 1198 | this->data_[this->size_] = ElCType{}; |
| 1199 | return begin() + index; |
| 1200 | } |
| 1201 | return begin(); |
| 1202 | } |
| 1203 | |
| 1204 | GI_DISABLE_METHOD_NONE |
| 1205 | iterator insert(const_iterator pos, const value_type &v) |
| 1206 | { |
| 1207 | auto cv = unwrap(v, Transfer()); |
| 1208 | return _insert(pos, cv); |
| 1209 | } |
| 1210 | GI_DISABLE_METHOD_NONE |
| 1211 | iterator insert(const_iterator pos, value_type &&v) |
| 1212 | { |
| 1213 | auto cv = unwrap(std::move(v), Transfer()); |
| 1214 | return _insert(pos, cv); |
| 1215 | } |
| 1216 | |
| 1217 | size_t capacity() const { return this->capacity_; } |
| 1218 | |
| 1219 | GI_DISABLE_METHOD_NONE |
| 1220 | void reserve(size_t capacity) { _reserve(capacity); } |
| 1221 | |
| 1222 | // erase to avoid memory free |
| 1223 | GI_DISABLE_METHOD_NONE |
| 1224 | void clear() { erase(begin(), end()); } |
| 1225 | |
| 1226 | protected: |
| 1227 | iterator _insert(const_iterator pos, ElCType v) |
| 1228 | { |
| 1229 | // establish index before potential create |
| 1230 | size_t index = pos - begin(); |
| 1231 | // grow if needed |
| 1232 | _reserve(this->size_ + 1); |
| 1233 | // move if needed |
| 1234 | if (index < this->size_) |
| 1235 | memmove(this->data_ + index + 1, this->data_ + index, |
| 1236 | sizeof(*this->data_) * (this->size_ - index)); |
| 1237 | // insert |
| 1238 | ++this->size_; |
| 1239 | this->data_[index] = v; |
| 1240 | // ensure ZT if applicable |
| 1241 | if (SIZE == ZT) { |
| 1242 | assert(this->size_ < this->capacity_); |
| 1243 | this->data_[this->size_] = ElCType{}; |
| 1244 | } |
| 1245 | return begin() + index; |
| 1246 | } |
| 1247 | }; |
| 1248 | |
| 1249 | // class tag |
| 1250 | class Container |
| 1251 | {}; |
| 1252 | |
| 1253 | // minor helper, essentially compile-time signal/notification |
| 1254 | template<typename T> |
| 1255 | struct Notifier |
| 1256 | { |
| 1257 | static constexpr bool construct_none = false; |
| 1258 | static void _updated(T *, bool /*create*/) {} |
| 1259 | }; |
| 1260 | |
| 1261 | // considered owning for container or full |
| 1262 | // unwrap will have to release_() |
| 1263 | template<typename ListType, typename T, typename Transfer = transfer_full_t, |
| 1264 | typename Notify = Notifier<void>, typename ExtraBase = Container> |
| 1265 | struct Collection : public CollectionBase<ListType, T, Transfer>, |
| 1266 | public ExtraBase |
| 1267 | { |
| 1268 | protected: |
| 1269 | using self_type = Collection; |
| 1270 | using super_type = CollectionBase<ListType, T, Transfer>; |
| 1271 | |
| 1272 | public: |
| 1273 | using list_ops = typename super_type::list_ops; |
| 1274 | |
| 1275 | protected: |
| 1276 | using ElTransfer = typename element_transfer<Transfer>::type; |
| 1277 | |
| 1278 | // decompose pair in case of map |
| 1279 | template<typename G> |
| 1280 | struct get_types |
| 1281 | { |
| 1282 | // map to harmless type if no key applicable |
| 1283 | using key_type = bool; |
| 1284 | using mapped_type = G; |
| 1285 | using cpp_type = typename traits::cpptype<G, ElTransfer>::type; |
| 1286 | }; |
| 1287 | |
| 1288 | template<typename T1, typename T2> |
| 1289 | struct get_types<std::pair<T1, T2>> |
| 1290 | { |
| 1291 | using key_type = T1; |
| 1292 | using mapped_type = T2; |
| 1293 | using cpp_type = std::pair<typename traits::cpptype<T1, ElTransfer>::type, |
| 1294 | typename traits::cpptype<T2, ElTransfer>::type>; |
| 1295 | }; |
| 1296 | |
| 1297 | using KeyCType = typename get_types<T>::key_type; |
| 1298 | using ElCType = typename get_types<T>::mapped_type; |
| 1299 | |
| 1300 | using KeyCppType = typename traits::cpptype<KeyCType, ElTransfer>::type; |
| 1301 | using ElCppType = typename traits::cpptype<ElCType, ElTransfer>::type; |
| 1302 | |
| 1303 | using ValueCppType = typename get_types<T>::cpp_type; |
| 1304 | |
| 1305 | // sanity/consistency checks |
| 1306 | // expect normalized template types |
| 1307 | static_assert( |
| 1308 | std::is_same<ListType, typename std::decay<ListType>::type>::value, ""); |
| 1309 | static_assert( |
| 1310 | std::is_same<KeyCppType, typename std::decay<KeyCppType>::type>::value, |
| 1311 | ""); |
| 1312 | static_assert( |
| 1313 | std::is_same<ElCppType, typename std::decay<ElCppType>::type>::value, ""); |
| 1314 | |
| 1315 | // internal wrap of list-case |
| 1316 | Collection(ListType *l, bool own) |
| 1317 | { |
| 1318 | this->data_ = ((!own && l) ? list_ops::ref(l) : l); |
| 1319 | } |
| 1320 | |
| 1321 | // internal wrap of span case |
| 1322 | // (also avoid conflict with a public constructor below) |
| 1323 | template<typename Enable = void> |
| 1324 | Collection(ElCType *d, size_t s, std::nullptr_t) : super_type(d, s) |
| 1325 | {} |
| 1326 | |
| 1327 | template<typename T1, typename T2, typename LVR> |
| 1328 | std::pair<gpointer, gpointer> unwrap_item(std::pair<T1, T2> &el, LVR) |
| 1329 | { |
| 1330 | // NOTE if compiler error occurs here, perhaps std::move() input |
| 1331 | auto item1 = unwrap(cast_ref(el.first, LVR()), ElTransfer()); |
| 1332 | static_assert(std::is_pointer<decltype(item1)>::value, "expected pointer"); |
| 1333 | auto item2 = unwrap(cast_ref(el.second, LVR()), ElTransfer()); |
| 1334 | static_assert(std::is_pointer<decltype(item2)>::value, "expected pointer"); |
| 1335 | return {(gpointer)item1, (gpointer)item2}; |
| 1336 | }; |
| 1337 | |
| 1338 | template<typename T1, typename LVR> |
| 1339 | typename list_ops::value_type unwrap_item(T1 &el, LVR) |
| 1340 | { |
| 1341 | // NOTE if compiler error occurs here, perhaps std::move() input |
| 1342 | auto item = unwrap(cast_ref(el, LVR()), ElTransfer()); |
| 1343 | // only force pointers or same size to same size |
| 1344 | using item_type = decltype(item); |
| 1345 | static_assert( |
| 1346 | std::is_pointer<item_type>::value == |
| 1347 | std::is_pointer<typename list_ops::value_type>::value && |
| 1348 | sizeof(item_type) == sizeof(typename list_ops::value_type), |
| 1349 | "expected pointer or matched size"); |
| 1350 | return (typename list_ops::value_type)item; |
| 1351 | } |
| 1352 | |
| 1353 | template<typename Iterator, typename LVR> |
| 1354 | void create_from(Iterator first, Iterator last, LVR) |
| 1355 | { |
| 1356 | // empty container is not null, so always init/create |
| 1357 | // NOTE should also add leading/initial zero if needed |
| 1358 | Notify::_updated(this, true); |
| 1359 | auto s = this->_create(); |
| 1360 | while (first != last) { |
| 1361 | auto &el = *first; |
| 1362 | auto item = unwrap_item(el, LVR()); |
| 1363 | s = this->_push(s, item); |
| 1364 | ++first; |
| 1365 | } |
| 1366 | // possible (size) check might be done here |
| 1367 | this->_finish(); |
| 1368 | } |
| 1369 | |
| 1370 | // array as surrogate generic container |
| 1371 | template<typename InputT> |
| 1372 | void create_from_array(InputT *d, size_t s, std::false_type) |
| 1373 | { |
| 1374 | create_from(d, d + s, std::true_type()); |
| 1375 | } |
| 1376 | |
| 1377 | // array shortcut case; from plain array to matching span |
| 1378 | template<typename InputT> |
| 1379 | void create_from_array(InputT *d, size_t s, std::true_type) |
| 1380 | { |
| 1381 | // should be around as this should be Span case |
| 1382 | // add cast to avoid const issues etc |
| 1383 | using v_type = typename list_ops::value_type; |
| 1384 | static_assert(sizeof(v_type) == sizeof(InputT), ""); |
| 1385 | Notify::_updated(this, true); |
| 1386 | this->_create_from((v_type *)d, s); |
| 1387 | } |
| 1388 | |
| 1389 | template<typename Out, typename Enable = void> |
| 1390 | struct get_value_type |
| 1391 | { |
| 1392 | using type = typename Out::container_type::value_type; |
| 1393 | }; |
| 1394 | |
| 1395 | template<typename Tp> |
| 1396 | struct get_value_type<Tp *> |
| 1397 | { |
| 1398 | using type = typename std::decay<Tp>::type; |
| 1399 | }; |
| 1400 | |
| 1401 | template<typename OutIterator, typename T1, typename T2> |
| 1402 | void wrap_item_add(std::pair<T1, T2> &el, OutIterator &out) |
| 1403 | { |
| 1404 | using DestType = typename get_value_type<OutIterator>::type; |
| 1405 | // expected to be a pair |
| 1406 | using DestType1 = typename std::tuple_element<0, DestType>::type; |
| 1407 | using DestType2 = typename std::tuple_element<1, DestType>::type; |
| 1408 | static_assert(!(std::is_same<Transfer, transfer_full_t>::value && |
| 1409 | (traits::is_reftype<DestType1>::value || |
| 1410 | traits::is_reftype<DestType2>::value)), |
| 1411 | "full transfer to reftype"); |
| 1412 | auto item1 = wrap((KeyCType)el.first, ElTransfer()); |
| 1413 | auto item2 = wrap((ElCType)el.second, ElTransfer()); |
| 1414 | *out = std::make_pair(std::move(item1), std::move(item2)); |
| 1415 | }; |
| 1416 | |
| 1417 | template<typename OutIterator, typename T1> |
| 1418 | void wrap_item_add(T1 &el, OutIterator &out) |
| 1419 | { |
| 1420 | // validation check; no transfer full to a reftype |
| 1421 | using DestType = typename get_value_type<OutIterator>::type; |
| 1422 | // sanity check that we probably got the right type |
| 1423 | static_assert(std::is_convertible<ElCppType, DestType>::value, ""); |
| 1424 | static_assert(!(std::is_same<Transfer, transfer_full_t>::value && |
| 1425 | traits::is_reftype<DestType>::value), |
| 1426 | "full transfer to reftype"); |
| 1427 | using item_type = typename std::decay<decltype(el)>::type; |
| 1428 | // would also lead to cast failure below, but make it more expressive |
| 1429 | static_assert( |
| 1430 | std::is_pointer<item_type>::value == std::is_pointer<ElCType>::value && |
| 1431 | sizeof(item_type) == sizeof(ElCType), |
| 1432 | "expected pointer or matched size"); |
| 1433 | // could have const char** or so, so let's (const) cast the hard way |
| 1434 | *out = wrap((ElCType)el, ElTransfer()); |
| 1435 | } |
| 1436 | |
| 1437 | public: |
| 1438 | // some common type blurbs |
| 1439 | using value_type = typename super_type::value_type; |
| 1440 | using size_type = std::size_t; |
| 1441 | using difference_type = std::ptrdiff_t; |
| 1442 | using reference = value_type &; |
| 1443 | using const_reference = const value_type &; |
| 1444 | |
| 1445 | static GType get_type_() { return list_ops::get_type_(); } |
| 1446 | |
| 1447 | // used by code generation (for non-plain array case) |
| 1448 | // but could be of general use |
| 1449 | template<typename OutIterator> |
| 1450 | void move_to(OutIterator out) && |
| 1451 | { |
| 1452 | // NOTE assumption; |
| 1453 | // out creates as it goes (e.g. back_inserter) |
| 1454 | // or there is enough output space |
| 1455 | typename list_ops::value_type item; |
| 1456 | auto it = this->_iterator(); |
| 1457 | while (it.next(item)) { |
| 1458 | wrap_item_add(item, out); |
| 1459 | ++out; |
| 1460 | } |
| 1461 | // clear out elements without destroying them if needed |
| 1462 | // (if we pulled their ownership with full transfer) |
| 1463 | constexpr bool steal = std::is_same<ElTransfer, transfer_full_t>::value; |
| 1464 | // provide for equally cleared state in either case |
| 1465 | Notify::_updated(this, false); |
| 1466 | if (steal && this->data_) { |
| 1467 | this->_steal(); |
| 1468 | } else if (this->data_) { |
| 1469 | this->~self_type(); |
| 1470 | this->data_ = nullptr; |
| 1471 | } |
| 1472 | } |
| 1473 | |
| 1474 | public: |
| 1475 | // group a few helpers |
| 1476 | struct _detail |
| 1477 | { |
| 1478 | using DataType = decltype(std::declval<super_type>().gobj_()); |
| 1479 | |
| 1480 | // check if it is possible to create a container |
| 1481 | // a none variant does not assume ownership |
| 1482 | // so it is not in a position to create a collection (with no other owner) |
| 1483 | // however, as usual, the ref types can always own |
| 1484 | // also, allow tweaking this restriction (e.g. for holder subclass below) |
| 1485 | static constexpr bool constructible = |
| 1486 | !std::is_same<Transfer, transfer_none_t>::value || list_ops::refcnt || |
| 1487 | Notify::construct_none; |
| 1488 | |
| 1489 | static constexpr bool is_span = !std::is_class<typename std::decay< |
| 1490 | decltype(*std::declval<super_type>().gobj_())>::type>::value; |
| 1491 | |
| 1492 | template<bool B, typename Enable = void> |
| 1493 | struct span_size_impl : public std::integral_constant<int, 0> |
| 1494 | {}; |
| 1495 | |
| 1496 | template<typename Enable> |
| 1497 | struct span_size_impl<true, Enable> |
| 1498 | : public std::integral_constant<int, |
| 1499 | super_type::SPAN_SIZE >= 0 ? super_type::SPAN_SIZE : 0> |
| 1500 | {}; |
| 1501 | |
| 1502 | static constexpr int span_size = span_size_impl<is_span>::value; |
| 1503 | static constexpr bool is_fixed_span = is_span && span_size > 0; |
| 1504 | |
| 1505 | // check if this is Span<> collection of plain V elements |
| 1506 | // in case none transfer, we can also copy around any typical element |
| 1507 | // (the deref of data_ is so we only end up matching a Span<> case) |
| 1508 | template<typename V> |
| 1509 | using is_plain = typename std::conditional< |
| 1510 | is_span && |
| 1511 | (traits::is_plain<typename std::decay<V>::type>::value || |
| 1512 | std::is_same<Transfer, transfer_none_t>::value) && |
| 1513 | std::is_same<typename list_ops::value_type, |
| 1514 | typename std::decay<V>::type>::value, |
| 1515 | std::true_type, std::false_type>::type; |
| 1516 | |
| 1517 | // check Cpp type compatibility |
| 1518 | // this could be triggered on non-complete V, in which case |
| 1519 | // std::is_convertible is undefined, so guard that approach |
| 1520 | template<typename V, bool Complete = false> |
| 1521 | struct is_acceptable_helper : public std::integral_constant<bool, false> |
| 1522 | {}; |
| 1523 | |
| 1524 | template<typename V> |
| 1525 | struct is_acceptable_helper<V, true> |
| 1526 | : public std::is_convertible<V, ValueCppType> |
| 1527 | {}; |
| 1528 | |
| 1529 | template<typename V> |
| 1530 | using is_acceptable = typename is_acceptable_helper<V, |
| 1531 | traits::is_type_complete<V>::value>::type; |
| 1532 | |
| 1533 | // likewise, deal with const variations on e.g. char** |
| 1534 | // (as char* can be converted to const char*) |
| 1535 | template<typename CType> |
| 1536 | using is_compatible = |
| 1537 | typename std::conditional<std::is_convertible<ElCType, CType>::value, |
| 1538 | std::true_type, std::false_type>::type; |
| 1539 | }; |
| 1540 | |
| 1541 | Collection(std::nullptr_t = nullptr) {} |
| 1542 | |
| 1543 | // a non-owning accepts from all other types |
| 1544 | // (either the other owns or there is another non-owner already) |
| 1545 | template<typename OtherTransfer, |
| 1546 | typename Enable = typename std::enable_if< |
| 1547 | !std::is_same<void, OtherTransfer>::value && |
| 1548 | std::is_same<Transfer, transfer_none_t>::value>::type> |
| 1549 | Collection(const Collection<ListType, T, OtherTransfer> &cother) |
| 1550 | { |
| 1551 | // never mind any const, as usual |
| 1552 | auto &other = const_cast<Collection<ListType, T, OtherTransfer> &>(cother); |
| 1553 | this->data_ = other.gobj_() ? list_ops::ref(other.gobj_()) : nullptr; |
| 1554 | } |
| 1555 | |
| 1556 | // construct from reasonably matching container |
| 1557 | // NOTE may need r-value input for non-movable (content) types |
| 1558 | template<typename Container, |
| 1559 | typename Check = typename std::enable_if< |
| 1560 | _detail::template is_acceptable< |
| 1561 | typename std::decay<Container>::type::value_type>::value && |
| 1562 | _detail::constructible>::type, |
| 1563 | typename Enable = |
| 1564 | typename detail::disable_if_same_or_derived<Collection, Container>> |
| 1565 | Collection(Container &&c) |
| 1566 | { |
| 1567 | create_from( |
| 1568 | std::begin(c), std::end(c), std::is_lvalue_reference<Container>()); |
| 1569 | } |
| 1570 | |
| 1571 | // construct from initializer list |
| 1572 | template<typename InitElement, |
| 1573 | typename Check = typename std::enable_if< |
| 1574 | _detail::template is_acceptable<InitElement>::value && |
| 1575 | _detail::constructible>::type, |
| 1576 | typename Enable = |
| 1577 | typename detail::disable_if_same_or_derived<Collection, Container>> |
| 1578 | Collection(std::initializer_list<InitElement> c) |
| 1579 | { |
| 1580 | // initializer_list iterator is a const iterator, so no moving from |
| 1581 | create_from(std::begin(c), std::end(c), std::true_type()); |
| 1582 | } |
| 1583 | |
| 1584 | // construct from array |
| 1585 | // (these arguments could come from a vector, or span, or ...) |
| 1586 | // NOTE no evident way to mark as r-value/movable-from, |
| 1587 | // so it will just have to do and work out |
| 1588 | template<typename InputT, |
| 1589 | typename Check = typename std::enable_if< |
| 1590 | _detail::template is_acceptable<InputT>::value && |
| 1591 | _detail::constructible>::type> |
| 1592 | Collection(InputT *d, size_t s) |
| 1593 | { |
| 1594 | create_from_array(d, s, typename _detail::template is_plain<InputT>()); |
| 1595 | } |
| 1596 | |
| 1597 | // construct from fixed-size array if this is a fixed-size Span<> case |
| 1598 | // NOTE other notes as above apply |
| 1599 | template<typename InputT, |
| 1600 | typename Check = typename std::enable_if< |
| 1601 | _detail::template is_acceptable<InputT>::value && |
| 1602 | _detail::constructible>::type, |
| 1603 | typename Enable = typename std::enable_if< |
| 1604 | !std::is_same<InputT, void>::value && _detail::is_fixed_span>::type> |
| 1605 | Collection(InputT (&d)[_detail::span_size]) |
| 1606 | { |
| 1607 | create_from_array(d, super_type::SPAN_SIZE, |
| 1608 | typename _detail::template is_plain<InputT>()); |
| 1609 | } |
| 1610 | |
| 1611 | // Avoid std::allocator type; check for const_iterator member. |
| 1612 | // Disable conversion to an std::initializer_list (also has const_iterator). |
| 1613 | // (in C++11 std::vector<T>::operator= is overloaded to take either a |
| 1614 | // std::vector<T> or an std::initializer_list<T>). |
| 1615 | |
| 1616 | // move to container |
| 1617 | // (clear/empty this one) |
| 1618 | template<typename Container, |
| 1619 | typename Check = typename std::enable_if< |
| 1620 | _detail::template is_acceptable< |
| 1621 | typename std::decay<Container>::type::value_type>::value && |
| 1622 | sizeof(typename std::decay<Container>::type::const_iterator) && |
| 1623 | !trait::is_initializer_list< |
| 1624 | typename std::remove_reference<Container>::type>::value>::type> |
| 1625 | operator Container() && |
| 1626 | { |
| 1627 | Container result; |
| 1628 | std::move(*this).move_to(std::inserter(result, result.end())); |
| 1629 | return result; |
| 1630 | } |
| 1631 | |
| 1632 | // generic method(s) |
| 1633 | |
| 1634 | GI_DISABLE_METHOD_NONE |
| 1635 | value_type &front() { return *this->begin(); } |
| 1636 | const value_type &front() const { return *this->begin(); } |
| 1637 | |
| 1638 | #define GI_ENABLE_METHOD_ARRAY_NOT_NONE \ |
| 1639 | template<typename Enable = void, \ |
| 1640 | typename Check = typename std::enable_if< \ |
| 1641 | std::is_void<Enable>::value && \ |
| 1642 | std::is_pointer<typename super_type::const_iterator>::value && \ |
| 1643 | !std::is_same<Transfer, transfer_none_t>::value>::type> |
| 1644 | |
| 1645 | #define GI_ENABLE_METHOD_ARRAY \ |
| 1646 | template<typename Enable = void, \ |
| 1647 | typename Check = typename std::enable_if< \ |
| 1648 | std::is_void<Enable>::value && \ |
| 1649 | std::is_pointer<typename super_type::const_iterator>::value>::type> |
| 1650 | |
| 1651 | GI_ENABLE_METHOD_ARRAY_NOT_NONE |
| 1652 | value_type &back() { return *(this->end() - 1); } |
| 1653 | GI_ENABLE_METHOD_ARRAY |
| 1654 | const value_type &back() const { return *(this->end() - 1); } |
| 1655 | |
| 1656 | GI_ENABLE_METHOD_ARRAY_NOT_NONE |
| 1657 | value_type *data() { return this->begin(); } |
| 1658 | GI_ENABLE_METHOD_ARRAY |
| 1659 | const value_type *data() const { return this->begin(); } |
| 1660 | |
| 1661 | GI_ENABLE_METHOD_ARRAY_NOT_NONE |
| 1662 | value_type &operator[](size_type pos) { return *(this->begin() + pos); } |
| 1663 | GI_ENABLE_METHOD_ARRAY |
| 1664 | const value_type &operator[](size_type pos) const |
| 1665 | { |
| 1666 | return *(this->begin() + pos); |
| 1667 | } |
| 1668 | |
| 1669 | GI_ENABLE_METHOD_ARRAY_NOT_NONE |
| 1670 | value_type &at(size_type pos) |
| 1671 | { |
| 1672 | if (pos >= this->size()) |
| 1673 | try_throw(std::out_of_range("Collection::at")); |
| 1674 | return *(this->begin() + pos); |
| 1675 | } |
| 1676 | GI_ENABLE_METHOD_ARRAY |
| 1677 | const value_type &at(size_type pos) const |
| 1678 | { |
| 1679 | if (pos >= this->size()) |
| 1680 | try_throw(std::out_of_range("Collection::at")); |
| 1681 | return *(this->begin() + pos); |
| 1682 | } |
| 1683 | |
| 1684 | GI_ENABLE_METHOD_ARRAY |
| 1685 | void resize(size_type s) |
| 1686 | { |
| 1687 | auto size = this->size(); |
| 1688 | if (s < size) { |
| 1689 | this->erase(this->begin() + s, this->end()); |
| 1690 | } else if (s > size) { |
| 1691 | auto cnt = s - size; |
| 1692 | while (cnt) { |
| 1693 | this->push_back({}); |
| 1694 | --cnt; |
| 1695 | } |
| 1696 | } |
| 1697 | } |
| 1698 | |
| 1699 | GI_DISABLE_METHOD_NONE |
| 1700 | void swap(Collection &other) |
| 1701 | { |
| 1702 | std::swap((super_type &)*this, (super_type &)other); |
| 1703 | } |
| 1704 | |
| 1705 | #undef GI_ENABLE_METHOD_ARRAY |
| 1706 | #undef GI_ENABLE_METHOD_ARRAY_NOT_NONE |
| 1707 | // in unwrap below; |
| 1708 | // refcnt based cases support all sorts of transfer, |
| 1709 | // otherwise transfer must match the Transfer encoded in type |
| 1710 | |
| 1711 | // r-value case; data can be snatched from this instance |
| 1712 | template<typename ReqTransfer, |
| 1713 | typename std::enable_if<std::is_same<Transfer, ReqTransfer>::value || |
| 1714 | list_ops::refcnt>::type * = nullptr> |
| 1715 | typename _detail::DataType _unwrap(const ReqTransfer &t) && |
| 1716 | { |
| 1717 | auto l = this->data_; |
| 1718 | // be nice to subtype below |
| 1719 | // avoid yanking away possibly owned value |
| 1720 | if (t.value != transfer_none.value) |
| 1721 | this->data_ = nullptr; |
| 1722 | return l; |
| 1723 | } |
| 1724 | |
| 1725 | // l-value case; no snatching, so only transfer none (if not refcnt) |
| 1726 | template<typename ReqTransfer, |
| 1727 | typename std::enable_if< |
| 1728 | list_ops::refcnt || |
| 1729 | (std::is_same<Transfer, ReqTransfer>::value && |
| 1730 | std::is_same<ReqTransfer, transfer_none_t>::value)>::type * = |
| 1731 | nullptr> |
| 1732 | typename _detail::DataType _unwrap(const ReqTransfer &t) & |
| 1733 | { |
| 1734 | if (list_ops::refcnt) |
| 1735 | return ((t.value != transfer_none.value) && this->data_) |
| 1736 | ? list_ops::ref(this->data_) |
| 1737 | : this->data_; |
| 1738 | // must be none transfer in this case |
| 1739 | return this->data_; |
| 1740 | } |
| 1741 | |
| 1742 | template<typename CppType, typename ReqTransfer, |
| 1743 | typename std::enable_if<std::is_same<Transfer, ReqTransfer>::value && |
| 1744 | !_detail::is_span>::type * = nullptr> |
| 1745 | static CppType _wrap(const ListType *obj, const ReqTransfer &t) |
| 1746 | { |
| 1747 | static_assert(sizeof(CppType) == sizeof(self_type), "invalid wrap"); |
| 1748 | static_assert(std::is_base_of<self_type, CppType>::value, "invalid wrap"); |
| 1749 | self_type w(const_cast<ListType *>(obj), t.value); |
| 1750 | return std::move(*static_cast<CppType *>(&w)); |
| 1751 | } |
| 1752 | |
| 1753 | // special case; wrap of 2 arguments (ptr and size) |
| 1754 | // (could be combined in a single span, |
| 1755 | // but it would have to be unpacked anyway) |
| 1756 | template<typename CppType, typename CType, typename ReqTransfer, |
| 1757 | typename std::enable_if< |
| 1758 | std::is_same<Transfer, ReqTransfer>::value && _detail::is_span && |
| 1759 | _detail::template is_compatible<CType>::value>::type * = nullptr> |
| 1760 | static CppType _wrap(CType *obj, int s, const ReqTransfer &) |
| 1761 | { |
| 1762 | static_assert(sizeof(CppType) == sizeof(self_type), "invalid wrap"); |
| 1763 | static_assert(std::is_base_of<self_type, CppType>::value, "invalid wrap"); |
| 1764 | // select internal protected constructor |
| 1765 | self_type w(const_cast<ElCType *>(obj), s, nullptr); |
| 1766 | return std::move(*static_cast<CppType *>(&w)); |
| 1767 | } |
| 1768 | |
| 1769 | // likewise, ZT |
| 1770 | template<typename CppType, typename CType, typename ReqTransfer, |
| 1771 | typename std::enable_if< |
| 1772 | std::is_same<Transfer, ReqTransfer>::value && _detail::is_span && |
| 1773 | _detail::span_size == SpanType::ZT>::type * = nullptr> |
| 1774 | static CppType _wrap(CType *obj, const ReqTransfer &) |
| 1775 | { |
| 1776 | return _wrap<CppType>(obj, -1, ReqTransfer()); |
| 1777 | } |
| 1778 | |
| 1779 | template<typename CppType, |
| 1780 | typename std::enable_if<!std::is_void<CppType>::value && |
| 1781 | list_ops::refcnt>::type * = nullptr> |
| 1782 | static CppType _get_value(const GValue *v) |
| 1783 | { |
| 1784 | auto wv = std::is_same<Transfer, transfer_none_t>::value() |
| 1785 | ? g_value_get_boxed(v) |
| 1786 | : g_value_dup_boxed(v); |
| 1787 | return _wrap<CppType>((ListType *)(wv), Transfer()); |
| 1788 | } |
| 1789 | |
| 1790 | template<typename CppType, |
| 1791 | typename std::enable_if<!std::is_void<CppType>::value && |
| 1792 | !list_ops::refcnt>::type * = nullptr> |
| 1793 | static CppType _get_value(const GValue *v) |
| 1794 | { |
| 1795 | // tracked as raw gpointer |
| 1796 | auto wv = g_value_get_pointer(v); |
| 1797 | return _wrap<CppType>((ElCType *)(wv), Transfer()); |
| 1798 | } |
| 1799 | |
| 1800 | void _set_value(GValue *v) |
| 1801 | { |
| 1802 | if (list_ops::refcnt) { |
| 1803 | g_value_set_boxed(v, this->gobj_()); |
| 1804 | } else { |
| 1805 | g_value_set_pointer(v, this->gobj_()); |
| 1806 | } |
| 1807 | } |
| 1808 | }; |
| 1809 | |
| 1810 | // add additional owner tracking data |
| 1811 | // (see below for reason of simple/silly separate type) |
| 1812 | struct OwnerData : public Container |
| 1813 | { |
| 1814 | bool own_ = false; |
| 1815 | }; |
| 1816 | |
| 1817 | template<typename... Args> |
| 1818 | struct NotifierThunk; |
| 1819 | |
| 1820 | // should only end up used with transfer_none |
| 1821 | template<typename ListType, typename T, typename Transfer = transfer_none_t> |
| 1822 | class CollectionHolder : public Collection<ListType, T, Transfer, |
| 1823 | NotifierThunk<ListType, T, Transfer>, OwnerData> |
| 1824 | { |
| 1825 | // ownership tracked by OwnerData |
| 1826 | // as notified by helper which needs access |
| 1827 | friend struct NotifierThunk<ListType, T, Transfer>; |
| 1828 | // to call here |
| 1829 | void _updated(bool create) |
| 1830 | { |
| 1831 | if (create) { |
| 1832 | this->own_ = true; |
| 1833 | } else { |
| 1834 | this->~CollectionHolder(); |
| 1835 | } |
| 1836 | } |
| 1837 | |
| 1838 | public: |
| 1839 | // NOTE OwnerData is inserted as a lower base class to ensure it is |
| 1840 | // initialized soon enough (before the using'ed constructor will notify |
| 1841 | // of creation) |
| 1842 | using super_type = Collection<ListType, T, Transfer, |
| 1843 | NotifierThunk<ListType, T, Transfer>, OwnerData>; |
| 1844 | |
| 1845 | using super_type::super_type; |
| 1846 | |
| 1847 | // only allowed (limited) move |
| 1848 | // (even if some parents might allow more) |
| 1849 | CollectionHolder(CollectionHolder &&other) : super_type(std::move(other)) |
| 1850 | { |
| 1851 | this->own_ = other.own_; |
| 1852 | other.own_ = false; |
| 1853 | } |
| 1854 | |
| 1855 | CollectionHolder &operator=(CollectionHolder &&other) |
| 1856 | { |
| 1857 | if (this != &other) { |
| 1858 | this->~CollectionHolder(); |
| 1859 | (super_type &)(*this) = std::move(other); |
| 1860 | this->own_ = other.own_; |
| 1861 | other.own_ = false; |
| 1862 | } |
| 1863 | return *this; |
| 1864 | } |
| 1865 | |
| 1866 | ~CollectionHolder() |
| 1867 | { |
| 1868 | // only need to act if super type does not handle (owns) anyway |
| 1869 | // only free/clear list, items not owned |
| 1870 | if (std::is_same<Transfer, transfer_none_t>::value && this->own_ && |
| 1871 | this->data_) { |
| 1872 | this->_steal(); |
| 1873 | this->own_ = false; |
| 1874 | } |
| 1875 | } |
| 1876 | }; |
| 1877 | |
| 1878 | #undef GI_DISABLE_METHOD_NONE |
| 1879 | |
| 1880 | template<typename... Args> |
| 1881 | struct NotifierThunk |
| 1882 | { |
| 1883 | static constexpr bool construct_none = true; |
| 1884 | static void _updated(Container *c, bool create) |
| 1885 | { |
| 1886 | using CT = CollectionHolder<Args...>; |
| 1887 | return static_cast<CT *>(c)->_updated(create); |
| 1888 | } |
| 1889 | }; |
| 1890 | |
| 1891 | struct glib_deleter |
| 1892 | { |
| 1893 | template<typename T> |
| 1894 | void operator()(T *p) |
| 1895 | { |
| 1896 | g_free(p); |
| 1897 | } |
| 1898 | }; |
| 1899 | |
| 1900 | template<typename T> |
| 1901 | using unique_ptr = std::unique_ptr<T, glib_deleter>; |
| 1902 | |
| 1903 | template<typename ListType, typename ElType, typename Transfer, |
| 1904 | typename Enable = void> |
| 1905 | struct ListAcceptorImpl |
| 1906 | { |
| 1907 | using type = Collection<ListType, ElType, Transfer>; |
| 1908 | }; |
| 1909 | |
| 1910 | template<typename ListType, typename ElType, typename Transfer> |
| 1911 | struct ListAcceptorImpl<ListType, ElType, Transfer, |
| 1912 | typename std::enable_if< |
| 1913 | (std::is_same<ListType, ::GList>::value || |
| 1914 | std::is_same<ListType, ::GSList>::value) && |
| 1915 | std::is_same<Transfer, transfer_none_t>::value>::type> |
| 1916 | { |
| 1917 | using type = CollectionHolder<ListType, ElType, Transfer>; |
| 1918 | }; |
| 1919 | |
| 1920 | } // namespace detail |
| 1921 | |
| 1922 | using detail::unwrap; |
| 1923 | |
| 1924 | using ZTSpan = detail::Span<detail::SpanType::ZT>; |
| 1925 | using DSpan = detail::Span<detail::SpanType::DYNAMIC>; |
| 1926 | template<int S> |
| 1927 | using FSpan = detail::Span<S>; |
| 1928 | |
| 1929 | // only the full_transfer version is really safe for general use |
| 1930 | // but let's export it all |
| 1931 | using detail::Collection; |
| 1932 | |
| 1933 | // type to use in (input) parameter declaration |
| 1934 | // use basic type for ref-cases, otherwise need Holder subtype |
| 1935 | // NOTE add a workaround hack; |
| 1936 | // use full transfer for a none transfer HashTable parameter |
| 1937 | // (as annotation there might be bogus; looking at add gst_uri_set_query_table) |
| 1938 | // (and if the annotiation is not bogus, the full should work out ok-ish) |
| 1939 | // NOTE list_ops is used directly hereto avoid instantiation of Collection |
| 1940 | // (as that might otherwise occur during declaration) |
| 1941 | template<typename ListType, typename T, typename Transfer> |
| 1942 | using CollectionParameter = typename std::conditional< |
| 1943 | std::is_same<ListType, ::GHashTable>::value && |
| 1944 | std::is_same<Transfer, transfer_none_t>::value, |
| 1945 | detail::Collection<ListType, T, transfer_full_t>, |
| 1946 | typename std::conditional<detail::list_ops<ListType, T, Transfer>::refcnt, |
| 1947 | detail::Collection<ListType, T, Transfer>, |
| 1948 | detail::CollectionHolder<ListType, T, Transfer>>::type>::type; |
| 1949 | |
| 1950 | namespace traits |
| 1951 | { |
| 1952 | // (many to one) map from Cpp to C type (so no other way around) |
| 1953 | template<typename ListType, typename El, typename Transfer> |
| 1954 | struct ctype<detail::Collection<ListType, El, Transfer>> |
| 1955 | { |
| 1956 | using type = |
| 1957 | typename detail::Collection<ListType, El, Transfer>::_detail::DataType; |
| 1958 | }; |
| 1959 | |
| 1960 | template<typename ListType, typename El, typename Transfer> |
| 1961 | struct ctype<const detail::Collection<ListType, El, Transfer>> |
| 1962 | { |
| 1963 | using type = const typename detail::Collection<ListType, El, |
| 1964 | Transfer>::_detail::DataType; |
| 1965 | }; |
| 1966 | } // namespace traits |
| 1967 | |
| 1968 | } // namespace gi |
| 1969 | |
| 1970 | #endif // GI_CONTAINER_HPP |