Tulip  4.1.0
Better Visualization Through Research
 All Classes Files Functions Variables Enumerations Enumerator Properties Groups Pages
ForEach.h
1 /*
2  *
3  * This file is part of Tulip (www.tulip-software.org)
4  *
5  * Authors: David Auber and the Tulip development Team
6  * from LaBRI, University of Bordeaux 1 and Inria Bordeaux - Sud Ouest
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 ///@cond DOXYGEN_HIDDEN
20 
21 #ifndef Tulip_FOREACH_H
22 #define Tulip_FOREACH_H
23 
24 #include <cassert>
25 #include <tulip/Iterator.h>
26 #include <tulip/StableIterator.h>
27 #include <tulip/tulipconf.h>
28 
29 #ifndef DOXYGEN_NOTFOR_DEVEL
30 namespace tlp {
31 /**
32 * @brief Encapsulation of a Tulip Iterator intended to be allocated on the stack instead of the heap,
33 * so it gets deleted when out of scope.
34 *
35 **/
36 template<typename TYPE>
37 struct _TLP_IT {
38  _TLP_IT(Iterator<TYPE> *_it) : _it(_it) {
39  }
40  ~_TLP_IT() {
41  delete _it;
42  }
43  Iterator<TYPE> *_it;
44 };
45 
46 /**
47 * @brief
48 **/
49 template<typename TYPE>
50 inline bool _tlp_if_test(TYPE &n, _TLP_IT<TYPE> &_it) {
51  assert(_it._it != NULL);
52 
53  if(_it._it->hasNext()) {
54  n = _it._it->next();
55  return true;
56  }
57  else {
58  return false;
59  }
60 }
61 }
62 #endif //DOXYGEN_NOTFOR_DEVEL
63 
64 /**
65  * @brief Allows to iterate on the nodes or edges of a Graph in a clear and concise way.
66  * It also avoid having to manage a tulip Iterator, whose deletion is often forgotten.
67  */
68 #define forEach(A, B) \
69 for(tlp::_TLP_IT<TYPEOF(A) > _it_foreach(B); tlp::_tlp_if_test(A, _it_foreach);)
70 
71 /**
72  * @brief Allows to iterate on the nodes or edges of a copy of a Graph in a clear and concise way.
73  * The stable Iterator creates a copy of the Graph, and iterates on the said copy.
74  * It allows deletion operations to be performed without invalidating the iterator.
75  * It also avoid having to manage a tulip Iterator, whose deletion is often forgotten.
76  */
77 #define stableForEach(A, B) \
78  for(tlp::_TLP_IT<TYPEOF(A) > _it_foreach(new StableIterator<TYPEOF(A) >(B)); tlp::_tlp_if_test(A, _it_foreach);)
79 
80 #endif
81 ///@endcond