ScriptCommon.h 1.6 KB

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