| 1 |
// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root |
| 2 |
// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception |
| 3 |
#ifndef DUNE_PYTHON_ISTL_ITERATOR_HH |
| 4 |
#define DUNE_PYTHON_ISTL_ITERATOR_HH |
| 5 |
|
| 6 |
#include <tuple> |
| 7 |
|
| 8 |
#include <dune/common/visibility.hh> |
| 9 |
|
| 10 |
#include <dune/python/pybind11/extensions.h> |
| 11 |
#include <dune/python/pybind11/pybind11.h> |
| 12 |
|
| 13 |
namespace Dune |
| 14 |
{ |
| 15 |
|
| 16 |
namespace Python |
| 17 |
{ |
| 18 |
|
| 19 |
namespace detail |
| 20 |
{ |
| 21 |
|
| 22 |
template< class T> |
| 23 |
struct DUNE_PRIVATE ISTLEnumerateState |
| 24 |
{ |
| 25 |
T *obj; |
| 26 |
pybind11::object pyObj; |
| 27 |
}; |
| 28 |
|
| 29 |
template< class T > |
| 30 |
struct DUNE_PRIVATE ISTLIteratorState |
| 31 |
{ |
| 32 |
typename T::iterator it, end; |
| 33 |
}; |
| 34 |
|
| 35 |
template< class T > |
| 36 |
struct DUNE_PRIVATE ISTLReverseIteratorState |
| 37 |
{ |
| 38 |
typename T::iterator it, end; |
| 39 |
}; |
| 40 |
|
| 41 |
template< class T > |
| 42 |
struct DUNE_PRIVATE ISTLEnumerateIteratorState |
| 43 |
{ |
| 44 |
typename T::iterator it, end; |
| 45 |
}; |
| 46 |
|
| 47 |
template< class T > |
| 48 |
struct DUNE_PRIVATE ISTLReverseEnumerateIteratorState |
| 49 |
{ |
| 50 |
typename T::iterator it, end; |
| 51 |
}; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
// registerISTLIterator |
| 56 |
// -------------------- |
| 57 |
|
| 58 |
template< class T, class... options > |
| 59 |
inline static void registerISTLIterators ( pybind11::class_< T, options... > cls ) |
| 60 |
{ |
| 61 |
if( !pybind11::already_registered< ISTLIteratorState< T > >() ) |
| 62 |
{ |
| 63 |
pybind11::class_< ISTLIteratorState< T > > cls( pybind11::handle(), "iterator", pybind11::module_local() ); |
| 64 |
cls.def( "__iter__", [] ( pybind11::object self ) { return self; } ); |
| 65 |
cls.def( "__next__", [] ( ISTLIteratorState< T > &state ) -> decltype( *state.it ) { |
| 66 |
if( state.it == state.end ) |
| 67 |
throw pybind11::stop_iteration(); |
| 68 |
decltype( *state.it ) result = *state.it; |
| 69 |
++state.it; |
| 70 |
return result; |
| 71 |
}, pybind11::keep_alive< 0, 1 >() ); |
| 72 |
} |
| 73 |
|
| 74 |
cls.def( "__iter__", [] ( T &self ) { return ISTLIteratorState< T >{ self.begin(), self.end() }; }, pybind11::keep_alive< 0, 1 >() ); |
| 75 |
|
| 76 |
if( !pybind11::already_registered< ISTLReverseIteratorState< T > >() ) |
| 77 |
{ |
| 78 |
pybind11::class_< ISTLReverseIteratorState< T > > cls( pybind11::handle(), "iterator", pybind11::module_local() ); |
| 79 |
cls.def( "__iter__", [] ( pybind11::object self ) { return self; } ); |
| 80 |
cls.def( "__next__", [] ( ISTLReverseIteratorState< T > &state ) -> decltype( *state.it ) { |
| 81 |
if( state.it == state.end ) |
| 82 |
throw pybind11::stop_iteration(); |
| 83 |
decltype( *state.it ) result = *state.it; |
| 84 |
--state.it; |
| 85 |
return result; |
| 86 |
}, pybind11::keep_alive< 0, 1 >() ); |
| 87 |
} |
| 88 |
|
| 89 |
cls.def( "__reversed__", [] ( T &self ) { return ISTLReverseIteratorState< T >{ self.beforeEnd(), self.beforeBegin() }; }, pybind11::keep_alive< 0, 1 >() ); |
| 90 |
|
| 91 |
if( !pybind11::already_registered< ISTLEnumerateState< T > >() ) |
| 92 |
{ |
| 93 |
if( !pybind11::already_registered< ISTLEnumerateIteratorState< T > >() ) |
| 94 |
{ |
| 95 |
pybind11::class_< ISTLEnumerateIteratorState< T > > cls( pybind11::handle(), "iterator", pybind11::module_local() ); |
| 96 |
cls.def( "__iter__", [] ( pybind11::object self ) { return self; } ); |
| 97 |
cls.def( "__next__", [] ( pybind11::object self ) { |
| 98 |
auto &state = pybind11::cast< ISTLEnumerateIteratorState< T > & >( self ); |
| 99 |
if( state.it == state.end ) |
| 100 |
throw pybind11::stop_iteration(); |
| 101 |
std::tuple< decltype( state.it.index() ), pybind11::object > result( state.it.index(), pybind11::cast( *state.it ) ); |
| 102 |
pybind11::detail::keep_alive_impl( self, std::get< 1 >( result ) ); |
| 103 |
++state.it; |
| 104 |
return result; |
| 105 |
} ); |
| 106 |
} |
| 107 |
|
| 108 |
if( !pybind11::already_registered< ISTLReverseEnumerateIteratorState< T > >() ) |
| 109 |
{ |
| 110 |
pybind11::class_< ISTLReverseEnumerateIteratorState< T > > cls( pybind11::handle(), "iterator", pybind11::module_local() ); |
| 111 |
cls.def( "__iter__", [] ( pybind11::object self ) { return self; } ); |
| 112 |
cls.def( "__next__", [] ( pybind11::object self ) { |
| 113 |
auto &state = pybind11::cast< ISTLReverseEnumerateIteratorState< T > & >( self ); |
| 114 |
if( state.it == state.end ) |
| 115 |
throw pybind11::stop_iteration(); |
| 116 |
std::tuple< decltype( state.it.index() ), pybind11::object > result( state.it.index(), pybind11::cast( *state.it ) ); |
| 117 |
pybind11::detail::keep_alive_impl( self, std::get< 1 >( result ) ); |
| 118 |
--state.it; |
| 119 |
return result; |
| 120 |
} ); |
| 121 |
} |
| 122 |
|
| 123 |
pybind11::class_< ISTLEnumerateState< T > > cls( pybind11::handle(), "enumerate", pybind11::module_local() ); |
| 124 |
cls.def( "__iter__", [] ( ISTLEnumerateState< T > &self ) { |
| 125 |
return ISTLEnumerateIteratorState< T >{ self.obj->begin(), self.obj->end() }; |
| 126 |
}, pybind11::keep_alive< 0, 1 >() ); |
| 127 |
cls.def( "__reversed__", [] ( ISTLEnumerateState< T > &self ) { |
| 128 |
return ISTLReverseEnumerateIteratorState< T >{ self.obj->beforeEnd(), self.obj->beforeBegin() }; |
| 129 |
}, pybind11::keep_alive< 0, 1 >() ); |
| 130 |
} |
| 131 |
|
| 132 |
cls.def_property_readonly( "enumerate", [] ( pybind11::object self ) { |
| 133 |
return ISTLEnumerateState< T >{ &pybind11::cast< T & >( self ), self }; |
| 134 |
} ); |
| 135 |
} |
| 136 |
|
| 137 |
} // namespace detail |
| 138 |
|
| 139 |
} // namespace Python |
| 140 |
|
| 141 |
} // namespace Dune |
| 142 |
|
| 143 |
#endif // #ifndef DUNE_PYTHON_ISTL_ITERATOR_HH |