1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
|
/** @file
* IPRT - Generic List Class.
*/
/*
* Copyright (C) 2011-2025 Oracle and/or its affiliates.
*
* This file is part of VirtualBox base platform packages, as
* available from https://www.virtualbox.org.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, in version 3 of the
* License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses>.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
* in the VirtualBox distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*
* SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
*/
#ifndef IPRT_INCLUDED_cpp_list_h
#define IPRT_INCLUDED_cpp_list_h
#ifndef RT_WITHOUT_PRAGMA_ONCE
# pragma once
#endif
#include <iprt/cpp/meta.h>
#include <iprt/mem.h>
#include <iprt/string.h> /* for memcpy */
#include <iprt/assert.h>
#include <new> /* For std::bad_alloc */
/** @defgroup grp_rt_cpp_list C++ List support
* @ingroup grp_rt_cpp
*
* @brief Generic C++ list class support.
*
* This list classes manage any amount of data in a fast and easy to use way.
* They have no dependencies on STL, only on generic memory management methods
* of IRPT. This allows list handling in situations where the use of STL
* container classes is forbidden.
*
* Not all of the functionality of STL container classes is implemented. There
* are no iterators or any other high level access/modifier methods (e.g.
* std::algorithms).
*
* The implementation is array based which allows fast access to the items.
* Appending items is usually also fast, cause the internal array is
* preallocated. To minimize the memory overhead, native types (that is
* everything smaller then the size of void*) are directly saved in the array.
* If bigger types are used (e.g. RTCString) the internal array is an array of
* pointers to the objects.
*
* The size of the internal array will usually not shrink, but grow
* automatically. Only certain methods, like RTCList::clear or the "=" operator
* will reset any previously allocated memory. You can call
* RTCList::setCapacity for manual adjustment. If the size of an new list will
* be known, calling the constructor with the necessary capacity will speed up
* the insertion of the new items.
*
* For the full public interface these list classes offer see RTCListBase.
*
* There are some requirements for the types used which follow:
* -# They need a default and a copy constructor.
* -# Some methods (e.g. RTCList::contains) need an equal operator.
* -# If the type is some complex class (that is, having a constructor which
* allocates members on the heap) it has to be greater than sizeof(void*) to
* be used correctly. If this is not the case you can manually overwrite the
* list behavior. Just add T* as a second parameter to the list template if
* your class is called T. Another possibility is to specialize the list for
* your target class. See below for more information.
*
* The native types like int, bool, ptr, ..., are meeting this criteria, so
* they are save to use.
*
* Please note that the return type of some of the getter methods are slightly
* different depending on the list type. Native types return the item by value,
* items with a size greater than sizeof(void*) by reference. As native types
* saved directly in the internal array, returning a reference to them (and
* saving them in a reference as well) would make them invalid (or pointing to
* a wrong item) when the list is changed in the meanwhile. Returning a
* reference for bigger types isn't problematic and makes sure we get out the
* best speed of the list. The one exception to this rule is the index
* operator[]. This operator always return a reference to make it possible to
* use it as a lvalue. Its your responsibility to make sure the list isn't
* changed when using the value as reference returned by this operator.
*
* The list class is reentrant. For a thread-safe variant see RTCMTList.
*
* Implementation details:
* It is possible to specialize any type. This might be necessary to get the
* best speed out of the list. Examples are the 64-bit types, which use the
* native (no pointers) implementation even on a 32-bit host. Consult the
* source code for more details.
*
* Current specialized implementations:
* - int64_t: RTCList<int64_t>
* - uint64_t: RTCList<uint64_t>
*
* @{
*/
/**
* The guard definition.
*/
template <bool G>
class RTCListGuard;
/**
* The default guard which does nothing.
*/
template <>
class RTCListGuard<false>
{
public:
inline void enterRead() const {}
inline void leaveRead() const {}
inline void enterWrite() {}
inline void leaveWrite() {}
/* Define our own new and delete. */
#ifdef RT_NEED_NEW_AND_DELETE
RTMEM_IMPLEMENT_NEW_AND_DELETE();
#else
RTMEMEF_NEW_AND_DELETE_OPERATORS();
#endif
};
/**
* General helper template for managing native values in RTCListBase.
*/
template <typename T1, typename T2>
class RTCListHelper
{
public:
static inline void set(T2 *p, size_t i, const T1 &v) { p[i] = v; }
static inline T1 & at(T2 *p, size_t i) { return p[i]; }
static inline const T1 &atConst(T2 const *p, size_t i) { return p[i]; }
static inline size_t find(T2 *p, const T1 &v, size_t cElements)
{
size_t i = cElements;
while (i-- > 0)
if (p[i] == v)
return i;
return cElements;
}
static inline void copyTo(T2 *p, T2 *const p1 , size_t iTo, size_t cSize)
{
if (cSize > 0)
memcpy(&p[iTo], &p1[0], sizeof(T1) * cSize);
}
static inline void erase(T2 * /* p */, size_t /* i */) { /* Nothing to do here. */ }
static inline void eraseRange(T2 * /* p */, size_t /* cFrom */, size_t /* cSize */) { /* Nothing to do here. */ }
};
/**
* Specialized helper template for managing pointer values in RTCListBase.
*/
template <typename T1>
class RTCListHelper<T1, T1*>
{
public:
static inline void set(T1 **p, size_t i, const T1 &v) { p[i] = new T1(v); }
static inline T1 & at(T1 **p, size_t i) { return *p[i]; }
static inline const T1 &atConst(T1 * const *p, size_t i) { return *p[i]; }
static inline size_t find(T1 **p, const T1 &v, size_t cElements)
{
size_t i = cElements;
while (i-- > 0)
if (*p[i] == v)
return i;
return cElements;
}
static inline void copyTo(T1 **p, T1 **const p1 , size_t iTo, size_t cSize)
{
for (size_t i = 0; i < cSize; ++i)
p[iTo + i] = new T1(*p1[i]);
}
static inline void erase(T1 **p, size_t i) { delete p[i]; }
static inline void eraseRange(T1 **p, size_t iFrom, size_t cItems)
{
while (cItems-- > 0)
delete p[iFrom++];
}
};
/**
* This is the base class for all other list classes. It implements the
* necessary list functionality in a type independent way and offers the public
* list interface to the user.
*/
template <class T, typename ITYPE, bool MT>
class RTCListBase
{
/** @name Traits.
*
* Defines the return type of most of the getter methods. If the internal
* used type is a pointer, we return a reference. If not we return by
* value.
*
* @{
*/
typedef typename RTCIfPtr<ITYPE, T&, T>::result GET_RTYPE;
typedef typename RTCIfPtr<ITYPE, const T&, T>::result GET_CRTYPE;
/** @} */
public:
/**
* Creates a new list.
*
* This preallocates @a cCapacity elements within the list.
*
* @param cCapacity The initial capacity the list has.
* @throws std::bad_alloc
*/
RTCListBase(size_t cCapacity = kDefaultCapacity)
: m_pArray(0)
, m_cElements(0)
, m_cCapacity(0)
{
if (cCapacity > 0)
growArray(cCapacity);
}
/**
* Creates a copy of another list.
*
* The other list will be fully copied and the capacity will be the same as
* the size of the other list.
*
* @param other The list to copy.
* @throws std::bad_alloc
*/
RTCListBase(const RTCListBase<T, ITYPE, MT>& other)
: m_pArray(0)
, m_cElements(0)
, m_cCapacity(0)
{
other.m_guard.enterRead();
size_t const cElementsOther = other.m_cElements;
resizeArrayNoErase(cElementsOther);
RTCListHelper<T, ITYPE>::copyTo(m_pArray, other.m_pArray, 0, cElementsOther);
m_cElements = cElementsOther;
other.m_guard.leaveRead();
}
/**
* Destructor.
*/
~RTCListBase()
{
RTCListHelper<T, ITYPE>::eraseRange(m_pArray, 0, m_cElements);
if (m_pArray)
{
RTMemFree(m_pArray);
m_pArray = NULL;
}
m_cElements = m_cCapacity = 0;
}
/**
* Sets a new capacity within the list.
*
* If the new capacity is bigger than the old size, it will be simply
* preallocated more space for the new items. If the new capacity is
* smaller than the previous size, items at the end of the list will be
* deleted.
*
* @param cCapacity The new capacity within the list.
* @throws std::bad_alloc
*/
void setCapacity(size_t cCapacity)
{
m_guard.enterWrite();
resizeArray(cCapacity);
m_guard.leaveWrite();
}
/**
* Return the current capacity of the list.
*
* @return The actual capacity.
*/
size_t capacity() const
{
m_guard.enterRead();
size_t cRet = m_cCapacity;
m_guard.leaveRead();
return cRet;
}
/**
* Check if an list contains any items.
*
* @return True if there is more than zero items, false otherwise.
*/
bool isEmpty() const
{
m_guard.enterRead();
bool fEmpty = m_cElements == 0;
m_guard.leaveRead();
return fEmpty;
}
/**
* Return the current count of elements within the list.
*
* @return The current element count.
*/
size_t size() const
{
m_guard.enterRead();
size_t cRet = m_cElements;
m_guard.leaveRead();
return cRet;
}
/**
* Inserts an item to the list at position @a i.
*
* @param i The position of the new item. The must be within or at the
* exact end of the list. Indexes specified beyond the end of
* the list will be changed to an append() operation and strict
* builds will raise an assert.
* @param val The new item.
* @return a reference to this list.
* @throws std::bad_alloc
*/
RTCListBase<T, ITYPE, MT> &insert(size_t i, const T &val)
{
m_guard.enterWrite();
AssertMsgStmt(i <= m_cElements, ("i=%zu m_cElements=%zu\n", i, m_cElements), i = m_cElements);
if (m_cElements == m_cCapacity)
growArray(m_cCapacity + kDefaultCapacity);
memmove(&m_pArray[i + 1], &m_pArray[i], (m_cElements - i) * sizeof(ITYPE));
RTCListHelper<T, ITYPE>::set(m_pArray, i, val);
++m_cElements;
m_guard.leaveWrite();
return *this;
}
/**
* Inserts a list to the list at position @a i.
*
* @param i The position of the new item. The must be within or at the
* exact end of the list. Indexes specified beyond the end of
* the list will be changed to an append() operation and strict
* builds will raise an assert.
* @param other The other list. This MUST not be the same as the destination
* list, will assert and return without doing anything if this
* happens.
* @return a reference to this list.
* @throws std::bad_alloc
*/
RTCListBase<T, ITYPE, MT> &insert(size_t i, const RTCListBase<T, ITYPE, MT> &other)
{
AssertReturn(this != &other, *this);
other.m_guard.enterRead();
m_guard.enterWrite();
AssertMsgStmt(i <= m_cElements, ("i=%zu m_cElements=%zu\n", i, m_cElements), i = m_cElements);
size_t cElementsOther = other.m_cElements;
if (RT_LIKELY(cElementsOther > 0))
{
if (m_cCapacity - m_cElements < cElementsOther)
growArray(m_cCapacity + (cElementsOther - (m_cCapacity - m_cElements)));
if (i < m_cElements)
memmove(&m_pArray[i + cElementsOther], &m_pArray[i], (m_cElements - i) * sizeof(ITYPE));
RTCListHelper<T, ITYPE>::copyTo(&m_pArray[i], other.m_pArray, 0, cElementsOther);
m_cElements += cElementsOther;
}
m_guard.leaveWrite();
other.m_guard.leaveRead();
return *this;
}
/**
* Prepend an item to the list.
*
* @param val The new item.
* @return a reference to this list.
* @throws std::bad_alloc
*/
RTCListBase<T, ITYPE, MT> &prepend(const T &val)
{
return insert(0, val);
}
/**
* Prepend a list of type T to the list.
*
* @param other The list to prepend.
* @return a reference to this list.
* @throws std::bad_alloc
*/
RTCListBase<T, ITYPE, MT> &prepend(const RTCListBase<T, ITYPE, MT> &other)
{
return insert(0, other);
}
/**
* Append a default item to the list.
*
* @return a mutable reference to the item
* @throws std::bad_alloc
*/
GET_RTYPE append()
{
m_guard.enterWrite();
if (m_cElements == m_cCapacity)
growArray(m_cCapacity + kDefaultCapacity);
RTCListHelper<T, ITYPE>::set(m_pArray, m_cElements, T());
GET_RTYPE rRet = RTCListHelper<T, ITYPE>::at(m_pArray, m_cElements);
++m_cElements;
m_guard.leaveWrite();
return rRet;
}
/**
* Append an item to the list.
*
* @param val The new item.
* @return a reference to this list.
* @throws std::bad_alloc
*/
RTCListBase<T, ITYPE, MT> &append(const T &val)
{
m_guard.enterWrite();
if (m_cElements == m_cCapacity)
growArray(m_cCapacity + kDefaultCapacity);
RTCListHelper<T, ITYPE>::set(m_pArray, m_cElements, val);
++m_cElements;
m_guard.leaveWrite();
return *this;
}
/**
* Append a list of type T to the list.
*
* @param other The list to append. Must not be the same as the destination
* list, will assert and return without doing anything.
* @return a reference to this list.
* @throws std::bad_alloc
*/
RTCListBase<T, ITYPE, MT> &append(const RTCListBase<T, ITYPE, MT> &other)
{
AssertReturn(this != &other, *this);
other.m_guard.enterRead();
m_guard.enterWrite();
insert(m_cElements, other);
m_guard.leaveWrite();
other.m_guard.leaveRead();
return *this;
}
/**
* Copy the items of the other list into this list.
*
* All previous items of this list are deleted.
*
* @param other The list to copy.
* @return a reference to this list.
*/
RTCListBase<T, ITYPE, MT> &operator=(const RTCListBase<T, ITYPE, MT>& other)
{
/* Prevent self assignment */
if (RT_LIKELY(this != &other))
{
other.m_guard.enterRead();
m_guard.enterWrite();
/* Delete all items. */
RTCListHelper<T, ITYPE>::eraseRange(m_pArray, 0, m_cElements);
/* Need we to realloc memory. */
if (other.m_cElements != m_cCapacity)
resizeArrayNoErase(other.m_cElements);
m_cElements = other.m_cElements;
/* Copy new items. */
RTCListHelper<T, ITYPE>::copyTo(m_pArray, other.m_pArray, 0, other.m_cElements);
m_guard.leaveWrite();
other.m_guard.leaveRead();
}
return *this;
}
/**
* Compares if this list's items match the other list.
*
* @returns \c true if both lists contain the same items, \c false if not.
* @param other The list to compare this list with.
*/
bool operator==(const RTCListBase<T, ITYPE, MT>& other)
{
/* Prevent self comparrison */
if (RT_LIKELY(this == &other))
return true;
other.m_guard.enterRead();
m_guard.enterRead();
bool fEqual = true;
if (other.m_cElements == m_cElements)
{
for (size_t i = 0; i < m_cElements; i++)
{
if (RTCListHelper<T, ITYPE>::at(m_pArray, i) != RTCListHelper<T, ITYPE>::at(other.m_pArray, i))
{
fEqual = false;
break;
}
}
}
else
fEqual = false;
m_guard.leaveRead();
other.m_guard.leaveRead();
return fEqual;
}
/**
* Compares if this list's items do not match the other list.
*
* @returns \c true if the lists do not match, \c false if otherwise.
* @param other The list to compare this list with.
*/
bool operator!=(const RTCListBase<T, ITYPE, MT>& other)
{
return !(*this == other);
}
/**
* Replace an item in the list.
*
* @param i The position of the item to replace. If this is out of range,
* the request will be ignored, strict builds will assert.
* @param val The new value.
* @return a reference to this list.
*/
RTCListBase<T, ITYPE, MT> &replace(size_t i, const T &val)
{
m_guard.enterWrite();
if (i < m_cElements)
{
RTCListHelper<T, ITYPE>::erase(m_pArray, i);
RTCListHelper<T, ITYPE>::set(m_pArray, i, val);
}
else
AssertMsgFailed(("i=%zu m_cElements=%zu\n", i, m_cElements));
m_guard.leaveWrite();
return *this;
}
/**
* Applies a filter of type T to this list.
*
* @param other The list which contains the elements to filter out from this list.
* @return a reference to this list.
*/
RTCListBase<T, ITYPE, MT> &filter(const RTCListBase<T, ITYPE, MT> &other)
{
AssertReturn(this != &other, *this);
other.m_guard.enterRead();
m_guard.enterWrite();
for (size_t i = 0; i < m_cElements; i++)
{
for (size_t f = 0; f < other.m_cElements; f++)
{
if (RTCListHelper<T, ITYPE>::at(m_pArray, i) == RTCListHelper<T, ITYPE>::at(other.m_pArray, f))
removeAtLocked(i);
}
}
m_guard.leaveWrite();
other.m_guard.leaveRead();
return *this;
}
/**
* Return the first item as constant object.
*
* @return A reference or pointer to the first item.
*
* @note No boundary checks are done. Make sure there is at least one
* element.
*/
GET_CRTYPE first() const
{
m_guard.enterRead();
Assert(m_cElements > 0);
GET_CRTYPE res = RTCListHelper<T, ITYPE>::at(m_pArray, 0);
m_guard.leaveRead();
return res;
}
/**
* Return the first item.
*
* @return A reference or pointer to the first item.
*
* @note No boundary checks are done. Make sure there is at least one
* element.
*/
GET_RTYPE first()
{
m_guard.enterRead();
Assert(m_cElements > 0);
GET_RTYPE res = RTCListHelper<T, ITYPE>::at(m_pArray, 0);
m_guard.leaveRead();
return res;
}
/**
* Return the last item as constant object.
*
* @return A reference or pointer to the last item.
*
* @note No boundary checks are done. Make sure there is at least one
* element.
*/
GET_CRTYPE last() const
{
m_guard.enterRead();
Assert(m_cElements > 0);
GET_CRTYPE res = RTCListHelper<T, ITYPE>::at(m_pArray, m_cElements - 1);
m_guard.leaveRead();
return res;
}
/**
* Return the last item.
*
* @return A reference or pointer to the last item.
*
* @note No boundary checks are done. Make sure there is at least one
* element.
*/
GET_RTYPE last()
{
m_guard.enterRead();
Assert(m_cElements > 0);
GET_RTYPE res = RTCListHelper<T, ITYPE>::at(m_pArray, m_cElements - 1);
m_guard.leaveRead();
return res;
}
/**
* Return the item at position @a i as constant object.
*
* @param i The position of the item to return. This better not be out of
* bounds, however should it be the last element of the array
* will be return and strict builds will raise an assertion.
* Should the array be empty, a crash is very likely.
* @return The item at position @a i.
*/
GET_CRTYPE at(size_t i) const
{
m_guard.enterRead();
AssertMsgStmt(i < m_cElements, ("i=%zu m_cElements=%zu\n", i, m_cElements), i = m_cElements - 1);
GET_CRTYPE res = RTCListHelper<T, ITYPE>::at(m_pArray, i);
m_guard.leaveRead();
return res;
}
/**
* Return the item at position @a i.
*
* @param i The position of the item to return. This better not be out of
* bounds, however should it be the last element of the array
* will be return and strict builds will raise an assertion.
* Should the array be empty, a crash is very likely.
* @return The item at position @a i.
*/
GET_RTYPE at(size_t i)
{
m_guard.enterRead();
AssertMsgStmt(i < m_cElements, ("i=%zu m_cElements=%zu\n", i, m_cElements), i = m_cElements - 1);
GET_RTYPE res = RTCListHelper<T, ITYPE>::at(m_pArray, i);
m_guard.leaveRead();
return res;
}
/**
* Return the item at position @a i as mutable reference.
*
* @param i The position of the item to return. This better not be out of
* bounds, however should it be the last element of the array
* will be return and strict builds will raise an assertion.
* Should the array be empty, a crash is very likely.
* @return The item at position @a i.
*/
T &operator[](size_t i)
{
m_guard.enterRead();
AssertMsgStmt(i < m_cElements, ("i=%zu m_cElements=%zu\n", i, m_cElements), i = m_cElements - 1);
T &res = RTCListHelper<T, ITYPE>::at(m_pArray, i);
m_guard.leaveRead();
return res;
}
/**
* Return the item at position @a i as immutable reference.
*
* @param i The position of the item to return. This better not be out of
* bounds, however should it be the last element of the array
* will be return and strict builds will raise an assertion.
* Should the array be empty, a crash is very likely.
* @return The item at position @a i.
*/
const T &operator[](size_t i) const
{
m_guard.enterRead();
AssertMsgStmt(i < m_cElements, ("i=%zu m_cElements=%zu\n", i, m_cElements), i = m_cElements - 1);
const T &rRet = RTCListHelper<T, ITYPE>::atConst(m_pArray, i);
m_guard.leaveRead();
return rRet;
}
/**
* Return a copy of the item at position @a i or default value if out of range.
*
* @param i The position of the item to return.
* @return Copy of the item at position @a i or default value.
*/
T value(size_t i) const
{
m_guard.enterRead();
if (RT_LIKELY(i < m_cElements))
{
T res = RTCListHelper<T, ITYPE>::at(m_pArray, i);
m_guard.leaveRead();
return res;
}
m_guard.leaveRead();
return T();
}
/**
* Return a copy of the item at position @a i, or @a defaultVal if out of range.
*
* @param i The position of the item to return.
* @param defaultVal The value to return in case @a i is invalid.
* @return Copy of the item at position @a i or @a defaultVal.
*/
T value(size_t i, const T &defaultVal) const
{
m_guard.enterRead();
if (RT_LIKELY(i < m_cElements))
{
T res = RTCListHelper<T, ITYPE>::at(m_pArray, i);
m_guard.leaveRead();
return res;
}
m_guard.leaveRead();
return defaultVal;
}
/**
* Check if @a val is contained in the array.
*
* @param val The value to check for.
* @return true if it is found, false otherwise.
*/
bool contains(const T &val) const
{
m_guard.enterRead();
bool fRc = RTCListHelper<T, ITYPE>::find(m_pArray, val, m_cElements) < m_cElements;
m_guard.leaveRead();
return fRc;
}
/**
* Remove the first item.
*
* @note You should make sure the list isn't empty. Strict builds will assert.
* The other builds will quietly ignore the request.
*/
void removeFirst()
{
removeAt(0);
}
/**
* Remove the last item.
*
* @note You should make sure the list isn't empty. Strict builds will assert.
* The other builds will quietly ignore the request.
*/
void removeLast()
{
m_guard.enterWrite();
removeAtLocked(m_cElements - 1);
m_guard.leaveWrite();
}
/**
* Remove the item at position @a i.
*
* @param i The position of the item to remove. Out of bounds values will
* be ignored and an assertion will be raised in strict builds.
*/
void removeAt(size_t i)
{
m_guard.enterWrite();
removeAtLocked(i);
m_guard.leaveWrite();
}
/**
* Remove a range of items from the list.
*
* @param iStart The start position of the items to remove.
* @param iEnd The end position of the items to remove (excluded).
*/
void removeRange(size_t iStart, size_t iEnd)
{
AssertReturnVoid(iStart <= iEnd);
m_guard.enterWrite();
AssertMsgStmt(iEnd <= m_cElements, ("iEnd=%zu m_cElements=%zu\n", iEnd, m_cElements), iEnd = m_cElements);
AssertMsgStmt(iStart < m_cElements, ("iStart=%zu m_cElements=%zu\n", iStart, m_cElements), iStart = m_cElements);
size_t const cElements = iEnd - iStart;
if (cElements > 0)
{
Assert(iStart < m_cElements);
RTCListHelper<T, ITYPE>::eraseRange(m_pArray, iStart, cElements);
if (m_cElements > iEnd)
memmove(&m_pArray[iStart], &m_pArray[iEnd], (m_cElements - iEnd) * sizeof(ITYPE));
m_cElements -= cElements;
}
m_guard.leaveWrite();
}
/**
* Delete all items in the list.
*/
void clear()
{
m_guard.enterWrite();
/* Values cleanup */
RTCListHelper<T, ITYPE>::eraseRange(m_pArray, 0, m_cElements);
if (m_cElements != kDefaultCapacity)
resizeArrayNoErase(kDefaultCapacity);
m_cElements = 0;
m_guard.leaveWrite();
}
/**
* Return the raw array.
*
* For native types this is a pointer to continuous memory of the items. For
* pointer types this is a continuous memory of pointers to the items.
*
* @warning If you change anything in the underlaying list, this memory
* will very likely become invalid. So take care when using this
* method and better try to avoid using it.
*
* @returns the raw memory.
*/
ITYPE *raw() const
{
m_guard.enterRead();
ITYPE *pRet = m_pArray;
m_guard.leaveRead();
return pRet;
}
RTCListBase<T, ITYPE, MT> &operator<<(const T &val)
{
return append(val);
}
/* Define our own new and delete. */
#ifdef RT_NEED_NEW_AND_DELETE
RTMEM_IMPLEMENT_NEW_AND_DELETE();
#else
RTMEMEF_NEW_AND_DELETE_OPERATORS();
#endif
/**
* The default capacity of the list. This is also used as grow factor.
*/
static const size_t kDefaultCapacity;
protected:
/**
* Generic resizes the array, surplus elements are erased.
*
* @param cElementsNew The new array size.
* @throws std::bad_alloc.
*/
void resizeArray(size_t cElementsNew)
{
/* Same size? */
if (cElementsNew == m_cCapacity)
return;
/* If we get smaller we have to delete some of the objects at the end
of the list. */
if ( cElementsNew < m_cElements
&& m_pArray)
RTCListHelper<T, ITYPE>::eraseRange(m_pArray, cElementsNew, m_cElements - cElementsNew);
resizeArrayNoErase(cElementsNew);
}
/**
* Resizes the array without doing the erase() thing on surplus elements.
*
* @param cElementsNew The new array size.
* @throws std::bad_alloc.
*/
void resizeArrayNoErase(size_t cElementsNew)
{
/* Same size? */
if (cElementsNew == m_cCapacity)
return;
/* Resize the array. */
if (cElementsNew > 0)
{
void *pvNew = RTMemRealloc(m_pArray, sizeof(ITYPE) * cElementsNew);
if (!pvNew)
{
#ifdef RT_EXCEPTIONS_ENABLED
throw std::bad_alloc();
#endif
return;
}
m_pArray = static_cast<ITYPE*>(pvNew);
}
/* If we get zero we delete the array it self. */
else if (m_pArray)
{
RTMemFree(m_pArray);
m_pArray = NULL;
}
m_cCapacity = cElementsNew;
if (m_cElements > cElementsNew)
m_cElements = cElementsNew;
}
/**
* Special realloc method which require that the array will grow.
*
* @param cElementsNew The new array size.
* @throws std::bad_alloc.
* @note No boundary checks are done!
*/
void growArray(size_t cElementsNew)
{
Assert(cElementsNew > m_cCapacity);
void *pvNew = RTMemRealloc(m_pArray, sizeof(ITYPE) * cElementsNew);
if (pvNew)
{
m_cCapacity = cElementsNew;
m_pArray = static_cast<ITYPE*>(pvNew);
}
else
{
#ifdef RT_EXCEPTIONS_ENABLED
throw std::bad_alloc();
#endif
}
}
/**
* Remove the item at position @a i.
*
* @param i The position of the item to remove. Out of bounds values will
* be ignored and an assertion will be raised in strict builds.
* @remarks
*/
void removeAtLocked(size_t i)
{
AssertMsgReturnVoid(i < m_cElements, ("i=%zu m_cElements=%zu\n", i, m_cElements));
RTCListHelper<T, ITYPE>::erase(m_pArray, i);
if (i < m_cElements - 1)
memmove(&m_pArray[i], &m_pArray[i + 1], (m_cElements - i - 1) * sizeof(ITYPE));
--m_cElements;
}
/** The internal list array. */
ITYPE *m_pArray;
/** The current count of items in use. */
size_t m_cElements;
/** The current capacity of the internal array. */
size_t m_cCapacity;
/** The guard used to serialize the access to the items. */
RTCListGuard<MT> m_guard;
};
template <class T, typename ITYPE, bool MT>
const size_t RTCListBase<T, ITYPE, MT>::kDefaultCapacity = 10;
/**
* Template class which automatically determines the type of list to use.
*
* @see RTCListBase
*/
template <class T, typename ITYPE = typename RTCIf<(sizeof(T) > sizeof(void*)), T*, T>::result>
class RTCList : public RTCListBase<T, ITYPE, false>
{
/* Traits */
typedef RTCListBase<T, ITYPE, false> BASE;
public:
/**
* Creates a new list.
*
* This preallocates @a cCapacity elements within the list.
*
* @param cCapacity The initial capacity the list has.
* @throws std::bad_alloc
*/
RTCList(size_t cCapacity = BASE::kDefaultCapacity)
: BASE(cCapacity) {}
RTCList(const BASE &other)
: BASE(other) {}
/* Define our own new and delete. */
#ifdef RT_NEED_NEW_AND_DELETE
RTMEM_IMPLEMENT_NEW_AND_DELETE();
#else
RTMEMEF_NEW_AND_DELETE_OPERATORS();
#endif
};
/**
* Specialized class for using the native type list for unsigned 64-bit
* values even on a 32-bit host.
*
* @see RTCListBase
*/
template <>
class RTCList<uint64_t>: public RTCListBase<uint64_t, uint64_t, false>
{
/* Traits */
typedef RTCListBase<uint64_t, uint64_t, false> BASE;
public:
/**
* Creates a new list.
*
* This preallocates @a cCapacity elements within the list.
*
* @param cCapacity The initial capacity the list has.
* @throws std::bad_alloc
*/
RTCList(size_t cCapacity = BASE::kDefaultCapacity)
: BASE(cCapacity) {}
/* Define our own new and delete. */
#ifdef RT_NEED_NEW_AND_DELETE
RTMEM_IMPLEMENT_NEW_AND_DELETE();
#else
RTMEMEF_NEW_AND_DELETE_OPERATORS();
#endif
};
/**
* Specialized class for using the native type list for signed 64-bit
* values even on a 32-bit host.
*
* @see RTCListBase
*/
template <>
class RTCList<int64_t>: public RTCListBase<int64_t, int64_t, false>
{
/* Traits */
typedef RTCListBase<int64_t, int64_t, false> BASE;
public:
/**
* Creates a new list.
*
* This preallocates @a cCapacity elements within the list.
*
* @param cCapacity The initial capacity the list has.
* @throws std::bad_alloc
*/
RTCList(size_t cCapacity = BASE::kDefaultCapacity)
: BASE(cCapacity) {}
/* Define our own new and delete. */
#ifdef RT_NEED_NEW_AND_DELETE
RTMEM_IMPLEMENT_NEW_AND_DELETE();
#else
RTMEMEF_NEW_AND_DELETE_OPERATORS();
#endif
};
/** @} */
#endif /* !IPRT_INCLUDED_cpp_list_h */
|