-
Notifications
You must be signed in to change notification settings - Fork 70
add support for arbitrary nested namespaces #312
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mattangus
wants to merge
1
commit into
RosettaCommons:master
Choose a base branch
from
HumanisingAutonomy:nested_namespaces
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
|
|
||
| #ifndef _INCLUDED_T51_NESTED_NAMESPACES_ | ||
| #define _INCLUDED_T51_NESTED_NAMESPACES_ | ||
|
|
||
| namespace aaaa::bbbb::cccc { | ||
| void foo_aaaa_bbbb_cccc() {} | ||
| namespace dddd{ | ||
| void bar_aaaa_bbbb_cccc_dddd() {} | ||
| } | ||
| namespace eeee { | ||
| void baz_aaaa_bbbb_cccc_eeee() {} | ||
| } | ||
| } | ||
|
|
||
| #endif // _INCLUDED_T51_NESTED_NAMESPACES_ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| // File: T51_nested_namespace.cpp | ||
| #include <T51.nested_namespace.hpp> // aaaa::bbbb::cccc::foo_aaaa_bbbb_cccc | ||
|
|
||
| #include <functional> | ||
| #include <pybind11/pybind11.h> | ||
| #include <string> | ||
|
|
||
| #ifndef BINDER_PYBIND11_TYPE_CASTER | ||
| #define BINDER_PYBIND11_TYPE_CASTER | ||
| PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr<T>, false) | ||
| PYBIND11_DECLARE_HOLDER_TYPE(T, T*, false) | ||
| PYBIND11_MAKE_OPAQUE(std::shared_ptr<void>) | ||
| #endif | ||
|
|
||
| void bind_T51_nested_namespace(std::function< pybind11::module &(std::string const &namespace_) > &M) | ||
| { | ||
| // aaaa::bbbb::cccc::foo_aaaa_bbbb_cccc() file:T51.nested_namespace.hpp line:6 | ||
| M("aaaa::bbbb::cccc").def("foo_aaaa_bbbb_cccc", (void (*)()) &aaaa::bbbb::cccc::foo_aaaa_bbbb_cccc, "C++: aaaa::bbbb::cccc::foo_aaaa_bbbb_cccc() --> void"); | ||
|
|
||
| } | ||
|
|
||
|
|
||
| // File: T51_nested_namespace_1.cpp | ||
| #include <T51.nested_namespace.hpp> // aaaa::bbbb::cccc::dddd::bar_aaaa_bbbb_cccc_dddd | ||
|
|
||
| #include <functional> | ||
| #include <pybind11/pybind11.h> | ||
| #include <string> | ||
|
|
||
| #ifndef BINDER_PYBIND11_TYPE_CASTER | ||
| #define BINDER_PYBIND11_TYPE_CASTER | ||
| PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr<T>, false) | ||
| PYBIND11_DECLARE_HOLDER_TYPE(T, T*, false) | ||
| PYBIND11_MAKE_OPAQUE(std::shared_ptr<void>) | ||
| #endif | ||
|
|
||
| void bind_T51_nested_namespace_1(std::function< pybind11::module &(std::string const &namespace_) > &M) | ||
| { | ||
| // aaaa::bbbb::cccc::dddd::bar_aaaa_bbbb_cccc_dddd() file:T51.nested_namespace.hpp line:8 | ||
| M("aaaa::bbbb::cccc::dddd").def("bar_aaaa_bbbb_cccc_dddd", (void (*)()) &aaaa::bbbb::cccc::dddd::bar_aaaa_bbbb_cccc_dddd, "C++: aaaa::bbbb::cccc::dddd::bar_aaaa_bbbb_cccc_dddd() --> void"); | ||
|
|
||
| } | ||
|
|
||
|
|
||
| // File: T51_nested_namespace_2.cpp | ||
| #include <T51.nested_namespace.hpp> // aaaa::bbbb::cccc::eeee::baz_aaaa_bbbb_cccc_eeee | ||
|
|
||
| #include <functional> | ||
| #include <pybind11/pybind11.h> | ||
| #include <string> | ||
|
|
||
| #ifndef BINDER_PYBIND11_TYPE_CASTER | ||
| #define BINDER_PYBIND11_TYPE_CASTER | ||
| PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr<T>, false) | ||
| PYBIND11_DECLARE_HOLDER_TYPE(T, T*, false) | ||
| PYBIND11_MAKE_OPAQUE(std::shared_ptr<void>) | ||
| #endif | ||
|
|
||
| void bind_T51_nested_namespace_2(std::function< pybind11::module &(std::string const &namespace_) > &M) | ||
| { | ||
| // aaaa::bbbb::cccc::eeee::baz_aaaa_bbbb_cccc_eeee() file:T51.nested_namespace.hpp line:11 | ||
| M("aaaa::bbbb::cccc::eeee").def("baz_aaaa_bbbb_cccc_eeee", (void (*)()) &aaaa::bbbb::cccc::eeee::baz_aaaa_bbbb_cccc_eeee, "C++: aaaa::bbbb::cccc::eeee::baz_aaaa_bbbb_cccc_eeee() --> void"); | ||
|
|
||
| } | ||
|
|
||
|
|
||
| #include <map> | ||
| #include <algorithm> | ||
| #include <functional> | ||
| #include <memory> | ||
| #include <stdexcept> | ||
| #include <string> | ||
| #include <iostream> | ||
|
|
||
| #include <pybind11/pybind11.h> | ||
|
|
||
| using ModuleGetter = std::function< pybind11::module & (std::string const &) >; | ||
|
|
||
| void bind_T51_nested_namespace(std::function< pybind11::module &(std::string const &namespace_) > &M); | ||
| void bind_T51_nested_namespace_1(std::function< pybind11::module &(std::string const &namespace_) > &M); | ||
| void bind_T51_nested_namespace_2(std::function< pybind11::module &(std::string const &namespace_) > &M); | ||
|
|
||
|
|
||
| static std::vector<std::string> const reserved_python_words{ | ||
| "nonlocal", | ||
| "global", | ||
| }; | ||
| std::string mangle_namespace_name(std::string const &ns) { | ||
| if( std::find(reserved_python_words.begin(), reserved_python_words.end(), ns) == reserved_python_words.end() ) return ns; | ||
| return ns + '_'; | ||
| } | ||
|
|
||
| void makeSubmodules(const std::vector<std::string> &moduleNames, std::map <std::string, pybind11::module> &modules) { | ||
| std::string prevNamespace = ""; | ||
| std::string curNamespace = ""; | ||
| for (const auto &curMod : moduleNames) { | ||
| std::cout << "making: " << curMod << std::endl; | ||
| if (curNamespace.size() > 0) | ||
| curNamespace += "::"; // don't add :: to the start | ||
| const auto mangledMod = mangle_namespace_name(curMod); | ||
| std::cout << "mangled: " << mangledMod << std::endl; | ||
| curNamespace += mangledMod; | ||
| if (modules.count(curNamespace) == 0) { | ||
| std::cout << "defining namespace " << curNamespace << " in " << prevNamespace << std::endl; | ||
| modules[curNamespace] = modules[prevNamespace].def_submodule(mangledMod.c_str(), ("Bindings for " + curNamespace + " namespace").c_str()); | ||
| } | ||
| prevNamespace = curNamespace; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| PYBIND11_MODULE(T51_nested_namespace, root_module) { | ||
| root_module.doc() = "T51_nested_namespace module"; | ||
|
|
||
| std::map <std::string, pybind11::module> modules; | ||
| ModuleGetter M = [&](std::string const &namespace_) -> pybind11::module & { | ||
| auto it = modules.find(mangle_namespace_name(namespace_)); | ||
| if( it == modules.end() ) throw std::runtime_error("Attempt to access pybind11::module for namespace " + namespace_ + " before it was created!!!"); | ||
| return it->second; | ||
| }; | ||
|
|
||
| modules[""] = root_module; | ||
|
|
||
| std::vector< std::vector<std::string> > sub_modules { | ||
| {"aaaa"}, | ||
| {"aaaa", "bbbb"}, | ||
| {"aaaa", "bbbb", "cccc"}, | ||
| {"aaaa", "bbbb", "cccc", "dddd"}, | ||
| {"aaaa", "bbbb", "cccc", "eeee"}, | ||
| }; | ||
| for(auto &p : sub_modules ) { | ||
| makeSubmodules(p, modules); | ||
| } | ||
|
|
||
| //pybind11::class_<std::shared_ptr<void>>(M(""), "_encapsulated_data_"); | ||
|
|
||
| bind_T51_nested_namespace(M); | ||
| bind_T51_nested_namespace_1(M); | ||
| bind_T51_nested_namespace_2(M); | ||
|
|
||
| } | ||
|
|
||
| // Source list file: /home/matt/workspace/binder/build/test//T51_nested_namespace.sources | ||
| // T51_nested_namespace.cpp | ||
| // T51_nested_namespace.cpp | ||
| // T51_nested_namespace_1.cpp | ||
| // T51_nested_namespace_2.cpp | ||
|
|
||
| // Modules list file: /home/matt/workspace/binder/build/test//T51_nested_namespace.modules | ||
| // |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please say a few words why do you want to rewrite this function? Thanks,