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>
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
;
43 void PyUNO_iterator_del( PyObject
* self
)
45 PyUNO_iterator
* me
= reinterpret_cast<PyUNO_iterator
*>(self
);
48 PyThreadDetach antiguard
;
54 PyObject
* PyUNO_iterator_iter( PyObject
*self
)
60 PyObject
* PyUNO_iterator_next( PyObject
*self
)
62 PyUNO_iterator
* me
= reinterpret_cast<PyUNO_iterator
*>(self
);
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
, "" );
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
) );
115 static PyTypeObject PyUNO_iterator_Type
=
117 PyVarObject_HEAD_INIT( &PyType_Type
, 0 )
119 sizeof (PyUNO_iterator
),
136 Py_TPFLAGS_HAVE_ITER
,
142 PyUNO_iterator_iter
, // Generic, reused between the iterator types
164 #if PY_VERSION_HEX >= 0x03040000
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
;
189 PyObject_Del( self
);
193 PyObject
* PyUNO_list_iterator_next( PyObject
*self
)
195 PyUNO_list_iterator
* me
= reinterpret_cast<PyUNO_list_iterator
*>(self
);
199 bool noMoreElements
= false;
204 PyThreadDetach antiguard
;
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
, "" );
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
) );
245 static PyTypeObject PyUNO_list_iterator_Type
=
247 PyVarObject_HEAD_INIT( &PyType_Type
, 0 )
249 sizeof (PyUNO_list_iterator
),
251 PyUNO_list_iterator_del
,
266 Py_TPFLAGS_HAVE_ITER
,
272 PyUNO_iterator_iter
, // Generic, reused between the iterator types
273 PyUNO_list_iterator_next
,
294 #if PY_VERSION_HEX >= 0x03040000
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: */