Commit: c2122c39ae7060470253ce3514b280ecdf2329c1 Author: Hans Goudey Date: Wed Aug 25 09:55:34 2021 -0500 Branches: temp-geometry-nodes-fields--fields https://developer.blender.org/rBc2122c39ae7060470253ce3514b280ecdf2329c1
Remove GArray, futher improvement =================================================================== M source/blender/functions/CMakeLists.txt M source/blender/functions/FN_field.hh D source/blender/functions/FN_generic_array.hh D source/blender/functions/tests/FN_generic_array_test.cc =================================================================== diff --git a/source/blender/functions/CMakeLists.txt b/source/blender/functions/CMakeLists.txt index a67667ba46e..a4ba9a61a85 100644 --- a/source/blender/functions/CMakeLists.txt +++ b/source/blender/functions/CMakeLists.txt @@ -40,7 +40,6 @@ set(SRC FN_cpp_type.hh FN_cpp_type_make.hh FN_field.hh - FN_generic_array.hh FN_generic_pointer.hh FN_generic_span.hh FN_generic_value_map.hh @@ -69,7 +68,6 @@ if(WITH_GTESTS) set(TEST_SRC tests/FN_cpp_type_test.cc tests/FN_field_test.cc - tests/FN_generic_array_test.cc tests/FN_generic_span_test.cc tests/FN_generic_vector_array_test.cc tests/FN_multi_function_procedure_test.cc diff --git a/source/blender/functions/FN_field.hh b/source/blender/functions/FN_field.hh index 2f701c7ec52..208f3ce2d76 100644 --- a/source/blender/functions/FN_field.hh +++ b/source/blender/functions/FN_field.hh @@ -111,7 +111,8 @@ void evaluate_fields(const Span<std::shared_ptr<Field>> fields, MFProcedure procedure; MFProcedureBuilder builder{procedure}; - Map<const InputField *, MFVariable *> fields_to_variables; + Map<const Field *, MFVariable *> fields_to_variables; + Map<const GMutableSpan, MFVariable *> outputs_to_variables; /* Add the unique inputs. */ for (blender::Map<const InputField *, GVArrayPtr>::Item item : computed_inputs.items()) { @@ -120,7 +121,11 @@ void evaluate_fields(const Span<std::shared_ptr<Field>> fields, } /* Add the inputs recursively for the entire group of nodes. */ - // builder.add_return(); + builder.add_return(); + for (const int i : outputs.index_range()) { + BLI_assert(fields_to_variables.contains(fields[i].get())); + builder.add_output_parameter(*fields_to_variables.lookup(fields[i].get())); + } // builder.add_output_parameter(*var4); BLI_assert(procedure.validate()); @@ -137,6 +142,7 @@ void evaluate_fields(const Span<std::shared_ptr<Field>> fields, /* Add the output arrays. */ for (const int i : fields.index_range()) { + BLI_assert(outputs[i].type() == fields[i]->type()); params.add_uninitialized_single_output(outputs[i]); } diff --git a/source/blender/functions/FN_generic_array.hh b/source/blender/functions/FN_generic_array.hh deleted file mode 100644 index 523df5e9566..00000000000 --- a/source/blender/functions/FN_generic_array.hh +++ /dev/null @@ -1,274 +0,0 @@ -/* - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - -#pragma once - -/** \file - * \ingroup fn - * - * This is a generic counterpart to `blender::Array`, used when the type is not known at runtime. - * - * `GArray` should generally only be used for passing data around in dynamic contexts. - * It does not support a few things that `blender::Array` supports: - * - Small object optimization / inline buffer. - * - Exception safety and various more specific constructors. - * - * Note that a generic vector is purposefully not added to Blender, since it would encourage use of - * the `append` function, which would be inefficient for dynamic types. Dynamic types should - * usually be processed in batches. - */ - -#include "BLI_allocator.hh" - -#include "FN_cpp_type.hh" -#include "FN_generic_span.hh" - -namespace blender::fn { - -template< - /** - * The allocator used by this array. Should rarely be changed, except when you don't want that - * MEM_* functions are used internally. - */ - typename Allocator = GuardedAllocator> -class GArray { - protected: - /** The type of the data in the array, will be null after the array is default constructed, - * but a value should be assigned before any other interaction with the array. */ - const CPPType *type_; - void *data_; - int64_t size_; - - Allocator allocator_; - - public: - /** - * The default constructor creates an empty array, the only situation in which the type is - * allowed to be null. This default constructor exists so `GArray` can be used in containers, - * but the type should be supplied before doing anything else to the array. - */ - GArray(Allocator allocator = {}) noexcept : allocator_(allocator) - { - type_ = nullptr; - data_ = nullptr; - size_ = 0; - } - - /** - * Create and allocate a new array, with elements default constructed - * (which does not do anything for trivial types). - */ - GArray(const CPPType &type, int64_t size, Allocator allocator = {}) : GArray(type, allocator) - { - BLI_assert(size >= 0); - size_ = size; - data_ = this->allocate(size_); - type_->default_construct_n(data_, size_); - } - - /** - * Create an empty array with just a type. - */ - GArray(const CPPType &type, Allocator allocator = {}) : GArray(allocator) - { - type_ = &type; - data_ = nullptr; - size_ = 0; - } - - /** - * Take ownership of a buffer with a provided size. The buffer should be - * allocated with the same allocator provided to the constructor. - */ - GArray(const CPPType &type, void *buffer, int64_t size, Allocator allocator = {}) - : GArray(type, allocator) - { - BLI_assert(size >= 0); - BLI_assert(buffer != nullptr || size == 0); - BLI_assert(type_->pointer_has_valid_alignment(buffer)); - - data_ = buffer; - size_ = size; - } - - /** - * Create an array by copying values from a generic span. - */ - GArray(const GSpan span, Allocator allocator = {}) : GArray(span.type(), span.size(), allocator) - { - if (span.data() != nullptr) { - BLI_assert(span.size() != 0); - type_->copy_construct_n(span.data(), data_, size_); - } - } - - /** - * Create an array by copying values from another generic array. - */ - GArray(const GArray &other) : GArray(other.as_span(), other.allocator()) - { - } - - /** - * Create an array by taking ownership of another array's data and destructing it. - */ - GArray(GArray &&other) : GArray(other.type(), other.data(), other.size(), other.allocator()) - { - other.data_ = nullptr; - other.size_ = 0; - } - - ~GArray() - { - if (data_ != nullptr) { - type_->destruct_n(data_, size_); - this->deallocate(data_); - } - } - - GArray &operator=(const GArray &other) - { - return copy_assign_container(*this, other); - } - - GArray &operator=(GArray &&other) - { - return move_assign_container(*this, std::move(other)); - } - - const CPPType &type() const - { - BLI_assert(type_ != nullptr); - return *type_; - } - - bool is_empty() const - { - return size_ == 0; - } - - /** - * Return the number of elements in the array (not the size in bytes). - */ - int64_t size() const - { - return size_; - } - - /** - * Get a pointer to the beginning of the array. - */ - const void *data() const - { - return data_; - } - void *data() - { - return data_; - } - - const void *operator[](int64_t index) const - { - BLI_assert(index < size_); - return POINTER_OFFSET(data_, type_->size() * index); - } - - void *operator[](int64_t index) - { - BLI_assert(index < size_); - return POINTER_OFFSET(data_, type_->size() * index); - } - - operator GSpan() const - { - BLI_assert(type_ != nullptr); - return GSpan(*type_, data_, size_); - } - - operator GMutableSpan() - { - BLI_assert(type_ != nullptr); - return GMutableSpan(*type_, data_, size_); - } - - GSpan as_span() const - { - return *this; - } - - GMutableSpan as_mutable_span() - { - return *this; - } - - /** - * Access the allocator used by this array. - */ - Allocator &allocator() - { - return allocator_; - } - const Allocator &allocator() const - { - return allocator_; - } - - /** - * Destruct values and create a new array of the given size. The values in the new array are - * default constructed. - */ - void reinitialize(const int64_t new_size) - { - BLI_assert(new_size >= 0); - int64_t old_size = size_; - - type_->destruct_n(data_, size_); - size_ = 0; - - if (new_size <= old_size) { - type_->default_construct_n(data_, new_size); - } - else { - void *new_data = this->allocate(new_size); - try { - type_->default_construct_n(new_data, new_size); - } - catch (...) { - this->deallocate(new_data); - throw; - } - this->deallocate(data_); - data_ = new_data; - } - - size_ = new_size; - } - - private: - void *allocate(int64_t size) - { - const int64_t item_size = type_->size(); - const int64_t alignment = type_->alignment(); - return allocator_.allocate(static_cast<size_t>(size) * item_size, alignment, AT); - } - - void deallocate(void *ptr) - { - allocator_.deallocate(ptr); - } -}; - -} // namespace blender::fn diff --git a/source/blender/functions/tests/FN_generic_array_test.cc b/source/blender/functions/tests/FN_generic_array_test.cc deleted file mode 100644 index e06013b4cf7..00000000000 --- a/source/blender/functions/tests/FN_generic_array_test.cc +++ /dev/null @@ -1,116 +0,0 @@ -/* Apache License, Version 2.0 */ - -#include "testing/testing.h" - -#include "MEM_guardedalloc.h" - -#include "BLI_array.hh" - -#include "FN_generic_array.hh" - -namespace blender::fn::tests { - -TEST(generic_array, TypeConstructor) -{ - GArray array(CPPType::get<float>()); - EXPECT_TRUE(array.data() == nullptr); - EXPECT_EQ(array.size(), 0); - EXPECT_EQ(array.as_span().typed<float>().size(), 0); - EXPECT_TRUE(array.is_empty()); -} - -TEST(generic_array, MoveConstructor) -{ - GArray array_a(CPPType::get<int32_t>(), (int64_t)10); - GMutableSpan span_a = array_a.as_mutable_sp @@ Diff output truncated at 10240 characters. @@ _______________________________________________ Bf-blender-cvs mailing list Bf-blender-cvs@blender.org List details, subscription details or unsubscribe: https://lists.blender.org/mailman/listinfo/bf-blender-cvs