|
| 1 | +// File: T51_nested_namespace.cpp |
| 2 | +#include <T51.nested_namespace.hpp> // aaaa::bbbb::cccc::foo_aaaa_bbbb_cccc |
| 3 | + |
| 4 | +#include <functional> |
| 5 | +#include <pybind11/pybind11.h> |
| 6 | +#include <string> |
| 7 | + |
| 8 | +#ifndef BINDER_PYBIND11_TYPE_CASTER |
| 9 | + #define BINDER_PYBIND11_TYPE_CASTER |
| 10 | + PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr<T>, false) |
| 11 | + PYBIND11_DECLARE_HOLDER_TYPE(T, T*, false) |
| 12 | + PYBIND11_MAKE_OPAQUE(std::shared_ptr<void>) |
| 13 | +#endif |
| 14 | + |
| 15 | +void bind_T51_nested_namespace(std::function< pybind11::module &(std::string const &namespace_) > &M) |
| 16 | +{ |
| 17 | + // aaaa::bbbb::cccc::foo_aaaa_bbbb_cccc() file:T51.nested_namespace.hpp line:6 |
| 18 | + 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"); |
| 19 | + |
| 20 | +} |
| 21 | + |
| 22 | + |
| 23 | +// File: T51_nested_namespace_1.cpp |
| 24 | +#include <T51.nested_namespace.hpp> // aaaa::bbbb::cccc::dddd::bar_aaaa_bbbb_cccc_dddd |
| 25 | + |
| 26 | +#include <functional> |
| 27 | +#include <pybind11/pybind11.h> |
| 28 | +#include <string> |
| 29 | + |
| 30 | +#ifndef BINDER_PYBIND11_TYPE_CASTER |
| 31 | + #define BINDER_PYBIND11_TYPE_CASTER |
| 32 | + PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr<T>, false) |
| 33 | + PYBIND11_DECLARE_HOLDER_TYPE(T, T*, false) |
| 34 | + PYBIND11_MAKE_OPAQUE(std::shared_ptr<void>) |
| 35 | +#endif |
| 36 | + |
| 37 | +void bind_T51_nested_namespace_1(std::function< pybind11::module &(std::string const &namespace_) > &M) |
| 38 | +{ |
| 39 | + // aaaa::bbbb::cccc::dddd::bar_aaaa_bbbb_cccc_dddd() file:T51.nested_namespace.hpp line:8 |
| 40 | + 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"); |
| 41 | + |
| 42 | +} |
| 43 | + |
| 44 | + |
| 45 | +// File: T51_nested_namespace_2.cpp |
| 46 | +#include <T51.nested_namespace.hpp> // aaaa::bbbb::cccc::eeee::baz_aaaa_bbbb_cccc_eeee |
| 47 | + |
| 48 | +#include <functional> |
| 49 | +#include <pybind11/pybind11.h> |
| 50 | +#include <string> |
| 51 | + |
| 52 | +#ifndef BINDER_PYBIND11_TYPE_CASTER |
| 53 | + #define BINDER_PYBIND11_TYPE_CASTER |
| 54 | + PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr<T>, false) |
| 55 | + PYBIND11_DECLARE_HOLDER_TYPE(T, T*, false) |
| 56 | + PYBIND11_MAKE_OPAQUE(std::shared_ptr<void>) |
| 57 | +#endif |
| 58 | + |
| 59 | +void bind_T51_nested_namespace_2(std::function< pybind11::module &(std::string const &namespace_) > &M) |
| 60 | +{ |
| 61 | + // aaaa::bbbb::cccc::eeee::baz_aaaa_bbbb_cccc_eeee() file:T51.nested_namespace.hpp line:11 |
| 62 | + 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"); |
| 63 | + |
| 64 | +} |
| 65 | + |
| 66 | + |
| 67 | +#include <map> |
| 68 | +#include <algorithm> |
| 69 | +#include <functional> |
| 70 | +#include <memory> |
| 71 | +#include <stdexcept> |
| 72 | +#include <string> |
| 73 | +#include <iostream> |
| 74 | + |
| 75 | +#include <pybind11/pybind11.h> |
| 76 | + |
| 77 | +using ModuleGetter = std::function< pybind11::module & (std::string const &) >; |
| 78 | + |
| 79 | +void bind_T51_nested_namespace(std::function< pybind11::module &(std::string const &namespace_) > &M); |
| 80 | +void bind_T51_nested_namespace_1(std::function< pybind11::module &(std::string const &namespace_) > &M); |
| 81 | +void bind_T51_nested_namespace_2(std::function< pybind11::module &(std::string const &namespace_) > &M); |
| 82 | + |
| 83 | + |
| 84 | +static std::vector<std::string> const reserved_python_words{ |
| 85 | + "nonlocal", |
| 86 | + "global", |
| 87 | +}; |
| 88 | +std::string mangle_namespace_name(std::string const &ns) { |
| 89 | + if( std::find(reserved_python_words.begin(), reserved_python_words.end(), ns) == reserved_python_words.end() ) return ns; |
| 90 | + return ns + '_'; |
| 91 | +} |
| 92 | + |
| 93 | +void makeSubmodules(const std::vector<std::string> &moduleNames, std::map <std::string, pybind11::module> &modules) { |
| 94 | + std::string prevNamespace = ""; |
| 95 | + std::string curNamespace = ""; |
| 96 | + for (const auto &curMod : moduleNames) { |
| 97 | + std::cout << "making: " << curMod << std::endl; |
| 98 | + if (curNamespace.size() > 0) |
| 99 | + curNamespace += "::"; // don't add :: to the start |
| 100 | + const auto mangledMod = mangle_namespace_name(curMod); |
| 101 | + std::cout << "mangled: " << mangledMod << std::endl; |
| 102 | + curNamespace += mangledMod; |
| 103 | + if (modules.count(curNamespace) == 0) { |
| 104 | + std::cout << "defining namespace " << curNamespace << " in " << prevNamespace << std::endl; |
| 105 | + modules[curNamespace] = modules[prevNamespace].def_submodule(mangledMod.c_str(), ("Bindings for " + curNamespace + " namespace").c_str()); |
| 106 | + } |
| 107 | + prevNamespace = curNamespace; |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | + |
| 112 | +PYBIND11_MODULE(T51_nested_namespace, root_module) { |
| 113 | + root_module.doc() = "T51_nested_namespace module"; |
| 114 | + |
| 115 | + std::map <std::string, pybind11::module> modules; |
| 116 | + ModuleGetter M = [&](std::string const &namespace_) -> pybind11::module & { |
| 117 | + auto it = modules.find(mangle_namespace_name(namespace_)); |
| 118 | + if( it == modules.end() ) throw std::runtime_error("Attempt to access pybind11::module for namespace " + namespace_ + " before it was created!!!"); |
| 119 | + return it->second; |
| 120 | + }; |
| 121 | + |
| 122 | + modules[""] = root_module; |
| 123 | + |
| 124 | + std::vector< std::vector<std::string> > sub_modules { |
| 125 | + {"aaaa"}, |
| 126 | + {"aaaa", "bbbb"}, |
| 127 | + {"aaaa", "bbbb", "cccc"}, |
| 128 | + {"aaaa", "bbbb", "cccc", "dddd"}, |
| 129 | + {"aaaa", "bbbb", "cccc", "eeee"}, |
| 130 | + }; |
| 131 | + for(auto &p : sub_modules ) { |
| 132 | + makeSubmodules(p, modules); |
| 133 | + } |
| 134 | + |
| 135 | + //pybind11::class_<std::shared_ptr<void>>(M(""), "_encapsulated_data_"); |
| 136 | + |
| 137 | + bind_T51_nested_namespace(M); |
| 138 | + bind_T51_nested_namespace_1(M); |
| 139 | + bind_T51_nested_namespace_2(M); |
| 140 | + |
| 141 | +} |
| 142 | + |
| 143 | +// Source list file: /home/matt/workspace/binder/build/test//T51_nested_namespace.sources |
| 144 | +// T51_nested_namespace.cpp |
| 145 | +// T51_nested_namespace.cpp |
| 146 | +// T51_nested_namespace_1.cpp |
| 147 | +// T51_nested_namespace_2.cpp |
| 148 | + |
| 149 | +// Modules list file: /home/matt/workspace/binder/build/test//T51_nested_namespace.modules |
| 150 | +// |
0 commit comments