VirtualBox

Changeset 36508 in vbox for trunk


Ignore:
Timestamp:
Apr 1, 2011 3:03:59 PM (14 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
70924
Message:

iprt/C++: some cleanup.

Location:
trunk
Files:
12 edited

Legend:

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

    r36499 r36508  
    44
    55/*
    6  * Copyright (C) 2008 Oracle Corporation
     6 * Copyright (C) 2008-2011 Oracle Corporation
    77 *
    88 * This file is part of VirtualBox Open Source Edition (OSE), as
     
    3131
    3232/**
    33  * @addtogroup grp_rt_cpp_util
    34  * @{
    35  */
    36 
    37 /**
    3833 * A simple class used to prevent copying and assignment.
    3934 *
    4035 * Inherit from this class in order to prevent automatic generation
    4136 * of the copy constructor and assignment operator in your class.
     37 *
     38 * @todo Functionality duplicated by iprt::non_copyable. grr!
     39 *
     40 * @addtogroup grp_rt_cpp_util
    4241 */
    4342class RTCNonCopyable
     
    5150};
    5251
    53 /** @} */
    54 
    55 /**
    56  * @defgroup grp_rt_cpp_autores    C++ Resource Management
     52
     53/** @defgroup grp_rt_cpp_autores    C++ Resource Management
    5754 * @ingroup grp_rt_cpp
    5855 * @{
     
    8582 */
    8683template <class T>
    87 inline void RTAutoResDestruct(T aHandle)
     84inline void RTAutoResDestruct(T a_h)
    8885{
    8986    AssertFatalMsgFailed(("Unspecialized template!\n"));
    90     NOREF(aHandle);
     87    NOREF(a_h);
    9188}
    9289
  • trunk/include/iprt/cpp/exception.h

    r36499 r36508  
    44
    55/*
    6  * Copyright (C) 2006-2010 Oracle Corporation
     6 * Copyright (C) 2006-2011 Oracle Corporation
    77 *
    88 * This file is part of VirtualBox Open Source Edition (OSE), as
     
    3232{
    3333
    34 /**
    35  * @defgroup grp_rt_cpp_exceptions    C++ Exceptions
     34/** @defgroup grp_rt_cpp_exceptions     C++ Exceptions
    3635 * @ingroup grp_rt_cpp
    3736 * @{
  • trunk/include/iprt/cpp/list.h

    r36500 r36508  
    2929#include <iprt/cpp/meta.h>
    3030#include <iprt/mem.h>
     31#include <iprt/string.h> /* for memcpy */
    3132
    3233#include <new> /* For std::bad_alloc */
     
    3536{
    3637
    37 /**
    38  * @defgroup grp_rt_cpp_list   C++ List support
     38/** @defgroup grp_rt_cpp_list   C++ List support
    3939 * @ingroup grp_rt_cpp
    4040 *
     
    312312    {
    313313        /* Prevent self assignment */
    314         if (this == &other) return *this;
     314        if (this == &other)
     315            return *this;
     316
    315317        /* Values cleanup */
    316318        ListHelper<T, list_type>::eraseRange(m_pArray, 0, m_cSize);
     319
    317320        /* Copy */
    318321        if (other.m_cSize != m_cCapacity)
     
    520523
    521524    /**
    522       * Generic realloc, which does some kind of boundary checking.
     525     * Generic realloc, which does some kind of boundary checking.
    523526     */
    524527    void realloc(size_t cNewSize)
     
    527530        if (cNewSize == m_cCapacity)
    528531            return;
     532
    529533        /* If we get smaller we have to delete some of the objects at the end
    530534           of the list. */
     
    535539            m_cSize -= m_cSize - cNewSize;
    536540        }
     541
    537542        /* If we get zero we delete the array it self. */
    538543        if (   cNewSize == 0
     
    543548        }
    544549        m_cCapacity = cNewSize;
     550
    545551        /* Resize the array. */
    546552        if (cNewSize > 0)
     
    549555            if (!m_pArray)
    550556            {
     557                /** @todo you leak memory. */
    551558                m_cCapacity = 0;
    552559                m_cSize = 0;
    553560#ifdef RT_EXCEPTIONS_ENABLED
    554561                throw std::bad_alloc();
    555 #endif /* RT_EXCEPTIONS_ENABLED */
     562#endif
    556563            }
    557564        }
     
    570577        if (!m_pArray)
    571578        {
     579            /** @todo you leak memory. */
    572580            m_cCapacity = 0;
    573581            m_cSize = 0;
    574582#ifdef RT_EXCEPTIONS_ENABLED
    575583            throw std::bad_alloc();
    576 #endif /* RT_EXCEPTIONS_ENABLED */
     584#endif
    577585        }
    578586    }
     
    604612 */
    605613template <class T, typename TYPE = typename if_<(sizeof(T) > sizeof(void*)), T*, T>::result>
    606 class list: public ListBase<T, TYPE> {};
     614class list : public ListBase<T, TYPE> {};
    607615
    608616/**
     
    628636} /* namespace iprt */
    629637
    630 #endif /* ___iprt_cpp_list_h */
    631 
     638#endif /* !___iprt_cpp_list_h */
     639
  • trunk/include/iprt/cpp/lock.h

    r36499 r36508  
    44
    55/*
    6  * Copyright (C) 2007 Oracle Corporation
     6 * Copyright (C) 2007-2011 Oracle Corporation
    77 *
    88 * This file is part of VirtualBox Open Source Edition (OSE), as
     
    3434RT_C_DECLS_BEGIN
    3535
    36 /**
    37  * @defgroup grp_rt_cpp_lock       C++ Scope-based Locking
     36/** @defgroup grp_rt_cpp_lock       C++ Scope-based Locking
    3837 * @ingroup grp_rt_cpp
    3938 * @{
     
    5352class RTLockMtx
    5453{
    55     friend class RTLock;
     54friend class RTLock;
    5655
    57     private:
    58         RTCRITSECT      mMtx;
     56private:
     57    RTCRITSECT      mMtx;
    5958
    60     public:
    61         RTLockMtx()
    62         {
     59public:
     60    RTLockMtx()
     61    {
    6362#ifdef RT_LOCK_STRICT_ORDER
    64             RTCritSectInitEx(&mMtx, 0 /*fFlags*/,
    65                              RTLockValidatorClassCreateUnique(RT_SRC_POS, NULL),
    66                              RTLOCKVAL_SUB_CLASS_NONE, NULL);
     63        RTCritSectInitEx(&mMtx, 0 /*fFlags*/,
     64                         RTLockValidatorClassCreateUnique(RT_SRC_POS, NULL),
     65                         RTLOCKVAL_SUB_CLASS_NONE, NULL);
    6766#else
    68             RTCritSectInit(&mMtx);
     67        RTCritSectInit(&mMtx);
    6968#endif
    70         }
     69    }
    7170
    72         /** Use to when creating locks that belongs in the same "class".  */
    73         RTLockMtx(RT_SRC_POS_DECL, uint32_t uSubClass = RTLOCKVAL_SUB_CLASS_NONE)
    74         {
     71    /** Use to when creating locks that belongs in the same "class".  */
     72    RTLockMtx(RT_SRC_POS_DECL, uint32_t uSubClass = RTLOCKVAL_SUB_CLASS_NONE)
     73    {
    7574#ifdef RT_LOCK_STRICT_ORDER
    76             RTCritSectInitEx(&mMtx, 0 /*fFlags*/,
    77                              RTLockValidatorClassForSrcPos(RT_SRC_POS_ARGS, NULL),
    78                              uSubClass, NULL);
     75        RTCritSectInitEx(&mMtx, 0 /*fFlags*/,
     76                         RTLockValidatorClassForSrcPos(RT_SRC_POS_ARGS, NULL),
     77                         uSubClass, NULL);
    7978#else
    80             NOREF(uSubClass);
    81             RTCritSectInit(&mMtx);
    82             RT_SRC_POS_NOREF();
     79        NOREF(uSubClass);
     80        RTCritSectInit(&mMtx);
     81        RT_SRC_POS_NOREF();
    8382#endif
    84         }
     83    }
    8584
    86         ~RTLockMtx()
    87         {
    88             RTCritSectDelete(&mMtx);
    89         }
     85    ~RTLockMtx()
     86    {
     87        RTCritSectDelete(&mMtx);
     88    }
    9089
    91     // lock() and unlock() are private so that only
    92     // friend RTLock can access them
    93     private:
    94         inline void lock()
    95         {
    96             RTCritSectEnter(&mMtx);
    97         }
     90// lock() and unlock() are private so that only
     91// friend RTLock can access them
     92private:
     93    inline void lock()
     94    {
     95        RTCritSectEnter(&mMtx);
     96    }
    9897
    99         inline void unlock()
    100         {
    101             RTCritSectLeave(&mMtx);
    102         }
     98    inline void unlock()
     99    {
     100        RTCritSectLeave(&mMtx);
     101    }
    103102};
    104103
     
    129128class RTLock
    130129{
    131     private:
    132         RTLockMtx  &mMtx;
    133         bool        mfLocked;
     130private:
     131    RTLockMtx  &mMtx;
     132    bool        mfLocked;
    134133
    135     public:
    136         RTLock(RTLockMtx &aMtx)
    137             : mMtx(aMtx)
     134public:
     135    RTLock(RTLockMtx &aMtx)
     136        : mMtx(aMtx)
     137    {
     138        mMtx.lock();
     139        mfLocked = true;
     140    }
     141
     142    ~RTLock()
     143    {
     144        if (mfLocked)
     145            mMtx.unlock();
     146    }
     147
     148    inline void release()
     149    {
     150        if (mfLocked)
    138151        {
    139             mMtx.lock();
    140             mfLocked = true;
     152            mMtx.unlock();
     153            mfLocked = false;
    141154        }
    142 
    143         ~RTLock()
    144         {
    145             if (mfLocked)
    146                 mMtx.unlock();
    147         }
    148 
    149         inline void release()
    150         {
    151             if (mfLocked)
    152             {
    153                 mMtx.unlock();
    154                 mfLocked = false;
    155             }
    156         }
     155    }
    157156};
    158157
  • trunk/include/iprt/cpp/meta.h

    r36500 r36508  
    3030{
    3131
    32 /**
    33  * @defgroup grp_rt_cpp_meta   C++ Meta programming utilities
     32/** @defgroup grp_rt_cpp_meta   C++ Meta programming utilities
    3433 * @ingroup grp_rt_cpp
    3534 * @{
  • trunk/include/iprt/cpp/ministring.h

    r36501 r36508  
    44
    55/*
    6  * Copyright (C) 2007-2009 Oracle Corporation
     6 * Copyright (C) 2007-2011 Oracle Corporation
    77 *
    88 * This file is part of VirtualBox Open Source Edition (OSE), as
     
    3737{
    3838
    39 /**
    40  * @defgroup grp_rt_cpp_string     C++ String support
     39/** @defgroup grp_rt_cpp_string     C++ String support
    4140 * @ingroup grp_rt_cpp
    4241 * @{
    4342 */
    4443
    45 /**
    46  * @brief Mini C++ string class.
     44/** @brief  Mini C++ string class.
    4745 *
    4846 * "MiniString" is a small C++ string class that does not depend on anything
     
    825823     * Splits a string separated by strSep into its parts.
    826824     *
    827      * @param   strSep       The separator to search for.
    828      * @param   mode         How should empty parts be handled.
     825     * @param   a_rstrSep   The separator to search for.
     826     * @param   a_enmMode   How should empty parts be handled.
    829827     * @returns separated strings as string list.
    830828     */
    831     iprt::list<iprt::MiniString, iprt::MiniString*> split(const iprt::MiniString &strSep, SplitMode mode = RemoveEmptyParts);
     829    iprt::list<iprt::MiniString, iprt::MiniString *> split(const iprt::MiniString &a_rstrSep,
     830                                                           SplitMode a_enmMode = RemoveEmptyParts);
    832831
    833832    /**
    834833     * Joins a list of strings together using the provided separator.
    835834     *
    836      * @param   list         The list to join.
    837      * @param   strSep       The separator used for joining.
     835     * @param   a_rList     The list to join.
     836     * @param   a_rstrSep   The separator used for joining.
    838837     * @returns joined string.
    839838     */
    840     static iprt::MiniString join(const iprt::list<iprt::MiniString, iprt::MiniString*> &list, const iprt::MiniString &strSep = "");
    841        
     839    static iprt::MiniString join(const iprt::list<iprt::MiniString, iprt::MiniString *> &a_rList,
     840                                 const iprt::MiniString &a_rstrSep = "");
     841
    842842protected:
    843843
     
    919919} /* namespace iprt */
    920920
    921 /**
    922  * @addtogroup grp_rt_cpp_string
     921/** @addtogroup grp_rt_cpp_string
    923922 * @{
    924923 */
    925924
    926925/**
     926 * Concatenate two strings.
     927 *
     928 * @param   a_rstr1     String one.
     929 * @param   a_rstr2     String two.
     930 * @returns the concatenate string.
     931 *
    927932 * @relates iprt::MiniString
    928  *
     933 */
     934RTDECL(const iprt::MiniString) operator+(const iprt::MiniString &a_rstr1, const iprt::MiniString &a_rstr2);
     935
     936/**
    929937 * Concatenate two strings.
    930938 *
    931  * @param   one        String one.
    932  * @param   other      String two.
     939 * @param   a_rstr1     String one.
     940 * @param   a_psz2      String two.
    933941 * @returns the concatenate string.
     942 *
     943 * @relates iprt::MiniString
    934944 */
    935 RTDECL(const iprt::MiniString) operator+(const iprt::MiniString &one, const iprt::MiniString &other);
     945RTDECL(const iprt::MiniString) operator+(const iprt::MiniString &a_rstr1, const char *a_psz2);
    936946
    937947/**
     948 * Concatenate two strings.
     949 *
     950 * @param   a_psz1      String one.
     951 * @param   a_rstr2     String two.
     952 * @returns the concatenate string.
     953 *
    938954 * @relates iprt::MiniString
    939  *
    940  * Concatenate two strings.
    941  *
    942  * @param   one        String one.
    943  * @param   pcszOther  String two.
    944  * @returns the concatenate string.
    945955 */
    946 RTDECL(const iprt::MiniString) operator+(const iprt::MiniString &one, const char *pcszOther);
    947 
    948 /**
    949  * @relates iprt::MiniString
    950  *
    951  * Concatenate two strings.
    952  *
    953  * @param   pcszOne    String one.
    954  * @param   other      String two.
    955  * @returns the concatenate string.
    956  */
    957 RTDECL(const iprt::MiniString) operator+(const char *pcszOne, const iprt::MiniString &other);
     956RTDECL(const iprt::MiniString) operator+(const char *a_psz1, const iprt::MiniString &a_rstr2);
    958957
    959958/** @} */
  • trunk/include/iprt/cpp/utils.h

    r36499 r36508  
    44
    55/*
    6  * Copyright (C) 2006-2007 Oracle Corporation
     6 * Copyright (C) 2006-2011 Oracle Corporation
    77 *
    88 * This file is part of VirtualBox Open Source Edition (OSE), as
     
    2727#define ___iprt_cpputils_h
    2828
    29 /**
    30  * @defgroup grp_rt_cpp         IPRT C++ support
    31  * @ingroup grp_rt
    32  */
     29/** @defgroup grp_rt_cpp        IPRT C++ APIs */
    3330
    34 /**
    35  * @defgroup grp_rt_cpp_util    C++ Utilitis
     31/** @defgroup grp_rt_cpp_util   C++ Utilities
    3632 * @ingroup grp_rt_cpp
    3733 * @{
     
    4137 * Shortcut to |const_cast<C &>()| that automatically derives the correct
    4238 * type (class) for the const_cast template's argument from its own argument.
     39 *
    4340 * Can be used to temporarily cancel the |const| modifier on the left-hand side
    4441 * of assignment expressions, like this:
    4542 * @code
    46  *      const Class that;
     43 *      const Class That;
    4744 *      ...
    48  *      unconst (that) = some_value;
     45 *      unconst(That) = SomeValue;
    4946 * @endcode
    5047 */
    5148template <class C>
    52 inline C& unconst(const C &that) { return const_cast<C&>(that); }
     49inline C &unconst(const C &that)
     50{
     51    return const_cast<C &>(that);
     52}
    5353
    5454
     
    5656 * Shortcut to |const_cast<C *>()| that automatically derives the correct
    5757 * type (class) for the const_cast template's argument from its own argument.
     58 *
    5859 * Can be used to temporarily cancel the |const| modifier on the left-hand side
    5960 * of assignment expressions, like this:
    6061 * @code
    61  *      const Class *that;
     62 *      const Class *pThat;
    6263 *      ...
    63  *      unconst (that) = some_value;
     64 *      unconst(pThat) = SomeValue;
    6465 * @endcode
    6566 */
    6667template <class C>
    67 inline C* unconst(const C *that) { return const_cast<C*>(that); }
     68inline C *unconst(const C *that)
     69{
     70    return const_cast<C *>(that);
     71}
    6872
    6973/** @} */
     
    7377
    7478/**
     79 * A simple class used to prevent copying and assignment.
     80 *
     81 * Inherit from this class in order to prevent automatic generation of the copy
     82 * constructor and assignment operator in your class.
     83 *
    7584 * @ingroup grp_rt_cpp_util
    76  * @{
    77  */
    78 
    79 /**
    80  * A simple class used to prevent copying and assignment.  Inherit from this
    81  * class in order to prevent automatic generation of the copy constructor
    82  * and assignment operator in your class.
     85 * @todo Functionality duplicated by RTCNonCopyable. grr!
    8386 */
    8487class non_copyable
     
    8891    ~non_copyable() {}
    8992private:
    90     non_copyable(non_copyable const&);
    91     non_copyable const &operator=(non_copyable const&);
     93    non_copyable(non_copyable const &);
     94    non_copyable const &operator=(non_copyable const &);
    9295};
    9396
    94 /** @} */
     97} /* namespace iprt */
    9598
    96 } // namespace iprt
     99#endif
    97100
    98 #endif // ___iprt_cpputils_h
    99 
  • trunk/include/iprt/cpp/xml.h

    r36499 r36508  
    3636#include <iprt/cpp/exception.h>
    3737
    38 /**
    39  * @defgroup grp_rt_cpp_xml    C++ XML support
     38/** @defgroup grp_rt_cpp_xml    C++ XML support
    4039 * @ingroup grp_rt_cpp
    4140 * @{
     
    747746
    748747#endif /* !___iprt_xml_h */
     748
  • trunk/include/iprt/initterm.h

    r33540 r36508  
    3232RT_C_DECLS_BEGIN
    3333
    34 /** @defgroup grp_rt    IPRT APIs
     34/** @defgroup grp_rt    IPRT C/C++ APIs
    3535 * @{
    3636 */
  • trunk/src/VBox/Runtime/common/string/ministring.cpp

    r36501 r36508  
    88
    99/*
    10  * Copyright (C) 2007-2010 Oracle Corporation
     10 * Copyright (C) 2007-2011 Oracle Corporation
    1111 *
    1212 * This file is part of VirtualBox Open Source Edition (OSE), as
     
    200200}
    201201
    202 size_t MiniString::find(const char *pcszFind, size_t pos /*= 0*/)
    203     const
     202size_t MiniString::find(const char *pcszFind, size_t pos /*= 0*/) const
    204203{
    205204    const char *pszThis, *p;
     
    224223}
    225224
    226 MiniString MiniString::substrCP(size_t pos /*= 0*/, size_t n /*= npos*/)
    227     const
     225MiniString MiniString::substrCP(size_t pos /*= 0*/, size_t n /*= npos*/) const
    228226{
    229227    MiniString ret;
     
    288286    if (cs == CaseSensitive)
    289287        return ::RTStrCmp(&m_psz[l], that.m_psz) == 0;
    290     else
    291         return ::RTStrICmp(&m_psz[l], that.m_psz) == 0;
     288    return ::RTStrICmp(&m_psz[l], that.m_psz) == 0;
    292289}
    293290
     
    304301    if (cs == CaseSensitive)
    305302        return ::RTStrNCmp(m_psz, that.m_psz, l2) == 0;
    306     else
    307         return ::RTStrNICmp(m_psz, that.m_psz, l2) == 0;
     303    return ::RTStrNICmp(m_psz, that.m_psz, l2) == 0;
    308304}
    309305
     
    314310    if (cs == CaseSensitive)
    315311        return ::RTStrStr(m_psz, that.m_psz) != NULL;
    316     else
    317         return ::RTStrIStr(m_psz, that.m_psz) != NULL;
     312    return ::RTStrIStr(m_psz, that.m_psz) != NULL;
    318313}
    319314
     
    332327}
    333328
    334 iprt::list<iprt::MiniString, iprt::MiniString*> MiniString::split(const iprt::MiniString &strSep, SplitMode mode /* = RemoveEmptyParts */)
    335 {
    336     iprt::list<iprt::MiniString> res;
     329iprt::list<iprt::MiniString, iprt::MiniString *>
     330MiniString::split(const iprt::MiniString &a_rstrSep, SplitMode mode /* = RemoveEmptyParts */)
     331{
     332    iprt::list<iprt::MiniString> strRet;
    337333    if (!m_psz)
    338         return res;
    339     if (strSep.isEmpty())
    340     {
    341         res.append(iprt::MiniString(m_psz));
    342         return res;
    343     }
    344 
    345     size_t cch = m_cch;
    346     char *pszTmp = m_psz;
    347     while(cch > 0)
    348     {
    349         char *pszNext = strstr(pszTmp, strSep.c_str());
     334        return strRet;
     335    if (a_rstrSep.isEmpty())
     336    {
     337        strRet.append(iprt::MiniString(m_psz));
     338        return strRet;
     339    }
     340
     341    size_t      cch    = m_cch;
     342    char const *pszTmp = m_psz;
     343    while (cch > 0)
     344    {
     345        char const *pszNext = strstr(pszTmp, a_rstrSep.c_str());
    350346        if (!pszNext)
     347        {
     348            strRet.append(iprt::MiniString(pszTmp, cch));
    351349            break;
    352         size_t chNext = pszNext - pszTmp;
    353         if (   chNext > 0
     350        }
     351        size_t cchNext = pszNext - pszTmp;
     352        if (   cchNext > 0
    354353            || mode == KeepEmptyParts)
    355             res.append(iprt::MiniString(pszTmp, chNext));
    356         pszTmp += chNext + strSep.length();
    357         cch    -= chNext + strSep.length();
    358     }
    359     /* Some characters left at the end? */
    360     if (cch > 0)
    361         res.append(iprt::MiniString(pszTmp, cch));
    362 
    363     return res;
     354            strRet.append(iprt::MiniString(pszTmp, cchNext));
     355        pszTmp += cchNext + a_rstrSep.length();
     356        cch    -= cchNext + a_rstrSep.length();
     357    }
     358
     359    return strRet;
    364360}
    365361
    366362/* static */
    367 iprt::MiniString MiniString::join(const iprt::list<iprt::MiniString, iprt::MiniString*> &list, const iprt::MiniString &strSep /* = "" */)
    368 {
    369     MiniString res;
    370     if (list.size() > 1)
    371     {
    372         for(size_t i = 0; i < list.size() - 1; ++i)
    373             res += list.at(i) + strSep;
    374         res += list.last();
    375     }
    376 
    377     return res;
    378 }
    379 
    380 const iprt::MiniString operator+(const iprt::MiniString &one, const iprt::MiniString &other)
    381 {
    382     iprt::MiniString res(one);
    383 
    384     return res += other;
    385 }
    386 
    387 const iprt::MiniString operator+(const iprt::MiniString &one, const char *pcszOther)
    388 {
    389     iprt::MiniString res(one);
    390 
    391     return res += pcszOther;
    392 }
    393 
    394 const iprt::MiniString operator+(const char *pcszOne, const iprt::MiniString &other)
    395 {
    396     iprt::MiniString res(pcszOne);
    397 
    398     return res += other;
    399 }
    400 
     363iprt::MiniString
     364MiniString::join(const iprt::list<iprt::MiniString, iprt::MiniString*> &a_rList,
     365                 const iprt::MiniString &a_rstrSep /* = "" */)
     366{
     367    MiniString strRet;
     368    if (a_rList.size() > 1)
     369    {
     370        for (size_t i = 0; i < a_rList.size() - 1; ++i)
     371            strRet += a_rList.at(i) + a_rstrSep;
     372        strRet += a_rList.last();
     373    }
     374
     375    return strRet;
     376}
     377
     378const iprt::MiniString operator+(const iprt::MiniString &a_rStr1, const iprt::MiniString &a_rStr2)
     379{
     380    iprt::MiniString strRet(a_rStr1);
     381    strRet += a_rStr2;
     382    return strRet;
     383}
     384
     385const iprt::MiniString operator+(const iprt::MiniString &a_rStr1, const char *a_pszStr2)
     386{
     387    iprt::MiniString strRet(a_rStr1);
     388    strRet += a_pszStr2;
     389    return strRet;
     390}
     391
     392const iprt::MiniString operator+(const char *a_psz1, const iprt::MiniString &a_rStr2)
     393{
     394    iprt::MiniString strRet(a_psz1);
     395    strRet += a_rStr2;
     396    return strRet;
     397}
     398
  • trunk/src/VBox/Runtime/testcase/Makefile.kmk

    r36500 r36508  
    6868        tstRTHeapSimple \
    6969        tstRTInlineAsm \
     70        tstIprtList \
    7071        tstIprtMiniString \
    71         tstIprtList \
    7272        tstLdr \
    7373        tstLdrLoad \
     
    262262tstRTInlineAsmPIC3_DEFS = PIC
    263263
     264tstIprtList_TEMPLATE = VBOXR3TSTEXE
     265tstIprtList_SOURCES = tstIprtList.cpp
     266
    264267tstIprtMiniString_TEMPLATE = VBOXR3TSTEXE
    265268tstIprtMiniString_SOURCES = tstIprtMiniString.cpp
    266 
    267 tstIprtList_TEMPLATE = VBOXR3TSTEXE
    268 tstIprtList_SOURCES = tstIprtList.cpp
    269269
    270270tstLdr_SOURCES = tstLdr.cpp
  • trunk/src/VBox/Runtime/testcase/tstIprtList.cpp

    r36500 r36508  
    3434#include <iprt/rand.h>
    3535
     36
    3637/*******************************************************************************
    37 *   Test Data                                                                  *
     38*   Global Variables                                                           *
    3839*******************************************************************************/
    39 static const char *gs_apcszTestStrings[] =
     40/** Used for the string test. */
     41static const char *g_apszTestStrings[] =
    4042{
    4143    "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
     
    141143};
    142144
    143 /*******************************************************************************
    144 *   Tests                                                                      *
    145 *******************************************************************************/
     145
     146/**
     147 * Does a list test.
     148 *
     149 * @param   T1          The list type.
     150 * @param   T2          The input type
     151 * @param   pcszDesc    The test description.
     152 * @param   paTestData  Pointer to the array with the test input data.
     153 * @param   cTestItems  The size of the input data.
     154 */
    146155template<typename T1, typename T2>
    147 static void test(const char* pcszDesc, T2 aTestData[], size_t cSize)
     156static void test(const char *pcszDesc, T2 paTestData[], size_t cTestItems)
    148157{
    149     RTTestISubF("%s with size of %u (items=%u)", pcszDesc, sizeof(T1), cSize);
     158    RTTestISubF("%s with size of %u (items=%u)", pcszDesc, sizeof(T1), cTestItems);
    150159
    151160    /*
     
    166175    /* Add the second half of the test data */
    167176    size_t cAdded = 1;
     177
    168178    /* Start adding the second half of our test list */
    169     for (size_t i = cSize / 2; i < cSize; ++i, ++cAdded)
     179    for (size_t i = cTestItems / 2; i < cTestItems; ++i, ++cAdded)
    170180    {
    171         testList.append(aTestData[i]);
     181        testList.append(paTestData[i]);
    172182        RTTESTI_CHECK_RETV(testList.size()    == cAdded);
    173         RTTESTI_CHECK(testList.at(0)          == aTestData[cSize / 2]);
    174         RTTESTI_CHECK(testList[0]             == aTestData[cSize / 2]);
    175         RTTESTI_CHECK(testList.first()        == aTestData[cSize / 2]);
    176         RTTESTI_CHECK(testList.at(cAdded - 1) == aTestData[i]);
    177         RTTESTI_CHECK(testList[cAdded - 1]    == aTestData[i]);
    178         RTTESTI_CHECK(testList.last()         == aTestData[i]);
     183        RTTESTI_CHECK(testList.at(0)          == paTestData[cTestItems / 2]);
     184        RTTESTI_CHECK(testList[0]             == paTestData[cTestItems / 2]);
     185        RTTESTI_CHECK(testList.first()        == paTestData[cTestItems / 2]);
     186        RTTESTI_CHECK(testList.at(cAdded - 1) == paTestData[i]);
     187        RTTESTI_CHECK(testList[cAdded - 1]    == paTestData[i]);
     188        RTTESTI_CHECK(testList.last()         == paTestData[i]);
    179189    }
     190
    180191    /* Check that all is correctly appended. */
    181     RTTESTI_CHECK_RETV(testList.size()        == cSize / 2);
     192    RTTESTI_CHECK_RETV(testList.size()        == cTestItems / 2);
    182193    RTTESTI_CHECK_RETV(testList.isEmpty()     == false);
    183194    for (size_t i = 0; i < testList.size(); ++i)
    184         RTTESTI_CHECK(testList.at(i) == aTestData[cSize / 2 + i]);
     195        RTTESTI_CHECK(testList.at(i) == paTestData[cTestItems / 2 + i]);
     196
    185197    /* Start prepending the first half of our test list. Iterate reverse to get
    186198     * the correct sorting back. */
    187     for (size_t i = cSize / 2; i > 0; --i, ++cAdded)
     199    for (size_t i = cTestItems / 2; i > 0; --i, ++cAdded)
    188200    {
    189         testList.prepend(aTestData[i - 1]);
     201        testList.prepend(paTestData[i - 1]);
    190202        RTTESTI_CHECK_RETV(testList.size()    == cAdded);
    191         RTTESTI_CHECK(testList.at(0)          == aTestData[i - 1]);
    192         RTTESTI_CHECK(testList[0]             == aTestData[i - 1]);
    193         RTTESTI_CHECK(testList.first()        == aTestData[i - 1]);
    194         RTTESTI_CHECK(testList.at(cAdded - 1) == aTestData[cSize - 1]);
    195         RTTESTI_CHECK(testList[cAdded - 1]    == aTestData[cSize - 1]);
    196         RTTESTI_CHECK(testList.last()         == aTestData[cSize - 1]);
     203        RTTESTI_CHECK(testList.at(0)          == paTestData[i - 1]);
     204        RTTESTI_CHECK(testList[0]             == paTestData[i - 1]);
     205        RTTESTI_CHECK(testList.first()        == paTestData[i - 1]);
     206        RTTESTI_CHECK(testList.at(cAdded - 1) == paTestData[cTestItems - 1]);
     207        RTTESTI_CHECK(testList[cAdded - 1]    == paTestData[cTestItems - 1]);
     208        RTTESTI_CHECK(testList.last()         == paTestData[cTestItems - 1]);
    197209    }
     210
    198211    /* Check that all is correctly prepended. */
    199     RTTESTI_CHECK_RETV(testList.size()        == cSize);
     212    RTTESTI_CHECK_RETV(testList.size()        == cTestItems);
    200213    RTTESTI_CHECK_RETV(testList.isEmpty()     == false);
    201214    for (size_t i = 0; i < testList.size(); ++i)
    202         RTTESTI_CHECK(testList.at(i) == aTestData[i]);
     215        RTTESTI_CHECK(testList.at(i) == paTestData[i]);
    203216
    204217    /*
     
    206219     */
    207220    iprt::list<T1> testList2(testList);
     221
    208222    /* Check that all is correctly appended. */
    209     RTTESTI_CHECK_RETV(testList2.size() == cSize);
    210     for (size_t i = 0; i < testList2.size(); ++i)
    211         RTTESTI_CHECK(testList2.at(i) == aTestData[i]);
     223    RTTESTI_CHECK_RETV(testList2.size() == cTestItems);
     224    for (size_t i = 0; i < testList2.size(); ++i)
     225        RTTESTI_CHECK(testList2.at(i) == paTestData[i]);
    212226
    213227    /*
     
    216230    iprt::list<T1> testList3;
    217231    testList3 = testList;
     232
    218233    /* Check that all is correctly appended. */
    219     RTTESTI_CHECK_RETV(testList3.size() == cSize);
     234    RTTESTI_CHECK_RETV(testList3.size() == cTestItems);
    220235    for (size_t i = 0; i < testList3.size(); ++i)
    221         RTTESTI_CHECK(testList3.at(i) == aTestData[i]);
     236        RTTESTI_CHECK(testList3.at(i) == paTestData[i]);
    222237
    223238    /*
     
    225240     */
    226241    testList2.append(testList3);
     242
    227243    /* Check that all is correctly appended. */
    228     RTTESTI_CHECK_RETV(testList2.size() == cSize * 2);
    229     for (size_t i = 0; i < testList2.size(); ++i)
    230         RTTESTI_CHECK(testList2.at(i) == aTestData[i % cSize]);
     244    RTTESTI_CHECK_RETV(testList2.size() == cTestItems * 2);
     245    for (size_t i = 0; i < testList2.size(); ++i)
     246        RTTESTI_CHECK(testList2.at(i) == paTestData[i % cTestItems]);
    231247
    232248    /*
     
    234250     */
    235251    testList2.prepend(testList3);
     252
    236253    /* Check that all is correctly appended. */
    237     RTTESTI_CHECK_RETV(testList2.size() == cSize * 3);
    238     for (size_t i = 0; i < testList2.size(); ++i)
    239         RTTESTI_CHECK(testList2.at(i) == aTestData[i % cSize]);
     254    RTTESTI_CHECK_RETV(testList2.size() == cTestItems * 3);
     255    for (size_t i = 0; i < testList2.size(); ++i)
     256        RTTESTI_CHECK(testList2.at(i) == paTestData[i % cTestItems]);
    240257
    241258    /*
     
    243260     */
    244261    for (size_t i = 0; i < testList2.size(); ++i)
    245         RTTESTI_CHECK(testList2.value(i)       == aTestData[i % cSize]);
    246     for (size_t i = 0; i < testList2.size(); ++i)
    247         RTTESTI_CHECK(testList2.value(i, T1()) == aTestData[i % cSize]);
     262        RTTESTI_CHECK(testList2.value(i)       == paTestData[i % cTestItems]);
     263    for (size_t i = 0; i < testList2.size(); ++i)
     264        RTTESTI_CHECK(testList2.value(i, T1()) == paTestData[i % cTestItems]);
    248265    RTTESTI_CHECK(testList2.value(testList2.size() + 1) == T1());       /* Invalid index */
    249266    RTTESTI_CHECK(testList2.value(testList2.size() + 1, T1()) == T1()); /* Invalid index */
     
    253270     */
    254271    for (size_t i = 0; i < testList.size(); ++i)
    255         RTTESTI_CHECK(testList[i] == aTestData[i]);
     272        RTTESTI_CHECK(testList[i] == paTestData[i]);
     273
    256274    /*
    257275     * operator[] (writing)
     
    259277     * Replace with inverted array.
    260278     */
    261     for (size_t i = 0; i < cSize; ++i)
    262         testList[i] = aTestData[cSize - i - 1];
    263     RTTESTI_CHECK_RETV(testList.size() == cSize);
    264     for (size_t i = 0; i < testList.size(); ++i)
    265         RTTESTI_CHECK(testList[i] == aTestData[cSize - i - 1]);
     279    for (size_t i = 0; i < cTestItems; ++i)
     280        testList[i] = paTestData[cTestItems - i - 1];
     281    RTTESTI_CHECK_RETV(testList.size() == cTestItems);
     282    for (size_t i = 0; i < testList.size(); ++i)
     283        RTTESTI_CHECK(testList[i] == paTestData[cTestItems - i - 1]);
    266284
    267285    /*
     
    270288     * Replace with inverted array (Must be original array when finished).
    271289     */
    272     for (size_t i = 0; i < cSize; ++i)
    273         testList.replace(i, aTestData[i]);
    274     RTTESTI_CHECK_RETV(testList.size() == cSize);
    275     for (size_t i = 0; i < testList.size(); ++i)
    276         RTTESTI_CHECK(testList[i] == aTestData[i]);
     290    for (size_t i = 0; i < cTestItems; ++i)
     291        testList.replace(i, paTestData[i]);
     292    RTTESTI_CHECK_RETV(testList.size() == cTestItems);
     293    for (size_t i = 0; i < testList.size(); ++i)
     294        RTTESTI_CHECK(testList[i] == paTestData[i]);
    277295
    278296    /*
     
    281299
    282300    /* Remove Range */
    283     testList2.removeRange(cSize, cSize * 2);
    284     RTTESTI_CHECK_RETV(testList2.size() == cSize * 2);
    285     for (size_t i = 0; i < testList2.size(); ++i)
    286         RTTESTI_CHECK(testList2.at(i) == aTestData[i % cSize]);
     301    testList2.removeRange(cTestItems, cTestItems * 2);
     302    RTTESTI_CHECK_RETV(testList2.size() == cTestItems * 2);
     303    for (size_t i = 0; i < testList2.size(); ++i)
     304        RTTESTI_CHECK(testList2.at(i) == paTestData[i % cTestItems]);
    287305
    288306    /* Remove the first half (reverse) */
    289307    size_t cRemoved = 1;
    290     for (size_t i = cSize / 2; i > 0; --i, ++cRemoved)
     308    for (size_t i = cTestItems / 2; i > 0; --i, ++cRemoved)
    291309    {
    292310        testList.removeAt(i - 1);
    293         RTTESTI_CHECK_RETV(testList.size() == cSize - cRemoved);
     311        RTTESTI_CHECK_RETV(testList.size() == cTestItems - cRemoved);
    294312    }
    295     RTTESTI_CHECK_RETV(testList.size() == cSize / 2);
     313    RTTESTI_CHECK_RETV(testList.size() == cTestItems / 2);
     314
    296315    /* Check that all is correctly removed and only the second part of the list
    297316     * is still there. */
    298317    for (size_t i = 0; i < testList.size(); ++i)
    299         RTTESTI_CHECK(testList.at(i) == aTestData[cSize / 2 + i]);
     318        RTTESTI_CHECK(testList.at(i) == paTestData[cTestItems / 2 + i]);
    300319
    301320    /*
    302321     * setCapacitiy
    303322     */
    304     testList.setCapacity(cSize * 5);
    305     RTTESTI_CHECK(testList.capacity()  == cSize * 5);
    306     RTTESTI_CHECK_RETV(testList.size() == cSize / 2);
     323    testList.setCapacity(cTestItems * 5);
     324    RTTESTI_CHECK(testList.capacity()  == cTestItems * 5);
     325    RTTESTI_CHECK_RETV(testList.size() == cTestItems / 2);
     326
    307327    /* As the capacity just increased, we should still have all entries from
    308328     * the previous list. */
    309329    for (size_t i = 0; i < testList.size(); ++i)
    310         RTTESTI_CHECK(testList.at(i) == aTestData[cSize / 2 + i]);
     330        RTTESTI_CHECK(testList.at(i) == paTestData[cTestItems / 2 + i]);
     331
    311332    /* Decrease the capacity so it will be smaller than the count of items in
    312333     * the list. The list should be shrink automatically, but the remaining
    313334     * items should be still valid. */
    314     testList.setCapacity(cSize / 4);
    315     RTTESTI_CHECK_RETV(testList.size() == cSize / 4);
    316     RTTESTI_CHECK(testList.capacity()  == cSize / 4);
    317     for (size_t i = 0; i < testList.size(); ++i)
    318         RTTESTI_CHECK(testList.at(i) == aTestData[cSize / 2 + i]);
     335    testList.setCapacity(cTestItems / 4);
     336    RTTESTI_CHECK_RETV(testList.size() == cTestItems / 4);
     337    RTTESTI_CHECK(testList.capacity()  == cTestItems / 4);
     338    for (size_t i = 0; i < testList.size(); ++i)
     339        RTTESTI_CHECK(testList.at(i) == paTestData[cTestItems / 2 + i]);
    319340
    320341    /* Clear all */
     
    325346}
    326347
     348
    327349int main()
    328350{
     
    331353
    332354    RTTEST hTest;
    333     int rc = RTTestInitAndCreate("tstIprtList", &hTest);
    334     if (rc)
    335         return rc;
     355    RTEXITCODE rcExit = RTTestInitAndCreate("tstIprtList", &hTest);
     356    if (rcExit)
     357        return rcExit;
    336358    RTTestBanner(hTest);
    337359
     
    348370    uint8_t au8TestInts[s_cTestCount];
    349371    for (size_t i = 0; i < RT_ELEMENTS(au8TestInts); ++i)
    350         au8TestInts[i] = (float)RTRandU32() / UINT32_MAX * UINT8_MAX;
     372        au8TestInts[i] = (uint8_t)RTRandU32Ex(0, UINT8_MAX);
    351373    test<uint8_t, uint8_t>("Native type", au8TestInts, RT_ELEMENTS(au8TestInts));
     374
    352375    uint16_t au16TestInts[s_cTestCount];
    353376    for (size_t i = 0; i < RT_ELEMENTS(au16TestInts); ++i)
    354         au16TestInts[i] = (float)RTRandU32() / UINT32_MAX * UINT16_MAX;
     377        au16TestInts[i] = (uint16_t)RTRandU32Ex(0, UINT16_MAX);
    355378    test<uint16_t, uint16_t>("Native type", au16TestInts, RT_ELEMENTS(au16TestInts));
     379
    356380    uint32_t au32TestInts[s_cTestCount];
    357381    for (size_t i = 0; i < RT_ELEMENTS(au32TestInts); ++i)
    358382        au32TestInts[i] = RTRandU32();
    359383    test<uint32_t, uint32_t>("Native type", au32TestInts, RT_ELEMENTS(au32TestInts));
     384
    360385    /*
    361386     * Specialized type.
     
    365390        au64TestInts[i] = RTRandU64();
    366391    test<uint64_t, uint64_t>("Specialized type", au64TestInts, RT_ELEMENTS(au64TestInts));
     392
    367393    /*
    368394     * Big size type (translate to internal pointer list).
    369395     */
    370     test<iprt::MiniString, const char*>("Class type", gs_apcszTestStrings, RT_ELEMENTS(gs_apcszTestStrings));
     396    test<iprt::MiniString, const char *>("Class type", g_apszTestStrings, RT_ELEMENTS(g_apszTestStrings));
    371397
    372398    /*
Note: See TracChangeset for help on using the changeset viewer.

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