Skip to content

Commit

Permalink
Add pybind11_include_file config file directive
Browse files Browse the repository at this point in the history
  • Loading branch information
kliegeois committed Oct 25, 2023
1 parent f1f30b3 commit b02d93a
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 6 deletions.
7 changes: 7 additions & 0 deletions documentation/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,10 @@ Config file directives:
.. code-block:: bash
+smart_holder example::class
* ``pybind11_include_file``: use to specify which header file of pybind11 should be included. The header pybind11/pybind11.h is used by default.

.. code-block:: bash
+pybind11_include_file pybind11/smart_holder.h
7 changes: 6 additions & 1 deletion source/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ void Config::read(string const &file_name)

string const _smart_holder_{"smart_holder"};

string const _pybind11_include_file_{"pybind11_include_file"};

string const _default_static_pointer_return_value_policy_{"default_static_pointer_return_value_policy"};
string const _default_static_lvalue_reference_return_value_policy_{"default_static_lvalue_reference_return_value_policy"};
string const _default_static_rvalue_reference_return_value_policy_{"default_static_rvalue_reference_return_value_policy"};
Expand Down Expand Up @@ -219,12 +221,15 @@ void Config::read(string const &file_name)
else if( token == _custom_shared_ ) holder_type_ = name_without_spaces;

else if( token == _smart_holder_ ) {
include_file_ = "pybind11/smart_holder.h";
if(bind) {
smart_held_classes.push_back(name_without_spaces);
}
}

else if( token == _pybind11_include_file_ ) {
pybind11_include_file_ = name_without_spaces;
}

else if( token == _default_static_pointer_return_value_policy_ ) default_static_pointer_return_value_policy_ = name_without_spaces;
else if( token == _default_static_lvalue_reference_return_value_policy_ ) default_static_lvalue_reference_return_value_policy_ = name_without_spaces;
else if( token == _default_static_rvalue_reference_return_value_policy_ ) default_static_rvalue_reference_return_value_policy_ = name_without_spaces;
Expand Down
4 changes: 2 additions & 2 deletions source/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class Config
string default_member_rvalue_reference_return_value_policy_ = "pybind11::return_value_policy::automatic";
string default_call_guard_ = "";
string holder_type_ = "std::shared_ptr";
string include_file_ = "pybind11/pybind11.h";
string pybind11_include_file_ = "pybind11/pybind11.h";
string prefix_for_static_member_functions_ = "";

std::vector<string> enums_to_bind, enums_to_skip;
Expand Down Expand Up @@ -91,7 +91,7 @@ class Config
string const &prefix_for_static_member_functions() { return prefix_for_static_member_functions_; }

string const &holder_type() const { return holder_type_; }
string const &include_file() const { return include_file_; }
string const &pybind11_include_file() const { return pybind11_include_file_; }

string prefix;

Expand Down
4 changes: 2 additions & 2 deletions source/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ void Context::generate(Config const &config)
string shared_declare = "PYBIND11_DECLARE_HOLDER_TYPE(T, "+holder_type+"<T>)";
string shared_make_opaque = "PYBIND11_MAKE_OPAQUE("+holder_type+"<void>)";

string const pybind11_include = "#include <" + Config::get().include_file() + ">";
string const pybind11_include = "#include <" + Config::get().pybind11_include_file() + ">";
code = generate_include_directives(includes) + fmt::format(module_header, pybind11_include, config.includes_code(), shared_declare, shared_make_opaque) + prefix_code + "void " + function_name + module_function_suffix + "\n{\n" + code + "}\n";

if( O_single_file ) root_module_file_handle << "// File: " << file_name << '\n' << code << "\n\n";
Expand All @@ -463,7 +463,7 @@ void Context::generate(Config const &config)
binding_function_calls += "\t" + f + "(M);\n";
}

string const pybind11_include = "#include <" + Config::get().include_file() + ">";
string const pybind11_include = "#include <" + Config::get().pybind11_include_file() + ">";

std::stringstream s;
s << fmt::format(main_module_header, pybind11_include, binding_function_decls, config.root_module, namespace_pairs, binding_function_calls);
Expand Down
3 changes: 2 additions & 1 deletion test/T61.smart_holder.config
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
+custom_shared my_shared_ptr
+smart_holder A
+smart_holder A
+pybind11_include_file pybind11/smart_holder.h

0 comments on commit b02d93a

Please sign in to comment.