calling_conventions_mf.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. //
  2. // adapted from bind_stdcall_mf_test.cpp - test for bind.hpp + __stdcall (free functions)
  3. // The purpose of this simple test is to determine if a function can be
  4. // called from Python with the various existing calling conventions
  5. //
  6. // Copyright (c) 2001 Peter Dimov and Multi Media Ltd.
  7. //
  8. // Distributed under the Boost Software License, Version 1.0. (See
  9. // accompanying file LICENSE_1_0.txt or copy at
  10. // http://www.boost.org/LICENSE_1_0.txt)
  11. //
  12. #if !defined(TEST_INCLUDE_RECURSION)
  13. #define TEST_INCLUDE_RECURSION
  14. //------------------------------------------------------------------------------
  15. // this section is the main body of the test extension module
  16. #if defined(_WIN32) && !defined(_WIN64)
  17. # define BOOST_PYTHON_ENABLE_CDECL
  18. # define BOOST_PYTHON_ENABLE_STDCALL
  19. # define BOOST_PYTHON_ENABLE_FASTCALL
  20. #endif
  21. #include <boost/preprocessor/cat.hpp>
  22. #include <boost/preprocessor/stringize.hpp>
  23. #include <boost/python.hpp>
  24. using namespace boost::python;
  25. // first define test functions for every calling convention
  26. #define TEST_DECLARE_FUNCTIONS
  27. #define TESTED_CALLING_CONVENTION __cdecl
  28. #include "calling_conventions_mf.cpp"
  29. #undef TESTED_CALLING_CONVENTION
  30. #define TESTED_CALLING_CONVENTION __stdcall
  31. #include "calling_conventions_mf.cpp"
  32. #undef TESTED_CALLING_CONVENTION
  33. #define TESTED_CALLING_CONVENTION __fastcall
  34. #include "calling_conventions_mf.cpp"
  35. #undef TESTED_CALLING_CONVENTION
  36. #undef TEST_DECLARE_FUNCTIONS
  37. // then create a module wrapping the defined functions for every calling convention
  38. BOOST_PYTHON_MODULE( calling_conventions_mf_ext )
  39. {
  40. #define TEST_WRAP_FUNCTIONS
  41. #define TESTED_CALLING_CONVENTION __cdecl
  42. #include "calling_conventions_mf.cpp"
  43. #undef TESTED_CALLING_CONVENTION
  44. #define TESTED_CALLING_CONVENTION __stdcall
  45. #include "calling_conventions_mf.cpp"
  46. #undef TESTED_CALLING_CONVENTION
  47. #define TESTED_CALLING_CONVENTION __fastcall
  48. #include "calling_conventions_mf.cpp"
  49. #undef TESTED_CALLING_CONVENTION
  50. #undef TEST_WRAP_FUNCTIONS
  51. }
  52. #else // !defined(TEST_INCLUDE_RECURSION)
  53. //------------------------------------------------------------------------------
  54. // this section defines the functions to be wrapped
  55. # if defined(TEST_DECLARE_FUNCTIONS)
  56. # if !defined(TESTED_CALLING_CONVENTION)
  57. # error "One calling convention must be defined"
  58. # endif // !defined(TESTED_CALLING_CONVENTION)
  59. namespace BOOST_PP_CAT(test, TESTED_CALLING_CONVENTION) {
  60. struct X
  61. {
  62. mutable unsigned int hash;
  63. X(): hash(0) {}
  64. void TESTED_CALLING_CONVENTION f0() { f1(17); }
  65. void TESTED_CALLING_CONVENTION g0() const { g1(17); }
  66. void TESTED_CALLING_CONVENTION f1(int a1) { hash = (hash * 17041 + a1) % 32768; }
  67. void TESTED_CALLING_CONVENTION g1(int a1) const { hash = (hash * 17041 + a1 * 2) % 32768; }
  68. void TESTED_CALLING_CONVENTION f2(int a1, int a2) { f1(a1); f1(a2); }
  69. void TESTED_CALLING_CONVENTION g2(int a1, int a2) const { g1(a1); g1(a2); }
  70. void TESTED_CALLING_CONVENTION f3(int a1, int a2, int a3) { f2(a1, a2); f1(a3); }
  71. void TESTED_CALLING_CONVENTION g3(int a1, int a2, int a3) const { g2(a1, a2); g1(a3); }
  72. void TESTED_CALLING_CONVENTION f4(int a1, int a2, int a3, int a4) { f3(a1, a2, a3); f1(a4); }
  73. void TESTED_CALLING_CONVENTION g4(int a1, int a2, int a3, int a4) const { g3(a1, a2, a3); g1(a4); }
  74. void TESTED_CALLING_CONVENTION f5(int a1, int a2, int a3, int a4, int a5) { f4(a1, a2, a3, a4); f1(a5); }
  75. void TESTED_CALLING_CONVENTION g5(int a1, int a2, int a3, int a4, int a5) const { g4(a1, a2, a3, a4); g1(a5); }
  76. void TESTED_CALLING_CONVENTION f6(int a1, int a2, int a3, int a4, int a5, int a6) { f5(a1, a2, a3, a4, a5); f1(a6); }
  77. void TESTED_CALLING_CONVENTION g6(int a1, int a2, int a3, int a4, int a5, int a6) const { g5(a1, a2, a3, a4, a5); g1(a6); }
  78. void TESTED_CALLING_CONVENTION f7(int a1, int a2, int a3, int a4, int a5, int a6, int a7) { f6(a1, a2, a3, a4, a5, a6); f1(a7); }
  79. void TESTED_CALLING_CONVENTION g7(int a1, int a2, int a3, int a4, int a5, int a6, int a7) const { g6(a1, a2, a3, a4, a5, a6); g1(a7); }
  80. void TESTED_CALLING_CONVENTION f8(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8) { f7(a1, a2, a3, a4, a5, a6, a7); f1(a8); }
  81. void TESTED_CALLING_CONVENTION g8(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8) const { g7(a1, a2, a3, a4, a5, a6, a7); g1(a8); }
  82. };
  83. } // namespace BOOST_PP_CAT(test, TESTED_CALLING_CONVENTION)
  84. # endif // defined(TEST_DECLARE_FUNCTIONS)
  85. //------------------------------------------------------------------------------
  86. // this section wraps the functions
  87. # if defined(TEST_WRAP_FUNCTIONS)
  88. # if !defined(TESTED_CALLING_CONVENTION)
  89. # error "One calling convention must be defined"
  90. # endif // !defined(TESTED_CALLING_CONVENTION)
  91. {
  92. typedef BOOST_PP_CAT(test, TESTED_CALLING_CONVENTION)::X X;
  93. class_<X>("X" BOOST_PP_STRINGIZE(TESTED_CALLING_CONVENTION))
  94. .def("f0", &X::f0)
  95. .def("g0", &X::g0)
  96. .def("f1", &X::f1)
  97. .def("g1", &X::g1)
  98. .def("f2", &X::f2)
  99. .def("g2", &X::g2)
  100. .def("f3", &X::f3)
  101. .def("g3", &X::g3)
  102. .def("f4", &X::f4)
  103. .def("g4", &X::g4)
  104. .def("f5", &X::f5)
  105. .def("g5", &X::g5)
  106. .def("f6", &X::f6)
  107. .def("g6", &X::g6)
  108. .def("f7", &X::f7)
  109. .def("g7", &X::g7)
  110. .def("f8", &X::f8)
  111. .def("g8", &X::g8)
  112. .def_readonly("hash", &X::hash)
  113. ;
  114. }
  115. # endif // defined(TEST_WRAP_FUNCTIONS)
  116. #endif // !defined(TEST_INCLUDE_RECURSION)