24 #ifndef PXR_BASE_TF_ITERATOR_H
25 #define PXR_BASE_TF_ITERATOR_H
36 #include <type_traits>
39 PXR_NAMESPACE_OPEN_SCOPE
44 struct Tf_ShouldIterateOverCopy : std::false_type {};
52 template <
class T,
bool Reverse>
53 struct Tf_IteratorInterface {
54 typedef typename T::iterator IteratorType;
55 static IteratorType Begin(T &c) {
return c.begin(); }
56 static IteratorType End(T &c) {
return c.end(); }
59 template <
class T,
bool Reverse>
60 struct Tf_IteratorInterface<const T, Reverse> {
61 typedef typename T::const_iterator IteratorType;
62 static IteratorType Begin(T
const &c) {
return c.begin(); }
63 static IteratorType End(T
const &c) {
return c.end(); }
67 struct Tf_IteratorInterface<T, true> {
68 typedef typename T::reverse_iterator IteratorType;
69 static IteratorType Begin(T &c) {
return c.rbegin(); }
70 static IteratorType End(T &c) {
return c.rend(); }
74 struct Tf_IteratorInterface<const T, true> {
75 typedef typename T::const_reverse_iterator IteratorType;
76 static IteratorType Begin(T
const &c) {
return c.rbegin(); }
77 static IteratorType End(T
const &c) {
return c.rend(); }
175 template <
class T,
bool Reverse=false>
179 struct _IteratorPairAndCopy;
180 struct _IteratorPair;
184 typedef typename std::conditional<
185 Tf_ShouldIterateOverCopy<T>::value,
186 _IteratorPairAndCopy, _IteratorPair
192 typedef Tf_IteratorInterface<T, Reverse> IterInterface;
193 typedef typename IterInterface::IteratorType Iterator;
195 typedef typename std::iterator_traits<Iterator>::reference Reference;
210 Tf_ShouldIterateOverCopy<
typename std::decay<T>::type>::value,
211 "TfIterator only allows rvalues that it has been told to copy "
212 "via Tf_ShouldIterateOverCopy");
228 return _data.current == _data.end;
236 return _data.current == iterator._data.current;
242 return !(*
this == iterator);
270 if (ARCH_UNLIKELY(!*
this))
272 return *_data.current;
278 if (ARCH_UNLIKELY(!*
this))
280 return *_data.current;
286 if (ARCH_UNLIKELY(!*
this))
288 return _data.current;
293 explicit operator bool()
const {
294 return !(_data.current == _data.end);
300 operator Iterator()
const {
301 return _data.current;
308 return _data.current;
323 struct _IteratorPair {
325 explicit _IteratorPair(T &c) {
328 current = IterInterface::Begin(c);
329 end = IterInterface::End(c);
331 _IteratorPair(Iterator
const &b, Iterator
const &e) :
332 current(b), end(e) {}
339 struct _IteratorPairAndCopy :
public _IteratorPair {
340 _IteratorPairAndCopy() {}
341 explicit _IteratorPairAndCopy(T
const &c) : _IteratorPair(), _copy(c) {
342 current = IterInterface::Begin(_copy);
343 end = IterInterface::End(_copy);
345 using _IteratorPair::current;
346 using _IteratorPair::end;
362 std::forward<T>(container));
367 TfMakeReverseIterator(T&& container)
370 std::forward<T>(container));
390 #define TF_FOR_ALL(iter, c) \
391 for (auto iter = TfMakeIterator(c); iter; ++iter)
399 #define TF_REVERSE_FOR_ALL(iter, c) \
400 for (auto iter = TfMakeReverseIterator(c); iter; ++iter)
406 template <
class T,
size_t N>
412 PXR_NAMESPACE_CLOSE_SCOPE
414 #endif // PXR_BASE_TF_ITERATOR_H
TfIterator(Iterator const &begin, Iterator const &end)
Constructs an iterator to traverse a subset of the elements in a container.
Reference operator*()
Returns the element referenced by this iterator.
constexpr size_t TfArraySize(const T(&array)[N]) noexcept
Returns the number of elements in a statically sized array.
TfIterator operator++(int)
Post-increment operator.
TfIterator & operator++()
Pre-increment operator.
Iterator & operator->()
Returns a pointer to the element referenced by this iterator.
#define TF_CODING_ERROR(fmt, args)
Issue an internal programming error, but continue execution.
TfIterator(T &container)
Constructs an iterator to traverse each element of the specified STL container object.
TfIterator< typename std::remove_reference< T >::type > TfMakeIterator(T &&container)
Helper functions for creating TfIterator objects.
A simple iterator adapter for STL containers.
TfIterator(T &&container)
Allow rvalues only if the container type T should be copied by TfIterator.
#define TF_FATAL_ERROR(fmt, args)
Issue a fatal error and end the program.
TfIterator()
Default constructor. This iterator is uninitialized.
Reference operator*() const
Returns the element referenced by this iterator.
bool operator!() const
Returns true if this iterator is exhausted.
TfIterator GetNext() const
Returns an iterator that is positioned at the next element in the sequence.
bool operator!=(const TfIterator &iterator) const
Returns false if (*this == iterator) returns true, returns true otherwise.
const Iterator & base() const
Returns an STL iterator that has the same position as this iterator.
bool operator==(const TfIterator &iterator) const
Returns true if this Iterator.has the same position in the sequence as the specified iterator...
Stripped down version of diagnostic.h that doesn't define std::string.