ScriptingEngine.h 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #ifndef SCRIPTING_ENGINE_H
  2. #define SCRIPTING_ENGINE_H
  3. #include <boost/python.hpp>
  4. #include "Common.h"
  5. #include "Object.h"
  6. /**
  7. *
  8. */
  9. class ScriptingEngine: public Object
  10. {
  11. public:
  12. ScriptingEngine(Object* parent = NULL);
  13. ~ScriptingEngine() {}
  14. /**
  15. * Execute python script
  16. * @param script Script source
  17. * @return true on success
  18. */
  19. bool execScript(const char* script, const char* scriptName = "unamed");
  20. /**
  21. * Expose a C++ variable to python
  22. * @param varName The name to referenced in python
  23. * @param var The actual variable
  24. */
  25. template<typename Type>
  26. void exposeVar(const char* varName, Type* var);
  27. private:
  28. python::object mainModule;
  29. python::object ankiModule;
  30. python::object mainNamespace;
  31. /**
  32. * Init python and modules
  33. */
  34. bool init();
  35. };
  36. inline ScriptingEngine::ScriptingEngine(Object* parent):
  37. Object(parent)
  38. {
  39. init();
  40. }
  41. template<typename Type>
  42. inline void ScriptingEngine::exposeVar(const char* varName, Type* var)
  43. {
  44. python::scope(ankiModule).attr(varName) = python::ptr(var);
  45. }
  46. #endif