NAP
pythonscript.h
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4 
5 #pragma once
6 
7 // Pybind includes
8 #include <pybind11/pybind11.h>
9 #include <pybind11/stl.h>
10 
11 // Nap includes
12 #include <nap/resource.h>
13 #include <nap/logger.h>
14 #include <rtti/factory.h>
15 #include <utility/errorstate.h>
16 
17 namespace nap
18 {
19 
20  // Forward declarations
21  class PythonScriptService;
22 
27  class NAPAPI PythonScript : public Resource
28  {
29  RTTI_ENABLE(Resource)
30  public:
32 
33  std::string mPath;
34 
35  bool init(utility::ErrorState& errorState) override;
36 
45  template <typename ReturnType, typename ...Args>
46  bool get(const std::string& identifier, utility::ErrorState& errorState, ReturnType& returnValue, Args&&... args);
47 
55  template <typename ...Args>
56  bool call(const std::string& identifier, utility::ErrorState& errorState, Args&&... args);
57 
62  pybind11::object get(const std::string& symbol);
63 
64  private:
65  pybind11::module mModule;
66  PythonScriptService* mService = nullptr;
67  };
68 
69 
70  template <typename ReturnType, typename ...Args>
71  bool PythonScript::get(const std::string& identifier, utility::ErrorState& errorState, ReturnType& returnValue, Args&&... args)
72  {
73  try
74  {
75  returnValue = mModule.attr(identifier.c_str())(std::forward<Args>(args)...).template cast<ReturnType>();
76  }
77  catch (const pybind11::error_already_set& err)
78  {
79  errorState.fail("Runtime python error while executing %s: %s", mPath.c_str(), err.what());
80  return false;
81  }
82  return true;
83  }
84 
85 
86  template <typename ...Args>
87  bool PythonScript::call(const std::string& identifier, utility::ErrorState& errorState, Args&&... args)
88  {
89  try
90  {
91  mModule.attr(identifier.c_str())(std::forward<Args>(args)...);
92  }
93  catch (const pybind11::error_already_set& err)
94  {
95  errorState.fail("Runtime python error while executing %s: %s", mPath.c_str(), err.what());
96  return false;
97  }
98  return true;
99  }
100 
101 
102  // Object creator used for constructing the the PythonScript
104 
105 }
nap::rtti::ObjectCreator
Definition: factory.h:49
nap::utility::ErrorState
Definition: errorstate.h:19
nap::utility::ErrorState::fail
void fail(T &&errorMessage)
Definition: errorstate.h:73
nap::PythonScript
Definition: pythonscript.h:27
nap::PythonScript::mPath
std::string mPath
property: 'Path' Path to the python script.
Definition: pythonscript.h:33
nap
Definition: templateapp.h:17
nap::PythonScript::call
bool call(const std::string &identifier, utility::ErrorState &errorState, Args &&... args)
Definition: pythonscript.h:87
nap::Resource
Definition: resource.h:19
nap::PythonScriptService
Definition: pythonscriptservice.h:18
nap::PythonScript::get
bool get(const std::string &identifier, utility::ErrorState &errorState, ReturnType &returnValue, Args &&... args)
Definition: pythonscript.h:71