ScriptingCommon.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /// @file
  2. /// This file is included by all the *.bpi.cpp files
  3. #include <boost/python.hpp>
  4. #include <boost/python/suite/indexing/vector_indexing_suite.hpp>
  5. using namespace boost;
  6. using namespace boost::python;
  7. /// Wrap a class
  8. #define WRAP(x) \
  9. void boostPythonWrap##x()
  10. /// Wrap a singleton class
  11. #define WRAP_SINGLETON(x) \
  12. WRAP(x) { \
  13. class_<x, noncopyable>(#x, no_init) \
  14. .def("getInstance", & x ::getInstance, return_value_policy<reference_existing_object>()) \
  15. .staticmethod("getInstance") \
  16. ; \
  17. }
  18. #define WRAP_CONTAINER(x) \
  19. class_<x >(#x) \
  20. .def(vector_indexing_suite<x >()) \
  21. ;
  22. //======================================================================================================================
  23. // Property for simple types =
  24. //======================================================================================================================
  25. template<typename ClassType, typename RetType, const RetType& (ClassType::* accessor)() const>
  26. RetType getterSv(const ClassType* t)
  27. {
  28. return (t->*accessor)();
  29. }
  30. template<typename ClassType, typename InType, void (ClassType::* accessor)(const InType&)>
  31. void setterSv(ClassType* t, InType in)
  32. {
  33. (t->*accessor)(in);
  34. }
  35. /// Boost python property for simple types (int, float etc) that cannot be wrapped by boost::python correctly
  36. #define BP_PROPERTY_BASIC_TYPE(Type__, Class__, var__, getter__, setter__) \
  37. .add_property(#var__, &getterSv<Class__, Type__, &Class__::getter__>, \
  38. &setterSv<Class__, Type__, &Class__::setter__>)
  39. //======================================================================================================================
  40. // Math library stuff =
  41. //======================================================================================================================
  42. template<typename ClassType, typename RetType, RetType (ClassType::* accessor)() const>
  43. RetType getM(const ClassType* t)
  44. {
  45. return (t->*accessor)();
  46. }
  47. template<typename ClassType, typename InType, InType& (ClassType::* accessor)()>
  48. void setM(ClassType* t, InType in)
  49. {
  50. (t->*accessor)() = in;
  51. }
  52. #define BP_PROPERTY_MATH(ClassType__, name__) \
  53. .add_property(#name__, &getM<ClassType__, float, &ClassType__::name__>, \
  54. &setM<ClassType__, float, &ClassType__::name__>)