mirror of
https://github.com/libretro/scummvm.git
synced 2024-12-02 23:26:44 +00:00
COMMON: Add standard data method to Common::Array
This matches the C++11 std::vector method of the same name, and replaces usage of taking the address of the first element of an array by &array[0] or &array.front() or &*array.begin(). The data method is better than these usages because it can be used even when the array is empty.
This commit is contained in:
parent
c867a1834f
commit
4938d5cc76
@ -87,10 +87,10 @@ public:
|
||||
* Construct an array by copying data from a regular array.
|
||||
*/
|
||||
template<class T2>
|
||||
Array(const T2 *data, size_type n) {
|
||||
Array(const T2 *array, size_type n) {
|
||||
_size = n;
|
||||
allocCapacity(n);
|
||||
uninitialized_copy(data, data + _size, _storage);
|
||||
uninitialized_copy(array, array + _size, _storage);
|
||||
}
|
||||
|
||||
~Array() {
|
||||
@ -123,6 +123,16 @@ public:
|
||||
_storage[_size].~T();
|
||||
}
|
||||
|
||||
/** Returns a pointer to the underlying memory serving as element storage. */
|
||||
const T *data() const {
|
||||
return _storage;
|
||||
}
|
||||
|
||||
/** Returns a pointer to the underlying memory serving as element storage. */
|
||||
T *data() {
|
||||
return _storage;
|
||||
}
|
||||
|
||||
/** Returns a reference to the first element of the array. */
|
||||
T &front() {
|
||||
assert(_size > 0);
|
||||
|
@ -353,6 +353,15 @@ class ArrayTestSuite : public CxxTest::TestSuite
|
||||
TS_ASSERT_EQUALS(array2.size(), (unsigned int)3);
|
||||
}
|
||||
|
||||
void test_data() {
|
||||
Common::Array<int> array;
|
||||
TS_ASSERT(array.data() == nullptr);
|
||||
array.resize(2);
|
||||
TS_ASSERT(array.data() != nullptr);
|
||||
TS_ASSERT_EQUALS(array.data(), &array.front());
|
||||
TS_ASSERT_EQUALS(array.data() + array.size() - 1, &array.back());
|
||||
}
|
||||
|
||||
void test_front_back_push_pop() {
|
||||
Common::Array<int> container;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user