Skip to content

Commit

Permalink
fix attribut binding
Browse files Browse the repository at this point in the history
  • Loading branch information
francoisbonneau committed Feb 27, 2024
1 parent 44269d9 commit f3ba712
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 68 deletions.
77 changes: 41 additions & 36 deletions bindings/python/src/basic/attribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,37 +24,40 @@
#include "../common.h"

#include <geode/basic/attribute.h>

#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" )
Expand All @@ -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
71 changes: 39 additions & 32 deletions bindings/python/src/basic/attribute_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,35 +25,38 @@

#include <geode/basic/attribute_manager.h>
#include <typeinfo>

#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(
Expand All @@ -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

0 comments on commit f3ba712

Please sign in to comment.