1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 "pyuno_impl.hxx"
24 #include <com/sun/star/container/XEnumeration.hpp>
25 #include <com/sun/star/container/XIndexAccess.hpp>
26 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
27 #include <com/sun/star/lang/WrappedTargetException.hpp>
28 #include <com/sun/star/script/CannotConvertException.hpp>
30 using com::sun::star::container::XEnumeration
;
31 using com::sun::star::container::XIndexAccess
;
32 using com::sun::star::lang::IndexOutOfBoundsException
;
33 using com::sun::star::lang::WrappedTargetException
;
34 using com::sun::star::uno::Any
;
35 using com::sun::star::uno::Reference
;
36 using com::sun::star::uno::RuntimeException
;
42 static void PyUNO_iterator_del( PyObject
* self
)
44 PyUNO_iterator
* me
= reinterpret_cast<PyUNO_iterator
*>(self
);
47 PyThreadDetach antiguard
;
53 static PyObject
* PyUNO_iterator_iter( PyObject
*self
)
59 static PyObject
* PyUNO_iterator_next( PyObject
*self
)
61 PyUNO_iterator
* me
= reinterpret_cast<PyUNO_iterator
*>(self
);
68 bool hasMoreElements
= false;
71 PyThreadDetach antiguard
;
73 hasMoreElements
= me
->members
->xEnumeration
->hasMoreElements();
74 if ( hasMoreElements
)
76 aRet
= me
->members
->xEnumeration
->nextElement();
80 if ( hasMoreElements
)
82 PyRef rRet
= runtime
.any2PyObject( aRet
);
83 return rRet
.getAcquired();
86 PyErr_SetString( PyExc_StopIteration
, "" );
89 catch( css::container::NoSuchElementException
&e
)
91 raisePyExceptionWithAny( css::uno::makeAny( e
) );
93 catch( css::script::CannotConvertException
&e
)
95 raisePyExceptionWithAny( css::uno::makeAny( e
) );
97 catch( css::lang::IllegalArgumentException
&e
)
99 raisePyExceptionWithAny( css::uno::makeAny( e
) );
101 catch( const css::lang::WrappedTargetException
&e
)
103 raisePyExceptionWithAny( css::uno::makeAny( e
) );
105 catch( const css::uno::RuntimeException
&e
)
107 raisePyExceptionWithAny( css::uno::makeAny( e
) );
113 static PyTypeObject PyUNO_iterator_Type
=
115 PyVarObject_HEAD_INIT( &PyType_Type
, 0 )
117 sizeof (PyUNO_iterator
),
120 #if PY_VERSION_HEX >= 0x03080000
121 0, // Py_ssize_t tp_vectorcall_offset
123 nullptr, // printfunc tp_print
138 Py_TPFLAGS_HAVE_ITER
,
144 PyUNO_iterator_iter
, // Generic, reused between the iterator types
166 #if PY_VERSION_HEX >= 0x03040000
168 #if PY_VERSION_HEX >= 0x03080000
169 , nullptr // vectorcallfunc tp_vectorcall
170 #if PY_VERSION_HEX < 0x03090000
171 #if defined __clang__
172 #pragma clang diagnostic push
173 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
175 , nullptr // tp_print
176 #if defined __clang__
177 #pragma clang diagnostic pop
184 PyObject
* PyUNO_iterator_new( const Reference
< XEnumeration
>& xEnumeration
)
186 PyUNO_iterator
* self
= PyObject_New( PyUNO_iterator
, &PyUNO_iterator_Type
);
187 if ( self
== nullptr )
188 return nullptr; // == error
189 self
->members
= new PyUNO_iterator_Internals
;
190 self
->members
->xEnumeration
= xEnumeration
;
191 return reinterpret_cast<PyObject
*>(self
);
194 ///////////////////////////////////////////////////////////////////////////////
196 static void PyUNO_list_iterator_del( PyObject
* self
)
198 PyUNO_list_iterator
* me
= reinterpret_cast<PyUNO_list_iterator
*>(self
);
201 PyThreadDetach antiguard
;
204 PyObject_Del( self
);
208 static PyObject
* PyUNO_list_iterator_next( PyObject
*self
)
210 PyUNO_list_iterator
* me
= reinterpret_cast<PyUNO_list_iterator
*>(self
);
217 bool noMoreElements
= false;
219 PyThreadDetach antiguard
;
221 aRet
= me
->members
->xIndexAccess
->getByIndex( me
->members
->index
);
223 catch( const css::lang::IndexOutOfBoundsException
& )
225 noMoreElements
= true;
229 if ( noMoreElements
)
231 PyErr_SetString( PyExc_StopIteration
, "" );
235 PyRef rRet
= runtime
.any2PyObject( aRet
);
236 me
->members
->index
++;
237 return rRet
.getAcquired();
239 catch( css::script::CannotConvertException
&e
)
241 raisePyExceptionWithAny( css::uno::makeAny( e
) );
243 catch( css::lang::IllegalArgumentException
&e
)
245 raisePyExceptionWithAny( css::uno::makeAny( e
) );
247 catch( const css::lang::WrappedTargetException
&e
)
249 raisePyExceptionWithAny( css::uno::makeAny( e
) );
251 catch( const css::uno::RuntimeException
&e
)
253 raisePyExceptionWithAny( css::uno::makeAny( e
) );
259 static PyTypeObject PyUNO_list_iterator_Type
=
261 PyVarObject_HEAD_INIT( &PyType_Type
, 0 )
263 sizeof (PyUNO_list_iterator
),
265 PyUNO_list_iterator_del
,
266 #if PY_VERSION_HEX >= 0x03080000
267 0, // Py_ssize_t tp_vectorcall_offset
269 nullptr, // printfunc tp_print
284 Py_TPFLAGS_HAVE_ITER
,
290 PyUNO_iterator_iter
, // Generic, reused between the iterator types
291 PyUNO_list_iterator_next
,
312 #if PY_VERSION_HEX >= 0x03040000
314 #if PY_VERSION_HEX >= 0x03080000
315 , nullptr // vectorcallfunc tp_vectorcall
316 #if PY_VERSION_HEX < 0x03090000
317 #if defined __clang__
318 #pragma clang diagnostic push
319 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
321 , nullptr // tp_print
322 #if defined __clang__
323 #pragma clang diagnostic pop
330 PyObject
* PyUNO_list_iterator_new( const Reference
<XIndexAccess
> &xIndexAccess
)
332 PyUNO_list_iterator
* self
= PyObject_New( PyUNO_list_iterator
, &PyUNO_list_iterator_Type
);
333 if ( self
== nullptr )
334 return nullptr; // == error
335 self
->members
= new PyUNO_list_iterator_Internals
;
336 self
->members
->xIndexAccess
= xIndexAccess
;
337 self
->members
->index
= 0;
338 return reinterpret_cast<PyObject
*>(self
);
343 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */