Tulip  5.4.0
Large graphs analysis and drawing
ConversionIterator.h
1 /*
2  *
3  * This file is part of Tulip (http://tulip.labri.fr)
4  *
5  * Authors: David Auber and the Tulip development Team
6  * from LaBRI, University of Bordeaux
7  *
8  * Tulip is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation, either version 3
11  * of the License, or (at your option) any later version.
12  *
13  * Tulip is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU General Public License for more details.
17  *
18  */
19 
20 #ifndef CONVERSIONITERATOR_H
21 #define CONVERSIONITERATOR_H
22 
23 #include <tulip/Iterator.h>
24 #include <tulip/StlIterator.h>
25 #include <tulip/memorypool.h>
26 
27 namespace tlp {
28 /**
29  * @class ConversionIterator
30  * @ingroup Iterators
31  * @brief Iterator that enables to convert an Iterator of type TYPEIN to an Iterator of type TYPEOUT
32  * @param it the iterator that should be converted
33  * @param convFunctor the functor or lamnda function that enables to convert TYPEIN to TYPEOUT
34  *
35  * The functor function should have the following form:
36  * @code
37  * class AConversionFunc {
38  * TYPEOUT operator()(TYPEIN a) {
39  * return a converted in TYPEOUT;
40  * }
41  * };
42  * @endcode
43  **/
44 template <typename TYPEIN, typename TYPEOUT, typename ConversionFunc>
45 class ConversionIterator : public Iterator<TYPEOUT> {
46 public:
47  ConversionIterator(Iterator<TYPEIN> *it, ConversionFunc convFunctor)
48  : _it(it), _convFunctor(convFunctor) {}
50  delete _it;
51  }
52  inline bool hasNext() {
53  return _it->hasNext();
54  }
55  inline TYPEOUT next() {
56  return _convFunctor(_it->next());
57  }
58 
59 private:
61  ConversionFunc _convFunctor;
62 };
63 /**
64  * @class MPConversionIterator
65  * @ingroup Iterators
66  * @brief MPConversionIterator implements memory pool for ConversionIterator
67  * @warning never inherit from that class
68  * @see ConversionIterator
69  **/
70 template <typename TIN, typename TOUT, typename ConversionFunc>
71 class MPConversionIterator : public ConversionIterator<TIN, TOUT, ConversionFunc>,
72  public MemoryPool<MPConversionIterator<TIN, TOUT, ConversionFunc>> {
73 public:
74  MPConversionIterator(Iterator<TIN> *it, ConversionFunc convFunctor)
76 };
77 
78 /**
79  * @brief Convenient function for creating a ConversionIterator.
80  * @ingroup Iterators
81  *
82  * @since Tulip 5.2
83  *
84  * Creates a ConversionIterator from another Iterator and a conversion function.
85  * The returned Iterator takes ownership of the one provided as parameter.
86  *
87  * @param it a Tulip Iterator
88  * @param convFunc functor or lambda function converting a value of type TYPEIN to type TYPEOUT
89  *
90  * @return a ConversionIterator
91  *
92  **/
93 template <typename TOUT, typename ConversionFunc, typename TIN>
95  ConversionFunc convFunc) {
96  return new MPConversionIterator<TIN, TOUT, ConversionFunc>(it, convFunc);
97 }
98 
99 /**
100  * @brief Convenient function for creating a ConversionIterator from a STL container.
101  * @ingroup Iterators
102  *
103  * @since Tulip 5.2
104  *
105  * Creates a ConversionIterator from a STL container (std::list, std::vector, std::set, std::map,
106  *...)
107  * and a conversion function.
108  *
109  * @param stlContainer any STL container
110  * @param convFunc functor or lambda function converting a value from the container to type TYPEOUT
111  *
112  * @return a ConversionIterator
113  *
114  **/
115 template <typename TOUT, typename ConversionFunc, typename Container>
116 typename std::enable_if<has_const_iterator<Container>::value,
118  *>::type inline conversionIterator(const Container &stlContainer,
119  ConversionFunc convFunc) {
121  stlIterator(stlContainer), convFunc);
122 }
123 } // namespace tlp
124 #endif // CONVERSIONITERATOR_H
Iterator that enables to convert an Iterator of type TYPEIN to an Iterator of type TYPEOUT...
virtual T next()=0
Moves the Iterator on the next element.
Interface for Tulip iterators. Allows basic iteration operations only.
Definition: Graph.h:43
ConversionIterator< TIN, TOUT, ConversionFunc > * conversionIterator(tlp::Iterator< TIN > *it, ConversionFunc convFunc)
Convenient function for creating a ConversionIterator.
bool hasNext()
Tells if the sequence is at its end.
TYPEOUT next()
Moves the Iterator on the next element.
virtual bool hasNext()=0
Tells if the sequence is at its end.
MPConversionIterator implements memory pool for ConversionIterator.
std::enable_if< has_const_iterator< Container >::value, StlIterator< typename Container::value_type, typename Container::const_iterator > * >::type stlIterator(const Container &stlContainer)
Convenient function for creating a StlIterator from a stl container.
Definition: StlIterator.h:95