list.h revision eb4b342c83eb47585ce579d9aee898e7a33a96d5
236b2935f217749893b7034e59da3e3568928acevboxsync/** @file
236b2935f217749893b7034e59da3e3568928acevboxsync * IPRT - Generic List Class.
236b2935f217749893b7034e59da3e3568928acevboxsync */
236b2935f217749893b7034e59da3e3568928acevboxsync
236b2935f217749893b7034e59da3e3568928acevboxsync/*
236b2935f217749893b7034e59da3e3568928acevboxsync * Copyright (C) 2011 Oracle Corporation
aa7f783d8893b6004cb8d993273c008944f81544vboxsync *
236b2935f217749893b7034e59da3e3568928acevboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
236b2935f217749893b7034e59da3e3568928acevboxsync * available from http://www.virtualbox.org. This file is free software;
236b2935f217749893b7034e59da3e3568928acevboxsync * you can redistribute it and/or modify it under the terms of the GNU
236b2935f217749893b7034e59da3e3568928acevboxsync * General Public License (GPL) as published by the Free Software
236b2935f217749893b7034e59da3e3568928acevboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
236b2935f217749893b7034e59da3e3568928acevboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
236b2935f217749893b7034e59da3e3568928acevboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
236b2935f217749893b7034e59da3e3568928acevboxsync *
236b2935f217749893b7034e59da3e3568928acevboxsync * The contents of this file may alternatively be used under the terms
236b2935f217749893b7034e59da3e3568928acevboxsync * of the Common Development and Distribution License Version 1.0
236b2935f217749893b7034e59da3e3568928acevboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
236b2935f217749893b7034e59da3e3568928acevboxsync * VirtualBox OSE distribution, in which case the provisions of the
236b2935f217749893b7034e59da3e3568928acevboxsync * CDDL are applicable instead of those of the GPL.
236b2935f217749893b7034e59da3e3568928acevboxsync *
236b2935f217749893b7034e59da3e3568928acevboxsync * You may elect to license modified versions of this file under the
236b2935f217749893b7034e59da3e3568928acevboxsync * terms and conditions of either the GPL or the CDDL or both.
236b2935f217749893b7034e59da3e3568928acevboxsync */
236b2935f217749893b7034e59da3e3568928acevboxsync
236b2935f217749893b7034e59da3e3568928acevboxsync#ifndef ___iprt_cpp_list_h
236b2935f217749893b7034e59da3e3568928acevboxsync#define ___iprt_cpp_list_h
236b2935f217749893b7034e59da3e3568928acevboxsync
236b2935f217749893b7034e59da3e3568928acevboxsync#include <iprt/cpp/meta.h>
157093a77f2752732368338110cb50fa6cd7717fvboxsync#include <iprt/mem.h>
236b2935f217749893b7034e59da3e3568928acevboxsync#include <iprt/string.h> /* for memcpy */
157093a77f2752732368338110cb50fa6cd7717fvboxsync
157093a77f2752732368338110cb50fa6cd7717fvboxsync#include <new> /* For std::bad_alloc */
157093a77f2752732368338110cb50fa6cd7717fvboxsync
157093a77f2752732368338110cb50fa6cd7717fvboxsync/** @defgroup grp_rt_cpp_list C++ List support
236b2935f217749893b7034e59da3e3568928acevboxsync * @ingroup grp_rt_cpp
157093a77f2752732368338110cb50fa6cd7717fvboxsync *
157093a77f2752732368338110cb50fa6cd7717fvboxsync * @brief Generic C++ list class support.
157093a77f2752732368338110cb50fa6cd7717fvboxsync *
157093a77f2752732368338110cb50fa6cd7717fvboxsync * This list classes manage any amount of data in a fast and easy to use way.
157093a77f2752732368338110cb50fa6cd7717fvboxsync * They have no dependencies on STL, only on generic memory management methods
157093a77f2752732368338110cb50fa6cd7717fvboxsync * of IRPT. This allows list handling in situations where the use of STL
236b2935f217749893b7034e59da3e3568928acevboxsync * container classes is forbidden.
5067a9619d7131c54d4ebb371d9dac91abdd34f6vboxsync *
5067a9619d7131c54d4ebb371d9dac91abdd34f6vboxsync * Not all of the functionality of STL container classes is implemented. There
236b2935f217749893b7034e59da3e3568928acevboxsync * are no iterators or any other high level access/modifier methods (e.g.
236b2935f217749893b7034e59da3e3568928acevboxsync * std::algorithms).
236b2935f217749893b7034e59da3e3568928acevboxsync *
236b2935f217749893b7034e59da3e3568928acevboxsync * The implementation is array based which allows fast access to the items.
236b2935f217749893b7034e59da3e3568928acevboxsync * Appending items is usually also fast, cause the internal array is
236b2935f217749893b7034e59da3e3568928acevboxsync * preallocated. To minimize the memory overhead, native types (that is
236b2935f217749893b7034e59da3e3568928acevboxsync * everything smaller then the size of void*) are directly saved in the array.
236b2935f217749893b7034e59da3e3568928acevboxsync * If bigger types are used (e.g. RTCString) the internal array is an array of
236b2935f217749893b7034e59da3e3568928acevboxsync * pointers to the objects.
236b2935f217749893b7034e59da3e3568928acevboxsync *
236b2935f217749893b7034e59da3e3568928acevboxsync * The size of the internal array will usually not shrink, but grow
157093a77f2752732368338110cb50fa6cd7717fvboxsync * automatically. Only certain methods, like RTCList::clear or the "=" operator
236b2935f217749893b7034e59da3e3568928acevboxsync * will reset any previously allocated memory. You can call
236b2935f217749893b7034e59da3e3568928acevboxsync * RTCList::setCapacity for manual adjustment. If the size of an new list will
236b2935f217749893b7034e59da3e3568928acevboxsync * be known, calling the constructor with the necessary capacity will speed up
236b2935f217749893b7034e59da3e3568928acevboxsync * the insertion of the new items.
236b2935f217749893b7034e59da3e3568928acevboxsync *
3357df142080b5abd7a5da3358371ce0bff34e91vboxsync * For the full public interface these list classes offer see RTCListBase.
236b2935f217749893b7034e59da3e3568928acevboxsync *
236b2935f217749893b7034e59da3e3568928acevboxsync * There are some requirements for the types used which follow:
236b2935f217749893b7034e59da3e3568928acevboxsync * -# They need a default and a copy constructor.
236b2935f217749893b7034e59da3e3568928acevboxsync * -# Some methods (e.g. RTCList::contains) need an equal operator.
236b2935f217749893b7034e59da3e3568928acevboxsync * -# If the type is some complex class (that is, having a constructor which
236b2935f217749893b7034e59da3e3568928acevboxsync * allocates members on the heap) it has to be greater than sizeof(void*) to
236b2935f217749893b7034e59da3e3568928acevboxsync * be used correctly. If this is not the case you can manually overwrite the
236b2935f217749893b7034e59da3e3568928acevboxsync * list behavior. Just add T* as a second parameter to the list template if
236b2935f217749893b7034e59da3e3568928acevboxsync * your class is called T. Another possibility is to specialize the list for
236b2935f217749893b7034e59da3e3568928acevboxsync * your target class. See below for more information.
236b2935f217749893b7034e59da3e3568928acevboxsync *
236b2935f217749893b7034e59da3e3568928acevboxsync * The native types like int, bool, ptr, ..., are meeting this criteria, so
236b2935f217749893b7034e59da3e3568928acevboxsync * they are save to use.
236b2935f217749893b7034e59da3e3568928acevboxsync *
236b2935f217749893b7034e59da3e3568928acevboxsync * Please note that the return type of some of the getter methods are slightly
236b2935f217749893b7034e59da3e3568928acevboxsync * different depending on the list type. Native types return the item by value,
236b2935f217749893b7034e59da3e3568928acevboxsync * items with a size greater than sizeof(void*) by reference. As native types
236b2935f217749893b7034e59da3e3568928acevboxsync * saved directly in the internal array, returning a reference to them (and
236b2935f217749893b7034e59da3e3568928acevboxsync * saving them in a reference as well) would make them invalid (or pointing to
236b2935f217749893b7034e59da3e3568928acevboxsync * a wrong item) when the list is changed in the meanwhile. Returning a
236b2935f217749893b7034e59da3e3568928acevboxsync * reference for bigger types isn't problematic and makes sure we get out the
236b2935f217749893b7034e59da3e3568928acevboxsync * best speed of the list. The one exception to this rule is the index
236b2935f217749893b7034e59da3e3568928acevboxsync * operator[]. This operator always return a reference to make it possible to
236b2935f217749893b7034e59da3e3568928acevboxsync * use it as a lvalue. Its your responsibility to make sure the list isn't
236b2935f217749893b7034e59da3e3568928acevboxsync * changed when using the value as reference returned by this operator.
236b2935f217749893b7034e59da3e3568928acevboxsync *
236b2935f217749893b7034e59da3e3568928acevboxsync * The list class is reentrant. For a thread-safe variant see RTCMTList.
26f7a9a33aba00f6fe644eece95158d147ae3dbdvboxsync *
26f7a9a33aba00f6fe644eece95158d147ae3dbdvboxsync * Implementation details:
26f7a9a33aba00f6fe644eece95158d147ae3dbdvboxsync * It is possible to specialize any type. This might be necessary to get the
26f7a9a33aba00f6fe644eece95158d147ae3dbdvboxsync * best speed out of the list. Examples are the 64-bit types, which use the
236b2935f217749893b7034e59da3e3568928acevboxsync * native (no pointers) implementation even on a 32-bit host. Consult the
236b2935f217749893b7034e59da3e3568928acevboxsync * source code for more details.
236b2935f217749893b7034e59da3e3568928acevboxsync *
236b2935f217749893b7034e59da3e3568928acevboxsync * Current specialized implementations:
236b2935f217749893b7034e59da3e3568928acevboxsync * - int64_t: RTCList<int64_t>
236b2935f217749893b7034e59da3e3568928acevboxsync * - uint64_t: RTCList<uint64_t>
236b2935f217749893b7034e59da3e3568928acevboxsync *
236b2935f217749893b7034e59da3e3568928acevboxsync * @{
1f60035b60989886e7b26f8db14812ca63dc2acbvboxsync */
236b2935f217749893b7034e59da3e3568928acevboxsync
236b2935f217749893b7034e59da3e3568928acevboxsync/**
4ab1e1de561327173718600b82c542cbe1cc878cvboxsync * The guard definition.
236b2935f217749893b7034e59da3e3568928acevboxsync */
236b2935f217749893b7034e59da3e3568928acevboxsynctemplate <bool G>
1f60035b60989886e7b26f8db14812ca63dc2acbvboxsyncclass RTCListGuard;
5067a9619d7131c54d4ebb371d9dac91abdd34f6vboxsync
5067a9619d7131c54d4ebb371d9dac91abdd34f6vboxsync/**
5067a9619d7131c54d4ebb371d9dac91abdd34f6vboxsync * The default guard which does nothing.
5067a9619d7131c54d4ebb371d9dac91abdd34f6vboxsync */
5067a9619d7131c54d4ebb371d9dac91abdd34f6vboxsynctemplate <>
5067a9619d7131c54d4ebb371d9dac91abdd34f6vboxsyncclass RTCListGuard<false>
236b2935f217749893b7034e59da3e3568928acevboxsync{
236b2935f217749893b7034e59da3e3568928acevboxsyncpublic:
236b2935f217749893b7034e59da3e3568928acevboxsync inline void enterRead() const {}
bc5cd42756b3f98351040bbfccc08dd9bacd103avboxsync inline void leaveRead() const {}
4ab1e1de561327173718600b82c542cbe1cc878cvboxsync inline void enterWrite() {}
5067a9619d7131c54d4ebb371d9dac91abdd34f6vboxsync inline void leaveWrite() {}
236b2935f217749893b7034e59da3e3568928acevboxsync
236b2935f217749893b7034e59da3e3568928acevboxsync /* Define our own new and delete. */
5067a9619d7131c54d4ebb371d9dac91abdd34f6vboxsync RTMEMEF_NEW_AND_DELETE_OPERATORS();
26f7a9a33aba00f6fe644eece95158d147ae3dbdvboxsync};
26f7a9a33aba00f6fe644eece95158d147ae3dbdvboxsync
26f7a9a33aba00f6fe644eece95158d147ae3dbdvboxsync/**
3357df142080b5abd7a5da3358371ce0bff34e91vboxsync * General helper template for managing native values in RTCListBase.
3357df142080b5abd7a5da3358371ce0bff34e91vboxsync */
5067a9619d7131c54d4ebb371d9dac91abdd34f6vboxsynctemplate <typename T1, typename T2>
38e53feb0f05157234978e9288833973bb0e2bedvboxsyncclass RTCListHelper
3357df142080b5abd7a5da3358371ce0bff34e91vboxsync{
3357df142080b5abd7a5da3358371ce0bff34e91vboxsyncpublic:
236b2935f217749893b7034e59da3e3568928acevboxsync static inline void set(T2 *p, size_t i, const T1 &v) { p[i] = v; }
5067a9619d7131c54d4ebb371d9dac91abdd34f6vboxsync static inline T1 & at(T2 *p, size_t i) { return p[i]; }
236b2935f217749893b7034e59da3e3568928acevboxsync static inline size_t find(T2 *p, const T1 &v, size_t cSize)
236b2935f217749893b7034e59da3e3568928acevboxsync {
236b2935f217749893b7034e59da3e3568928acevboxsync size_t i = 0;
236b2935f217749893b7034e59da3e3568928acevboxsync while(i < cSize)
26f7a9a33aba00f6fe644eece95158d147ae3dbdvboxsync {
236b2935f217749893b7034e59da3e3568928acevboxsync if (p[i] == v)
236b2935f217749893b7034e59da3e3568928acevboxsync break;
236b2935f217749893b7034e59da3e3568928acevboxsync ++i;
236b2935f217749893b7034e59da3e3568928acevboxsync }
8d9e0655efb148a5dd672705e315de97a1ad12f1vboxsync return i;
bb50cfa1b9de06edfb8d96f8d47dbd5394fb4ce3vboxsync }
bb50cfa1b9de06edfb8d96f8d47dbd5394fb4ce3vboxsync static inline void copyTo(T2 *p, T2 *const p1 , size_t iTo, size_t cSize)
26f7a9a33aba00f6fe644eece95158d147ae3dbdvboxsync {
bb50cfa1b9de06edfb8d96f8d47dbd5394fb4ce3vboxsync if (cSize > 0)
bb50cfa1b9de06edfb8d96f8d47dbd5394fb4ce3vboxsync memcpy(&p[iTo], &p1[0], sizeof(T1) * cSize);
bb50cfa1b9de06edfb8d96f8d47dbd5394fb4ce3vboxsync }
bb50cfa1b9de06edfb8d96f8d47dbd5394fb4ce3vboxsync static inline void erase(T2 *p, size_t /* i */) { /* Nothing to do here. */ }
bb50cfa1b9de06edfb8d96f8d47dbd5394fb4ce3vboxsync static inline void eraseRange(T2 * /* p */, size_t /* cFrom */, size_t /* cSize */) { /* Nothing to do here. */ }
bb50cfa1b9de06edfb8d96f8d47dbd5394fb4ce3vboxsync};
bb50cfa1b9de06edfb8d96f8d47dbd5394fb4ce3vboxsync
bb50cfa1b9de06edfb8d96f8d47dbd5394fb4ce3vboxsync/**
157093a77f2752732368338110cb50fa6cd7717fvboxsync * Specialized helper template for managing pointer values in RTCListBase.
157093a77f2752732368338110cb50fa6cd7717fvboxsync */
157093a77f2752732368338110cb50fa6cd7717fvboxsynctemplate <typename T1>
157093a77f2752732368338110cb50fa6cd7717fvboxsyncclass RTCListHelper<T1, T1*>
bb50cfa1b9de06edfb8d96f8d47dbd5394fb4ce3vboxsync{
bb50cfa1b9de06edfb8d96f8d47dbd5394fb4ce3vboxsyncpublic:
bb50cfa1b9de06edfb8d96f8d47dbd5394fb4ce3vboxsync static inline void set(T1 **p, size_t i, const T1 &v) { p[i] = new T1(v); }
bb50cfa1b9de06edfb8d96f8d47dbd5394fb4ce3vboxsync static inline T1 & at(T1 **p, size_t i) { return *p[i]; }
157093a77f2752732368338110cb50fa6cd7717fvboxsync static inline size_t find(T1 **p, const T1 &v, size_t cSize)
157093a77f2752732368338110cb50fa6cd7717fvboxsync {
157093a77f2752732368338110cb50fa6cd7717fvboxsync size_t i = 0;
157093a77f2752732368338110cb50fa6cd7717fvboxsync while(i < cSize)
bb50cfa1b9de06edfb8d96f8d47dbd5394fb4ce3vboxsync {
157093a77f2752732368338110cb50fa6cd7717fvboxsync if (*p[i] == v)
157093a77f2752732368338110cb50fa6cd7717fvboxsync break;
236b2935f217749893b7034e59da3e3568928acevboxsync ++i;
236b2935f217749893b7034e59da3e3568928acevboxsync }
26f7a9a33aba00f6fe644eece95158d147ae3dbdvboxsync return i;
26f7a9a33aba00f6fe644eece95158d147ae3dbdvboxsync }
26f7a9a33aba00f6fe644eece95158d147ae3dbdvboxsync static inline void copyTo(T1 **p, T1 **const p1 , size_t iTo, size_t cSize)
26f7a9a33aba00f6fe644eece95158d147ae3dbdvboxsync {
236b2935f217749893b7034e59da3e3568928acevboxsync for (size_t i = 0; i < cSize; ++i)
236b2935f217749893b7034e59da3e3568928acevboxsync p[iTo + i] = new T1(*p1[i]);
236b2935f217749893b7034e59da3e3568928acevboxsync }
236b2935f217749893b7034e59da3e3568928acevboxsync static inline void erase(T1 **p, size_t i) { delete p[i]; }
236b2935f217749893b7034e59da3e3568928acevboxsync static inline void eraseRange(T1 **p, size_t cFrom, size_t cSize)
236b2935f217749893b7034e59da3e3568928acevboxsync {
236b2935f217749893b7034e59da3e3568928acevboxsync for (size_t i = cFrom; i < cFrom + cSize; ++i)
236b2935f217749893b7034e59da3e3568928acevboxsync delete p[i];
4ae0ac3d0dc641305a0b0197db43ee7c4b40f747vboxsync }
236b2935f217749893b7034e59da3e3568928acevboxsync};
236b2935f217749893b7034e59da3e3568928acevboxsync
236b2935f217749893b7034e59da3e3568928acevboxsync/**
236b2935f217749893b7034e59da3e3568928acevboxsync * This is the base class for all other list classes. It implements the
236b2935f217749893b7034e59da3e3568928acevboxsync * necessary list functionality in a type independent way and offers the public
5067a9619d7131c54d4ebb371d9dac91abdd34f6vboxsync * list interface to the user.
236b2935f217749893b7034e59da3e3568928acevboxsync */
5067a9619d7131c54d4ebb371d9dac91abdd34f6vboxsynctemplate <class T, typename ITYPE, bool MT>
3357df142080b5abd7a5da3358371ce0bff34e91vboxsyncclass RTCListBase
3357df142080b5abd7a5da3358371ce0bff34e91vboxsync{
3357df142080b5abd7a5da3358371ce0bff34e91vboxsync /**
3357df142080b5abd7a5da3358371ce0bff34e91vboxsync * Traits
3357df142080b5abd7a5da3358371ce0bff34e91vboxsync *
5067a9619d7131c54d4ebb371d9dac91abdd34f6vboxsync * Defines the return type of most of the getter methods. If the internal
236b2935f217749893b7034e59da3e3568928acevboxsync * used type is a pointer, we return a reference. If not we return by
236b2935f217749893b7034e59da3e3568928acevboxsync * value.
236b2935f217749893b7034e59da3e3568928acevboxsync */
236b2935f217749893b7034e59da3e3568928acevboxsync typedef typename RTCIfPtr<ITYPE, T&, T>::result GET_RTYPE;
236b2935f217749893b7034e59da3e3568928acevboxsync typedef typename RTCIfPtr<ITYPE, const T&, T>::result GET_CRTYPE;
5067a9619d7131c54d4ebb371d9dac91abdd34f6vboxsync
236b2935f217749893b7034e59da3e3568928acevboxsyncpublic:
236b2935f217749893b7034e59da3e3568928acevboxsync /**
5067a9619d7131c54d4ebb371d9dac91abdd34f6vboxsync * Creates a new list.
236b2935f217749893b7034e59da3e3568928acevboxsync *
236b2935f217749893b7034e59da3e3568928acevboxsync * This preallocates @a cCapacity elements within the list.
236b2935f217749893b7034e59da3e3568928acevboxsync *
236b2935f217749893b7034e59da3e3568928acevboxsync * @param cCapacitiy The initial capacity the list has.
236b2935f217749893b7034e59da3e3568928acevboxsync * @throws std::bad_alloc
236b2935f217749893b7034e59da3e3568928acevboxsync */
236b2935f217749893b7034e59da3e3568928acevboxsync RTCListBase(size_t cCapacity = DefaultCapacity)
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync : m_pArray(0)
236b2935f217749893b7034e59da3e3568928acevboxsync , m_cSize(0)
236b2935f217749893b7034e59da3e3568928acevboxsync , m_cCapacity(0)
236b2935f217749893b7034e59da3e3568928acevboxsync {
236b2935f217749893b7034e59da3e3568928acevboxsync realloc_grow(cCapacity);
236b2935f217749893b7034e59da3e3568928acevboxsync }
236b2935f217749893b7034e59da3e3568928acevboxsync
236b2935f217749893b7034e59da3e3568928acevboxsync /**
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync * Creates a copy of another list.
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync *
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync * The other list will be fully copied and the capacity will be the same as
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync * the size of the other list.
4ae0ac3d0dc641305a0b0197db43ee7c4b40f747vboxsync *
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync * @param other The list to copy.
26f7a9a33aba00f6fe644eece95158d147ae3dbdvboxsync * @throws std::bad_alloc
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync */
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync RTCListBase(const RTCListBase<T, ITYPE, MT>& other)
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync : m_pArray(0)
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync , m_cSize(0)
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync , m_cCapacity(0)
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync {
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync realloc_grow(other.m_cSize);
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync RTCListHelper<T, ITYPE>::copyTo(m_pArray, other.m_pArray, 0, other.m_cSize);
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync m_cSize = other.m_cSize;
26f7a9a33aba00f6fe644eece95158d147ae3dbdvboxsync }
4ae0ac3d0dc641305a0b0197db43ee7c4b40f747vboxsync
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync /**
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync * Destructor.
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync */
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync ~RTCListBase()
4ae0ac3d0dc641305a0b0197db43ee7c4b40f747vboxsync {
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync RTCListHelper<T, ITYPE>::eraseRange(m_pArray, 0, m_cSize);
4ae0ac3d0dc641305a0b0197db43ee7c4b40f747vboxsync if (m_pArray)
4ae0ac3d0dc641305a0b0197db43ee7c4b40f747vboxsync RTMemFree(m_pArray);
4ae0ac3d0dc641305a0b0197db43ee7c4b40f747vboxsync }
4ae0ac3d0dc641305a0b0197db43ee7c4b40f747vboxsync
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync /**
4ae0ac3d0dc641305a0b0197db43ee7c4b40f747vboxsync * Sets a new capacity within the list.
4ae0ac3d0dc641305a0b0197db43ee7c4b40f747vboxsync *
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync * If the new capacity is bigger than the old size, it will be simply
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync * preallocated more space for the new items. If the new capacity is
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync * smaller than the previous size, items at the end of the list will be
236b2935f217749893b7034e59da3e3568928acevboxsync * deleted.
236b2935f217749893b7034e59da3e3568928acevboxsync *
236b2935f217749893b7034e59da3e3568928acevboxsync * @param cCapacity The new capacity within the list.
236b2935f217749893b7034e59da3e3568928acevboxsync * @throws std::bad_alloc
236b2935f217749893b7034e59da3e3568928acevboxsync */
236b2935f217749893b7034e59da3e3568928acevboxsync void setCapacity(size_t cCapacity)
236b2935f217749893b7034e59da3e3568928acevboxsync {
236b2935f217749893b7034e59da3e3568928acevboxsync m_guard.enterWrite();
236b2935f217749893b7034e59da3e3568928acevboxsync realloc(cCapacity);
236b2935f217749893b7034e59da3e3568928acevboxsync m_guard.leaveWrite();
236b2935f217749893b7034e59da3e3568928acevboxsync }
236b2935f217749893b7034e59da3e3568928acevboxsync
236b2935f217749893b7034e59da3e3568928acevboxsync /**
236b2935f217749893b7034e59da3e3568928acevboxsync * Return the current capacity of the list.
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync *
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync * @return The actual capacity.
26f7a9a33aba00f6fe644eece95158d147ae3dbdvboxsync */
26f7a9a33aba00f6fe644eece95158d147ae3dbdvboxsync size_t capacity() const { return m_cCapacity; }
26f7a9a33aba00f6fe644eece95158d147ae3dbdvboxsync
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync /**
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync * Check if an list contains any items.
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync *
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync * @return True if there is more than zero items, false otherwise.
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync */
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync bool isEmpty() const { return m_cSize == 0; }
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync /**
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync * Return the current count of elements within the list.
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync *
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync * @return The current element count.
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync */
26f7a9a33aba00f6fe644eece95158d147ae3dbdvboxsync size_t size() const { return m_cSize; }
26f7a9a33aba00f6fe644eece95158d147ae3dbdvboxsync
4ae0ac3d0dc641305a0b0197db43ee7c4b40f747vboxsync /**
4ae0ac3d0dc641305a0b0197db43ee7c4b40f747vboxsync * Inserts an item to the list at position @a i.
4ae0ac3d0dc641305a0b0197db43ee7c4b40f747vboxsync *
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync * @param i The position of the new item.
26f7a9a33aba00f6fe644eece95158d147ae3dbdvboxsync * @param val The new item.
26f7a9a33aba00f6fe644eece95158d147ae3dbdvboxsync * @return a reference to this list.
26f7a9a33aba00f6fe644eece95158d147ae3dbdvboxsync * @throws std::bad_alloc
26f7a9a33aba00f6fe644eece95158d147ae3dbdvboxsync */
26f7a9a33aba00f6fe644eece95158d147ae3dbdvboxsync RTCListBase<T, ITYPE, MT> &insert(size_t i, const T &val)
26f7a9a33aba00f6fe644eece95158d147ae3dbdvboxsync {
26f7a9a33aba00f6fe644eece95158d147ae3dbdvboxsync m_guard.enterWrite();
26f7a9a33aba00f6fe644eece95158d147ae3dbdvboxsync if (m_cSize == m_cCapacity)
26f7a9a33aba00f6fe644eece95158d147ae3dbdvboxsync realloc_grow(m_cCapacity + DefaultCapacity);
26f7a9a33aba00f6fe644eece95158d147ae3dbdvboxsync memmove(&m_pArray[i + 1], &m_pArray[i], (m_cSize - i) * sizeof(ITYPE));
26f7a9a33aba00f6fe644eece95158d147ae3dbdvboxsync RTCListHelper<T, ITYPE>::set(m_pArray, i, val);
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync ++m_cSize;
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync m_guard.leaveWrite();
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync
4ae0ac3d0dc641305a0b0197db43ee7c4b40f747vboxsync return *this;
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync }
4ae0ac3d0dc641305a0b0197db43ee7c4b40f747vboxsync
4ae0ac3d0dc641305a0b0197db43ee7c4b40f747vboxsync /**
4ae0ac3d0dc641305a0b0197db43ee7c4b40f747vboxsync * Prepend an item to the list.
4ae0ac3d0dc641305a0b0197db43ee7c4b40f747vboxsync *
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync * @param val The new item.
4ae0ac3d0dc641305a0b0197db43ee7c4b40f747vboxsync * @return a reference to this list.
4ae0ac3d0dc641305a0b0197db43ee7c4b40f747vboxsync * @throws std::bad_alloc
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync */
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync RTCListBase<T, ITYPE, MT> &prepend(const T &val)
4ae0ac3d0dc641305a0b0197db43ee7c4b40f747vboxsync {
236b2935f217749893b7034e59da3e3568928acevboxsync return insert(0, val);
236b2935f217749893b7034e59da3e3568928acevboxsync }
236b2935f217749893b7034e59da3e3568928acevboxsync
236b2935f217749893b7034e59da3e3568928acevboxsync /**
236b2935f217749893b7034e59da3e3568928acevboxsync * Prepend a list of type T to the list.
236b2935f217749893b7034e59da3e3568928acevboxsync *
236b2935f217749893b7034e59da3e3568928acevboxsync * @param other The list to prepend.
236b2935f217749893b7034e59da3e3568928acevboxsync * @return a reference to this list.
236b2935f217749893b7034e59da3e3568928acevboxsync * @throws std::bad_alloc
236b2935f217749893b7034e59da3e3568928acevboxsync */
236b2935f217749893b7034e59da3e3568928acevboxsync RTCListBase<T, ITYPE, MT> &prepend(const RTCListBase<T, ITYPE, MT> &other)
236b2935f217749893b7034e59da3e3568928acevboxsync {
236b2935f217749893b7034e59da3e3568928acevboxsync m_guard.enterWrite();
236b2935f217749893b7034e59da3e3568928acevboxsync if (m_cCapacity - m_cSize < other.m_cSize)
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync realloc_grow(m_cCapacity + (other.m_cSize - (m_cCapacity - m_cSize)));
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync memmove(&m_pArray[other.m_cSize], &m_pArray[0], m_cSize * sizeof(ITYPE));
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync RTCListHelper<T, ITYPE>::copyTo(m_pArray, other.m_pArray, 0, other.m_cSize);
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync m_cSize += other.m_cSize;
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync m_guard.leaveWrite();
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync return *this;
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync }
236b2935f217749893b7034e59da3e3568928acevboxsync
236b2935f217749893b7034e59da3e3568928acevboxsync /**
236b2935f217749893b7034e59da3e3568928acevboxsync * Append an item to the list.
157093a77f2752732368338110cb50fa6cd7717fvboxsync *
157093a77f2752732368338110cb50fa6cd7717fvboxsync * @param val The new item.
157093a77f2752732368338110cb50fa6cd7717fvboxsync * @return a reference to this list.
157093a77f2752732368338110cb50fa6cd7717fvboxsync * @throws std::bad_alloc
157093a77f2752732368338110cb50fa6cd7717fvboxsync */
157093a77f2752732368338110cb50fa6cd7717fvboxsync RTCListBase<T, ITYPE, MT> &append(const T &val)
157093a77f2752732368338110cb50fa6cd7717fvboxsync {
157093a77f2752732368338110cb50fa6cd7717fvboxsync m_guard.enterWrite();
157093a77f2752732368338110cb50fa6cd7717fvboxsync if (m_cSize == m_cCapacity)
157093a77f2752732368338110cb50fa6cd7717fvboxsync realloc_grow(m_cCapacity + DefaultCapacity);
157093a77f2752732368338110cb50fa6cd7717fvboxsync RTCListHelper<T, ITYPE>::set(m_pArray, m_cSize, val);
157093a77f2752732368338110cb50fa6cd7717fvboxsync ++m_cSize;
157093a77f2752732368338110cb50fa6cd7717fvboxsync m_guard.leaveWrite();
157093a77f2752732368338110cb50fa6cd7717fvboxsync
157093a77f2752732368338110cb50fa6cd7717fvboxsync return *this;
157093a77f2752732368338110cb50fa6cd7717fvboxsync }
157093a77f2752732368338110cb50fa6cd7717fvboxsync
157093a77f2752732368338110cb50fa6cd7717fvboxsync /**
157093a77f2752732368338110cb50fa6cd7717fvboxsync * Append a list of type T to the list.
157093a77f2752732368338110cb50fa6cd7717fvboxsync *
157093a77f2752732368338110cb50fa6cd7717fvboxsync * @param other The list to append.
157093a77f2752732368338110cb50fa6cd7717fvboxsync * @return a reference to this list.
157093a77f2752732368338110cb50fa6cd7717fvboxsync * @throws std::bad_alloc
157093a77f2752732368338110cb50fa6cd7717fvboxsync */
157093a77f2752732368338110cb50fa6cd7717fvboxsync RTCListBase<T, ITYPE, MT> &append(const RTCListBase<T, ITYPE, MT> &other)
157093a77f2752732368338110cb50fa6cd7717fvboxsync {
157093a77f2752732368338110cb50fa6cd7717fvboxsync m_guard.enterWrite();
157093a77f2752732368338110cb50fa6cd7717fvboxsync if (m_cCapacity - m_cSize < other.m_cSize)
157093a77f2752732368338110cb50fa6cd7717fvboxsync realloc_grow(m_cCapacity + (other.m_cSize - (m_cCapacity - m_cSize)));
157093a77f2752732368338110cb50fa6cd7717fvboxsync RTCListHelper<T, ITYPE>::copyTo(m_pArray, other.m_pArray, m_cSize, other.m_cSize);
157093a77f2752732368338110cb50fa6cd7717fvboxsync m_cSize += other.m_cSize;
157093a77f2752732368338110cb50fa6cd7717fvboxsync m_guard.leaveWrite();
157093a77f2752732368338110cb50fa6cd7717fvboxsync
157093a77f2752732368338110cb50fa6cd7717fvboxsync return *this;
157093a77f2752732368338110cb50fa6cd7717fvboxsync }
157093a77f2752732368338110cb50fa6cd7717fvboxsync
157093a77f2752732368338110cb50fa6cd7717fvboxsync /**
157093a77f2752732368338110cb50fa6cd7717fvboxsync * Copy the items of the other list into this list. All previous items of
157093a77f2752732368338110cb50fa6cd7717fvboxsync * this list are deleted.
157093a77f2752732368338110cb50fa6cd7717fvboxsync *
157093a77f2752732368338110cb50fa6cd7717fvboxsync * @param other The list to copy.
157093a77f2752732368338110cb50fa6cd7717fvboxsync * @return a reference to this list.
157093a77f2752732368338110cb50fa6cd7717fvboxsync */
157093a77f2752732368338110cb50fa6cd7717fvboxsync RTCListBase<T, ITYPE, MT> &operator=(const RTCListBase<T, ITYPE, MT>& other)
157093a77f2752732368338110cb50fa6cd7717fvboxsync {
157093a77f2752732368338110cb50fa6cd7717fvboxsync /* Prevent self assignment */
157093a77f2752732368338110cb50fa6cd7717fvboxsync if (this == &other)
157093a77f2752732368338110cb50fa6cd7717fvboxsync return *this;
157093a77f2752732368338110cb50fa6cd7717fvboxsync
157093a77f2752732368338110cb50fa6cd7717fvboxsync m_guard.enterWrite();
157093a77f2752732368338110cb50fa6cd7717fvboxsync /* Values cleanup */
157093a77f2752732368338110cb50fa6cd7717fvboxsync RTCListHelper<T, ITYPE>::eraseRange(m_pArray, 0, m_cSize);
157093a77f2752732368338110cb50fa6cd7717fvboxsync
157093a77f2752732368338110cb50fa6cd7717fvboxsync /* Copy */
157093a77f2752732368338110cb50fa6cd7717fvboxsync if (other.m_cSize != m_cCapacity)
157093a77f2752732368338110cb50fa6cd7717fvboxsync realloc_grow(other.m_cSize);
157093a77f2752732368338110cb50fa6cd7717fvboxsync m_cSize = other.m_cSize;
157093a77f2752732368338110cb50fa6cd7717fvboxsync RTCListHelper<T, ITYPE>::copyTo(m_pArray, other.m_pArray, 0, other.m_cSize);
157093a77f2752732368338110cb50fa6cd7717fvboxsync m_guard.leaveWrite();
157093a77f2752732368338110cb50fa6cd7717fvboxsync
157093a77f2752732368338110cb50fa6cd7717fvboxsync return *this;
157093a77f2752732368338110cb50fa6cd7717fvboxsync }
157093a77f2752732368338110cb50fa6cd7717fvboxsync
157093a77f2752732368338110cb50fa6cd7717fvboxsync /**
157093a77f2752732368338110cb50fa6cd7717fvboxsync * Replace an item in the list.
157093a77f2752732368338110cb50fa6cd7717fvboxsync *
157093a77f2752732368338110cb50fa6cd7717fvboxsync * @note No boundary checks are done. Make sure @a i is equal or greater zero
157093a77f2752732368338110cb50fa6cd7717fvboxsync * and smaller than RTCList::size.
157093a77f2752732368338110cb50fa6cd7717fvboxsync *
157093a77f2752732368338110cb50fa6cd7717fvboxsync * @param i The position of the item to replace.
157093a77f2752732368338110cb50fa6cd7717fvboxsync * @param val The new value.
157093a77f2752732368338110cb50fa6cd7717fvboxsync * @return a reference to this list.
157093a77f2752732368338110cb50fa6cd7717fvboxsync */
157093a77f2752732368338110cb50fa6cd7717fvboxsync RTCListBase<T, ITYPE, MT> &replace(size_t i, const T &val)
157093a77f2752732368338110cb50fa6cd7717fvboxsync {
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync m_guard.enterWrite();
157093a77f2752732368338110cb50fa6cd7717fvboxsync RTCListHelper<T, ITYPE>::erase(m_pArray, i);
157093a77f2752732368338110cb50fa6cd7717fvboxsync RTCListHelper<T, ITYPE>::set(m_pArray, i, val);
157093a77f2752732368338110cb50fa6cd7717fvboxsync m_guard.leaveWrite();
157093a77f2752732368338110cb50fa6cd7717fvboxsync
157093a77f2752732368338110cb50fa6cd7717fvboxsync return *this;
157093a77f2752732368338110cb50fa6cd7717fvboxsync }
157093a77f2752732368338110cb50fa6cd7717fvboxsync
157093a77f2752732368338110cb50fa6cd7717fvboxsync /**
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync * Return the first item as constant object.
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync *
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync * @note No boundary checks are done. Make sure @a i is equal or greater zero
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync * and smaller than RTCList::size.
157093a77f2752732368338110cb50fa6cd7717fvboxsync *
157093a77f2752732368338110cb50fa6cd7717fvboxsync * @return The first item.
157093a77f2752732368338110cb50fa6cd7717fvboxsync */
157093a77f2752732368338110cb50fa6cd7717fvboxsync GET_CRTYPE first() const
157093a77f2752732368338110cb50fa6cd7717fvboxsync {
157093a77f2752732368338110cb50fa6cd7717fvboxsync m_guard.enterRead();
157093a77f2752732368338110cb50fa6cd7717fvboxsync GET_CRTYPE res = RTCListHelper<T, ITYPE>::at(m_pArray, 0);
157093a77f2752732368338110cb50fa6cd7717fvboxsync m_guard.leaveRead();
157093a77f2752732368338110cb50fa6cd7717fvboxsync return res;
157093a77f2752732368338110cb50fa6cd7717fvboxsync }
157093a77f2752732368338110cb50fa6cd7717fvboxsync
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync /**
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync * Return the first item.
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync *
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync * @note No boundary checks are done. Make sure @a i is equal or greater zero
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync * and smaller than RTCList::size.
157093a77f2752732368338110cb50fa6cd7717fvboxsync *
157093a77f2752732368338110cb50fa6cd7717fvboxsync * @return The first item.
157093a77f2752732368338110cb50fa6cd7717fvboxsync */
157093a77f2752732368338110cb50fa6cd7717fvboxsync GET_RTYPE first()
157093a77f2752732368338110cb50fa6cd7717fvboxsync {
157093a77f2752732368338110cb50fa6cd7717fvboxsync m_guard.enterRead();
157093a77f2752732368338110cb50fa6cd7717fvboxsync GET_RTYPE res = RTCListHelper<T, ITYPE>::at(m_pArray, 0);
157093a77f2752732368338110cb50fa6cd7717fvboxsync m_guard.leaveRead();
157093a77f2752732368338110cb50fa6cd7717fvboxsync return res;
157093a77f2752732368338110cb50fa6cd7717fvboxsync }
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync /**
157093a77f2752732368338110cb50fa6cd7717fvboxsync * Return the last item as constant object.
157093a77f2752732368338110cb50fa6cd7717fvboxsync *
157093a77f2752732368338110cb50fa6cd7717fvboxsync * @note No boundary checks are done. Make sure @a i is equal or greater zero
157093a77f2752732368338110cb50fa6cd7717fvboxsync * and smaller than RTCList::size.
157093a77f2752732368338110cb50fa6cd7717fvboxsync *
157093a77f2752732368338110cb50fa6cd7717fvboxsync * @return The last item.
157093a77f2752732368338110cb50fa6cd7717fvboxsync */
157093a77f2752732368338110cb50fa6cd7717fvboxsync GET_CRTYPE last() const
157093a77f2752732368338110cb50fa6cd7717fvboxsync {
157093a77f2752732368338110cb50fa6cd7717fvboxsync m_guard.enterRead();
157093a77f2752732368338110cb50fa6cd7717fvboxsync GET_CRTYPE res = RTCListHelper<T, ITYPE>::at(m_pArray, m_cSize - 1);
157093a77f2752732368338110cb50fa6cd7717fvboxsync m_guard.leaveRead();
157093a77f2752732368338110cb50fa6cd7717fvboxsync return res;
157093a77f2752732368338110cb50fa6cd7717fvboxsync }
157093a77f2752732368338110cb50fa6cd7717fvboxsync
157093a77f2752732368338110cb50fa6cd7717fvboxsync /**
157093a77f2752732368338110cb50fa6cd7717fvboxsync * Return the last item.
157093a77f2752732368338110cb50fa6cd7717fvboxsync *
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync * @note No boundary checks are done. Make sure @a i is equal or greater zero
157093a77f2752732368338110cb50fa6cd7717fvboxsync * and smaller than RTCList::size.
157093a77f2752732368338110cb50fa6cd7717fvboxsync *
157093a77f2752732368338110cb50fa6cd7717fvboxsync * @return The last item.
157093a77f2752732368338110cb50fa6cd7717fvboxsync */
157093a77f2752732368338110cb50fa6cd7717fvboxsync GET_RTYPE last()
157093a77f2752732368338110cb50fa6cd7717fvboxsync {
157093a77f2752732368338110cb50fa6cd7717fvboxsync m_guard.enterRead();
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync GET_RTYPE res = RTCListHelper<T, ITYPE>::at(m_pArray, m_cSize - 1);
157093a77f2752732368338110cb50fa6cd7717fvboxsync m_guard.leaveRead();
157093a77f2752732368338110cb50fa6cd7717fvboxsync return res;
157093a77f2752732368338110cb50fa6cd7717fvboxsync }
157093a77f2752732368338110cb50fa6cd7717fvboxsync
157093a77f2752732368338110cb50fa6cd7717fvboxsync /**
157093a77f2752732368338110cb50fa6cd7717fvboxsync * Return the item at position @a i as constant object.
157093a77f2752732368338110cb50fa6cd7717fvboxsync *
157093a77f2752732368338110cb50fa6cd7717fvboxsync * @note No boundary checks are done. Make sure @a i is equal or greater zero
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync * and smaller than RTCList::size.
157093a77f2752732368338110cb50fa6cd7717fvboxsync *
157093a77f2752732368338110cb50fa6cd7717fvboxsync * @param i The position of the item to return.
157093a77f2752732368338110cb50fa6cd7717fvboxsync * @return The item at position @a i.
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync */
157093a77f2752732368338110cb50fa6cd7717fvboxsync GET_CRTYPE at(size_t i) const
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync {
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync m_guard.enterRead();
157093a77f2752732368338110cb50fa6cd7717fvboxsync GET_CRTYPE res = RTCListHelper<T, ITYPE>::at(m_pArray, i);
157093a77f2752732368338110cb50fa6cd7717fvboxsync m_guard.leaveRead();
157093a77f2752732368338110cb50fa6cd7717fvboxsync return res;
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync }
157093a77f2752732368338110cb50fa6cd7717fvboxsync
157093a77f2752732368338110cb50fa6cd7717fvboxsync /**
157093a77f2752732368338110cb50fa6cd7717fvboxsync * Return the item at position @a i.
157093a77f2752732368338110cb50fa6cd7717fvboxsync *
157093a77f2752732368338110cb50fa6cd7717fvboxsync * @note No boundary checks are done. Make sure @a i is equal or greater zero
157093a77f2752732368338110cb50fa6cd7717fvboxsync * and smaller than RTCList::size.
157093a77f2752732368338110cb50fa6cd7717fvboxsync *
157093a77f2752732368338110cb50fa6cd7717fvboxsync * @param i The position of the item to return.
157093a77f2752732368338110cb50fa6cd7717fvboxsync * @return The item at position @a i.
157093a77f2752732368338110cb50fa6cd7717fvboxsync */
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync GET_RTYPE at(size_t i)
157093a77f2752732368338110cb50fa6cd7717fvboxsync {
157093a77f2752732368338110cb50fa6cd7717fvboxsync m_guard.enterRead();
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync GET_RTYPE res = RTCListHelper<T, ITYPE>::at(m_pArray, i);
157093a77f2752732368338110cb50fa6cd7717fvboxsync m_guard.leaveRead();
157093a77f2752732368338110cb50fa6cd7717fvboxsync return res;
157093a77f2752732368338110cb50fa6cd7717fvboxsync }
157093a77f2752732368338110cb50fa6cd7717fvboxsync
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync /**
157093a77f2752732368338110cb50fa6cd7717fvboxsync * Return the item at position @a i as mutable reference.
157093a77f2752732368338110cb50fa6cd7717fvboxsync *
157093a77f2752732368338110cb50fa6cd7717fvboxsync * @note No boundary checks are done. Make sure @a i is equal or greater zero
157093a77f2752732368338110cb50fa6cd7717fvboxsync * and smaller than RTCList::size.
157093a77f2752732368338110cb50fa6cd7717fvboxsync *
157093a77f2752732368338110cb50fa6cd7717fvboxsync * @param i The position of the item to return.
157093a77f2752732368338110cb50fa6cd7717fvboxsync * @return The item at position @a i.
157093a77f2752732368338110cb50fa6cd7717fvboxsync */
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync T &operator[](size_t i)
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync {
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync m_guard.enterRead();
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync T &res = RTCListHelper<T, ITYPE>::at(m_pArray, i);
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync m_guard.leaveRead();
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync return res;
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync }
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync /**
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync * Return the item at position @a i. If @a i isn't valid within the list a
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync * default value is returned.
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync *
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync * @param i The position of the item to return.
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync * @return The item at position @a i.
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync */
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync T value(size_t i) const
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync {
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync m_guard.enterRead();
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync if (i >= m_cSize)
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync {
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync m_guard.leaveRead();
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync return T();
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync }
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync T res = RTCListHelper<T, ITYPE>::at(m_pArray, i);
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync m_guard.leaveRead();
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync return res;
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync }
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync /**
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync * Return the item at position @a i. If @a i isn't valid within the list
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync * @a defaultVal is returned.
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync *
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync * @param i The position of the item to return.
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync * @param defaultVal The value to return in case @a i is invalid.
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync * @return The item at position @a i.
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync */
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync T value(size_t i, const T &defaultVal) const
{
m_guard.enterRead();
if (i >= m_cSize)
{
m_guard.leaveRead();
return defaultVal;
}
T res = RTCListHelper<T, ITYPE>::at(m_pArray, i);
m_guard.leaveRead();
return res;
}
/**
* 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 res = RTCListHelper<T, ITYPE>::find(m_pArray, val, m_cSize) != m_cSize;
m_guard.leaveRead();
return res;
}
/**
* Remove the first item.
*
* @note No boundary checks are done. Make sure @a i is equal or greater zero
* and smaller than RTCList::size.
*/
void removeFirst()
{
removeAt(0);
}
/**
* Remove the last item.
*
* @note No boundary checks are done. Make sure @a i is equal or greater zero
* and smaller than RTCList::size.
*/
void removeLast()
{
removeAt(m_cSize - 1);
}
/**
* Remove the item at position @a i.
*
* @note No boundary checks are done. Make sure @a i is equal or greater zero
* and smaller than RTCList::size.
*
* @param i The position of the item to remove.
*/
void removeAt(size_t i)
{
m_guard.enterWrite();
RTCListHelper<T, ITYPE>::erase(m_pArray, i);
/* Not last element? */
if (i < m_cSize - 1)
memmove(&m_pArray[i], &m_pArray[i + 1], (m_cSize - i - 1) * sizeof(ITYPE));
--m_cSize;
m_guard.leaveWrite();
}
/**
* Remove a range of items from the list.
*
* @note No boundary checks are done. Make sure @a iFrom is equal or
* greater zero and smaller than RTCList::size. @a iTo has to be
* greater than @a iFrom and equal or smaller than RTCList::size.
*
* @param iFrom The start position of the items to remove.
* @param iTo The end position of the items to remove (excluded).
*/
void removeRange(size_t iFrom, size_t iTo)
{
m_guard.enterWrite();
RTCListHelper<T, ITYPE>::eraseRange(m_pArray, iFrom, iTo - iFrom);
/* Not last elements? */
if (m_cSize - iTo > 0)
memmove(&m_pArray[iFrom], &m_pArray[iTo], (m_cSize - iTo) * sizeof(ITYPE));
m_cSize -= iTo - iFrom;
m_guard.leaveWrite();
}
/**
* Delete all items in the list.
*/
void clear()
{
m_guard.enterWrite();
/* Values cleanup */
RTCListHelper<T, ITYPE>::eraseRange(m_pArray, 0, m_cSize);
if (m_cSize != DefaultCapacity)
realloc_grow(DefaultCapacity);
m_cSize = 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* res = m_pArray;
m_guard.leaveRead();
return res;
}
/* Define our own new and delete. */
RTMEMEF_NEW_AND_DELETE_OPERATORS();
/**
* The default capacity of the list. This is also used as grow factor.
*/
static const size_t DefaultCapacity;
protected:
/**
* Generic realloc, which does some kind of boundary checking.
*/
void realloc(size_t cNewSize)
{
/* Same size? */
if (cNewSize == m_cCapacity)
return;
/* If we get smaller we have to delete some of the objects at the end
of the list. */
if ( cNewSize < m_cSize
&& m_pArray)
{
RTCListHelper<T, ITYPE>::eraseRange(m_pArray, cNewSize, m_cSize - cNewSize);
m_cSize -= m_cSize - cNewSize;
}
/* If we get zero we delete the array it self. */
if ( cNewSize == 0
&& m_pArray)
{
RTMemFree(m_pArray);
m_pArray = 0;
}
m_cCapacity = cNewSize;
/* Resize the array. */
if (cNewSize > 0)
{
m_pArray = static_cast<ITYPE*>(RTMemRealloc(m_pArray, sizeof(ITYPE) * cNewSize));
if (!m_pArray)
{
/** @todo you leak memory. */
m_cCapacity = 0;
m_cSize = 0;
#ifdef RT_EXCEPTIONS_ENABLED
throw std::bad_alloc();
#endif
}
}
}
/**
* Special realloc method which require that the array will grow.
*
* @note No boundary checks are done!
*/
void realloc_grow(size_t cNewSize)
{
/* Resize the array. */
m_cCapacity = cNewSize;
m_pArray = static_cast<ITYPE*>(RTMemRealloc(m_pArray, sizeof(ITYPE) * cNewSize));
if (!m_pArray)
{
/** @todo you leak memory. */
m_cCapacity = 0;
m_cSize = 0;
#ifdef RT_EXCEPTIONS_ENABLED
throw std::bad_alloc();
#endif
}
}
/** The internal list array. */
ITYPE *m_pArray;
/** The current count of items in use. */
size_t m_cSize;
/** 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>::DefaultCapacity = 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 cCapacitiy The initial capacity the list has.
* @throws std::bad_alloc
*/
RTCList(size_t cCapacity = BASE::DefaultCapacity)
: BASE(cCapacity) {}
/* Define our own new and delete. */
RTMEMEF_NEW_AND_DELETE_OPERATORS();
};
/**
* 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 cCapacitiy The initial capacity the list has.
* @throws std::bad_alloc
*/
RTCList(size_t cCapacity = BASE::DefaultCapacity)
: BASE(cCapacity) {}
/* Define our own new and delete. */
RTMEMEF_NEW_AND_DELETE_OPERATORS();
};
/**
* 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 cCapacitiy The initial capacity the list has.
* @throws std::bad_alloc
*/
RTCList(size_t cCapacity = BASE::DefaultCapacity)
: BASE(cCapacity) {}
/* Define our own new and delete. */
RTMEMEF_NEW_AND_DELETE_OPERATORS();
};
/** @} */
#endif /* !___iprt_cpp_list_h */