From f3ba712dbcb15201c2cefbe7f9610d2170732295 Mon Sep 17 00:00:00 2001 From: Francois Bonneau Date: Tue, 27 Feb 2024 20:09:40 +0100 Subject: [PATCH] fix attribut binding --- bindings/python/src/basic/attribute.cpp | 77 ++++++++++--------- .../python/src/basic/attribute_manager.cpp | 71 +++++++++-------- 2 files changed, 80 insertions(+), 68 deletions(-) diff --git a/bindings/python/src/basic/attribute.cpp b/bindings/python/src/basic/attribute.cpp index 59f5792a2..6bb372086 100644 --- a/bindings/python/src/basic/attribute.cpp +++ b/bindings/python/src/basic/attribute.cpp @@ -24,37 +24,40 @@ #include "../common.h" #include - -#define PYTHON_ATTRIBUTE_CLASS( type, name ) \ - const auto read##name = std::string{ "ReadOnlyAttribute" } + #name; \ - pybind11::class_< ReadOnlyAttribute< type >, AttributeBase, \ - std::shared_ptr< ReadOnlyAttribute< type > > >( \ - module, read##name.c_str() ) \ - .def( "value", &ReadOnlyAttribute< type >::value ); \ - const auto constant##name = std::string{ "ConstantAttribute" } + #name; \ - pybind11::class_< ConstantAttribute< type >, ReadOnlyAttribute< type >, \ - std::shared_ptr< ConstantAttribute< type > > >( \ - module, constant##name.c_str() ) \ - .def( "constant_value", \ - static_cast< const type& (ConstantAttribute< type >::*) () \ - const >( &ConstantAttribute< type >::value ) ) \ - .def( "set_value", &ConstantAttribute< type >::set_value ) \ - .def( "default_value", &ConstantAttribute< type >::default_value ); \ - const auto variable##name = std::string{ "VariableAttribute" } + #name; \ - pybind11::class_< VariableAttribute< type >, ReadOnlyAttribute< type >, \ - std::shared_ptr< VariableAttribute< type > > >( \ - module, variable##name.c_str() ) \ - .def( "set_value", &VariableAttribute< type >::set_value ) \ - .def( "default_value", &VariableAttribute< type >::default_value ); \ - const auto sparse##name = std::string{ "SparseAttribute" } + #name; \ - pybind11::class_< SparseAttribute< type >, ReadOnlyAttribute< type >, \ - std::shared_ptr< SparseAttribute< type > > >( \ - module, sparse##name.c_str() ) \ - .def( "set_value", &SparseAttribute< type >::set_value ) \ - .def( "default_value", &SparseAttribute< type >::default_value ) - namespace geode { + template < typename type > + void python_attribute_class( + pybind11::module& module, const std::string& typestr ) + { + const auto read_name = absl::StrCat( "ReadOnlyAttribute", typestr ); + pybind11::class_< ReadOnlyAttribute< type >, AttributeBase, + std::shared_ptr< ReadOnlyAttribute< type > > >( + module, read_name.c_str() ) + .def( "value", &ReadOnlyAttribute< type >::value ); + const auto constant_name = absl::StrCat( "ConstantAttribute", typestr ); + pybind11::class_< ConstantAttribute< type >, ReadOnlyAttribute< type >, + std::shared_ptr< ConstantAttribute< type > > >( + module, constant_name.c_str() ) + .def( "constant_value", + static_cast< const type& (ConstantAttribute< type >::*) () + const >( &ConstantAttribute< type >::value ) ) + .def( "set_value", &ConstantAttribute< type >::set_value ) + .def( "default_value", &ConstantAttribute< type >::default_value ); + const auto variable_name = absl::StrCat( "VariableAttribute", typestr ); + pybind11::class_< VariableAttribute< type >, ReadOnlyAttribute< type >, + std::shared_ptr< VariableAttribute< type > > >( + module, variable_name.c_str() ) + .def( "set_value", &VariableAttribute< type >::set_value ) + .def( "default_value", &VariableAttribute< type >::default_value ); + const auto sparse_name = absl::StrCat( "SparseAttribute", typestr ); + pybind11::class_< SparseAttribute< type >, ReadOnlyAttribute< type >, + std::shared_ptr< SparseAttribute< type > > >( + module, sparse_name.c_str() ) + .def( "set_value", &SparseAttribute< type >::set_value ) + .def( "default_value", &SparseAttribute< type >::default_value ); + } + void define_attributes( pybind11::module& module ) { pybind11::class_< AttributeProperties >( module, "AttributeProperties" ) @@ -69,12 +72,14 @@ namespace geode .def( "generic_value", &AttributeBase::generic_value ) .def( "properties", &AttributeBase::properties ) .def( "is_genericable", &AttributeBase::is_genericable ); - PYTHON_ATTRIBUTE_CLASS( bool, Bool ); - PYTHON_ATTRIBUTE_CLASS( int, Int ); - PYTHON_ATTRIBUTE_CLASS( unsigned int, UInt ); - PYTHON_ATTRIBUTE_CLASS( float, Float ); - PYTHON_ATTRIBUTE_CLASS( double, Double ); - PYTHON_ATTRIBUTE_CLASS( std::array< double, 2 >, ArrayDouble2 ); - PYTHON_ATTRIBUTE_CLASS( std::array< double, 3 >, ArrayDouble3 ); + python_attribute_class< bool >( module, "Bool" ); + python_attribute_class< int >( module, "Int" ); + python_attribute_class< unsigned int >( module, "UInt" ); + python_attribute_class< float >( module, "Float" ); + python_attribute_class< double >( module, "Double" ); + python_attribute_class< std::array< double, 2 > >( + module, "ArrayDouble2" ); + python_attribute_class< std::array< double, 3 > >( + module, "ArrayDouble3" ); } } // namespace geode diff --git a/bindings/python/src/basic/attribute_manager.cpp b/bindings/python/src/basic/attribute_manager.cpp index 6a1a34833..d3148f5e3 100644 --- a/bindings/python/src/basic/attribute_manager.cpp +++ b/bindings/python/src/basic/attribute_manager.cpp @@ -25,35 +25,38 @@ #include #include - -#define PYTHON_ATTRIBUTE_TYPE( type, suffix ) \ - const auto read##suffix = std::string{ "find_attribute_" } + #suffix; \ - manager.def( \ - read##suffix.c_str(), &AttributeManager::find_attribute< type > ); \ - const auto constant##suffix = \ - std::string{ "find_or_create_attribute_constant_" } + #suffix; \ - manager.def( constant##suffix.c_str(), \ - static_cast< std::shared_ptr< ConstantAttribute< type > > ( \ - AttributeManager::* )( absl::string_view, type ) >( \ - &AttributeManager::find_or_create_attribute< ConstantAttribute, \ - type > ) ); \ - const auto variable##suffix = \ - std::string{ "find_or_create_attribute_variable_" } + #suffix; \ - manager.def( variable##suffix.c_str(), \ - static_cast< std::shared_ptr< VariableAttribute< type > > ( \ - AttributeManager::* )( absl::string_view, type ) >( \ - &AttributeManager::find_or_create_attribute< VariableAttribute, \ - type > ) ); \ - const auto sparse##suffix = \ - std::string{ "find_or_create_attribute_sparse_" } + #suffix; \ - manager.def( sparse##suffix.c_str(), \ - static_cast< std::shared_ptr< SparseAttribute< type > > ( \ - AttributeManager::* )( absl::string_view, type ) >( \ - &AttributeManager::find_or_create_attribute< SparseAttribute, \ - type > ) ) - namespace geode { + template < typename type > + void python_attribute_class( pybind11::class_< AttributeManager >& manager, + const std::string& suffix ) + { + const auto read_suffix = absl::StrCat( "find_attribute_", suffix ); + manager.def( + read_suffix.c_str(), &AttributeManager::find_attribute< type > ); + const auto constant_suffix = + absl::StrCat( "find_or_create_attribute_constant_", suffix ); + manager.def( constant_suffix.c_str(), + static_cast< std::shared_ptr< ConstantAttribute< type > > ( + AttributeManager::* )( absl::string_view, type ) >( + &AttributeManager::find_or_create_attribute< ConstantAttribute, + type > ) ); + const auto variable_suffix = + absl::StrCat( "find_or_create_attribute_variable_", suffix ); + manager.def( variable_suffix.c_str(), + static_cast< std::shared_ptr< VariableAttribute< type > > ( + AttributeManager::* )( absl::string_view, type ) >( + &AttributeManager::find_or_create_attribute< VariableAttribute, + type > ) ); + const auto sparse_suffix = + absl::StrCat( "find_or_create_attribute_sparse_", suffix ); + manager.def( sparse_suffix.c_str(), + static_cast< std::shared_ptr< SparseAttribute< type > > ( + AttributeManager::* )( absl::string_view, type ) >( + &AttributeManager::find_or_create_attribute< SparseAttribute, + type > ) ); + } + void define_attribute_manager( pybind11::module& module ) { pybind11::class_< AttributeManager > manager( @@ -73,10 +76,14 @@ namespace geode .def( "set_attribute_properties", &AttributeManager::set_attribute_properties ) .def( "delete_elements", &AttributeManager::delete_elements ); - PYTHON_ATTRIBUTE_TYPE( bool, bool ); - PYTHON_ATTRIBUTE_TYPE( int, int ); - PYTHON_ATTRIBUTE_TYPE( unsigned int, uint ); - PYTHON_ATTRIBUTE_TYPE( float, float ); - PYTHON_ATTRIBUTE_TYPE( double, double ); + python_attribute_class< bool >( manager, "bool" ); + python_attribute_class< int >( manager, "int" ); + python_attribute_class< unsigned int >( manager, "uint" ); + python_attribute_class< float >( manager, "float" ); + python_attribute_class< double >( manager, "double" ); + python_attribute_class< std::array< double, 2 > >( + manager, "arraydouble2" ); + python_attribute_class< std::array< double, 3 > >( + manager, "arraydouble3" ); } } // namespace geode