1 # TODO: Figure out how many of the pass-by-value copies the compiler can eliminate.
4 #################### string.from_py ####################
7 cdef cppclass string
"std::string":
9 string
(char* c_str
, size_t size
)
10 cdef char* __Pyx_PyObject_AsStringAndSize
(object, Py_ssize_t
*) except NULL
13 cdef string
{{cname
}}(object o
) except *:
14 cdef Py_ssize_t length
15 cdef char* data
= __Pyx_PyObject_AsStringAndSize
(o
, &length
)
16 return string
(data
, length
)
19 #################### string.to_py ####################
22 #from libcpp.string cimport string
24 cdef cppclass string
"const std::string":
27 cdef object __Pyx_PyObject_FromStringAndSize
(char*, size_t
)
30 cdef object {{cname
}}(string
& s
):
31 return __Pyx_PyObject_FromStringAndSize
(s
.data
(), s
.size
())
34 #################### vector.from_py ####################
36 {{template_type_declarations
}}
39 cdef cppclass vector
"std::vector" [T
]:
43 cdef vector
[X
] {{cname
}}(object o
) except *:
46 v
.push_back
(X_from_py
(item
))
50 #################### vector.to_py ####################
52 {{template_type_declarations
}}
55 cdef cppclass vector
"const std::vector" [T
]:
60 cdef object {{cname
}}(vector
[X
]& v
):
61 return [X_to_py
(v
[i
]) for i
in range(v
.size
())]
64 #################### list.from_py ####################
66 {{template_type_declarations
}}
69 cdef cppclass cpp_list
"std::list" [T
]:
73 cdef cpp_list
[X
] {{cname
}}(object o
) except *:
76 l
.push_back
(X_from_py
(item
))
80 #################### list.to_py ####################
84 {{template_type_declarations
}}
87 cdef cppclass cpp_list
"std::list" [T
]:
88 cppclass const_iterator
:
90 const_iterator operator
++()
91 bint operator
!=(const_iterator
)
92 const_iterator begin
()
94 cdef cppclass const_cpp_list
"const std::list" [T
] (cpp_list
):
98 cdef object {{cname
}}(const_cpp_list
[X
]& v
):
100 cdef cpp_list
[X
].const_iterator iter
= v
.begin
()
101 while iter
!= v
.end
():
102 o
.append
(X_to_py
(cython
.operator
.dereference
(iter
)))
103 cython
.operator
.preincrement
(iter
)
107 #################### set.from_py ####################
109 {{template_type_declarations
}}
112 cdef cppclass set
"std::{{maybe_unordered}}set" [T
]:
116 cdef set
[X
] {{cname
}}(object o
) except *:
119 s
.insert
(X_from_py
(item
))
123 #################### set.to_py ####################
127 {{template_type_declarations
}}
130 cdef cppclass cpp_set
"std::{{maybe_unordered}}set" [T
]:
131 cppclass const_iterator
:
133 const_iterator operator
++()
134 bint operator
!=(const_iterator
)
135 const_iterator begin
()
137 cdef cppclass const_cpp_set
"const std::{{maybe_unordered}}set" [T
](cpp_set
):
141 cdef object {{cname
}}(const_cpp_set
[X
]& s
):
143 cdef cpp_set
[X
].const_iterator iter
= s
.begin
()
144 while iter
!= s
.end
():
145 o
.add
(X_to_py
(cython
.operator
.dereference
(iter
)))
146 cython
.operator
.preincrement
(iter
)
149 #################### pair.from_py ####################
151 {{template_type_declarations
}}
154 cdef cppclass pair
"std::pair" [T
, U
]:
159 cdef pair
[X
,Y
] {{cname
}}(object o
) except *:
161 return pair
[X
,Y
](X_from_py
(x
), Y_from_py
(y
))
164 #################### pair.to_py ####################
166 {{template_type_declarations
}}
169 cdef cppclass pair
"const std::pair" [T
, U
]:
174 cdef object {{cname
}}(pair
[X
,Y
]& p
):
175 return X_to_py
(p
.first
), Y_to_py
(p
.second
)
178 #################### map.from_py ####################
180 {{template_type_declarations
}}
183 cdef cppclass pair
"std::pair" [T
, U
]:
185 cdef cppclass
map "std::{{maybe_unordered}}map" [T
, U
]:
186 void insert
(pair
[T
, U
]&)
188 cdef cppclass pair
"std::pair" [T
, U
]:
190 cdef cppclass vector
"std::vector" [T
]:
195 cdef map[X
,Y
] {{cname
}}(object o
) except *:
198 for key
, value
in d
.iteritems
():
199 m
.insert
(pair
[X
,Y
](X_from_py
(key
), Y_from_py
(value
)))
203 #################### map.to_py ####################
204 # TODO: Work out const so that this can take a const
205 # reference rather than pass by value.
209 {{template_type_declarations
}}
212 cdef cppclass
map "std::{{maybe_unordered}}map" [T
, U
]:
217 value_type
& operator
*()
218 iterator operator
++()
219 bint operator
!=(iterator
)
224 cdef object {{cname
}}(map[X
,Y
] s
):
226 cdef map[X
,Y
].value_type
*key_value
227 cdef map[X
,Y
].iterator iter
= s
.begin
()
228 while iter
!= s
.end
():
229 key_value
= &cython
.operator
.dereference
(iter
)
230 o
[X_to_py
(key_value
.first
)] = Y_to_py
(key_value
.second
)
231 cython
.operator
.preincrement
(iter
)