Skip to content

Commit

Permalink
Add buffer_protocol support for class constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
kkrissian-gpfw committed Aug 24, 2022
1 parent 38a7cc4 commit d38ec90
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
11 changes: 9 additions & 2 deletions source/class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1180,10 +1180,17 @@ void ClassBinder::bind(Context &context)

string maybe_trampoline = callback_structure_constructible ? ", " + binding_qualified_name : "";

// Add buffer protocol
auto bp = Config::get().buffer_protocols;
bool has_buffer_protocol = std::find(bp.begin(), bp.end(), qualified_name_without_template) != bp.end();
std::string buffer_protocol_annotation = "";
if (has_buffer_protocol)
buffer_protocol_annotation = ", pybind11::buffer_protocol()";

if( named_class )
c += '\t' +
R"(pybind11::class_<{}{}{}{}> cl({}, "{}", "{}");)"_format(qualified_name, maybe_holder_type, maybe_trampoline, maybe_base_classes(context), module_variable_name, python_class_name(C),
generate_documentation_string_for_declaration(C)) +
R"(pybind11::class_<{}{}{}{}> cl({}, "{}", "{}" {});)"_format(qualified_name, maybe_holder_type, maybe_trampoline, maybe_base_classes(context), module_variable_name, python_class_name(C),
generate_documentation_string_for_declaration(C), buffer_protocol_annotation) +
'\n';
// c += "\tpybind11::handle cl_type = cl;\n\n";

Expand Down
7 changes: 7 additions & 0 deletions source/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ void Config::read(string const &file_name)
string const _include_for_class_{"include_for_class"};
string const _include_for_namespace_{"include_for_namespace"};

string const _buffer_protocol_{"buffer_protocol"};

string const _binder_{"binder"};
string const _add_on_binder_{"add_on_binder"};

Expand Down Expand Up @@ -144,6 +146,11 @@ void Config::read(string const &file_name)
throw std::runtime_error("include_for_namespace must be '+' configuration.");
}
}
else if( token == _buffer_protocol_ ) {
if(bind) {
buffer_protocols.push_back(name_without_spaces);
}
}
else if( token == _binder_ ) {

if( bind ) {
Expand Down
1 change: 1 addition & 0 deletions source/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class Config
string root_module;

std::vector<string> namespaces_to_bind, classes_to_bind, functions_to_bind, namespaces_to_skip, classes_to_skip, functions_to_skip, includes_to_add, includes_to_skip;
std::vector<string> buffer_protocols;

std::map<string, string> const &binders() const { return binders_; }
std::map<string, string> const &add_on_binders() const { return add_on_binders_; }
Expand Down

0 comments on commit d38ec90

Please sign in to comment.