Support Class Library
A set of tools providing classes and utility
StlAdapter.h
Go to the documentation of this file.
1 #pragma once
3 #include <iterator>
5 
6 namespace scl{
7  namespace stream{
8  namespace details{
9  namespace iterator{
10  template <class>
12 
17  template <class T>
18  class StlAdapter{
19  public:
25 
31 
32 
37  using iterator_category = std::forward_iterator_tag;
38 
44 
49  using difference_type = std::ptrdiff_t;
50 
55  using pointer = value_type*;
56 
62 
63 
64 
65  protected:
72 
78 
79  public:
84  explicit StlAdapter(iterator_type* sit = nullptr) : StlAdapter(sit, payload_type::withoutValue()) {
85  }
86 
92  StlAdapter(iterator_type* sit, payload_type payload) : it{sit}, payload{std::move(payload)} {
93  ++(*this); //increment because stream iterators are lazier (start without initial value)
94  }
95 
101  if(it){
102  if(it->hasNext()){
103  payload = it->next();
104  }else
105  payload = payload_type::withoutValue();
106  }
107 
108  return *this;
109  }
110 
116  auto ret = *this;
117  ++(*this);
118  return ret;
119  }
120 
129  bool operator==(const StlAdapter& rhs) const{
130  auto&& lhs = *this;
131 
132  /*if(lhs.it != rhs.it)
133  return false;*/
134 
135  return lhs.payload.isInvalid() && rhs.payload.isInvalid();
136  }
137 
145  bool operator !=(const StlAdapter& rhs) const{
146  auto&& lhs = *this;
147  return !(lhs == rhs);
148  }
149 
155  return payload;
156  }
157  };
158  }
159  }
160  }
161 }
Abstract base class for stream iterators defining the required behavior.
Definition: StlAdapter.h:11
virtual payload_type next()=0
Retrieve the next iterator payload.
StreamIteratorPayload< T > payload_type
Type alias for the payload used.
Global namespace of the SCL.
Definition: alias.hpp:3
payload_type operator*() const
Dereference operator.
Definition: StlAdapter.h:154
value_type * pointer
Pointer type for the value type.
Definition: StlAdapter.h:55
value_type & reference
Reference type for the value type.
Definition: StlAdapter.h:61
StlAdapter(iterator_type *sit=nullptr)
Construct an adapter from an iterator.
Definition: StlAdapter.h:84
typename iterator_type::payload_type payload_type
The type of stream iterator payload.
Definition: StlAdapter.h:30
StlAdapter & operator++()
Pre-increment operator.
Definition: StlAdapter.h:100
iterator_type * it
A pointer to the iterator handled by this adapter.
Definition: StlAdapter.h:71
std::forward_iterator_tag iterator_category
The category of this iterator type.
Definition: StlAdapter.h:37
An adapter for STL-like iterators from stream iterators.
Definition: StlAdapter.h:18
bool operator!=(const StlAdapter &rhs) const
Difference operator.
Definition: StlAdapter.h:145
StlAdapter operator++(int)
Post-increment operator.
Definition: StlAdapter.h:115
bool operator==(const StlAdapter &rhs) const
Equality operator.
Definition: StlAdapter.h:129
std::ptrdiff_t difference_type
The type of the difference in size between iterators.
Definition: StlAdapter.h:49
payload_type payload
The current payload.
Definition: StlAdapter.h:77
virtual bool hasNext() const =0
Determines whether or not this iterator can produce another value payload.
payload_type value_type
The type of value provided by the iterators.
Definition: StlAdapter.h:43
StlAdapter(iterator_type *sit, payload_type payload)
Construt an adapter from an iterator and a specific payload.
Definition: StlAdapter.h:92