Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / pyuno / source / module / pyuno_iterator.cxx
blob71ccb03ac6cdb52f350e4a20786b6e5f6adbd702
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/config.h>
22 #include <cassert>
24 #include "pyuno_impl.hxx"
26 #include <com/sun/star/container/XEnumeration.hpp>
27 #include <com/sun/star/container/XIndexAccess.hpp>
28 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
29 #include <com/sun/star/lang/WrappedTargetException.hpp>
31 using com::sun::star::container::XEnumeration;
32 using com::sun::star::container::XIndexAccess;
33 using com::sun::star::lang::IndexOutOfBoundsException;
34 using com::sun::star::lang::WrappedTargetException;
35 using com::sun::star::uno::Any;
36 using com::sun::star::uno::Reference;
37 using com::sun::star::uno::RuntimeException;
40 namespace pyuno
43 void PyUNO_iterator_del( PyObject* self )
45 PyUNO_iterator* me = reinterpret_cast<PyUNO_iterator*>(self);
48 PyThreadDetach antiguard;
49 delete me->members;
51 PyObject_Del( self );
54 PyObject* PyUNO_iterator_iter( PyObject *self )
56 Py_INCREF( self );
57 return self;
60 PyObject* PyUNO_iterator_next( PyObject *self )
62 PyUNO_iterator* me = reinterpret_cast<PyUNO_iterator*>(self);
64 Runtime runtime;
65 Any aRet;
67 try
69 bool hasMoreElements = false;
72 PyThreadDetach antiguard;
74 hasMoreElements = me->members->xEnumeration->hasMoreElements();
75 if ( hasMoreElements )
77 aRet = me->members->xEnumeration->nextElement();
81 if ( hasMoreElements )
83 PyRef rRet = runtime.any2PyObject( aRet );
84 return rRet.getAcquired();
87 PyErr_SetString( PyExc_StopIteration, "" );
88 return nullptr;
90 catch( css::container::NoSuchElementException &e )
92 raisePyExceptionWithAny( css::uno::makeAny( e ) );
94 catch( css::script::CannotConvertException &e )
96 raisePyExceptionWithAny( css::uno::makeAny( e ) );
98 catch( css::lang::IllegalArgumentException &e )
100 raisePyExceptionWithAny( css::uno::makeAny( e ) );
102 catch( const css::lang::WrappedTargetException &e )
104 raisePyExceptionWithAny( css::uno::makeAny( e ) );
106 catch( const css::uno::RuntimeException &e )
108 raisePyExceptionWithAny( css::uno::makeAny( e ) );
111 return nullptr;
115 static PyTypeObject PyUNO_iterator_Type =
117 PyVarObject_HEAD_INIT( &PyType_Type, 0 )
118 "PyUNO_iterator",
119 sizeof (PyUNO_iterator),
121 PyUNO_iterator_del,
122 nullptr,
123 nullptr,
124 nullptr,
125 nullptr,
126 nullptr,
127 nullptr,
128 nullptr,
129 nullptr,
130 nullptr,
131 nullptr,
132 nullptr,
133 nullptr,
134 nullptr,
135 nullptr,
136 Py_TPFLAGS_HAVE_ITER,
137 nullptr,
138 nullptr,
139 nullptr,
140 nullptr,
142 PyUNO_iterator_iter, // Generic, reused between the iterator types
143 PyUNO_iterator_next,
144 nullptr,
145 nullptr,
146 nullptr,
147 nullptr,
148 nullptr,
149 nullptr,
150 nullptr,
152 nullptr,
153 nullptr,
154 nullptr,
155 nullptr,
156 nullptr,
157 nullptr,
158 nullptr,
159 nullptr,
160 nullptr,
161 nullptr,
162 nullptr,
164 #if PY_VERSION_HEX >= 0x03040000
165 , nullptr
166 #endif
169 PyObject* PyUNO_iterator_new( const Reference< XEnumeration >& xEnumeration )
171 PyUNO_iterator* self = PyObject_New( PyUNO_iterator, &PyUNO_iterator_Type );
172 if ( self == nullptr )
173 return nullptr; // == error
174 self->members = new PyUNO_iterator_Internals();
175 self->members->xEnumeration = xEnumeration;
176 return reinterpret_cast<PyObject*>(self);
179 ///////////////////////////////////////////////////////////////////////////////
181 void PyUNO_list_iterator_del( PyObject* self )
183 PyUNO_list_iterator* me = reinterpret_cast<PyUNO_list_iterator*>(self);
186 PyThreadDetach antiguard;
187 delete me->members;
189 PyObject_Del( self );
193 PyObject* PyUNO_list_iterator_next( PyObject *self )
195 PyUNO_list_iterator* me = reinterpret_cast<PyUNO_list_iterator*>(self);
197 Runtime runtime;
198 Any aRet;
199 bool noMoreElements = false;
204 PyThreadDetach antiguard;
205 try {
206 aRet = me->members->xIndexAccess->getByIndex( me->members->index );
208 catch( css::lang::IndexOutOfBoundsException )
210 noMoreElements = true;
214 if ( noMoreElements )
216 PyErr_SetString( PyExc_StopIteration, "" );
217 return nullptr;
220 PyRef rRet = runtime.any2PyObject( aRet );
221 me->members->index++;
222 return rRet.getAcquired();
224 catch( css::script::CannotConvertException &e )
226 raisePyExceptionWithAny( css::uno::makeAny( e ) );
228 catch( css::lang::IllegalArgumentException &e )
230 raisePyExceptionWithAny( css::uno::makeAny( e ) );
232 catch( const css::lang::WrappedTargetException &e )
234 raisePyExceptionWithAny( css::uno::makeAny( e ) );
236 catch( const css::uno::RuntimeException &e )
238 raisePyExceptionWithAny( css::uno::makeAny( e ) );
241 return nullptr;
245 static PyTypeObject PyUNO_list_iterator_Type =
247 PyVarObject_HEAD_INIT( &PyType_Type, 0 )
248 "PyUNO_iterator",
249 sizeof (PyUNO_list_iterator),
251 PyUNO_list_iterator_del,
252 nullptr,
253 nullptr,
254 nullptr,
255 nullptr,
256 nullptr,
257 nullptr,
258 nullptr,
259 nullptr,
260 nullptr,
261 nullptr,
262 nullptr,
263 nullptr,
264 nullptr,
265 nullptr,
266 Py_TPFLAGS_HAVE_ITER,
267 nullptr,
268 nullptr,
269 nullptr,
270 nullptr,
272 PyUNO_iterator_iter, // Generic, reused between the iterator types
273 PyUNO_list_iterator_next,
274 nullptr,
275 nullptr,
276 nullptr,
277 nullptr,
278 nullptr,
279 nullptr,
280 nullptr,
282 nullptr,
283 nullptr,
284 nullptr,
285 nullptr,
286 nullptr,
287 nullptr,
288 nullptr,
289 nullptr,
290 nullptr,
291 nullptr,
292 nullptr,
294 #if PY_VERSION_HEX >= 0x03040000
295 , nullptr
296 #endif
299 PyObject* PyUNO_list_iterator_new( const Reference<XIndexAccess> &xIndexAccess )
301 PyUNO_list_iterator* self = PyObject_New( PyUNO_list_iterator, &PyUNO_list_iterator_Type );
302 if ( self == nullptr )
303 return nullptr; // == error
304 self->members = new PyUNO_list_iterator_Internals();
305 self->members->xIndexAccess = xIndexAccess;
306 self->members->index = 0;
307 return reinterpret_cast<PyObject*>(self);
312 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */