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
) );
114 static PyTypeObject PyUNO_iterator_Type
=
116 PyVarObject_HEAD_INIT( &PyType_Type
, 0 )
118 sizeof (PyUNO_iterator
),
121 #if PY_VERSION_HEX >= 0x03080000
122 0, // Py_ssize_t tp_vectorcall_offset
124 nullptr, // printfunc tp_print
139 Py_TPFLAGS_HAVE_ITER
,
145 PyUNO_iterator_iter
, // Generic, reused between the iterator types
167 #if PY_VERSION_HEX >= 0x03040000
169 #if PY_VERSION_HEX >= 0x03080000
170 , nullptr // vectorcallfunc tp_vectorcall
175 PyObject
* PyUNO_iterator_new( const Reference
< XEnumeration
>& xEnumeration
)
177 PyUNO_iterator
* self
= PyObject_New( PyUNO_iterator
, &PyUNO_iterator_Type
);
178 if ( self
== nullptr )
179 return nullptr; // == error
180 self
->members
= new PyUNO_iterator_Internals
;
181 self
->members
->xEnumeration
= xEnumeration
;
182 return reinterpret_cast<PyObject
*>(self
);
185 ///////////////////////////////////////////////////////////////////////////////
187 static void PyUNO_list_iterator_del( PyObject
* self
)
189 PyUNO_list_iterator
* me
= reinterpret_cast<PyUNO_list_iterator
*>(self
);
192 PyThreadDetach antiguard
;
195 PyObject_Del( self
);
199 static PyObject
* PyUNO_list_iterator_next( PyObject
*self
)
201 PyUNO_list_iterator
* me
= reinterpret_cast<PyUNO_list_iterator
*>(self
);
205 bool noMoreElements
= false;
210 PyThreadDetach antiguard
;
212 aRet
= me
->members
->xIndexAccess
->getByIndex( me
->members
->index
);
214 catch( const css::lang::IndexOutOfBoundsException
& )
216 noMoreElements
= true;
220 if ( noMoreElements
)
222 PyErr_SetString( PyExc_StopIteration
, "" );
226 PyRef rRet
= runtime
.any2PyObject( aRet
);
227 me
->members
->index
++;
228 return rRet
.getAcquired();
230 catch( css::script::CannotConvertException
&e
)
232 raisePyExceptionWithAny( css::uno::makeAny( e
) );
234 catch( css::lang::IllegalArgumentException
&e
)
236 raisePyExceptionWithAny( css::uno::makeAny( e
) );
238 catch( const css::lang::WrappedTargetException
&e
)
240 raisePyExceptionWithAny( css::uno::makeAny( e
) );
242 catch( const css::uno::RuntimeException
&e
)
244 raisePyExceptionWithAny( css::uno::makeAny( e
) );
251 static PyTypeObject PyUNO_list_iterator_Type
=
253 PyVarObject_HEAD_INIT( &PyType_Type
, 0 )
255 sizeof (PyUNO_list_iterator
),
257 PyUNO_list_iterator_del
,
258 #if PY_VERSION_HEX >= 0x03080000
259 0, // Py_ssize_t tp_vectorcall_offset
261 nullptr, // printfunc tp_print
276 Py_TPFLAGS_HAVE_ITER
,
282 PyUNO_iterator_iter
, // Generic, reused between the iterator types
283 PyUNO_list_iterator_next
,
304 #if PY_VERSION_HEX >= 0x03040000
306 #if PY_VERSION_HEX >= 0x03080000
307 , nullptr // vectorcallfunc tp_vectorcall
312 PyObject
* PyUNO_list_iterator_new( const Reference
<XIndexAccess
> &xIndexAccess
)
314 PyUNO_list_iterator
* self
= PyObject_New( PyUNO_list_iterator
, &PyUNO_list_iterator_Type
);
315 if ( self
== nullptr )
316 return nullptr; // == error
317 self
->members
= new PyUNO_list_iterator_Internals
;
318 self
->members
->xIndexAccess
= xIndexAccess
;
319 self
->members
->index
= 0;
320 return reinterpret_cast<PyObject
*>(self
);
325 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */