[Scummvm-git-logs] scummvm master -> 4938d5cc76b0ba1037be1b9b589dd2093c62509f

sev- sev at scummvm.org
Sat Sep 30 11:17:57 CEST 2017


This automated email contains information about 2 new commits which have been
pushed to the 'scummvm' repo located at https://github.com/scummvm/scummvm .

Summary:
c867a1834f COMMON: Add standard count & count+copy array constructors
4938d5cc76 COMMON: Add standard data method to Common::Array


Commit: c867a1834ff9ac08214b19e34a71aeae215f00a4
    https://github.com/scummvm/scummvm/commit/c867a1834ff9ac08214b19e34a71aeae215f00a4
Author: Colin Snover (github.com at zetafleet.com)
Date: 2017-09-30T11:17:53+02:00

Commit Message:
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).

Changed paths:
    common/array.h
    common/memory.h
    test/common/array.h


diff --git a/common/array.h b/common/array.h
index 04ec9f9..ed54cd5 100644
--- a/common/array.h
+++ b/common/array.h
@@ -59,6 +59,23 @@ protected:
 public:
 	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) {
 		if (array._storage) {
 			allocCapacity(_size);
diff --git a/common/memory.h b/common/memory.h
index c32af42..91a3200 100644
--- a/common/memory.h
+++ b/common/memory.h
@@ -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
  * uninitialized.
  */
-/*template<class Type, class Value>
+template<class Type, class Value>
 void uninitialized_fill_n(Type *dst, size_t n, const Value &x) {
 	while (n--)
 		new ((void *)dst++) Type(x);
-}*/
+}
 
 } // End of namespace Common
 
diff --git a/test/common/array.h b/test/common/array.h
index 515dae3..7506162 100644
--- a/test/common/array.h
+++ b/test/common/array.h
@@ -298,6 +298,49 @@ class ArrayTestSuite : public CxxTest::TestSuite
 		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() {
 		const char *array1[] = { "a", "b", "c" };
 


Commit: 4938d5cc76b0ba1037be1b9b589dd2093c62509f
    https://github.com/scummvm/scummvm/commit/4938d5cc76b0ba1037be1b9b589dd2093c62509f
Author: Colin Snover (github.com at zetafleet.com)
Date: 2017-09-30T11:17:53+02:00

Commit Message:
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.

Changed paths:
    common/array.h
    test/common/array.h


diff --git a/common/array.h b/common/array.h
index ed54cd5..bb7e03c 100644
--- a/common/array.h
+++ b/common/array.h
@@ -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);
diff --git a/test/common/array.h b/test/common/array.h
index 7506162..45be993 100644
--- a/test/common/array.h
+++ b/test/common/array.h
@@ -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;
 





More information about the Scummvm-git-logs mailing list