Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(92)

Side by Side Diff: third_party/cython/src/Cython/Utility/CppConvert.pyx

Issue 385073004: Adding cython v0.20.2 in third-party. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Reference cython dev list thread. Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 # TODO: Figure out how many of the pass-by-value copies the compiler can elimina te.
2
3
4 #################### string.from_py ####################
5
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
11
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)
17
18
19 #################### string.to_py ####################
20
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)
28
29 @cname("{{cname}}")
30 cdef object {{cname}}(string& s):
31 return __Pyx_PyObject_FromStringAndSize(s.data(), s.size())
32
33
34 #################### vector.from_py ####################
35
36 {{template_type_declarations}}
37
38 cdef extern from *:
39 cdef cppclass vector "std::vector" [T]:
40 void push_back(T&)
41
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
48
49
50 #################### vector.to_py ####################
51
52 {{template_type_declarations}}
53
54 cdef extern from *:
55 cdef cppclass vector "const std::vector" [T]:
56 size_t size()
57 T& operator[](size_t)
58
59 @cname("{{cname}}")
60 cdef object {{cname}}(vector[X]& v):
61 return [X_to_py(v[i]) for i in range(v.size())]
62
63
64 #################### list.from_py ####################
65
66 {{template_type_declarations}}
67
68 cdef extern from *:
69 cdef cppclass cpp_list "std::list" [T]:
70 void push_back(T&)
71
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
78
79
80 #################### list.to_py ####################
81
82 cimport cython
83
84 {{template_type_declarations}}
85
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
96
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
105
106
107 #################### set.from_py ####################
108
109 {{template_type_declarations}}
110
111 cdef extern from *:
112 cdef cppclass set "std::{{maybe_unordered}}set" [T]:
113 void insert(T&)
114
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
121
122
123 #################### set.to_py ####################
124
125 cimport cython
126
127 {{template_type_declarations}}
128
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
139
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
148
149 #################### pair.from_py ####################
150
151 {{template_type_declarations}}
152
153 cdef extern from *:
154 cdef cppclass pair "std::pair" [T, U]:
155 pair()
156 pair(T&, U&)
157
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))
162
163
164 #################### pair.to_py ####################
165
166 {{template_type_declarations}}
167
168 cdef extern from *:
169 cdef cppclass pair "const std::pair" [T, U]:
170 T first
171 U second
172
173 @cname("{{cname}}")
174 cdef object {{cname}}(pair[X,Y]& p):
175 return X_to_py(p.first), Y_to_py(p.second)
176
177
178 #################### map.from_py ####################
179
180 {{template_type_declarations}}
181
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]&)
187
188 cdef cppclass pair "std::pair" [T, U]:
189 pass
190 cdef cppclass vector "std::vector" [T]:
191 pass
192
193
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
201
202
203 #################### map.to_py ####################
204 # TODO: Work out const so that this can take a const
205 # reference rather than pass by value.
206
207 cimport cython
208
209 {{template_type_declarations}}
210
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()
222
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
OLDNEW
« no previous file with comments | « third_party/cython/src/Cython/Utility/CommonTypes.c ('k') | third_party/cython/src/Cython/Utility/CppSupport.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698