| 
									
										
										
										
											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. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifndef __FN_MULTI_FUNCTION_SIGNATURE_HH__
 | 
					
						
							|  |  |  | #define __FN_MULTI_FUNCTION_SIGNATURE_HH__
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** \file
 | 
					
						
							|  |  |  |  * \ingroup fn | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * The signature of a multi-function contains the functions name and expected parameters. New | 
					
						
							|  |  |  |  * signatures should be build using the MFSignatureBuilder class. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "FN_multi_function_param_type.hh"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "BLI_vector.hh"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-03 14:25:20 +02:00
										 |  |  | namespace blender::fn { | 
					
						
							| 
									
										
										
										
											2020-06-16 16:35:57 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | struct MFSignature { | 
					
						
							|  |  |  |   std::string function_name; | 
					
						
							| 
									
										
										
										
											2020-06-30 18:01:14 +02:00
										 |  |  |   /* Use RawAllocator so that a MultiFunction can have static storage duration. */ | 
					
						
							|  |  |  |   Vector<std::string, 4, RawAllocator> param_names; | 
					
						
							|  |  |  |   Vector<MFParamType, 4, RawAllocator> param_types; | 
					
						
							|  |  |  |   Vector<uint, 4, RawAllocator> param_data_indices; | 
					
						
							| 
									
										
										
										
											2020-06-16 16:35:57 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   uint data_index(uint param_index) const | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     return param_data_indices[param_index]; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class MFSignatureBuilder { | 
					
						
							|  |  |  |  private: | 
					
						
							| 
									
										
										
										
											2020-07-03 14:20:42 +02:00
										 |  |  |   MFSignature &data_; | 
					
						
							|  |  |  |   uint span_count_ = 0; | 
					
						
							|  |  |  |   uint virtual_span_count_ = 0; | 
					
						
							|  |  |  |   uint virtual_array_span_count_ = 0; | 
					
						
							|  |  |  |   uint vector_array_count_ = 0; | 
					
						
							| 
									
										
										
										
											2020-06-16 16:35:57 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |  public: | 
					
						
							| 
									
										
										
										
											2020-07-03 14:20:42 +02:00
										 |  |  |   MFSignatureBuilder(MFSignature &data) : data_(data) | 
					
						
							| 
									
										
										
										
											2020-06-16 16:35:57 +02:00
										 |  |  |   { | 
					
						
							|  |  |  |     BLI_assert(data.param_names.is_empty()); | 
					
						
							|  |  |  |     BLI_assert(data.param_types.is_empty()); | 
					
						
							|  |  |  |     BLI_assert(data.param_data_indices.is_empty()); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* Input Param Types */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   template<typename T> void single_input(StringRef name) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     this->single_input(name, CPPType::get<T>()); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   void single_input(StringRef name, const CPPType &type) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     this->input(name, MFDataType::ForSingle(type)); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   template<typename T> void vector_input(StringRef name) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     this->vector_input(name, CPPType::get<T>()); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   void vector_input(StringRef name, const CPPType &base_type) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     this->input(name, MFDataType::ForVector(base_type)); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   void input(StringRef name, MFDataType data_type) | 
					
						
							|  |  |  |   { | 
					
						
							| 
									
										
										
										
											2020-07-03 14:20:42 +02:00
										 |  |  |     data_.param_names.append(name); | 
					
						
							|  |  |  |     data_.param_types.append(MFParamType(MFParamType::Input, data_type)); | 
					
						
							| 
									
										
										
										
											2020-06-16 16:35:57 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     switch (data_type.category()) { | 
					
						
							|  |  |  |       case MFDataType::Single: | 
					
						
							| 
									
										
										
										
											2020-07-03 14:20:42 +02:00
										 |  |  |         data_.param_data_indices.append(virtual_span_count_++); | 
					
						
							| 
									
										
										
										
											2020-06-16 16:35:57 +02:00
										 |  |  |         break; | 
					
						
							|  |  |  |       case MFDataType::Vector: | 
					
						
							| 
									
										
										
										
											2020-07-03 14:20:42 +02:00
										 |  |  |         data_.param_data_indices.append(virtual_array_span_count_++); | 
					
						
							| 
									
										
										
										
											2020-06-16 16:35:57 +02:00
										 |  |  |         break; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* Output Param Types */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   template<typename T> void single_output(StringRef name) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     this->single_output(name, CPPType::get<T>()); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   void single_output(StringRef name, const CPPType &type) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     this->output(name, MFDataType::ForSingle(type)); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   template<typename T> void vector_output(StringRef name) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     this->vector_output(name, CPPType::get<T>()); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   void vector_output(StringRef name, const CPPType &base_type) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     this->output(name, MFDataType::ForVector(base_type)); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   void output(StringRef name, MFDataType data_type) | 
					
						
							|  |  |  |   { | 
					
						
							| 
									
										
										
										
											2020-07-03 14:20:42 +02:00
										 |  |  |     data_.param_names.append(name); | 
					
						
							|  |  |  |     data_.param_types.append(MFParamType(MFParamType::Output, data_type)); | 
					
						
							| 
									
										
										
										
											2020-06-16 16:35:57 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     switch (data_type.category()) { | 
					
						
							|  |  |  |       case MFDataType::Single: | 
					
						
							| 
									
										
										
										
											2020-07-03 14:20:42 +02:00
										 |  |  |         data_.param_data_indices.append(span_count_++); | 
					
						
							| 
									
										
										
										
											2020-06-16 16:35:57 +02:00
										 |  |  |         break; | 
					
						
							|  |  |  |       case MFDataType::Vector: | 
					
						
							| 
									
										
										
										
											2020-07-03 14:20:42 +02:00
										 |  |  |         data_.param_data_indices.append(vector_array_count_++); | 
					
						
							| 
									
										
										
										
											2020-06-16 16:35:57 +02:00
										 |  |  |         break; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* Mutable Param Types */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   template<typename T> void single_mutable(StringRef name) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     this->single_mutable(name, CPPType::get<T>()); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   void single_mutable(StringRef name, const CPPType &type) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     this->mutable_(name, MFDataType::ForSingle(type)); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2020-06-22 15:48:08 +02:00
										 |  |  |   template<typename T> void vector_mutable(StringRef name) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     this->vector_mutable(name, CPPType::get<T>()); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2020-06-16 16:35:57 +02:00
										 |  |  |   void vector_mutable(StringRef name, const CPPType &base_type) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     this->mutable_(name, MFDataType::ForVector(base_type)); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   void mutable_(StringRef name, MFDataType data_type) | 
					
						
							|  |  |  |   { | 
					
						
							| 
									
										
										
										
											2020-07-03 14:20:42 +02:00
										 |  |  |     data_.param_names.append(name); | 
					
						
							|  |  |  |     data_.param_types.append(MFParamType(MFParamType::Mutable, data_type)); | 
					
						
							| 
									
										
										
										
											2020-06-16 16:35:57 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     switch (data_type.category()) { | 
					
						
							|  |  |  |       case MFDataType::Single: | 
					
						
							| 
									
										
										
										
											2020-07-03 14:20:42 +02:00
										 |  |  |         data_.param_data_indices.append(span_count_++); | 
					
						
							| 
									
										
										
										
											2020-06-16 16:35:57 +02:00
										 |  |  |         break; | 
					
						
							|  |  |  |       case MFDataType::Vector: | 
					
						
							| 
									
										
										
										
											2020-07-03 14:20:42 +02:00
										 |  |  |         data_.param_data_indices.append(vector_array_count_++); | 
					
						
							| 
									
										
										
										
											2020-06-16 16:35:57 +02:00
										 |  |  |         break; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-03 14:25:20 +02:00
										 |  |  | }  // namespace blender::fn
 | 
					
						
							| 
									
										
										
										
											2020-06-16 16:35:57 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | #endif /* __FN_MULTI_FUNCTION_SIGNATURE_HH__ */
 |