From d261ea25583fa9e1a2a03a2fa7056289e77ca494 Mon Sep 17 00:00:00 2001 From: Paul Licameli Date: Sat, 22 Dec 2018 09:35:22 -0500 Subject: [PATCH] Define and use utility template ValueIterator... ... generalizing the fix of commit 7591891 to other cases, making the iterators safe for adaptation with std::reverse_iterator should the need ever arise --- src/MemoryX.h | 13 +++++++++++++ src/Track.h | 11 +---------- src/WaveTrack.h | 2 +- src/toolbars/ToolDock.h | 2 +- 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/MemoryX.h b/src/MemoryX.h index 694cd01f2..5b9bab279 100644 --- a/src/MemoryX.h +++ b/src/MemoryX.h @@ -430,6 +430,19 @@ template< typename T > ValueRestorer< T > valueRestorer( T& var, const T& newValue ) { return ValueRestorer< T >{ var, newValue }; } +/** + \brief A convenience for defining iterators that return rvalue types, so that + they cooperate correctly with stl algorithms and std::reverse_iterator + */ +template< typename Value, typename Category = std::forward_iterator_tag > +using ValueIterator = std::iterator< + Category, const Value, ptrdiff_t, + // void pointer type so that operator -> is disabled + void, + // make "reference type" really the same as the value type + const Value +>; + /** \brief A convenience for use with range-for */ diff --git a/src/Track.h b/src/Track.h index d917c57c9..614dfbf61 100644 --- a/src/Track.h +++ b/src/Track.h @@ -834,16 +834,7 @@ template < typename TrackType > struct TrackIterRange; template < typename TrackType // Track or a subclass, maybe const-qualified > class TrackIter - : public std::iterator< - std::bidirectional_iterator_tag, - TrackType *const, - ptrdiff_t, - // pointer is void to disable operator -> in the reverse_iterator... - void, - // ... because what operator * returns is really a value type, - // so you can't take its address - TrackType *const - > + : public ValueIterator< TrackType *, std::bidirectional_iterator_tag > { public: // Type of predicate taking pointer to const TrackType diff --git a/src/WaveTrack.h b/src/WaveTrack.h index 905e77140..d17642e82 100644 --- a/src/WaveTrack.h +++ b/src/WaveTrack.h @@ -378,7 +378,7 @@ private: // Get access to all clips (in some unspecified sequence), // including those hidden in cutlines. class AllClipsIterator - : public std::iterator< std::forward_iterator_tag, WaveClip* > + : public ValueIterator< WaveClip * > { public: // Constructs an "end" iterator diff --git a/src/toolbars/ToolDock.h b/src/toolbars/ToolDock.h index 4d701b362..996efd0b4 100644 --- a/src/toolbars/ToolDock.h +++ b/src/toolbars/ToolDock.h @@ -114,7 +114,7 @@ public: // This iterator visits the nodes of the forest in pre-order, and at each // stop, reports its Place class Iterator - : public std::iterator + : public ValueIterator { public: const Place &operator * () const { return mPlace; }