VirtualBox

Changeset 67748 in vbox


Ignore:
Timestamp:
Jul 3, 2017 9:29:29 AM (8 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
116637
Message:

iprt/cpp/list.h: Added append method without any parameters that returns a mutable reference to the object it appended. Added const operator[] implementation.

File:
1 edited

Legend:

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

    r62474 r67748  
    135135    static inline void      set(T2 *p, size_t i, const T1 &v) { p[i] = v; }
    136136    static inline T1 &      at(T2 *p, size_t i) { return p[i]; }
     137    static inline const T1 &atConst(T2 const *p, size_t i) { return p[i]; }
    137138    static inline size_t    find(T2 *p, const T1 &v, size_t cElements)
    138139    {
     
    161162    static inline void      set(T1 **p, size_t i, const T1 &v) { p[i] = new T1(v); }
    162163    static inline T1 &      at(T1 **p, size_t i) { return *p[i]; }
     164    static inline const T1 &atConst(T1 * const *p, size_t i) { return *p[i]; }
    163165    static inline size_t    find(T1 **p, const T1 &v, size_t cElements)
    164166    {
     
    407409
    408410    /**
     411     * Append a default item to the list.
     412     *
     413     * @return  a mutable reference to the item
     414     * @throws  std::bad_alloc
     415     */
     416    GET_RTYPE append()
     417    {
     418        m_guard.enterWrite();
     419        if (m_cElements == m_cCapacity)
     420            growArray(m_cCapacity + kDefaultCapacity);
     421        RTCListHelper<T, ITYPE>::set(m_pArray, m_cElements, T());
     422        GET_RTYPE rRet = RTCListHelper<T, ITYPE>::at(m_pArray, m_cElements);
     423        ++m_cElements;
     424        m_guard.leaveWrite();
     425
     426        return rRet;
     427    }
     428
     429    /**
    409430     * Append an item to the list.
    410431     *
     
    628649
    629650    /**
    630      * Return the item at position @a i or default value if out of range.
     651     * Return the item at position @a i as immutable reference.
     652     *
     653     * @param   i     The position of the item to return.  This better not be out of
     654     *                bounds, however should it be the last element of the array
     655     *                will be return and strict builds will raise an assertion.
     656     *                Should the array be empty, a crash is very likely.
     657     * @return   The item at position @a i.
     658     */
     659    const T &operator[](size_t i) const
     660    {
     661        m_guard.enterRead();
     662        AssertMsgStmt(i < m_cElements, ("i=%zu m_cElements=%zu\n", i, m_cElements), i = m_cElements - 1);
     663        const T &rRet = RTCListHelper<T, ITYPE>::atConst(m_pArray, i);
     664        m_guard.leaveRead();
     665        return rRet;
     666    }
     667
     668    /**
     669     * Return a copy of the item at position @a i or default value if out of range.
    631670     *
    632671     * @param   i              The position of the item to return.
    633      * @return  The item at position @a i or default value.
     672     * @return  Copy of the item at position @a i or default value.
    634673     */
    635674    T value(size_t i) const
     
    647686
    648687    /**
    649      * Return the item at position @a i, or @a defaultVal if out of range.
     688     * Return a copy of the item at position @a i, or @a defaultVal if out of range.
    650689     *
    651690     * @param   i              The position of the item to return.
    652691     * @param   defaultVal     The value to return in case @a i is invalid.
    653      * @return  The item at position @a i or @a defaultVal.
     692     * @return  Copy of the item at position @a i or @a defaultVal.
    654693     */
    655694    T value(size_t i, const T &defaultVal) const
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