Add list initialization support for Vector & LocalVector
This commit is contained in:
parent
d2ac67d55e
commit
c0d3bdc0ca
|
@ -36,6 +36,8 @@
|
||||||
#include "core/templates/sort_array.h"
|
#include "core/templates/sort_array.h"
|
||||||
#include "core/templates/vector.h"
|
#include "core/templates/vector.h"
|
||||||
|
|
||||||
|
#include <initializer_list>
|
||||||
|
|
||||||
template <class T, class U = uint32_t, bool force_trivial = false>
|
template <class T, class U = uint32_t, bool force_trivial = false>
|
||||||
class LocalVector {
|
class LocalVector {
|
||||||
private:
|
private:
|
||||||
|
@ -228,6 +230,12 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
_FORCE_INLINE_ LocalVector() {}
|
_FORCE_INLINE_ LocalVector() {}
|
||||||
|
_FORCE_INLINE_ LocalVector(std::initializer_list<T> p_init) {
|
||||||
|
reserve(p_init.size());
|
||||||
|
for (const T &element : p_init) {
|
||||||
|
push_back(element);
|
||||||
|
}
|
||||||
|
}
|
||||||
_FORCE_INLINE_ LocalVector(const LocalVector &p_from) {
|
_FORCE_INLINE_ LocalVector(const LocalVector &p_from) {
|
||||||
resize(p_from.size());
|
resize(p_from.size());
|
||||||
for (U i = 0; i < p_from.count; i++) {
|
for (U i = 0; i < p_from.count; i++) {
|
||||||
|
|
|
@ -43,6 +43,8 @@
|
||||||
#include "core/templates/search_array.h"
|
#include "core/templates/search_array.h"
|
||||||
#include "core/templates/sort_array.h"
|
#include "core/templates/sort_array.h"
|
||||||
|
|
||||||
|
#include <initializer_list>
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
class VectorWriteProxy {
|
class VectorWriteProxy {
|
||||||
public:
|
public:
|
||||||
|
@ -258,6 +260,15 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
_FORCE_INLINE_ Vector() {}
|
_FORCE_INLINE_ Vector() {}
|
||||||
|
_FORCE_INLINE_ Vector(std::initializer_list<T> p_init) {
|
||||||
|
Error err = _cowdata.resize(p_init.size());
|
||||||
|
ERR_FAIL_COND(err);
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
for (const T &element : p_init) {
|
||||||
|
_cowdata.set(i++, element);
|
||||||
|
}
|
||||||
|
}
|
||||||
_FORCE_INLINE_ Vector(const Vector &p_from) { _cowdata._ref(p_from._cowdata); }
|
_FORCE_INLINE_ Vector(const Vector &p_from) { _cowdata._ref(p_from._cowdata); }
|
||||||
|
|
||||||
_FORCE_INLINE_ ~Vector() {}
|
_FORCE_INLINE_ ~Vector() {}
|
||||||
|
|
|
@ -33,6 +33,7 @@
|
||||||
#include "core/config/engine.h"
|
#include "core/config/engine.h"
|
||||||
#include "core/config/project_settings.h"
|
#include "core/config/project_settings.h"
|
||||||
#include "core/string/ustring.h"
|
#include "core/string/ustring.h"
|
||||||
|
#include "core/templates/local_vector.h"
|
||||||
#include "core/version.h"
|
#include "core/version.h"
|
||||||
#include "servers/rendering/rendering_device.h"
|
#include "servers/rendering/rendering_device.h"
|
||||||
|
|
||||||
|
@ -41,7 +42,6 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
|
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
|
||||||
#define APP_SHORT_NAME "GodotEngine"
|
#define APP_SHORT_NAME "GodotEngine"
|
||||||
|
@ -212,7 +212,7 @@ VkBool32 VulkanContext::_check_layers(uint32_t check_count, const char *const *c
|
||||||
}
|
}
|
||||||
|
|
||||||
Error VulkanContext::_get_preferred_validation_layers(uint32_t *count, const char *const **names) {
|
Error VulkanContext::_get_preferred_validation_layers(uint32_t *count, const char *const **names) {
|
||||||
static const std::vector<std::vector<const char *>> instance_validation_layers_alt{
|
static const LocalVector<LocalVector<const char *>> instance_validation_layers_alt{
|
||||||
// Preferred set of validation layers
|
// Preferred set of validation layers
|
||||||
{ "VK_LAYER_KHRONOS_validation" },
|
{ "VK_LAYER_KHRONOS_validation" },
|
||||||
|
|
||||||
|
@ -249,10 +249,10 @@ Error VulkanContext::_get_preferred_validation_layers(uint32_t *count, const cha
|
||||||
}
|
}
|
||||||
|
|
||||||
for (uint32_t i = 0; i < instance_validation_layers_alt.size(); i++) {
|
for (uint32_t i = 0; i < instance_validation_layers_alt.size(); i++) {
|
||||||
if (_check_layers(instance_validation_layers_alt[i].size(), instance_validation_layers_alt[i].data(), instance_layer_count, instance_layers)) {
|
if (_check_layers(instance_validation_layers_alt[i].size(), instance_validation_layers_alt[i].ptr(), instance_layer_count, instance_layers)) {
|
||||||
*count = instance_validation_layers_alt[i].size();
|
*count = instance_validation_layers_alt[i].size();
|
||||||
if (names != nullptr) {
|
if (names != nullptr) {
|
||||||
*names = instance_validation_layers_alt[i].data();
|
*names = instance_validation_layers_alt[i].ptr();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,6 +37,17 @@
|
||||||
|
|
||||||
namespace TestLocalVector {
|
namespace TestLocalVector {
|
||||||
|
|
||||||
|
TEST_CASE("[LocalVector] List Initialization.") {
|
||||||
|
LocalVector<int> vector{ 0, 1, 2, 3, 4 };
|
||||||
|
|
||||||
|
CHECK(vector.size() == 5);
|
||||||
|
CHECK(vector[0] == 0);
|
||||||
|
CHECK(vector[1] == 1);
|
||||||
|
CHECK(vector[2] == 2);
|
||||||
|
CHECK(vector[3] == 3);
|
||||||
|
CHECK(vector[4] == 4);
|
||||||
|
}
|
||||||
|
|
||||||
TEST_CASE("[LocalVector] Push Back.") {
|
TEST_CASE("[LocalVector] Push Back.") {
|
||||||
LocalVector<int> vector;
|
LocalVector<int> vector;
|
||||||
vector.push_back(0);
|
vector.push_back(0);
|
||||||
|
|
|
@ -37,6 +37,17 @@
|
||||||
|
|
||||||
namespace TestVector {
|
namespace TestVector {
|
||||||
|
|
||||||
|
TEST_CASE("[Vector] List initialization") {
|
||||||
|
Vector<int> vector{ 0, 1, 2, 3, 4 };
|
||||||
|
|
||||||
|
CHECK(vector.size() == 5);
|
||||||
|
CHECK(vector[0] == 0);
|
||||||
|
CHECK(vector[1] == 1);
|
||||||
|
CHECK(vector[2] == 2);
|
||||||
|
CHECK(vector[3] == 3);
|
||||||
|
CHECK(vector[4] == 4);
|
||||||
|
}
|
||||||
|
|
||||||
TEST_CASE("[Vector] Push back and append") {
|
TEST_CASE("[Vector] Push back and append") {
|
||||||
Vector<int> vector;
|
Vector<int> vector;
|
||||||
vector.push_back(0);
|
vector.push_back(0);
|
||||||
|
|
Loading…
Reference in New Issue