CppConvert.pyx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. # TODO: Figure out how many of the pass-by-value copies the compiler can eliminate.
  2. #################### string.from_py ####################
  3. cdef extern from *:
  4. cdef cppclass string "{{type}}":
  5. string()
  6. string(char* c_str, size_t size)
  7. cdef const char* __Pyx_PyObject_AsStringAndSize(object, Py_ssize_t*) except NULL
  8. @cname("{{cname}}")
  9. cdef string {{cname}}(object o) except *:
  10. cdef Py_ssize_t length = 0
  11. cdef const char* data = __Pyx_PyObject_AsStringAndSize(o, &length)
  12. return string(data, length)
  13. #################### string.to_py ####################
  14. #cimport cython
  15. #from libcpp.string cimport string
  16. cdef extern from *:
  17. cdef cppclass string "{{type}}":
  18. char* data()
  19. size_t size()
  20. {{for py_type in ['PyObject', 'PyUnicode', 'PyStr', 'PyBytes', 'PyByteArray']}}
  21. cdef extern from *:
  22. cdef object __Pyx_{{py_type}}_FromStringAndSize(const char*, size_t)
  23. @cname("{{cname.replace("PyObject", py_type, 1)}}")
  24. cdef inline object {{cname.replace("PyObject", py_type, 1)}}(const string& s):
  25. return __Pyx_{{py_type}}_FromStringAndSize(s.data(), s.size())
  26. {{endfor}}
  27. #################### vector.from_py ####################
  28. cdef extern from *:
  29. cdef cppclass vector "std::vector" [T]:
  30. void push_back(T&)
  31. @cname("{{cname}}")
  32. cdef vector[X] {{cname}}(object o) except *:
  33. cdef vector[X] v
  34. for item in o:
  35. v.push_back(<X>item)
  36. return v
  37. #################### vector.to_py ####################
  38. cdef extern from *:
  39. cdef cppclass vector "const std::vector" [T]:
  40. size_t size()
  41. T& operator[](size_t)
  42. @cname("{{cname}}")
  43. cdef object {{cname}}(vector[X]& v):
  44. return [v[i] for i in range(v.size())]
  45. #################### list.from_py ####################
  46. cdef extern from *:
  47. cdef cppclass cpp_list "std::list" [T]:
  48. void push_back(T&)
  49. @cname("{{cname}}")
  50. cdef cpp_list[X] {{cname}}(object o) except *:
  51. cdef cpp_list[X] l
  52. for item in o:
  53. l.push_back(<X>item)
  54. return l
  55. #################### list.to_py ####################
  56. cimport cython
  57. cdef extern from *:
  58. cdef cppclass cpp_list "std::list" [T]:
  59. cppclass const_iterator:
  60. T& operator*()
  61. const_iterator operator++()
  62. bint operator!=(const_iterator)
  63. const_iterator begin()
  64. const_iterator end()
  65. @cname("{{cname}}")
  66. cdef object {{cname}}(const cpp_list[X]& v):
  67. o = []
  68. cdef cpp_list[X].const_iterator iter = v.begin()
  69. while iter != v.end():
  70. o.append(cython.operator.dereference(iter))
  71. cython.operator.preincrement(iter)
  72. return o
  73. #################### set.from_py ####################
  74. cdef extern from *:
  75. cdef cppclass set "std::{{maybe_unordered}}set" [T]:
  76. void insert(T&)
  77. @cname("{{cname}}")
  78. cdef set[X] {{cname}}(object o) except *:
  79. cdef set[X] s
  80. for item in o:
  81. s.insert(<X>item)
  82. return s
  83. #################### set.to_py ####################
  84. cimport cython
  85. cdef extern from *:
  86. cdef cppclass cpp_set "std::{{maybe_unordered}}set" [T]:
  87. cppclass const_iterator:
  88. T& operator*()
  89. const_iterator operator++()
  90. bint operator!=(const_iterator)
  91. const_iterator begin()
  92. const_iterator end()
  93. @cname("{{cname}}")
  94. cdef object {{cname}}(const cpp_set[X]& s):
  95. o = set()
  96. cdef cpp_set[X].const_iterator iter = s.begin()
  97. while iter != s.end():
  98. o.add(cython.operator.dereference(iter))
  99. cython.operator.preincrement(iter)
  100. return o
  101. #################### pair.from_py ####################
  102. cdef extern from *:
  103. cdef cppclass pair "std::pair" [T, U]:
  104. pair()
  105. pair(T&, U&)
  106. @cname("{{cname}}")
  107. cdef pair[X,Y] {{cname}}(object o) except *:
  108. x, y = o
  109. return pair[X,Y](<X>x, <Y>y)
  110. #################### pair.to_py ####################
  111. cdef extern from *:
  112. cdef cppclass pair "std::pair" [T, U]:
  113. T first
  114. U second
  115. @cname("{{cname}}")
  116. cdef object {{cname}}(const pair[X,Y]& p):
  117. return p.first, p.second
  118. #################### map.from_py ####################
  119. cdef extern from *:
  120. cdef cppclass pair "std::pair" [T, U]:
  121. pair(T&, U&)
  122. cdef cppclass map "std::{{maybe_unordered}}map" [T, U]:
  123. void insert(pair[T, U]&)
  124. cdef cppclass vector "std::vector" [T]:
  125. pass
  126. @cname("{{cname}}")
  127. cdef map[X,Y] {{cname}}(object o) except *:
  128. cdef dict d = o
  129. cdef map[X,Y] m
  130. for key, value in d.iteritems():
  131. m.insert(pair[X,Y](<X>key, <Y>value))
  132. return m
  133. #################### map.to_py ####################
  134. # TODO: Work out const so that this can take a const
  135. # reference rather than pass by value.
  136. cimport cython
  137. cdef extern from *:
  138. cdef cppclass map "std::{{maybe_unordered}}map" [T, U]:
  139. cppclass value_type:
  140. T first
  141. U second
  142. cppclass const_iterator:
  143. value_type& operator*()
  144. const_iterator operator++()
  145. bint operator!=(const_iterator)
  146. const_iterator begin()
  147. const_iterator end()
  148. @cname("{{cname}}")
  149. cdef object {{cname}}(const map[X,Y]& s):
  150. o = {}
  151. cdef const map[X,Y].value_type *key_value
  152. cdef map[X,Y].const_iterator iter = s.begin()
  153. while iter != s.end():
  154. key_value = &cython.operator.dereference(iter)
  155. o[key_value.first] = key_value.second
  156. cython.operator.preincrement(iter)
  157. return o
  158. #################### complex.from_py ####################
  159. cdef extern from *:
  160. cdef cppclass std_complex "std::complex" [T]:
  161. std_complex()
  162. std_complex(T, T) except +
  163. @cname("{{cname}}")
  164. cdef std_complex[X] {{cname}}(object o) except *:
  165. cdef double complex z = o
  166. return std_complex[X](<X>z.real, <X>z.imag)
  167. #################### complex.to_py ####################
  168. cdef extern from *:
  169. cdef cppclass std_complex "std::complex" [T]:
  170. X real()
  171. X imag()
  172. @cname("{{cname}}")
  173. cdef object {{cname}}(const std_complex[X]& z):
  174. cdef double complex tmp
  175. tmp.real = <double>z.real()
  176. tmp.imag = <double>z.imag()
  177. return tmp