Skip to content

Commit

Permalink
talipot-core/StlIterator: Add reversed overrides using container copies
Browse files Browse the repository at this point in the history
This is required when passing a temporary container object to the reversed
function or a crash occurs otherwise.
  • Loading branch information
anlambert committed Nov 28, 2024
1 parent 7fe9e2f commit d0db946
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion library/talipot-core/include/talipot/StlIterator.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
*
* Copyright (C) 2019-2021 The Talipot developers
* Copyright (C) 2019-2024 The Talipot developers
*
* Talipot is a fork of Tulip, created by David Auber
* and the Tulip development Team from LaBRI, University of Bordeaux
Expand Down Expand Up @@ -156,6 +156,19 @@ struct reverse_wrapper {
}
};

template <typename Container>
struct reverse_wrapper_copy {
Container _c;
reverse_wrapper_copy(Container c) : _c(std::move(c)) {}

typename Container::reverse_iterator begin() {
return _c.rbegin();
}
typename Container::reverse_iterator end() {
return _c.rend();
}
};

template <typename Container>
struct const_reverse_wrapper {
const Container &_c;
Expand All @@ -169,15 +182,39 @@ struct const_reverse_wrapper {
}
};

template <typename Container>
struct const_reverse_wrapper_copy {
const Container _c;
const_reverse_wrapper_copy(const Container c) : _c(std::move(c)) {}

typename Container::const_reverse_iterator begin() const {
return _c.rbegin();
}
typename Container::const_reverse_iterator end() const {
return _c.rend();
}
};

template <typename Container>
inline reverse_wrapper<Container> reversed(Container &c) {
return reverse_wrapper<Container>(c);
}

template <typename Container>
inline reverse_wrapper_copy<Container> reversed(Container &&c) {
return reverse_wrapper_copy<Container>(std::move(c));
}

template <typename Container>
inline const_reverse_wrapper<Container> reversed(const Container &c) {
return const_reverse_wrapper<Container>(c);
}

template <typename Container>
inline const_reverse_wrapper_copy<Container> reversed(const Container &&c) {
return const_reverse_wrapper_copy<Container>(std::move(c));
}

}

#endif // TALIPOT_STL_ITERATOR_H

0 comments on commit d0db946

Please sign in to comment.