| Directory: | ./ |
|---|---|
| File: | bindings/python/crocoddyl/utils/namespace.hpp |
| Date: | 2025-03-26 19:23:43 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 11 | 11 | 100.0% |
| Branches: | 14 | 28 | 50.0% |
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /////////////////////////////////////////////////////////////////////////////// | ||
| 2 | // BSD 3-Clause License | ||
| 3 | // | ||
| 4 | // Copyright (C) 2024-2025, Heriot-Watt University | ||
| 5 | // Copyright note valid unless otherwise stated in individual files. | ||
| 6 | // All rights reserved. | ||
| 7 | /////////////////////////////////////////////////////////////////////////////// | ||
| 8 | |||
| 9 | #ifndef BINDINGS_PYTHON_CROCODDYL_UTILS_NAMESPACE_HPP_ | ||
| 10 | #define BINDINGS_PYTHON_CROCODDYL_UTILS_NAMESPACE_HPP_ | ||
| 11 | |||
| 12 | #include <boost/python.hpp> | ||
| 13 | #include <string> | ||
| 14 | |||
| 15 | namespace crocoddyl { | ||
| 16 | namespace python { | ||
| 17 | |||
| 18 | /** | ||
| 19 | * @brief Get the current Python scope | ||
| 20 | * @return the name of the current Python scope. | ||
| 21 | */ | ||
| 22 | 1470 | inline std::string getCurrentScopeName() { | |
| 23 | namespace bp = boost::python; | ||
| 24 |
1/2✓ Branch 1 taken 1470 times.
✗ Branch 2 not taken.
|
1470 | bp::scope current_scope; |
| 25 |
5/10✓ Branch 2 taken 1470 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1470 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 1470 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 1470 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 1470 times.
✗ Branch 15 not taken.
|
4410 | return std::string(bp::extract<const char *>(current_scope.attr("__name__"))); |
| 26 | 1470 | } | |
| 27 | |||
| 28 | /** | ||
| 29 | * @brief Get or create a Python scope | ||
| 30 | * | ||
| 31 | * @param submodule_name name of the submodule | ||
| 32 | * @return the submodule related to the namespace name. | ||
| 33 | */ | ||
| 34 | 1470 | inline boost::python::object getOrCreatePythonNamespace( | |
| 35 | const std::string &submodule_name) { | ||
| 36 | namespace bp = boost::python; | ||
| 37 | const std::string complete_submodule_name = | ||
| 38 |
3/6✓ Branch 1 taken 1470 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1470 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 1470 times.
✗ Branch 8 not taken.
|
2940 | getCurrentScopeName() + "." + submodule_name; |
| 39 | bp::object submodule( | ||
| 40 |
2/4✓ Branch 2 taken 1470 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 1470 times.
✗ Branch 7 not taken.
|
1470 | bp::borrowed(PyImport_AddModule(complete_submodule_name.c_str()))); |
| 41 |
1/2✓ Branch 1 taken 1470 times.
✗ Branch 2 not taken.
|
1470 | bp::scope current_scope; |
| 42 |
2/4✓ Branch 2 taken 1470 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1470 times.
✗ Branch 6 not taken.
|
1470 | current_scope.attr(submodule_name.c_str()) = submodule; |
| 43 | 2940 | return submodule; | |
| 44 | 1470 | } | |
| 45 | |||
| 46 | } // namespace python | ||
| 47 | } // namespace crocoddyl | ||
| 48 | |||
| 49 | #endif // BINDINGS_PYTHON_CROCODDYL_UTILS_NAMESPACE_HPP_ | ||
| 50 |