Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. cdef extern from "XY.h":
  2. cdef cppclass _XY:
  3. vector[double] getVector() except +
  4.  
  5. cdef class XY:
  6. cdef _XY _this
  7.  
  8. ...
  9.  
  10. def getVector():
  11. return self._this.getVector()
  12.  
  13. cdef extern from "<utility>" namespace "std":
  14. vector[double] move(vector[double]) # Cython has no function templates
  15.  
  16. def getVector():
  17. return move(self._this.getVector())
  18.  
  19. # distutils: language = c++
  20.  
  21. cdef extern from * namespace "polyfill":
  22. """
  23. namespace polyfill {
  24.  
  25. template <typename T>
  26. inline typename std::remove_reference<T>::type&& move(T& t) {
  27. return std::move(t);
  28. }
  29.  
  30. template <typename T>
  31. inline typename std::remove_reference<T>::type&& move(T&& t) {
  32. return std::move(t);
  33. }
  34.  
  35. } // namespace polyfill
  36. """
  37. cdef T move[T](T)
  38.  
  39. # distutils: language = c++
  40.  
  41. cdef extern from *:
  42. """
  43. #include <iostream>
  44.  
  45. #define PRINT() std::cout << __PRETTY_FUNCTION__ << std::endl
  46.  
  47. struct Test {
  48. Test() { PRINT(); }
  49. ~Test() { PRINT(); }
  50. Test(const Test&) { PRINT(); }
  51. Test(Test&&) { PRINT(); }
  52. Test& operator=(const Test&) { PRINT(); return *this; }
  53. Test& operator=(Test&&) { PRINT(); return *this; }
  54. };
  55.  
  56. void f(const Test&) { PRINT(); }
  57. void f(Test&&) { PRINT(); }
  58. """
  59.  
  60. cdef cppclass Test:
  61. pass
  62.  
  63. cdef void f(Test)
  64.  
  65. from move cimport move
  66.  
  67. cdef Test t1, t2
  68.  
  69. print("# t1 = t2")
  70. t1 = t2
  71.  
  72. print("# t1 = move(t2)")
  73. t1 = move(t2)
  74.  
  75. print("# f(t1)")
  76. f(t1)
  77.  
  78. print("# f(move(t1))")
  79. f(move(t1))
  80.  
  81. print("# f(move(move(t1)))")
  82. f(move(move(t1)))
  83.  
  84. print("# f(move(move(move(t1))))")
  85. f(move(move(move(t1))))
  86.  
  87. $ python3 -c "import test"
  88. Test::Test()
  89. Test::Test()
  90. # t1 = t2
  91. Test& Test::operator=(const Test&)
  92. # t1 = move(t2)
  93. Test& Test::operator=(Test&&)
  94. # f(t1)
  95. void f(const Test&)
  96. # f(move(t1))
  97. void f(Test&&)
  98. # f(move(move(t1)))
  99. void f(Test&&)
  100. # f(move(move(move(t1))))
  101. void f(Test&&)
  102. Test::~Test()
  103. Test::~Test()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement