VirtualBox

Changeset 36532 in vbox for trunk/include/iprt/cpp


Ignore:
Timestamp:
Apr 4, 2011 2:41:14 PM (14 years ago)
Author:
vboxsync
Message:

IPRT-C++: iprt::list -> RTCList; iprt::mtlist -> RTCMTList

Location:
trunk/include/iprt/cpp
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/iprt/cpp/list.h

    r36527 r36532  
    3333#include <new> /* For std::bad_alloc */
    3434
    35 namespace iprt
    36 {
    37 
    3835/** @defgroup grp_rt_cpp_list   C++ List support
    3936 * @ingroup grp_rt_cpp
     
    5855 *
    5956 * The size of the internal array will usually not shrink, but grow
    60  * automatically. Only certain methods, like list::clear or the "=" operator
    61  * will reset any previously allocated memory. You can call list::setCapacity
    62  * for manual adjustment. If the size of an new list will be known, calling the
    63  * constructor with the necessary capacity will speed up the insertion of the
    64  * 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.
    6764 *
    6865 * There are some requirements for the types used which follow:
     
    9087 * changed when using the value as reference returned by this operator.
    9188 *
    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.
    9390 *
    9491 * Implementation details:
     
    9996 *
    10097 * 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>
    103100 *
    104101 * @{
     
    109106 */
    110107template <bool G>
    111 class ListGuard;
     108class RTCListGuard;
    112109
    113110/**
     
    115112 */
    116113template <>
    117 class ListGuard<false>
     114class RTCListGuard<false>
    118115{
    119116public:
     
    125122
    126123/**
    127  * General helper template for managing native values in ListBase.
     124 * General helper template for managing native values in RTCListBase.
    128125 */
    129126template <typename T1, typename T2>
    130 class ListHelper
     127class RTCListHelper
    131128{
    132129public:
     
    143140
    144141/**
    145  * Specialized helper template for managing pointer values in ListBase.
     142 * Specialized helper template for managing pointer values in RTCListBase.
    146143 */
    147144template <typename T1>
    148 class ListHelper<T1, T1*>
     145class RTCListHelper<T1, T1*>
    149146{
    150147public:
     
    170167 */
    171168template <class T, typename ITYPE, bool MT>
    172 class ListBase
     169class RTCListBase
    173170{
    174171    /**
     
    177174     * value.
    178175     */
    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;
    181178
    182179public:
     
    189186     * @throws  std::bad_alloc
    190187     */
    191     ListBase(size_t cCapacity = DefaultCapacity)
     188    RTCListBase(size_t cCapacity = DefaultCapacity)
    192189      : m_pArray(0)
    193190      , m_cSize(0)
     
    206203     * @throws  std::bad_alloc
    207204     */
    208     ListBase(const ListBase<T, ITYPE, MT>& other)
     205    RTCListBase(const RTCListBase<T, ITYPE, MT>& other)
    209206      : m_pArray(0)
    210207      , m_cSize(0)
     
    212209    {
    213210        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);
    215212        m_cSize = other.m_cSize;
    216213    }
     
    219216     * Destructor.
    220217     */
    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);
    224221        if (m_pArray)
    225222            RTMemFree(m_pArray);
     
    273270     * @throws  std::bad_alloc
    274271     */
    275     ListBase<T, ITYPE, MT> &insert(size_t i, const T &val)
     272    RTCListBase<T, ITYPE, MT> &insert(size_t i, const T &val)
    276273    {
    277274        m_guard.enterWrite();
     
    279276            realloc_grow(m_cCapacity + DefaultCapacity);
    280277        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);
    282279        ++m_cSize;
    283280        m_guard.leaveWrite();
     
    293290     * @throws  std::bad_alloc
    294291     */
    295     ListBase<T, ITYPE, MT> &prepend(const T &val)
     292    RTCListBase<T, ITYPE, MT> &prepend(const T &val)
    296293    {
    297294        return insert(0, val);
     
    305302     * @throws  std::bad_alloc
    306303     */
    307     ListBase<T, ITYPE, MT> &prepend(const ListBase<T, ITYPE, MT> &other)
     304    RTCListBase<T, ITYPE, MT> &prepend(const RTCListBase<T, ITYPE, MT> &other)
    308305    {
    309306        m_guard.enterWrite();
     
    311308            realloc_grow(m_cCapacity + (other.m_cSize - (m_cCapacity - m_cSize)));
    312309        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);
    314311        m_cSize += other.m_cSize;
    315312        m_guard.leaveWrite();
     
    325322     * @throws  std::bad_alloc
    326323     */
    327     ListBase<T, ITYPE, MT> &append(const T &val)
     324    RTCListBase<T, ITYPE, MT> &append(const T &val)
    328325    {
    329326        m_guard.enterWrite();
    330327        if (m_cSize == m_cCapacity)
    331328            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);
    333330        ++m_cSize;
    334331        m_guard.leaveWrite();
     
    344341     * @throws  std::bad_alloc
    345342     */
    346     ListBase<T, ITYPE, MT> &append(const ListBase<T, ITYPE, MT> &other)
     343    RTCListBase<T, ITYPE, MT> &append(const RTCListBase<T, ITYPE, MT> &other)
    347344    {
    348345        m_guard.enterWrite();
    349346        if (m_cCapacity - m_cSize < other.m_cSize)
    350347            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);
    352349        m_cSize += other.m_cSize;
    353350        m_guard.leaveWrite();
     
    363360     * @return  a reference to this list.
    364361     */
    365     ListBase<T, ITYPE, MT> &operator=(const ListBase<T, ITYPE, MT>& other)
     362    RTCListBase<T, ITYPE, MT> &operator=(const RTCListBase<T, ITYPE, MT>& other)
    366363    {
    367364        /* Prevent self assignment */
     
    371368        m_guard.enterWrite();
    372369        /* Values cleanup */
    373         ListHelper<T, ITYPE>::eraseRange(m_pArray, 0, m_cSize);
     370        RTCListHelper<T, ITYPE>::eraseRange(m_pArray, 0, m_cSize);
    374371
    375372        /* Copy */
     
    377374            realloc_grow(other.m_cSize);
    378375        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);
    380377        m_guard.leaveWrite();
    381378
     
    387384     *
    388385     * @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.
    390387     *
    391388     * @param   i     The position of the item to replace.
     
    393390     * @return  a reference to this list.
    394391     */
    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);
    400397        m_guard.leaveWrite();
    401398
     
    407404     *
    408405     * @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.
    410407     *
    411408     * @return   The first item.
     
    414411    {
    415412        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);
    417414        m_guard.leaveRead();
    418415        return res;
     
    423420     *
    424421     * @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.
    426423     *
    427424     * @return   The first item.
     
    430427    {
    431428        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);
    433430        m_guard.leaveRead();
    434431        return res;
     
    439436     *
    440437     * @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.
    442439     *
    443440     * @return   The last item.
     
    446443    {
    447444        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);
    449446        m_guard.leaveRead();
    450447        return res;
     
    455452     *
    456453     * @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.
    458455     *
    459456     * @return   The last item.
     
    462459    {
    463460        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);
    465462        m_guard.leaveRead();
    466463        return res;
     
    471468     *
    472469     * @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.
    474471     *
    475472     * @param   i     The position of the item to return.
     
    479476    {
    480477        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);
    482479        m_guard.leaveRead();
    483480        return res;
     
    488485     *
    489486     * @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.
    491488     *
    492489     * @param   i     The position of the item to return.
     
    496493    {
    497494        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);
    499496        m_guard.leaveRead();
    500497        return res;
     
    505502     *
    506503     * @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.
    508505     *
    509506     * @param   i     The position of the item to return.
     
    513510    {
    514511        m_guard.enterRead();
    515         T &res = ListHelper<T, ITYPE>::at(m_pArray, i);
     512        T &res = RTCListHelper<T, ITYPE>::at(m_pArray, i);
    516513        m_guard.leaveRead();
    517514        return res;
     
    533530            return T();
    534531        }
    535         T res = ListHelper<T, ITYPE>::at(m_pArray, i);
     532        T res = RTCListHelper<T, ITYPE>::at(m_pArray, i);
    536533        m_guard.leaveRead();
    537534        return res;
     
    554551            return defaultVal;
    555552        }
    556         T res = ListHelper<T, ITYPE>::at(m_pArray, i);
     553        T res = RTCListHelper<T, ITYPE>::at(m_pArray, i);
    557554        m_guard.leaveRead();
    558555        return res;
     
    563560     *
    564561     * @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.
    566563     *
    567564     * @param   i   The position of the item to remove.
     
    570567    {
    571568        m_guard.enterWrite();
    572         ListHelper<T, ITYPE>::erase(m_pArray, i);
     569        RTCListHelper<T, ITYPE>::erase(m_pArray, i);
    573570        /* Not last element? */
    574571        if (i < m_cSize - 1)
     
    582579     *
    583580     * @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 be
    585      *       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.
    586583     *
    587584     * @param   iFrom   The start position of the items to remove.
     
    591588    {
    592589        m_guard.enterWrite();
    593         ListHelper<T, ITYPE>::eraseRange(m_pArray, iFrom, iTo - iFrom);
     590        RTCListHelper<T, ITYPE>::eraseRange(m_pArray, iFrom, iTo - iFrom);
    594591        /* Not last elements? */
    595592        if (m_cSize - iTo > 0)
     
    606603        m_guard.enterWrite();
    607604        /* Values cleanup */
    608         ListHelper<T, ITYPE>::eraseRange(m_pArray, 0, m_cSize);
     605        RTCListHelper<T, ITYPE>::eraseRange(m_pArray, 0, m_cSize);
    609606        if (m_cSize != DefaultCapacity)
    610607            realloc_grow(DefaultCapacity);
     
    633630            && m_pArray)
    634631        {
    635             ListHelper<T, ITYPE>::eraseRange(m_pArray, cNewSize, m_cSize - cNewSize);
     632            RTCListHelper<T, ITYPE>::eraseRange(m_pArray, cNewSize, m_cSize - cNewSize);
    636633            m_cSize -= m_cSize - cNewSize;
    637634        }
     
    690687    size_t m_cCapacity;
    691688    /** The guard used to serialize the access to the items. */
    692     ListGuard<MT> m_guard;
     689    RTCListGuard<MT> m_guard;
    693690};
    694691
    695692template <class T, typename ITYPE, bool MT>
    696 const size_t ListBase<T, ITYPE, MT>::DefaultCapacity = 10;
     693const size_t RTCListBase<T, ITYPE, MT>::DefaultCapacity = 10;
    697694
    698695/**
    699696 * Template class which automatically determines the type of list to use.
    700697 *
    701  * @see ListBase
    702  */
    703 template <class T, typename ITYPE = typename if_<(sizeof(T) > sizeof(void*)), T*, T>::result>
    704 class list : public ListBase<T, ITYPE, false> {};
     698 * @see RTCListBase
     699 */
     700template <class T, typename ITYPE = typename RTCIf<(sizeof(T) > sizeof(void*)), T*, T>::result>
     701class RTCList : public RTCListBase<T, ITYPE, false> {};
    705702
    706703/**
     
    708705 * values even on a 32-bit host.
    709706 *
    710  * @see ListBase
     707 * @see RTCListBase
    711708 */
    712709template <>
    713 class list<uint64_t>: public ListBase<uint64_t, uint64_t, false> {};
     710class RTCList<uint64_t>: public RTCListBase<uint64_t, uint64_t, false> {};
    714711
    715712/**
     
    717714 * values even on a 32-bit host.
    718715 *
    719  * @see ListBase
     716 * @see RTCListBase
    720717 */
    721718template <>
    722 class list<int64_t>: public ListBase<int64_t, int64_t, false> {};
     719class RTCList<int64_t>: public RTCListBase<int64_t, int64_t, false> {};
    723720
    724721/** @} */
    725722
    726 } /* namespace iprt */
    727 
    728723#endif /* !___iprt_cpp_list_h */
    729724
  • trunk/include/iprt/cpp/meta.h

    r36524 r36532  
    2727#define ___iprt_cpp_meta_h
    2828
    29 namespace iprt
    30 {
    31 
    3229/** @defgroup grp_rt_cpp_meta   C++ Meta programming utilities
    3330 * @ingroup grp_rt_cpp
     
    4441 */
    4542template <bool Condition, typename TrueResult, typename FalseResult>
    46 struct if_;
     43struct RTCIf;
    4744
    4845/**
     
    5350 */
    5451template <typename TrueResult, typename FalseResult>
    55 struct if_<true, TrueResult, FalseResult>
     52struct RTCIf<true, TrueResult, FalseResult>
    5653{
    5754    typedef TrueResult result;
     
    6562 */
    6663template <typename TrueResult, typename FalseResult>
    67 struct if_<false, TrueResult, FalseResult>
     64struct RTCIf<false, TrueResult, FalseResult>
    6865{
    6966    typedef FalseResult result;
     
    8178 */
    8279template <class T, typename TrueResult, typename FalseResult>
    83 struct if_ptr
     80struct RTCIfPtr
    8481{
    8582    typedef FalseResult result;
     
    9794 */
    9895template <class T, typename TrueResult, typename FalseResult>
    99 struct if_ptr<T*, TrueResult, FalseResult>
     96struct RTCIfPtr<T*, TrueResult, FalseResult>
    10097{
    10198    typedef TrueResult result;
     
    104101/** @} */
    105102
    106 } /* namespace iprt */
    107 
    108103#endif /* !___iprt_cpp_meta_h */
    109104
  • trunk/include/iprt/cpp/ministring.h

    r36527 r36532  
    825825     * @returns separated strings as string list.
    826826     */
    827     iprt::list<RTCString, RTCString *> split(const RTCString &a_rstrSep,
    828                                              SplitMode a_enmMode = RemoveEmptyParts);
     827    RTCList<RTCString, RTCString *> split(const RTCString &a_rstrSep,
     828                                          SplitMode a_enmMode = RemoveEmptyParts);
    829829
    830830    /**
     
    835835     * @returns joined string.
    836836     */
    837     static RTCString join(const iprt::list<RTCString, RTCString *> &a_rList,
     837    static RTCString join(const RTCList<RTCString, RTCString *> &a_rList,
    838838                          const RTCString &a_rstrSep = "");
    839839
  • trunk/include/iprt/cpp/mtlist.h

    r36524 r36532  
    3131#include <iprt/semaphore.h>
    3232
    33 namespace iprt
    34 {
    35 
    3633/** @addtogroup grp_rt_cpp_list
    3734 * @{
     
    4239 */
    4340template <>
    44 class ListGuard<true>
     41class RTCListGuard<true>
    4542{
    4643public:
    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); }
    4946    inline void enterRead() const { int rc = RTSemRWRequestRead(m_hRWSem, RT_INDEFINITE_WAIT); AssertRC(rc); }
    5047    inline void leaveRead() const { int rc = RTSemRWReleaseRead(m_hRWSem); AssertRC(rc); }
     
    5956 * @brief Generic thread-safe list class.
    6057 *
    61  * mtlist is a thread-safe implementation of the list class. It uses a
     58 * RTCMTList is a thread-safe implementation of the list class. It uses a
    6259 * read/write semaphore to serialize the access to the items. Several readers
    6360 * can simultaneous access different or the same item. If one thread is writing
     
    7067 * implementation.
    7168 *
    72  * @see ListBase
     69 * @see RTCListBase
    7370 */
    74 template <class T, typename ITYPE = typename if_<(sizeof(T) > sizeof(void*)), T*, T>::result>
    75 class mtlist : public ListBase<T, ITYPE, true> {};
     71template <class T, typename ITYPE = typename RTCIf<(sizeof(T) > sizeof(void*)), T*, T>::result>
     72class RTCMTList : public RTCListBase<T, ITYPE, true> {};
    7673
    7774/**
     
    7976 * unsigned 64-bit values even on a 32-bit host.
    8077 *
    81  * @see ListBase
     78 * @see RTCListBase
    8279 */
    8380template <>
    84 class mtlist<uint64_t>: public ListBase<uint64_t, uint64_t, true> {};
     81class RTCMTList<uint64_t>: public RTCListBase<uint64_t, uint64_t, true> {};
    8582
    8683/**
     
    8885 * signed 64-bit values even on a 32-bit host.
    8986 *
    90  * @see ListBase
     87 * @see RTCListBase
    9188 */
    9289template <>
    93 class mtlist<int64_t>: public ListBase<int64_t, uint64_t, true> {};
     90class RTCMTList<int64_t>: public RTCListBase<int64_t, uint64_t, true> {};
    9491
    9592/** @} */
    9693
    97 } /* namespace iprt */
    98 
    9994#endif /* !___iprt_cpp_mtlist_h */
    10095
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette