Remove obsolete entries from .gitignore.
[chromium-blink-merge.git] / third_party / cython / src / Cython / Utility / CppConvert.pyx
blobd9530ecaa1b908288eff8f0449987efd90e9cc59
1 # TODO: Figure out how many of the pass-by-value copies the compiler can eliminate.
4 #################### string.from_py ####################
6 cdef extern from *:
7 cdef cppclass string "std::string":
8 string()
9 string(char* c_str, size_t size)
10 cdef char* __Pyx_PyObject_AsStringAndSize(object, Py_ssize_t*) except NULL
12 @cname("{{cname}}")
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 ####################
21 #cimport cython
22 #from libcpp.string cimport string
23 cdef extern from *:
24 cdef cppclass string "const std::string":
25 char* data()
26 size_t size()
27 cdef object __Pyx_PyObject_FromStringAndSize(char*, size_t)
29 @cname("{{cname}}")
30 cdef object {{cname}}(string& s):
31 return __Pyx_PyObject_FromStringAndSize(s.data(), s.size())
34 #################### vector.from_py ####################
36 {{template_type_declarations}}
38 cdef extern from *:
39 cdef cppclass vector "std::vector" [T]:
40 void push_back(T&)
42 @cname("{{cname}}")
43 cdef vector[X] {{cname}}(object o) except *:
44 cdef vector[X] v
45 for item in o:
46 v.push_back(X_from_py(item))
47 return v
50 #################### vector.to_py ####################
52 {{template_type_declarations}}
54 cdef extern from *:
55 cdef cppclass vector "const std::vector" [T]:
56 size_t size()
57 T& operator[](size_t)
59 @cname("{{cname}}")
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}}
68 cdef extern from *:
69 cdef cppclass cpp_list "std::list" [T]:
70 void push_back(T&)
72 @cname("{{cname}}")
73 cdef cpp_list[X] {{cname}}(object o) except *:
74 cdef cpp_list[X] l
75 for item in o:
76 l.push_back(X_from_py(item))
77 return l
80 #################### list.to_py ####################
82 cimport cython
84 {{template_type_declarations}}
86 cdef extern from *:
87 cdef cppclass cpp_list "std::list" [T]:
88 cppclass const_iterator:
89 T& operator*()
90 const_iterator operator++()
91 bint operator!=(const_iterator)
92 const_iterator begin()
93 const_iterator end()
94 cdef cppclass const_cpp_list "const std::list" [T] (cpp_list):
95 pass
97 @cname("{{cname}}")
98 cdef object {{cname}}(const_cpp_list[X]& v):
99 o = []
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)
104 return o
107 #################### set.from_py ####################
109 {{template_type_declarations}}
111 cdef extern from *:
112 cdef cppclass set "std::{{maybe_unordered}}set" [T]:
113 void insert(T&)
115 @cname("{{cname}}")
116 cdef set[X] {{cname}}(object o) except *:
117 cdef set[X] s
118 for item in o:
119 s.insert(X_from_py(item))
120 return s
123 #################### set.to_py ####################
125 cimport cython
127 {{template_type_declarations}}
129 cdef extern from *:
130 cdef cppclass cpp_set "std::{{maybe_unordered}}set" [T]:
131 cppclass const_iterator:
132 T& operator*()
133 const_iterator operator++()
134 bint operator!=(const_iterator)
135 const_iterator begin()
136 const_iterator end()
137 cdef cppclass const_cpp_set "const std::{{maybe_unordered}}set" [T](cpp_set):
138 pass
140 @cname("{{cname}}")
141 cdef object {{cname}}(const_cpp_set[X]& s):
142 o = set()
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)
147 return o
149 #################### pair.from_py ####################
151 {{template_type_declarations}}
153 cdef extern from *:
154 cdef cppclass pair "std::pair" [T, U]:
155 pair()
156 pair(T&, U&)
158 @cname("{{cname}}")
159 cdef pair[X,Y] {{cname}}(object o) except *:
160 x, y = o
161 return pair[X,Y](X_from_py(x), Y_from_py(y))
164 #################### pair.to_py ####################
166 {{template_type_declarations}}
168 cdef extern from *:
169 cdef cppclass pair "const std::pair" [T, U]:
170 T first
171 U second
173 @cname("{{cname}}")
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}}
182 cdef extern from *:
183 cdef cppclass pair "std::pair" [T, U]:
184 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]:
189 pass
190 cdef cppclass vector "std::vector" [T]:
191 pass
194 @cname("{{cname}}")
195 cdef map[X,Y] {{cname}}(object o) except *:
196 cdef dict d = o
197 cdef map[X,Y] m
198 for key, value in d.iteritems():
199 m.insert(pair[X,Y](X_from_py(key), Y_from_py(value)))
200 return m
203 #################### map.to_py ####################
204 # TODO: Work out const so that this can take a const
205 # reference rather than pass by value.
207 cimport cython
209 {{template_type_declarations}}
211 cdef extern from *:
212 cdef cppclass map "std::{{maybe_unordered}}map" [T, U]:
213 cppclass value_type:
214 T first
215 U second
216 cppclass iterator:
217 value_type& operator*()
218 iterator operator++()
219 bint operator!=(iterator)
220 iterator begin()
221 iterator end()
223 @cname("{{cname}}")
224 cdef object {{cname}}(map[X,Y] s):
225 o = {}
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)
232 return o