2020-06-16 16:35:57 +02:00
|
|
|
/*
|
|
|
|
* 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 file provides an MFParams and MFParamsBuilder structure.
|
|
|
|
*
|
|
|
|
* `MFParamsBuilder` is used by a function caller to be prepare all parameters that are passed into
|
|
|
|
* the function. `MFParams` is then used inside the called function to access the parameters.
|
|
|
|
*/
|
|
|
|
|
2021-03-21 19:31:24 +01:00
|
|
|
#include "BLI_resource_collector.hh"
|
|
|
|
|
2020-06-16 16:35:57 +02:00
|
|
|
#include "FN_generic_vector_array.hh"
|
2021-03-21 19:31:24 +01:00
|
|
|
#include "FN_generic_virtual_vector_array.hh"
|
2020-06-16 16:35:57 +02:00
|
|
|
#include "FN_multi_function_signature.hh"
|
|
|
|
|
2020-07-03 14:25:20 +02:00
|
|
|
namespace blender::fn {
|
2020-06-16 16:35:57 +02:00
|
|
|
|
|
|
|
class MFParamsBuilder {
|
|
|
|
private:
|
2021-03-21 19:31:24 +01:00
|
|
|
ResourceCollector resources_;
|
2020-07-03 14:20:42 +02:00
|
|
|
const MFSignature *signature_;
|
2020-07-20 12:16:20 +02:00
|
|
|
int64_t min_array_size_;
|
2021-03-21 19:31:24 +01:00
|
|
|
Vector<const GVArray *> virtual_arrays_;
|
2020-07-03 14:20:42 +02:00
|
|
|
Vector<GMutableSpan> mutable_spans_;
|
2021-03-21 19:31:24 +01:00
|
|
|
Vector<const GVVectorArray *> virtual_vector_arrays_;
|
2020-07-03 14:20:42 +02:00
|
|
|
Vector<GVectorArray *> vector_arrays_;
|
2020-06-16 16:35:57 +02:00
|
|
|
|
|
|
|
friend class MFParams;
|
|
|
|
|
|
|
|
public:
|
2020-07-20 12:16:20 +02:00
|
|
|
MFParamsBuilder(const MFSignature &signature, int64_t min_array_size)
|
2020-07-03 14:20:42 +02:00
|
|
|
: signature_(&signature), min_array_size_(min_array_size)
|
2020-06-16 16:35:57 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-07-20 12:16:20 +02:00
|
|
|
MFParamsBuilder(const class MultiFunction &fn, int64_t min_array_size);
|
2020-06-16 16:35:57 +02:00
|
|
|
|
2020-07-23 17:57:11 +02:00
|
|
|
template<typename T> void add_readonly_single_input(const T *value, StringRef expected_name = "")
|
2020-06-16 16:35:57 +02:00
|
|
|
{
|
2021-03-21 19:31:24 +01:00
|
|
|
this->add_readonly_single_input(resources_.construct<GVArrayForSingleValueRef>(
|
|
|
|
__func__, CPPType::get<T>(), min_array_size_, value),
|
|
|
|
expected_name);
|
|
|
|
}
|
|
|
|
void add_readonly_single_input(const GSpan span, StringRef expected_name = "")
|
|
|
|
{
|
|
|
|
this->add_readonly_single_input(resources_.construct<GVArrayForGSpan>(__func__, span),
|
2020-07-23 17:57:11 +02:00
|
|
|
expected_name);
|
2020-06-16 16:35:57 +02:00
|
|
|
}
|
2021-03-21 19:31:24 +01:00
|
|
|
void add_readonly_single_input(const GVArray &ref, StringRef expected_name = "")
|
2020-06-16 16:35:57 +02:00
|
|
|
{
|
2020-07-23 17:57:11 +02:00
|
|
|
this->assert_current_param_type(MFParamType::ForSingleInput(ref.type()), expected_name);
|
2020-07-03 14:20:42 +02:00
|
|
|
BLI_assert(ref.size() >= min_array_size_);
|
2021-03-21 19:31:24 +01:00
|
|
|
virtual_arrays_.append(&ref);
|
2020-06-16 16:35:57 +02:00
|
|
|
}
|
|
|
|
|
2021-03-21 19:31:24 +01:00
|
|
|
void add_readonly_vector_input(const GVectorArray &vector_array, StringRef expected_name = "")
|
|
|
|
{
|
|
|
|
this->add_readonly_vector_input(
|
|
|
|
resources_.construct<GVVectorArrayForGVectorArray>(__func__, vector_array), expected_name);
|
|
|
|
}
|
|
|
|
void add_readonly_vector_input(const GVVectorArray &ref, StringRef expected_name = "")
|
2020-06-16 16:35:57 +02:00
|
|
|
{
|
2020-07-23 17:57:11 +02:00
|
|
|
this->assert_current_param_type(MFParamType::ForVectorInput(ref.type()), expected_name);
|
2020-07-03 14:20:42 +02:00
|
|
|
BLI_assert(ref.size() >= min_array_size_);
|
2021-03-21 19:31:24 +01:00
|
|
|
virtual_vector_arrays_.append(&ref);
|
2020-06-16 16:35:57 +02:00
|
|
|
}
|
|
|
|
|
2020-07-23 17:57:11 +02:00
|
|
|
template<typename T> void add_uninitialized_single_output(T *value, StringRef expected_name = "")
|
2020-07-21 17:20:05 +02:00
|
|
|
{
|
2020-07-23 17:57:11 +02:00
|
|
|
this->add_uninitialized_single_output(GMutableSpan(CPPType::get<T>(), value, 1),
|
|
|
|
expected_name);
|
2020-07-21 17:20:05 +02:00
|
|
|
}
|
2020-07-23 17:57:11 +02:00
|
|
|
void add_uninitialized_single_output(GMutableSpan ref, StringRef expected_name = "")
|
2020-06-16 16:35:57 +02:00
|
|
|
{
|
2020-07-23 17:57:11 +02:00
|
|
|
this->assert_current_param_type(MFParamType::ForSingleOutput(ref.type()), expected_name);
|
2020-07-03 14:20:42 +02:00
|
|
|
BLI_assert(ref.size() >= min_array_size_);
|
|
|
|
mutable_spans_.append(ref);
|
2020-06-16 16:35:57 +02:00
|
|
|
}
|
|
|
|
|
2020-07-23 17:57:11 +02:00
|
|
|
void add_vector_output(GVectorArray &vector_array, StringRef expected_name = "")
|
2020-06-16 16:35:57 +02:00
|
|
|
{
|
2020-07-23 17:57:11 +02:00
|
|
|
this->assert_current_param_type(MFParamType::ForVectorOutput(vector_array.type()),
|
|
|
|
expected_name);
|
2020-07-03 14:20:42 +02:00
|
|
|
BLI_assert(vector_array.size() >= min_array_size_);
|
|
|
|
vector_arrays_.append(&vector_array);
|
2020-06-16 16:35:57 +02:00
|
|
|
}
|
|
|
|
|
2020-07-23 17:57:11 +02:00
|
|
|
void add_single_mutable(GMutableSpan ref, StringRef expected_name = "")
|
2020-06-16 16:35:57 +02:00
|
|
|
{
|
2020-07-23 17:57:11 +02:00
|
|
|
this->assert_current_param_type(MFParamType::ForMutableSingle(ref.type()), expected_name);
|
2020-07-03 14:20:42 +02:00
|
|
|
BLI_assert(ref.size() >= min_array_size_);
|
|
|
|
mutable_spans_.append(ref);
|
2020-06-16 16:35:57 +02:00
|
|
|
}
|
|
|
|
|
2020-07-23 17:57:11 +02:00
|
|
|
void add_vector_mutable(GVectorArray &vector_array, StringRef expected_name = "")
|
2020-06-16 16:35:57 +02:00
|
|
|
{
|
2020-07-23 17:57:11 +02:00
|
|
|
this->assert_current_param_type(MFParamType::ForMutableVector(vector_array.type()),
|
|
|
|
expected_name);
|
2020-07-03 14:20:42 +02:00
|
|
|
BLI_assert(vector_array.size() >= min_array_size_);
|
|
|
|
vector_arrays_.append(&vector_array);
|
2020-06-16 16:35:57 +02:00
|
|
|
}
|
|
|
|
|
2020-07-20 12:16:20 +02:00
|
|
|
GMutableSpan computed_array(int param_index)
|
2020-06-16 16:35:57 +02:00
|
|
|
{
|
2020-07-03 14:20:42 +02:00
|
|
|
BLI_assert(ELEM(signature_->param_types[param_index].category(),
|
2020-06-16 16:35:57 +02:00
|
|
|
MFParamType::SingleOutput,
|
|
|
|
MFParamType::SingleMutable));
|
2020-07-20 12:16:20 +02:00
|
|
|
int data_index = signature_->data_index(param_index);
|
2020-07-03 14:20:42 +02:00
|
|
|
return mutable_spans_[data_index];
|
2020-06-16 16:35:57 +02:00
|
|
|
}
|
|
|
|
|
2020-07-20 12:16:20 +02:00
|
|
|
GVectorArray &computed_vector_array(int param_index)
|
2020-06-16 16:35:57 +02:00
|
|
|
{
|
2020-07-03 14:20:42 +02:00
|
|
|
BLI_assert(ELEM(signature_->param_types[param_index].category(),
|
2020-06-16 16:35:57 +02:00
|
|
|
MFParamType::VectorOutput,
|
|
|
|
MFParamType::VectorMutable));
|
2020-07-20 12:16:20 +02:00
|
|
|
int data_index = signature_->data_index(param_index);
|
2020-07-03 14:20:42 +02:00
|
|
|
return *vector_arrays_[data_index];
|
2020-06-16 16:35:57 +02:00
|
|
|
}
|
|
|
|
|
2021-03-21 19:31:24 +01:00
|
|
|
ResourceCollector &resources()
|
|
|
|
{
|
|
|
|
return resources_;
|
|
|
|
}
|
|
|
|
|
2020-06-16 16:35:57 +02:00
|
|
|
private:
|
2020-07-23 17:57:11 +02:00
|
|
|
void assert_current_param_type(MFParamType param_type, StringRef expected_name = "")
|
2020-06-16 16:35:57 +02:00
|
|
|
{
|
2020-07-23 17:57:11 +02:00
|
|
|
UNUSED_VARS_NDEBUG(param_type, expected_name);
|
2020-06-16 16:35:57 +02:00
|
|
|
#ifdef DEBUG
|
2020-07-20 12:16:20 +02:00
|
|
|
int param_index = this->current_param_index();
|
2020-07-23 17:57:11 +02:00
|
|
|
|
|
|
|
if (expected_name != "") {
|
|
|
|
StringRef actual_name = signature_->param_names[param_index];
|
|
|
|
BLI_assert(actual_name == expected_name);
|
|
|
|
}
|
|
|
|
|
2020-07-03 14:20:42 +02:00
|
|
|
MFParamType expected_type = signature_->param_types[param_index];
|
2020-06-16 16:35:57 +02:00
|
|
|
BLI_assert(expected_type == param_type);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-07-20 12:16:20 +02:00
|
|
|
int current_param_index() const
|
2020-06-16 16:35:57 +02:00
|
|
|
{
|
2021-03-21 19:31:24 +01:00
|
|
|
return virtual_arrays_.size() + mutable_spans_.size() + virtual_vector_arrays_.size() +
|
2020-07-03 14:20:42 +02:00
|
|
|
vector_arrays_.size();
|
2020-06-16 16:35:57 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class MFParams {
|
|
|
|
private:
|
2020-07-03 14:20:42 +02:00
|
|
|
MFParamsBuilder *builder_;
|
2020-06-16 16:35:57 +02:00
|
|
|
|
|
|
|
public:
|
2020-07-03 14:20:42 +02:00
|
|
|
MFParams(MFParamsBuilder &builder) : builder_(&builder)
|
2020-06-16 16:35:57 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-03-21 19:31:24 +01:00
|
|
|
template<typename T> const VArray<T> &readonly_single_input(int param_index, StringRef name = "")
|
2020-06-16 16:35:57 +02:00
|
|
|
{
|
2021-03-21 19:31:24 +01:00
|
|
|
const GVArray &array = this->readonly_single_input(param_index, name);
|
|
|
|
return builder_->resources_.construct<VArrayForGVArray<T>>(__func__, array);
|
2020-06-16 16:35:57 +02:00
|
|
|
}
|
2021-03-21 19:31:24 +01:00
|
|
|
const GVArray &readonly_single_input(int param_index, StringRef name = "")
|
2020-06-16 16:35:57 +02:00
|
|
|
{
|
|
|
|
this->assert_correct_param(param_index, name, MFParamType::SingleInput);
|
2020-07-20 12:16:20 +02:00
|
|
|
int data_index = builder_->signature_->data_index(param_index);
|
2021-03-21 19:31:24 +01:00
|
|
|
return *builder_->virtual_arrays_[data_index];
|
2020-06-16 16:35:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2020-07-20 12:16:20 +02:00
|
|
|
MutableSpan<T> uninitialized_single_output(int param_index, StringRef name = "")
|
2020-06-16 16:35:57 +02:00
|
|
|
{
|
|
|
|
return this->uninitialized_single_output(param_index, name).typed<T>();
|
|
|
|
}
|
2020-07-20 12:16:20 +02:00
|
|
|
GMutableSpan uninitialized_single_output(int param_index, StringRef name = "")
|
2020-06-16 16:35:57 +02:00
|
|
|
{
|
|
|
|
this->assert_correct_param(param_index, name, MFParamType::SingleOutput);
|
2020-07-20 12:16:20 +02:00
|
|
|
int data_index = builder_->signature_->data_index(param_index);
|
2020-07-03 14:20:42 +02:00
|
|
|
return builder_->mutable_spans_[data_index];
|
2020-06-16 16:35:57 +02:00
|
|
|
}
|
|
|
|
|
2021-03-21 19:31:24 +01:00
|
|
|
template<typename T>
|
|
|
|
const VVectorArray<T> &readonly_vector_input(int param_index, StringRef name = "")
|
2020-06-16 16:35:57 +02:00
|
|
|
{
|
2021-03-21 19:31:24 +01:00
|
|
|
const GVVectorArray &vector_array = this->readonly_vector_input(param_index, name);
|
|
|
|
return builder_->resources_.construct<VVectorArrayForGVVectorArray<T>>(__func__, vector_array);
|
2020-06-16 16:35:57 +02:00
|
|
|
}
|
2021-03-21 19:31:24 +01:00
|
|
|
const GVVectorArray &readonly_vector_input(int param_index, StringRef name = "")
|
2020-06-16 16:35:57 +02:00
|
|
|
{
|
|
|
|
this->assert_correct_param(param_index, name, MFParamType::VectorInput);
|
2020-07-20 12:16:20 +02:00
|
|
|
int data_index = builder_->signature_->data_index(param_index);
|
2021-03-21 19:31:24 +01:00
|
|
|
return *builder_->virtual_vector_arrays_[data_index];
|
2020-06-16 16:35:57 +02:00
|
|
|
}
|
|
|
|
|
2021-03-21 19:31:24 +01:00
|
|
|
template<typename T>
|
|
|
|
GVectorArray_TypedMutableRef<T> vector_output(int param_index, StringRef name = "")
|
2020-06-16 16:35:57 +02:00
|
|
|
{
|
2021-03-21 19:31:24 +01:00
|
|
|
return {this->vector_output(param_index, name)};
|
2020-06-16 16:35:57 +02:00
|
|
|
}
|
2020-07-20 12:16:20 +02:00
|
|
|
GVectorArray &vector_output(int param_index, StringRef name = "")
|
2020-06-16 16:35:57 +02:00
|
|
|
{
|
|
|
|
this->assert_correct_param(param_index, name, MFParamType::VectorOutput);
|
2020-07-20 12:16:20 +02:00
|
|
|
int data_index = builder_->signature_->data_index(param_index);
|
2020-07-03 14:20:42 +02:00
|
|
|
return *builder_->vector_arrays_[data_index];
|
2020-06-16 16:35:57 +02:00
|
|
|
}
|
|
|
|
|
2020-07-20 12:16:20 +02:00
|
|
|
template<typename T> MutableSpan<T> single_mutable(int param_index, StringRef name = "")
|
2020-06-16 16:35:57 +02:00
|
|
|
{
|
|
|
|
return this->single_mutable(param_index, name).typed<T>();
|
|
|
|
}
|
2020-07-20 12:16:20 +02:00
|
|
|
GMutableSpan single_mutable(int param_index, StringRef name = "")
|
2020-06-16 16:35:57 +02:00
|
|
|
{
|
|
|
|
this->assert_correct_param(param_index, name, MFParamType::SingleMutable);
|
2020-07-20 12:16:20 +02:00
|
|
|
int data_index = builder_->signature_->data_index(param_index);
|
2020-07-03 14:20:42 +02:00
|
|
|
return builder_->mutable_spans_[data_index];
|
2020-06-16 16:35:57 +02:00
|
|
|
}
|
|
|
|
|
2021-03-21 19:31:24 +01:00
|
|
|
template<typename T>
|
|
|
|
GVectorArray_TypedMutableRef<T> vector_mutable(int param_index, StringRef name = "")
|
2020-06-22 15:48:08 +02:00
|
|
|
{
|
2021-03-21 19:31:24 +01:00
|
|
|
return {this->vector_mutable(param_index, name)};
|
2020-06-22 15:48:08 +02:00
|
|
|
}
|
2020-07-20 12:16:20 +02:00
|
|
|
GVectorArray &vector_mutable(int param_index, StringRef name = "")
|
2020-06-16 16:35:57 +02:00
|
|
|
{
|
|
|
|
this->assert_correct_param(param_index, name, MFParamType::VectorMutable);
|
2020-07-20 12:16:20 +02:00
|
|
|
int data_index = builder_->signature_->data_index(param_index);
|
2020-07-03 14:20:42 +02:00
|
|
|
return *builder_->vector_arrays_[data_index];
|
2020-06-16 16:35:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-07-20 12:16:20 +02:00
|
|
|
void assert_correct_param(int param_index, StringRef name, MFParamType param_type)
|
2020-06-16 16:35:57 +02:00
|
|
|
{
|
|
|
|
UNUSED_VARS_NDEBUG(param_index, name, param_type);
|
|
|
|
#ifdef DEBUG
|
2020-07-03 14:20:42 +02:00
|
|
|
BLI_assert(builder_->signature_->param_types[param_index] == param_type);
|
2020-06-16 16:35:57 +02:00
|
|
|
if (name.size() > 0) {
|
2020-07-03 14:20:42 +02:00
|
|
|
BLI_assert(builder_->signature_->param_names[param_index] == name);
|
2020-06-16 16:35:57 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-07-20 12:16:20 +02:00
|
|
|
void assert_correct_param(int param_index, StringRef name, MFParamType::Category category)
|
2020-06-16 16:35:57 +02:00
|
|
|
{
|
|
|
|
UNUSED_VARS_NDEBUG(param_index, name, category);
|
|
|
|
#ifdef DEBUG
|
2020-07-03 14:20:42 +02:00
|
|
|
BLI_assert(builder_->signature_->param_types[param_index].category() == category);
|
2020-06-16 16:35:57 +02:00
|
|
|
if (name.size() > 0) {
|
2020-07-03 14:20:42 +02:00
|
|
|
BLI_assert(builder_->signature_->param_names[param_index] == name);
|
2020-06-16 16:35:57 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-07-03 14:25:20 +02:00
|
|
|
} // namespace blender::fn
|