Changeset 36532 in vbox for trunk/include/iprt/cpp
- Timestamp:
- Apr 4, 2011 2:41:14 PM (14 years ago)
- Location:
- trunk/include/iprt/cpp
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/include/iprt/cpp/list.h
r36527 r36532 33 33 #include <new> /* For std::bad_alloc */ 34 34 35 namespace iprt36 {37 38 35 /** @defgroup grp_rt_cpp_list C++ List support 39 36 * @ingroup grp_rt_cpp … … 58 55 * 59 56 * The size of the internal array will usually not shrink, but grow 60 * automatically. Only certain methods, like list::clear or the "=" operator61 * will reset any previously allocated memory. You can call list::setCapacity62 * for manual adjustment. If the size of an new list will be known, calling the63 * constructor with the necessary capacity will speed up the insertion of the64 * new items.65 * 66 * For the full public interface these list classes offer see ListBase.57 * automatically. Only certain methods, like RTCList::clear or the "=" operator 58 * will reset any previously allocated memory. You can call 59 * RTCList::setCapacity for manual adjustment. If the size of an new list will 60 * be known, calling the constructor with the necessary capacity will speed up 61 * the insertion of the new items. 62 * 63 * For the full public interface these list classes offer see RTCListBase. 67 64 * 68 65 * There are some requirements for the types used which follow: … … 90 87 * changed when using the value as reference returned by this operator. 91 88 * 92 * The list class is reentrant. For a thread-safe variant see mtlist.89 * The list class is reentrant. For a thread-safe variant see RTCMTList. 93 90 * 94 91 * Implementation details: … … 99 96 * 100 97 * Current specialized implementations: 101 * - int64_t: iprt::list<int64_t>102 * - uint64_t: iprt::list<uint64_t>98 * - int64_t: RTCList<int64_t> 99 * - uint64_t: RTCList<uint64_t> 103 100 * 104 101 * @{ … … 109 106 */ 110 107 template <bool G> 111 class ListGuard;108 class RTCListGuard; 112 109 113 110 /** … … 115 112 */ 116 113 template <> 117 class ListGuard<false>114 class RTCListGuard<false> 118 115 { 119 116 public: … … 125 122 126 123 /** 127 * General helper template for managing native values in ListBase.124 * General helper template for managing native values in RTCListBase. 128 125 */ 129 126 template <typename T1, typename T2> 130 class ListHelper127 class RTCListHelper 131 128 { 132 129 public: … … 143 140 144 141 /** 145 * Specialized helper template for managing pointer values in ListBase.142 * Specialized helper template for managing pointer values in RTCListBase. 146 143 */ 147 144 template <typename T1> 148 class ListHelper<T1, T1*>145 class RTCListHelper<T1, T1*> 149 146 { 150 147 public: … … 170 167 */ 171 168 template <class T, typename ITYPE, bool MT> 172 class ListBase169 class RTCListBase 173 170 { 174 171 /** … … 177 174 * value. 178 175 */ 179 typedef typename if_ptr<ITYPE, T&, T>::result GET_RTYPE;180 typedef typename if_ptr<ITYPE, const T&, const T>::result GET_CRTYPE;176 typedef typename RTCIfPtr<ITYPE, T&, T>::result GET_RTYPE; 177 typedef typename RTCIfPtr<ITYPE, const T&, const T>::result GET_CRTYPE; 181 178 182 179 public: … … 189 186 * @throws std::bad_alloc 190 187 */ 191 ListBase(size_t cCapacity = DefaultCapacity)188 RTCListBase(size_t cCapacity = DefaultCapacity) 192 189 : m_pArray(0) 193 190 , m_cSize(0) … … 206 203 * @throws std::bad_alloc 207 204 */ 208 ListBase(constListBase<T, ITYPE, MT>& other)205 RTCListBase(const RTCListBase<T, ITYPE, MT>& other) 209 206 : m_pArray(0) 210 207 , m_cSize(0) … … 212 209 { 213 210 realloc_grow(other.m_cSize); 214 ListHelper<T, ITYPE>::copyTo(m_pArray, other.m_pArray, 0, other.m_cSize);211 RTCListHelper<T, ITYPE>::copyTo(m_pArray, other.m_pArray, 0, other.m_cSize); 215 212 m_cSize = other.m_cSize; 216 213 } … … 219 216 * Destructor. 220 217 */ 221 ~ ListBase()222 { 223 ListHelper<T, ITYPE>::eraseRange(m_pArray, 0, m_cSize);218 ~RTCListBase() 219 { 220 RTCListHelper<T, ITYPE>::eraseRange(m_pArray, 0, m_cSize); 224 221 if (m_pArray) 225 222 RTMemFree(m_pArray); … … 273 270 * @throws std::bad_alloc 274 271 */ 275 ListBase<T, ITYPE, MT> &insert(size_t i, const T &val)272 RTCListBase<T, ITYPE, MT> &insert(size_t i, const T &val) 276 273 { 277 274 m_guard.enterWrite(); … … 279 276 realloc_grow(m_cCapacity + DefaultCapacity); 280 277 memmove(&m_pArray[i + 1], &m_pArray[i], (m_cSize - i) * sizeof(ITYPE)); 281 ListHelper<T, ITYPE>::set(m_pArray, i, val);278 RTCListHelper<T, ITYPE>::set(m_pArray, i, val); 282 279 ++m_cSize; 283 280 m_guard.leaveWrite(); … … 293 290 * @throws std::bad_alloc 294 291 */ 295 ListBase<T, ITYPE, MT> &prepend(const T &val)292 RTCListBase<T, ITYPE, MT> &prepend(const T &val) 296 293 { 297 294 return insert(0, val); … … 305 302 * @throws std::bad_alloc 306 303 */ 307 ListBase<T, ITYPE, MT> &prepend(constListBase<T, ITYPE, MT> &other)304 RTCListBase<T, ITYPE, MT> &prepend(const RTCListBase<T, ITYPE, MT> &other) 308 305 { 309 306 m_guard.enterWrite(); … … 311 308 realloc_grow(m_cCapacity + (other.m_cSize - (m_cCapacity - m_cSize))); 312 309 memmove(&m_pArray[other.m_cSize], &m_pArray[0], m_cSize * sizeof(ITYPE)); 313 ListHelper<T, ITYPE>::copyTo(m_pArray, other.m_pArray, 0, other.m_cSize);310 RTCListHelper<T, ITYPE>::copyTo(m_pArray, other.m_pArray, 0, other.m_cSize); 314 311 m_cSize += other.m_cSize; 315 312 m_guard.leaveWrite(); … … 325 322 * @throws std::bad_alloc 326 323 */ 327 ListBase<T, ITYPE, MT> &append(const T &val)324 RTCListBase<T, ITYPE, MT> &append(const T &val) 328 325 { 329 326 m_guard.enterWrite(); 330 327 if (m_cSize == m_cCapacity) 331 328 realloc_grow(m_cCapacity + DefaultCapacity); 332 ListHelper<T, ITYPE>::set(m_pArray, m_cSize, val);329 RTCListHelper<T, ITYPE>::set(m_pArray, m_cSize, val); 333 330 ++m_cSize; 334 331 m_guard.leaveWrite(); … … 344 341 * @throws std::bad_alloc 345 342 */ 346 ListBase<T, ITYPE, MT> &append(constListBase<T, ITYPE, MT> &other)343 RTCListBase<T, ITYPE, MT> &append(const RTCListBase<T, ITYPE, MT> &other) 347 344 { 348 345 m_guard.enterWrite(); 349 346 if (m_cCapacity - m_cSize < other.m_cSize) 350 347 realloc_grow(m_cCapacity + (other.m_cSize - (m_cCapacity - m_cSize))); 351 ListHelper<T, ITYPE>::copyTo(m_pArray, other.m_pArray, m_cSize, other.m_cSize);348 RTCListHelper<T, ITYPE>::copyTo(m_pArray, other.m_pArray, m_cSize, other.m_cSize); 352 349 m_cSize += other.m_cSize; 353 350 m_guard.leaveWrite(); … … 363 360 * @return a reference to this list. 364 361 */ 365 ListBase<T, ITYPE, MT> &operator=(constListBase<T, ITYPE, MT>& other)362 RTCListBase<T, ITYPE, MT> &operator=(const RTCListBase<T, ITYPE, MT>& other) 366 363 { 367 364 /* Prevent self assignment */ … … 371 368 m_guard.enterWrite(); 372 369 /* Values cleanup */ 373 ListHelper<T, ITYPE>::eraseRange(m_pArray, 0, m_cSize);370 RTCListHelper<T, ITYPE>::eraseRange(m_pArray, 0, m_cSize); 374 371 375 372 /* Copy */ … … 377 374 realloc_grow(other.m_cSize); 378 375 m_cSize = other.m_cSize; 379 ListHelper<T, ITYPE>::copyTo(m_pArray, other.m_pArray, 0, other.m_cSize);376 RTCListHelper<T, ITYPE>::copyTo(m_pArray, other.m_pArray, 0, other.m_cSize); 380 377 m_guard.leaveWrite(); 381 378 … … 387 384 * 388 385 * @note No boundary checks are done. Make sure @a i is equal or greater zero 389 * and smaller than list::size.386 * and smaller than RTCList::size. 390 387 * 391 388 * @param i The position of the item to replace. … … 393 390 * @return a reference to this list. 394 391 */ 395 ListBase<T, ITYPE, MT> &replace(size_t i, const T &val)396 { 397 m_guard.enterWrite(); 398 ListHelper<T, ITYPE>::erase(m_pArray, i);399 ListHelper<T, ITYPE>::set(m_pArray, i, val);392 RTCListBase<T, ITYPE, MT> &replace(size_t i, const T &val) 393 { 394 m_guard.enterWrite(); 395 RTCListHelper<T, ITYPE>::erase(m_pArray, i); 396 RTCListHelper<T, ITYPE>::set(m_pArray, i, val); 400 397 m_guard.leaveWrite(); 401 398 … … 407 404 * 408 405 * @note No boundary checks are done. Make sure @a i is equal or greater zero 409 * and smaller than list::size.406 * and smaller than RTCList::size. 410 407 * 411 408 * @return The first item. … … 414 411 { 415 412 m_guard.enterRead(); 416 GET_CRTYPE res = ListHelper<T, ITYPE>::at(m_pArray, 0);413 GET_CRTYPE res = RTCListHelper<T, ITYPE>::at(m_pArray, 0); 417 414 m_guard.leaveRead(); 418 415 return res; … … 423 420 * 424 421 * @note No boundary checks are done. Make sure @a i is equal or greater zero 425 * and smaller than list::size.422 * and smaller than RTCList::size. 426 423 * 427 424 * @return The first item. … … 430 427 { 431 428 m_guard.enterRead(); 432 GET_RTYPE res = ListHelper<T, ITYPE>::at(m_pArray, 0);429 GET_RTYPE res = RTCListHelper<T, ITYPE>::at(m_pArray, 0); 433 430 m_guard.leaveRead(); 434 431 return res; … … 439 436 * 440 437 * @note No boundary checks are done. Make sure @a i is equal or greater zero 441 * and smaller than list::size.438 * and smaller than RTCList::size. 442 439 * 443 440 * @return The last item. … … 446 443 { 447 444 m_guard.enterRead(); 448 GET_CRTYPE res = ListHelper<T, ITYPE>::at(m_pArray, m_cSize - 1);445 GET_CRTYPE res = RTCListHelper<T, ITYPE>::at(m_pArray, m_cSize - 1); 449 446 m_guard.leaveRead(); 450 447 return res; … … 455 452 * 456 453 * @note No boundary checks are done. Make sure @a i is equal or greater zero 457 * and smaller than list::size.454 * and smaller than RTCList::size. 458 455 * 459 456 * @return The last item. … … 462 459 { 463 460 m_guard.enterRead(); 464 GET_RTYPE res = ListHelper<T, ITYPE>::at(m_pArray, m_cSize - 1);461 GET_RTYPE res = RTCListHelper<T, ITYPE>::at(m_pArray, m_cSize - 1); 465 462 m_guard.leaveRead(); 466 463 return res; … … 471 468 * 472 469 * @note No boundary checks are done. Make sure @a i is equal or greater zero 473 * and smaller than list::size.470 * and smaller than RTCList::size. 474 471 * 475 472 * @param i The position of the item to return. … … 479 476 { 480 477 m_guard.enterRead(); 481 GET_CRTYPE res = ListHelper<T, ITYPE>::at(m_pArray, i);478 GET_CRTYPE res = RTCListHelper<T, ITYPE>::at(m_pArray, i); 482 479 m_guard.leaveRead(); 483 480 return res; … … 488 485 * 489 486 * @note No boundary checks are done. Make sure @a i is equal or greater zero 490 * and smaller than list::size.487 * and smaller than RTCList::size. 491 488 * 492 489 * @param i The position of the item to return. … … 496 493 { 497 494 m_guard.enterRead(); 498 GET_RTYPE res = ListHelper<T, ITYPE>::at(m_pArray, i);495 GET_RTYPE res = RTCListHelper<T, ITYPE>::at(m_pArray, i); 499 496 m_guard.leaveRead(); 500 497 return res; … … 505 502 * 506 503 * @note No boundary checks are done. Make sure @a i is equal or greater zero 507 * and smaller than list::size.504 * and smaller than RTCList::size. 508 505 * 509 506 * @param i The position of the item to return. … … 513 510 { 514 511 m_guard.enterRead(); 515 T &res = ListHelper<T, ITYPE>::at(m_pArray, i);512 T &res = RTCListHelper<T, ITYPE>::at(m_pArray, i); 516 513 m_guard.leaveRead(); 517 514 return res; … … 533 530 return T(); 534 531 } 535 T res = ListHelper<T, ITYPE>::at(m_pArray, i);532 T res = RTCListHelper<T, ITYPE>::at(m_pArray, i); 536 533 m_guard.leaveRead(); 537 534 return res; … … 554 551 return defaultVal; 555 552 } 556 T res = ListHelper<T, ITYPE>::at(m_pArray, i);553 T res = RTCListHelper<T, ITYPE>::at(m_pArray, i); 557 554 m_guard.leaveRead(); 558 555 return res; … … 563 560 * 564 561 * @note No boundary checks are done. Make sure @a i is equal or greater zero 565 * and smaller than list::size.562 * and smaller than RTCList::size. 566 563 * 567 564 * @param i The position of the item to remove. … … 570 567 { 571 568 m_guard.enterWrite(); 572 ListHelper<T, ITYPE>::erase(m_pArray, i);569 RTCListHelper<T, ITYPE>::erase(m_pArray, i); 573 570 /* Not last element? */ 574 571 if (i < m_cSize - 1) … … 582 579 * 583 580 * @note No boundary checks are done. Make sure @a iFrom is equal or 584 * greater zero and smaller than list::size. @a iTo has to be585 * greater than @a iFrom and equal or smaller than list::size.581 * greater zero and smaller than RTCList::size. @a iTo has to be 582 * greater than @a iFrom and equal or smaller than RTCList::size. 586 583 * 587 584 * @param iFrom The start position of the items to remove. … … 591 588 { 592 589 m_guard.enterWrite(); 593 ListHelper<T, ITYPE>::eraseRange(m_pArray, iFrom, iTo - iFrom);590 RTCListHelper<T, ITYPE>::eraseRange(m_pArray, iFrom, iTo - iFrom); 594 591 /* Not last elements? */ 595 592 if (m_cSize - iTo > 0) … … 606 603 m_guard.enterWrite(); 607 604 /* Values cleanup */ 608 ListHelper<T, ITYPE>::eraseRange(m_pArray, 0, m_cSize);605 RTCListHelper<T, ITYPE>::eraseRange(m_pArray, 0, m_cSize); 609 606 if (m_cSize != DefaultCapacity) 610 607 realloc_grow(DefaultCapacity); … … 633 630 && m_pArray) 634 631 { 635 ListHelper<T, ITYPE>::eraseRange(m_pArray, cNewSize, m_cSize - cNewSize);632 RTCListHelper<T, ITYPE>::eraseRange(m_pArray, cNewSize, m_cSize - cNewSize); 636 633 m_cSize -= m_cSize - cNewSize; 637 634 } … … 690 687 size_t m_cCapacity; 691 688 /** The guard used to serialize the access to the items. */ 692 ListGuard<MT> m_guard;689 RTCListGuard<MT> m_guard; 693 690 }; 694 691 695 692 template <class T, typename ITYPE, bool MT> 696 const size_t ListBase<T, ITYPE, MT>::DefaultCapacity = 10;693 const size_t RTCListBase<T, ITYPE, MT>::DefaultCapacity = 10; 697 694 698 695 /** 699 696 * Template class which automatically determines the type of list to use. 700 697 * 701 * @see ListBase702 */ 703 template <class T, typename ITYPE = typename if_<(sizeof(T) > sizeof(void*)), T*, T>::result>704 class list : publicListBase<T, ITYPE, false> {};698 * @see RTCListBase 699 */ 700 template <class T, typename ITYPE = typename RTCIf<(sizeof(T) > sizeof(void*)), T*, T>::result> 701 class RTCList : public RTCListBase<T, ITYPE, false> {}; 705 702 706 703 /** … … 708 705 * values even on a 32-bit host. 709 706 * 710 * @see ListBase707 * @see RTCListBase 711 708 */ 712 709 template <> 713 class list<uint64_t>: publicListBase<uint64_t, uint64_t, false> {};710 class RTCList<uint64_t>: public RTCListBase<uint64_t, uint64_t, false> {}; 714 711 715 712 /** … … 717 714 * values even on a 32-bit host. 718 715 * 719 * @see ListBase716 * @see RTCListBase 720 717 */ 721 718 template <> 722 class list<int64_t>: publicListBase<int64_t, int64_t, false> {};719 class RTCList<int64_t>: public RTCListBase<int64_t, int64_t, false> {}; 723 720 724 721 /** @} */ 725 722 726 } /* namespace iprt */727 728 723 #endif /* !___iprt_cpp_list_h */ 729 724 -
trunk/include/iprt/cpp/meta.h
r36524 r36532 27 27 #define ___iprt_cpp_meta_h 28 28 29 namespace iprt30 {31 32 29 /** @defgroup grp_rt_cpp_meta C++ Meta programming utilities 33 30 * @ingroup grp_rt_cpp … … 44 41 */ 45 42 template <bool Condition, typename TrueResult, typename FalseResult> 46 struct if_;43 struct RTCIf; 47 44 48 45 /** … … 53 50 */ 54 51 template <typename TrueResult, typename FalseResult> 55 struct if_<true, TrueResult, FalseResult>52 struct RTCIf<true, TrueResult, FalseResult> 56 53 { 57 54 typedef TrueResult result; … … 65 62 */ 66 63 template <typename TrueResult, typename FalseResult> 67 struct if_<false, TrueResult, FalseResult>64 struct RTCIf<false, TrueResult, FalseResult> 68 65 { 69 66 typedef FalseResult result; … … 81 78 */ 82 79 template <class T, typename TrueResult, typename FalseResult> 83 struct if_ptr80 struct RTCIfPtr 84 81 { 85 82 typedef FalseResult result; … … 97 94 */ 98 95 template <class T, typename TrueResult, typename FalseResult> 99 struct if_ptr<T*, TrueResult, FalseResult>96 struct RTCIfPtr<T*, TrueResult, FalseResult> 100 97 { 101 98 typedef TrueResult result; … … 104 101 /** @} */ 105 102 106 } /* namespace iprt */107 108 103 #endif /* !___iprt_cpp_meta_h */ 109 104 -
trunk/include/iprt/cpp/ministring.h
r36527 r36532 825 825 * @returns separated strings as string list. 826 826 */ 827 iprt::list<RTCString, RTCString *> split(const RTCString &a_rstrSep,828 827 RTCList<RTCString, RTCString *> split(const RTCString &a_rstrSep, 828 SplitMode a_enmMode = RemoveEmptyParts); 829 829 830 830 /** … … 835 835 * @returns joined string. 836 836 */ 837 static RTCString join(const iprt::list<RTCString, RTCString *> &a_rList,837 static RTCString join(const RTCList<RTCString, RTCString *> &a_rList, 838 838 const RTCString &a_rstrSep = ""); 839 839 -
trunk/include/iprt/cpp/mtlist.h
r36524 r36532 31 31 #include <iprt/semaphore.h> 32 32 33 namespace iprt34 {35 36 33 /** @addtogroup grp_rt_cpp_list 37 34 * @{ … … 42 39 */ 43 40 template <> 44 class ListGuard<true>41 class RTCListGuard<true> 45 42 { 46 43 public: 47 ListGuard() { int rc = RTSemRWCreate(&m_hRWSem); AssertRC(rc); }48 ~ ListGuard() { RTSemRWDestroy(m_hRWSem); }44 RTCListGuard() { int rc = RTSemRWCreate(&m_hRWSem); AssertRC(rc); } 45 ~RTCListGuard() { RTSemRWDestroy(m_hRWSem); } 49 46 inline void enterRead() const { int rc = RTSemRWRequestRead(m_hRWSem, RT_INDEFINITE_WAIT); AssertRC(rc); } 50 47 inline void leaveRead() const { int rc = RTSemRWReleaseRead(m_hRWSem); AssertRC(rc); } … … 59 56 * @brief Generic thread-safe list class. 60 57 * 61 * mtlist is a thread-safe implementation of the list class. It uses a58 * RTCMTList is a thread-safe implementation of the list class. It uses a 62 59 * read/write semaphore to serialize the access to the items. Several readers 63 60 * can simultaneous access different or the same item. If one thread is writing … … 70 67 * implementation. 71 68 * 72 * @see ListBase69 * @see RTCListBase 73 70 */ 74 template <class T, typename ITYPE = typename if_<(sizeof(T) > sizeof(void*)), T*, T>::result>75 class mtlist : publicListBase<T, ITYPE, true> {};71 template <class T, typename ITYPE = typename RTCIf<(sizeof(T) > sizeof(void*)), T*, T>::result> 72 class RTCMTList : public RTCListBase<T, ITYPE, true> {}; 76 73 77 74 /** … … 79 76 * unsigned 64-bit values even on a 32-bit host. 80 77 * 81 * @see ListBase78 * @see RTCListBase 82 79 */ 83 80 template <> 84 class mtlist<uint64_t>: publicListBase<uint64_t, uint64_t, true> {};81 class RTCMTList<uint64_t>: public RTCListBase<uint64_t, uint64_t, true> {}; 85 82 86 83 /** … … 88 85 * signed 64-bit values even on a 32-bit host. 89 86 * 90 * @see ListBase87 * @see RTCListBase 91 88 */ 92 89 template <> 93 class mtlist<int64_t>: publicListBase<int64_t, uint64_t, true> {};90 class RTCMTList<int64_t>: public RTCListBase<int64_t, uint64_t, true> {}; 94 91 95 92 /** @} */ 96 93 97 } /* namespace iprt */98 99 94 #endif /* !___iprt_cpp_mtlist_h */ 100 95
Note:
See TracChangeset
for help on using the changeset viewer.