diff --git a/dune/pybindxi/CMakeLists.txt b/dune/pybindxi/CMakeLists.txt index 1a8f400c9696767855795d0bf5424440914a7319..e66a99e4c98663b9279e08356b2773300a83d4fe 100644 --- a/dune/pybindxi/CMakeLists.txt +++ b/dune/pybindxi/CMakeLists.txt @@ -5,6 +5,8 @@ # Authors: # Felix Schindler (2016) +dune_library_add_sources(dunepybindxi SOURCES interpreter.cc) + install(FILES attr.h buffer_info.h @@ -16,6 +18,7 @@ install(FILES embed.h eval.h functional.h + interpreter.hh iostream.h numpy.h operators.h diff --git a/dune/pybindxi/interpreter.cc b/dune/pybindxi/interpreter.cc new file mode 100644 index 0000000000000000000000000000000000000000..f12371612af56806770c4edc5ce617c10cc25bdf --- /dev/null +++ b/dune/pybindxi/interpreter.cc @@ -0,0 +1,34 @@ +// This file is part of the dune-pybindxi project: +// https://github.com/dune-community/dune-pybindxi +// Copyright 2009-2017 dune-xt-la developers and contributors. All rights +// reserved. +// License: Dual licensed as BSD 2-Clause License +// (http://opensource.org/licenses/BSD-2-Clause) +// or GPL-2.0+ (http://opensource.org/licenses/gpl-license) +// with "runtime exception" (http://www.dune-project.org/license.html) +// Authors: +// Felix Schindler (2017) + +#include "interpreter.hh" + +namespace Dune { +namespace PybindXI { + +pybind11::module +ScopedInterpreter::import_module(const std::string &module_name) { + auto result = modules_.find(module_name); + if (result != modules_.end()) + return result->second; + else { + modules_[module_name] = pybind11::module::import(module_name.c_str()); + return modules_[module_name]; + } +} // ... load_module(...) + +ScopedInterpreter &GlobalInterpreter() { + static ScopedInterpreter global_interpreter; + return global_interpreter; +} + +} // namespace PybindXI +} // namespace Dune diff --git a/dune/pybindxi/interpreter.hh b/dune/pybindxi/interpreter.hh new file mode 100644 index 0000000000000000000000000000000000000000..b6f40fd3440f45effee56f9fb3c3350057ddc3b3 --- /dev/null +++ b/dune/pybindxi/interpreter.hh @@ -0,0 +1,41 @@ +// This file is part of the dune-pybindxi project: +// https://github.com/dune-community/dune-pybindxi +// Copyright 2009-2017 dune-xt-la developers and contributors. All rights +// reserved. +// License: Dual licensed as BSD 2-Clause License +// (http://opensource.org/licenses/BSD-2-Clause) +// or GPL-2.0+ (http://opensource.org/licenses/gpl-license) +// with "runtime exception" (http://www.dune-project.org/license.html) +// Authors: +// Felix Schindler (2017) + +#ifndef DUNE_PYBINDXI_INTERPRETER_HH +#define DUNE_PYBINDXI_INTERPRETER_HH + +#include <map> +#include <string> + +#include "embed.h" + +namespace Dune { +namespace PybindXI { + +/** + * \note Most likely, you do not want to use this class directly, but + * GlobalInterpreter instead! + */ +class ScopedInterpreter { +public: + pybind11::module import_module(const std::string &module_name); + +private: + pybind11::scoped_interpreter interpreter_; + std::map<std::string, pybind11::module> modules_; +}; // class ScopedInterpreter + +ScopedInterpreter &GlobalInterpreter(); + +} // namespace PybindXI +} // namespace Dune + +#endif // DUNE_PYBINDXI_INTERPRETER_HH