Skip to content

Commit

Permalink
adding support for python_builtin config config option
Browse files Browse the repository at this point in the history
  • Loading branch information
zwimer authored Oct 5, 2022
1 parent db02c0c commit 7ac60d9
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
12 changes: 12 additions & 0 deletions documentation/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,18 @@ Config file directives:
* ``python_builtin``, specify if particular class/struct should be considered a python builtin and assume existing bindings for it already exist.
The purpose of this directive is to allow developer to allow developers to toggle if bindings for types like ``std::optional`` or ``pybind11::dict`` should be
generated, or if binder should assume such bindings already exist somewhere else. Alternatively, a developer could declare a type as not-builtin if they
would prefer to force binder to generate bindings for it. Note that ``-python_builtin ab`c` always overrides ``+python_builtin abc``.

.. code-block:: bash
-python_builtin std::less
+python_builtin std::vector
* ``function``, specify if particular function should be bound. This could be used for both template and normal function.

.. code-block:: bash
Expand Down
7 changes: 7 additions & 0 deletions source/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ void Config::read(string const &file_name)
string const _function_{"function"};
string const _class_{"class"};

string const _python_builtin_{"python_builtin"};

string const _include_{"include"};
string const _include_for_class_{"include_for_class"};
string const _include_for_namespace_{"include_for_namespace"};
Expand Down Expand Up @@ -116,6 +118,11 @@ void Config::read(string const &file_name)
if( bind ) classes_to_bind.push_back(name_without_spaces);
else classes_to_skip.push_back(name_without_spaces);
}
else if( token == _python_builtin_ ) {

if (bind) python_builtins.insert(name_without_spaces);
else not_python_builtins.insert(name_without_spaces);
}
else if( token == _function_ ) {

if( bind ) functions_to_bind.push_back(name_without_spaces);
Expand Down
3 changes: 3 additions & 0 deletions source/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#ifndef _INCLUDED_config_hpp_
#define _INCLUDED_config_hpp_

#include <set>
#include <map>
#include <string>
#include <vector>
Expand Down Expand Up @@ -68,6 +69,8 @@ class Config
std::map<string, string> const &binder_for_namespaces() const { return binder_for_namespaces_; }
std::map<string, string> const &add_on_binder_for_namespaces() const { return add_on_binder_for_namespaces_; }

std::set<string> python_builtins, not_python_builtins;

std::map<string, std::vector<string> > const &class_includes() const { return class_includes_; }
std::map<string, std::vector<string> > const &namespace_includes() const { return namespace_includes_; }

Expand Down
11 changes: 9 additions & 2 deletions source/type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -725,8 +725,15 @@ bool is_python_builtin(NamedDecl const *C)
"pybind11::array_t",
};

for( auto &k : known_builtin ) {
// if( begins_with(name, k) ) return true;
// Not builtin's
for ( const auto &k : Config::get().not_python_builtins ) {
if ( name == k ) return false;
}
// Builtins
for( const auto &k : known_builtin ) {
if( name == k ) return true;
}
for( const auto &k : Config::get().python_builtins ) {
if( name == k ) return true;
}

Expand Down

0 comments on commit 7ac60d9

Please sign in to comment.