mirror of
https://github.com/libretro/scummvm.git
synced 2025-04-12 03:36:42 +00:00
COMMON: Add standard count & count+copy array constructors
These are additions to match C++11 std::vector common init patterns, to make Common::Array cover more common use cases where C-style arrays are currently used (and should not be).
This commit is contained in:
parent
8bc745fb55
commit
c867a1834f
@ -59,6 +59,23 @@ protected:
|
|||||||
public:
|
public:
|
||||||
Array() : _capacity(0), _size(0), _storage(0) {}
|
Array() : _capacity(0), _size(0), _storage(0) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs an array with `count` default-inserted instances of T. No
|
||||||
|
* copies are made.
|
||||||
|
*/
|
||||||
|
explicit Array(size_type count) : _size(0) {
|
||||||
|
allocCapacity(count);
|
||||||
|
resize(count);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs an array with `count` copies of elements with value `value`.
|
||||||
|
*/
|
||||||
|
Array(size_type count, const T &value) : _size(count) {
|
||||||
|
allocCapacity(count);
|
||||||
|
uninitialized_fill_n(_storage, count, value);
|
||||||
|
}
|
||||||
|
|
||||||
Array(const Array<T> &array) : _capacity(array._size), _size(array._size), _storage(0) {
|
Array(const Array<T> &array) : _capacity(array._size), _size(array._size), _storage(0) {
|
||||||
if (array._storage) {
|
if (array._storage) {
|
||||||
allocCapacity(_size);
|
allocCapacity(_size);
|
||||||
|
@ -55,11 +55,11 @@ void uninitialized_fill(Type *first, Type *last, const Value &x) {
|
|||||||
* It requires the range [dst, dst + n) to be valid and
|
* It requires the range [dst, dst + n) to be valid and
|
||||||
* uninitialized.
|
* uninitialized.
|
||||||
*/
|
*/
|
||||||
/*template<class Type, class Value>
|
template<class Type, class Value>
|
||||||
void uninitialized_fill_n(Type *dst, size_t n, const Value &x) {
|
void uninitialized_fill_n(Type *dst, size_t n, const Value &x) {
|
||||||
while (n--)
|
while (n--)
|
||||||
new ((void *)dst++) Type(x);
|
new ((void *)dst++) Type(x);
|
||||||
}*/
|
}
|
||||||
|
|
||||||
} // End of namespace Common
|
} // End of namespace Common
|
||||||
|
|
||||||
|
@ -298,6 +298,49 @@ class ArrayTestSuite : public CxxTest::TestSuite
|
|||||||
TS_ASSERT_EQUALS(array2.size(), (unsigned int)3);
|
TS_ASSERT_EQUALS(array2.size(), (unsigned int)3);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class Copyable {
|
||||||
|
bool _copied;
|
||||||
|
int _value;
|
||||||
|
Copyable &operator=(Copyable &);
|
||||||
|
public:
|
||||||
|
Copyable() : _copied(false), _value(1) {}
|
||||||
|
explicit Copyable(const int v) : _copied(false), _value(v) {}
|
||||||
|
Copyable(const Copyable &other) : _copied(true), _value(other._value) {}
|
||||||
|
bool copied() const { return _copied; }
|
||||||
|
int value() const { return _value; }
|
||||||
|
};
|
||||||
|
|
||||||
|
void test_array_constructor_count() {
|
||||||
|
Common::Array<int> array(10);
|
||||||
|
TS_ASSERT_EQUALS(array.size(), 10U);
|
||||||
|
TS_ASSERT_EQUALS(array[0], 0);
|
||||||
|
TS_ASSERT_EQUALS(array[9], 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_array_constructor_count_copy_value() {
|
||||||
|
Common::Array<int> trivial(5, 1);
|
||||||
|
TS_ASSERT_EQUALS(trivial.size(), 5U);
|
||||||
|
TS_ASSERT_EQUALS(trivial[0], 1);
|
||||||
|
TS_ASSERT_EQUALS(trivial[4], 1);
|
||||||
|
|
||||||
|
Copyable c(123);
|
||||||
|
typedef Common::Array<Copyable> NonTrivialArray;
|
||||||
|
|
||||||
|
NonTrivialArray nonTrivialCopy(3, c);
|
||||||
|
TS_ASSERT_EQUALS(nonTrivialCopy.size(), 3U);
|
||||||
|
for (NonTrivialArray::size_type i = 0; i < nonTrivialCopy.size(); ++i) {
|
||||||
|
TS_ASSERT_EQUALS(nonTrivialCopy[0].value(), 123);
|
||||||
|
TS_ASSERT(nonTrivialCopy[0].copied());
|
||||||
|
}
|
||||||
|
|
||||||
|
NonTrivialArray nonTrivialDefault(3);
|
||||||
|
TS_ASSERT_EQUALS(nonTrivialDefault.size(), 3U);
|
||||||
|
for (NonTrivialArray::size_type i = 0; i < nonTrivialDefault.size(); ++i) {
|
||||||
|
TS_ASSERT_EQUALS(nonTrivialDefault[0].value(), 1);
|
||||||
|
TS_ASSERT(!nonTrivialDefault[0].copied());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void test_array_constructor_str() {
|
void test_array_constructor_str() {
|
||||||
const char *array1[] = { "a", "b", "c" };
|
const char *array1[] = { "a", "b", "c" };
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user